I'm trying to use FetchContent
CMake helper to automate building required dependencies in case they are not installed/available through find_package
, but hitting a wall at how to handle exporting the build targets.
My own project e.g. needs TinyXML2 as a dependency:
## TinyXML2
find_package(tinyxml2 QUIET)
if (${tinyxml2_FOUND})
message(STATUS "tinyxml2 FOUND!(${tinyxml2_LIBRARIES})")
echo_all_cmake_variable_values()
else()
message(STATUS "tinyxml2 NOT FOUND, fetching from source!")
FetchContent_Declare(tinyxml2
GIT_REPOSITORY https://github.com/leethomason/TinyXML2
GIT_TAG 9.0.0
)
FetchContent_MakeAvailable(tinyxml2)
set(tinyxml2_LIBRARIES tinyxml2)
endif()
Which is then used to link the project targets:
# ...
target_link_libraries(${PROJECT_NAME} PUBLIC ${Boost_LIBRARIES}
pthread
${tinyxml2_LIBRARIES})
And the linked target is then exported:
# ...
export(EXPORT ${PROJECT_NAME}Targets
FILE ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake
NAMESPACE ${PROJECT_NAME}::)
The issue is that, when fetching from source, the export step fails:
CMake Error in src/myproject/CMakeLists.txt:
export called with target "myproject" which requires target "tinyxml2" that
is not in any export set.
Is there a way to also auto-export tinyxml2
as a required dependency?