I have a CMake Project Foobar
that contains a subdirectory examples
that can also be used as a stand-alone CMake build. To do that this sub-directory performs a find_package(Foobar)
and uses the exported targets. Foobar
provides a FoobarConfig.cmake
, FoobarConfigVersion.cmake
, and a FoobarExports.cmake
and can be used without a FindModule.
The code roughly looks like this:
### Top-Level CMakeLists.txt ###
cmake_minimum_required(VERSION 3.0.0)
project(Foobar)
add_library(X SHARED ${my_sources})
install(TARGETS X EXPORT FoobarExports
LIBRARY DESTINATION ${my_install_destination})
install(EXPORT FoobarExports DESTINATION ${my_install_destination})
# Create the FoobarExports.cmake for the local build tree
export(EXPORT FoobarExports) # the problematic command
# Setup FoobarConfig.cmake etc
# FoobarConfig.cmake includes FoobarExports.cmake
# ...
# Force find_package to FOOBAR_DIR
option(BUILD_EXAMPLES "Build examples" ON)
if(BUILD_EXAMPLES)
set(FOOBAR_DIR "${CMAKE_BINARY_DIR}")
add_subdirectory(examples)
endif()
### examples/CMakeLists.txt ###
cmake_minimum_required(VERSION 3.0.0)
project(FoobarExamples)
# Uses FOOBAR_DIR set above
find_package(Foobar NO_MODULE REQUIRED)
add_executable(my_exe ${some_sources})
# Use X from Foobar
target_link_library(my_exe X)
The problem is that export(EXPORT FoobarExports)
will only create the FoobarExports.cmake
file at the end of generation time to make sure it has the complete FoobarExports
export set.
So this will fail:
cmake . -DBUILD_EXAMPLES=ON
# Error: FoobarExports.cmake not found
What works, however, is:
cmake .
cmake . -DBUILD_EXAMPLES=ON # rerun cmake with changed cache variable
How can I either force the FoobarExports.cmake
file to be written at the time of the call to export
or force CMake to run twice, if the file has not been created yet?