My top-level project has a number of "subprojects".
Each of those subprojects uses a different compiler toolchain than my top-level project. Each subproject has a toolchain file. Some subprojects build host executables, others generate executables for use on other platforms.
In my top-level project, I generate the CMakeLists.txt
for each subproject using configure_file()
...
project(topProject ...)
...
configure_file(${CMAKE_SOURCE_DIR}/cmake/subproject_template.cmake
${CMAKE_CURRENT_BINARY_DIR}/subA/CMakeLists.txt
@ONLY)
...
I cannot use add_subdirectory(subA)
here, because that file needs a different toolchain, so I presume I need to create a custom target (still in the top-level project) to actually build that subproject...
add_custom_target(build_subA
DEPENDS ...
COMMAND
${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/subA/build
COMMAND
${CMAKE_COMMAND}
-DCMAKE_TOOLCHAIN_FILE="${CMAKE_SOURCE_DIR}/cmake/toolchain_subA.cmake"
-G${CMAKE_GENERATOR}
-S${CMAKE_CURRENT_BINARY_DIR}/subA
-B${CMAKE_CURRENT_BINARY_DIR}/subA/build
COMMAND
${CMAKE_COMMAND}
--build ${CMAKE_CURRENT_BINARY_DIR}/subA/build
--target subA
)
Of course, elements in the subproject can depend on elements in my top-project. That seems straightforward as subproject_template.cmake
will define IMPORTED
libraries from my topProject (and will use find_executable()
and find_file()
, etc. DEPENDS
in the build_subA
target above will ensure that everything is ready for the subproject.
Dependencies the OTHER way seem more difficult. It's difficult to import subA
back into topProject...
# I cannot do this (or similarly for add_library) because these are
# typically not for the host platform, and the file does not exist when
# cmake builds on the topProject
add_executable(subA)
set_target_properties(subA PROPERTIES
# This file does not exist when cmake builds topProject
IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/subA/build/subA_target_file.exe
) # Cannot use `$<TARGET_FILE_DIR>` or `$<TARGET>` from other projects either...?
# What I can do is directly *use* the things in the subprojects...
add_custom_target(
DEPENDS build_subA
COMMAND
foo --elf=${CMAKE_CURRENT_BINARY_DIR}/subA/build/subA_target_file.exe ...
)
What's the right way to do this? Thank you!