[QUESTION] using xtd in a pre-written CMake project
Opened this issue · 9 comments
Your question
I'm trying to incorporate xtd into a project via the cpm.cmake package manager. However, this doesn't work because I have to first execute find_package
and then when I try to do target_xxx
CMake either: (1) claims that target_link_libraries(...) has already been used, or (2) the library tries to generate assembly information, but it does it in a weird way (it does something like ${CMAKE_SOURCE_DIR}/{generated_code_here}
). Obviously, this doesn't work. What am I doing wrong?
Hi,
First of all, thank you for your interest in xtd.
xtd does not yet have package management, see #221. This may explain your problem.
I don't know cpm.cmake
. Maybe adding find_package
can be a solution. But I think the best way to do this is to start with a minimal project and for example not to use the xtd.cmake
specific commands but the standard cmake commands.
Creates a project too simple like this and try it with cpm.cmake
.
- my_app.cpp
#include <xtd/xtd>
auto main()->int {
xtd::forms::application::run(xtd::forms::form::create("My app"));
}
- CMakeLists.txt :
cmake_minimum_required(VERSION 3.20)
project(my_app)
find_package(xtd REQUIRED)
add_executable(${PROJECT_NAME} WIN32 MACOSX_BUNDLE my_app.cpp)
target_link_libraries(${PROJECT_NAME} xtd)
After that, if it works, add specific commands to xtd.cmake like add_sources
, add_resources
, target_type
...
and see what happens.
I hope this helps.
@gammasoft71 cpm.cmake is pretty neat. I'll give your suggestion a try though - thanks!
@gammasoft71 Update: I tried it and it didn't work. It still complained about CMake trying to override my project (saying that, e.g., target_xxx_xxx()
(like target_link_libraries
) was already called on my CMake project). Essentially what I'm trying to do is download and build xtd alongside my project instead of forcing people to install it separately.
Sorry this is not working. I don't have enough information to help you.
I think the problem may be that xtd doesn't have package management #221 yet.
Apart from that, I really don't see how xtd's cmake management could be blocking.
The cmake commands added by xtd only set variables to prepare the work of the target_type
command.
And it's only when target_type
is called that it has specific processing for xtd (such as managing resources, languages and settings).
And if you use standard cmake commands, xtd does nothing...
Stupid question: have you ever tried the same operation with a library as complex as xtd, such as fltk, wxwidgets, or other ?
@gammasoft71 Sorry for getting back to you on this after so long, but yes, I have and it worked fine, if I remember right. I will try to whip up a project again to see if the latest tag still causes this issue.
Okay, so I just tried again, and I still get weird CMake configure errors from xtd (v 0.1.2-beta):
-- CPM: Adding package xtd@0.1.2-beta (v0.1.2-beta)
CMake Error at build/_deps/xtd-src/src/xtd.drawing.native.wxwidgets/CMakeLists.txt:19 (include):
include called with wrong number of arguments. include() only takes one
file.
CMake Error at build/_deps/xtd-src/src/xtd.forms.native.wxwidgets/CMakeLists.txt:19 (include):
include called with wrong number of arguments. include() only takes one
file.
CMake Error at build/_deps/xtd-src/scripts/cmake/xtd_commands.cmake:1521 (string):
string no output variable specified
Call Stack (most recent call first):
build/_deps/xtd-src/scripts/cmake/xtd_commands.cmake:1120 (write_windows_target_informations_file)
build/_deps/xtd-src/scripts/cmake/xtd_commands.cmake:304 (write_target_informations_file)
CMakeLists.txt:104 (target_type)
CMake Error at build/_deps/xtd-src/scripts/cmake/xtd_commands.cmake:1164 (file):
file failed to open for writing (Invalid argument):
C:/Users/Ethin/source/cppmudclient/#pragma region xtd generated code
Call Stack (most recent call first):
build/_deps/xtd-src/scripts/cmake/xtd_commands.cmake:305 (write_assembly_informations)
CMakeLists.txt:104 (target_type)
CMake Error at build/_deps/xtd-src/scripts/cmake/xtd_commands.cmake:329 (add_executable):
The target name "C:/Users/Ethin/source/cppmudclient/build/resources/.rc" is
reserved or not valid for certain CMake features, such as generator
expressions, and may result in undefined behavior.
Call Stack (most recent call first):
CMakeLists.txt:104 (target_type)
CMake Error at build/_deps/xtd-src/scripts/cmake/xtd_commands.cmake:381 (target_compile_options):
Cannot specify compile options for target "PRIVATE" which is not built by
this project.
Call Stack (most recent call first):
CMakeLists.txt:104 (target_type)
CMake Error at build/_deps/xtd-src/scripts/cmake/xtd_commands.cmake:382 (target_compile_options):
Cannot specify compile options for target "PRIVATE" which is not built by
this project.
Call Stack (most recent call first):
CMakeLists.txt:104 (target_type)
CMake Error at build/_deps/xtd-src/scripts/cmake/xtd_commands.cmake:393 (string):
string sub-command SUBSTRING requires four arguments.
Call Stack (most recent call first):
CMakeLists.txt:104 (target_type)
-- Configuring incomplete, errors occurred!
I pull in xtd like so:
CPMAddPackage(
NAME xtd
VERSION 0.1.2-beta
GITHUB_REPOSITORY "gammasoft71/xtd"
GIT_TAG v0.1.2-beta
OPTIONS
"XTD_BUILD_SHARED_LIBRARIES ON"
"XTD_BUILD_TESTS OFF"
"XTD_BUILD_TOOLS OFF"
"XTD_BUILD_WITH_CONTINUOUS_INTEGRATION_SYSTEM OFF"
"XTD_ENABLE_RUN_ASTYLE OFF"
"XTD_ENABLE_RUN_CPPCHECK OFF"
"XTD_ENABLE_RUN_DEVELOPPER_REFERENCE_GUIDE OFF"
"XTD_ENABLE_RUN_REFERENCE_GUIDE OFF"
"XTD_INSTALL_EXAMPLES OFF"
"XTD_INSTALL_RESOURCES OFF"
"XTD_KEEP_CLONED_CONTROLS OFF"
)
file(GLOB_RECURSE headers CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
file(GLOB_RECURSE sources CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
if (WIN32)
add_executable(${PROJECT_NAME} WIN32 ${headers} ${sources})
else()
add_executable(${PROJECT_NAME} ${headers} ${sources})
endif()
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 17)
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<COMPILE_LANG_AND_ID:CXX,MSVC>:/permissive->")
target_type(GUI_APPLICATION)
target_link_libraries(${PROJECT_NAME} PRIVATE CAF::core CAF::net)
target_include_directories(
${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
)
I know that this is a WIP but usually projects don't always need to be specially designed for this kind of use. So I'm uncertain what xtd is doing that's causing these problems. For all I know it could be on my end of things.
What can I do to reproduce the problem? What's the procedure?
For me it was as simple as:
CPMAddPackage(
NAME xtd
VERSION 0.1.2-beta
GITHUB_REPOSITORY "gammasoft71/xtd"
GIT_TAG v0.1.2-beta
OPTIONS
"XTD_BUILD_SHARED_LIBRARIES ON"
"XTD_BUILD_TESTS OFF"
"XTD_BUILD_TOOLS OFF"
"XTD_DOWNLOAD_ASTYLE ON"
"XTD_DOWNLOAD_CPPCHECK ON"
"XTD_DOWNLOAD_DOXYGEN ON"
"XTD_INSTALL_EXAMPLES OFF"
)
file(GLOB_RECURSE headers CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
file(GLOB_RECURSE sources CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
if (WIN32)
add_executable(${PROJECT_NAME} WIN32 ${headers} ${sources})
target_type(GUI_APPLICATION)
else()
add_executable(${PROJECT_NAME} ${headers} ${sources})
target_type(GUI_APPLICATION)
endif()
At least, this is the relevant part. You'll need to include/build WXwidgets though. I can try to create a full project you can play with if you like.
I managed to solve the first error by moving target_type
to the very end of my CMakeLists.txt
file. The problem I think is that I'm using add_executable
and target_xxx
to affect only the specific project I'm creating, and not the older commands (i.e. include_directories
), which I believe affect all targets (but I may be wrong here), whereas xtd appears to be not doing the same thing. Looking at the demos, the second line after project()
is add_sources()
, s all of these errors may honestly come down to me just using the entire project setup for xtd incorrectly. I'll keep playing with it and see if I can get it working. If it is indeed that I'm just failing to use it properly it'll take a bit of a mental shift for me :D I'm so used to doing it the add_executable/target_include_directories/target_link_libraries
way that it was just automatic for me even when opening this issue to assume that xtd would work the same way.
Okay, so here is my current CMake project (which is broken). Sorry if the formatting is weird (cmake-format
doesn't want to work on my system for some reason):
cmake_minimum_required(VERSION 3.14...3.22)
# ---- Project ----
project(
test
VERSION 0.1.0
LANGUAGES CXX
)
if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
message(
FATAL_ERROR
"In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there."
)
endif()
include(cmake/CPM.cmake)
CPMAddPackage(
name wxWidgets
VERSION 3.2.5
GITHUB_REPOSITORY "wxWidgets/wxWidgets"
GIT_TAG v3.2.5
OPTIONS
"wxBUILD_MONOLITHIC ON"
"wxBUILD_SAMPLES OFF"
"wxBUILD_TESTS OFF"
"wxBUILD_DEMOS OFF"
"wxBUILD_BENCHMARKS OFF"
"wxBUILD_PRECOMP ON"
"wxBUILD_INSTALL OFF"
"wxBUILD_USE_STATIC_RUNTIME ON"
"wxBUILD_MSVC_MULTIPROC ON"
"wxBUILD_LARGEFILE_SUPPORT ON"
"wxBUILD_OPTIMISE ON"
"wxBUILD_STRIPPED_RELEASE ON"
"wxBUILD_PIC ON"
"wxUSE_NO_RTTI ON"
"wxUSE_STD_IOSTREAM ON"
"wxUSE_STD_CONTAINERS ON"
"wxUSE_UNICODE_UTF8 ON"
"wxUSE_UTF8_LOCALE_ONLY ON"
"wxUSE_STD_STRING_CONV_IN_WXSTRING ON"
"wxUSE_REPRODUCIBLE_BUILD ON"
"wxUSE_REGEX builtin"
"wxUSE_ZLIB builtin"
"wxUSE_EXPAT builtin"
"wxUSE_LIBJPEG builtin"
"wxUSE_LIBPNG builtin"
"wxUSE_LIBTIFF builtin"
"wxUSE_NANOSVG builtin"
"wxUSE_OPENGL ON"
"wxUSE_STL ON"
"wxUSE_INTL ON"
"wxUSE_XLOCALE ON"
"wxUSE_CONFIG ON"
"wxUSE_SOCKETS ON"
"wxUSE_IPV6 ON"
"wxUSE_OLE ON"
"wxUSE_DATAOBJ ON"
"wxUSE_IPC ON"
"wxUSE_CONSOLE_EVENTLOOP OFF"
"wxUSE_ANY ON"
"wxUSE_APPLE_IEEE ON"
"wxUSE_ARCHIVE_STREAMS ON"
"wxUSE_BASE64 ON"
"wxUSE_STACKWALKER ON"
"wxUSE_ON_FATAL_EXCEPTION ON"
"wxUSE_CMDLINE_PARSER ON"
"wxUSE_DATETIME ON"
"wxUSE_DEBUGREPORT ON"
"wxUSE_DIALUP_MANAGER OFF"
"wxUSE_DYNLIB_CLASS ON"
"wxUSE_DYNAMIC_LOADER ON"
"wxUSE_EXCEPTIONS ON"
"wxUSE_EXTENDED_RTTI OFF"
"wxUSE_FFILE ON"
"wxUSE_FILE ON"
"wxUSE_FILE_HISTORY ON"
"wxUSE_FILESYSTEM ON"
"wxUSE_FONTENUM ON"
"wxUSE_FONTMAP ON"
"wxUSE_FS_ARCHIVE ON"
"wxUSE_FS_INET ON"
"wxUSE_FSVOLUME ON"
"wxUSE_FSWATCHER ON"
"wxUSE_GEOMETRY ON"
"wxUSE_LOG ON"
"wxUSE_LONGLONG ON"
"wxUSE_MIMETYPE ON"
"wxUSE_PRINTF_POS_PARAMS ON"
"wxUSE_SECRETSTORE ON"
"wxUSE_SNGLINST_CHECKER ON"
"wxUSE_SOUND OFF"
"wxUSE_SPELLCHECK ON"
"wxUSE_STDPATHS ON"
"wxUSE_STOPWATCH ON"
"wxUSE_STREAMS ON"
"wxUSE_SYSTEM_OPTIONS ON"
"wxUSE_TARSTREAM ON"
"wxUSE_TEXTBUFFER ON"
"wxUSE_TEXTFILE ON"
"wxUSE_TIMER ON"
"wxUSE_VARIANT ON"
"wxUSE_WEBREQUEST ON"
"wxUSE_ZIPSTREAM ON"
"wxUSE_URL ON"
"wxUSE_PROTOCOL ON"
"wxUSE_PROTOCOL_HTTP ON"
"wxUSE_PROTOCOL_FTP ON"
"wxUSE_PROTOCOL_FILE ON"
"wxUSE_THREADS ON"
"wxUSE_DOC_VIEW_ARCHITECTURE ON"
"wxUSE_HELP ON"
"wxUSE_HTML ON"
"wxUSE_WXHTML_HELP ON"
"wxUSE_XML ON"
"wxUSE_AUI ON"
"wxUSE_PROPGRID ON"
"wxUSE_RIBBON ON"
"wxUSE_STC ON"
"wxUSE_CONSTRAINTS ON"
"wxUSE_LOGGUI ON"
"wxUSE_LOGWINDOW ON"
"wxUSE_LOG_DIALOG ON"
"wxUSE_MDI ON"
"wxUSE_MDI_ARCHITECTURE ON"
"wxUSE_MEDIACTRL ON"
"wxUSE_RICHTEXT ON"
"wxUSE_PRINTING_ARCHITECTURE ON"
"wxUSE_SVG ON"
"wxUSE_WEBVIEW ON"
"wxUSE_GRAPHICS_CONTEXT ON"
"wxUSE_GRAPHICS_DIRECT2D ON"
"wxUSE_CLIPBOARD ON"
"wxUSE_DRAG_AND_DROP ON"
"wxUSE_CONTROLS ON"
"wxUSE_MARKUP ON"
"wxUSE_COMMON_DIALOGS ON"
"wxUSE_MENUS ON"
"wxUSE_MENUBAR ON"
"wxUSE_MINIFRAME ON"
"wxUSE_TOOLTIPS ON"
"wxUSE_SPLINES ON"
"wxUSE_MOUSEWHEEL ON"
"wxUSE_VALIDATORS ON"
"wxUSE_BUSYINFO ON"
"wxUSE_HOTKEY ON"
"wxUSE_METAFILE ON"
"wxUSE_DRAGIMAGE ON"
"wxUSE_UIACTIONSIMULATOR ON"
"wxUSE_DC_TRANSFORM_MATRIX ON"
"wxUSE_PALETTE ON"
"wxUSE_IMAGE ON"
"wxUSE_GIF ON"
"wxUSE_PCX ON"
"wxUSE_TGA ON"
"wxUSE_IFF ON"
"wxUSE_PNM ON"
"wxUSE_XPM ON"
"wxUSE_ICO_CUR ON"
"wxUSE_ACCESSIBILITY ON"
"wxUSE_AUTOID_MANAGEMENT ON"
)
CPMAddPackage(
NAME openssl-cmake
URL https://github.com/jimmy-park/openssl-cmake/archive/main.tar.gz
OPTIONS
"OPENSSL_CONFIGURE_OPTIONS no-tests"
)
CPMAddPackage(
name caf
VERSION 1.0.0
GITHUB_REPOSITORY "actor-framework/actor-framework"
GIT_TAG 1.0.0
OPTIONS
"CAF_ENABLE_RUNTIME_CHECKS $<IF:$<CONFIG:Debug>,ON,$<IF:$<CONFIG:Release>,OFF,OFF>>"
"CAF_ENABLE_EXAMPLES OFF"
"CAF_ENABLE_EXCEPTIONS ON"
"CAF_ENABLE_TESTING OFF"
"CAF_CXX_VERSION 20"
"CAF_ENABLE_IO_MODULE OFF"
"CAF_USE_STD_FORMAT ON"
)
CPMAddPackage(
NAME xtd
VERSION 0.1.2-beta
GITHUB_REPOSITORY "gammasoft71/xtd"
GIT_TAG v0.1.2-beta
OPTIONS
"XTD_BUILD_SHARED_LIBRARIES ON"
"XTD_BUILD_TESTS OFF"
"XTD_BUILD_TOOLS OFF"
"XTD_DOWNLOAD_ASTYLE ON"
"XTD_DOWNLOAD_CPPCHECK ON"
"XTD_DOWNLOAD_DOXYGEN ON"
"XTD_INSTALL_EXAMPLES OFF"
)
file(GLOB_RECURSE headers CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
file(GLOB_RECURSE sources CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
add_sources(${headers} ${sources})
target_name(${PROJECT_NAME})
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 17)
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<COMPILE_LANG_AND_ID:CXX,MSVC>:/permissive->")
CPMAddPackage(
NAME botan
VERSION 3.4.0
GITHUB_REPOSITORY randombit/botan
GIT_TAG 3.4.0
DOWNLOAD_ONLY YES
)
if (botan_ADDED)
find_package(Python3 REQUIRED COMPONENTS Interpreter)
message(STATUS "Generating botan amalgamation")
execute_process(
COMMAND ${Python3_EXECUTABLE} ${botan_SOURCE_DIR}/configure.py --amalgamation --link-method=copy --no-install-python-module --without-documentation --without-sqlite3 --disable-shared
WORKING_DIRECTORY ${botan_SOURCE_DIR}
)
target_include_directories(${PROJECT_NAME} PRIVATE ${botan_SOURCE_DIR})
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<COMPILE_LANG_AND_ID:CXX,MSVC>:/bigobj>")
target_sources(${PROJECT_NAME} PRIVATE ${botan_SOURCE_DIR}/botan_all.cpp)
else()
message(FATAL_ERROR "Botan could not be acquired!")
endif()
target_link_libraries(${PROJECT_NAME} CAF::core CAF::net)
target_include_directories(
${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
)
target_type(GUI_APPLICATION)
I'm messing up somewhere (or missing something), just unsure what exactly I'm doing wrong.