I have an app, an interface library and plugins.
I define the targets in the main lists file.
Plugins are included using add_subdirectory(plugins)
.
I want plugins to be able to use find_package
in the build and install tree.
I exported targets like this
include(CMakePackageConfigHelpers)
set(LIB_EXPORT_NAME AlbertTargets)
set(LIB_TARGETS_FILE "${LIB_EXPORT_NAME}.cmake")
set(LIB_CONFIG_FILE "AlbertConfig.cmake")
set(LIB_VERSION_FILE "AlbertConfigVersion.cmake")
set(LIB_MACROS_FILE "AlbertMacros.cmake")
set(LIB_CMAKE_MODULE_DIR "${PROJECT_SOURCE_DIR}/cmake")
set(INSTALL_CONFIGDIR "${CMAKE_INSTALL_LIBDIR}/cmake/Albert")
# Install the target
# https://cmake.org/cmake/help/latest/command/install.html#targets
install(
TARGETS ${TARGET_LIB}
EXPORT ${LIB_EXPORT_NAME} # just an association
FRAMEWORK DESTINATION .
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/albert
)
include(CMakePackageConfigHelpers)
# Create version file in build tree
write_basic_package_version_file(
"${PROJECT_BINARY_DIR}/${LIB_VERSION_FILE}"
VERSION "${PROJECT_VERSION}"
COMPATIBILITY AnyNewerVersion
)
# Create config file in build tree
configure_package_config_file(
"${LIB_CMAKE_MODULE_DIR}/${LIB_CONFIG_FILE}.in"
"${PROJECT_BINARY_DIR}/${LIB_CONFIG_FILE}"
INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
)
# Copy the macros to the build directory
configure_file(
"${LIB_CMAKE_MODULE_DIR}/${LIB_MACROS_FILE}"
"${PROJECT_BINARY_DIR}/${LIB_MACROS_FILE}"
COPYONLY
)
# Create a targets file in install tree
# https://cmake.org/cmake/help/latest/command/install.html#export
# By default the generated file will be called <export-name>.cmake
install(
EXPORT ${LIB_EXPORT_NAME}
# NAMESPACE ${PROJECT_NAME}::
DESTINATION ${INSTALL_CONFIGDIR}
)
# Create a targets file in build tree (matching install(EXPORT))
# https://cmake.org/cmake/help/latest/command/export.html#exporting-targets-matching-install-export
# Seems like a shorthand for export(TARGETS)
export(
EXPORT ${LIB_EXPORT_NAME}
# NAMESPACE ${PROJECT_NAME}::
FILE "${CMAKE_BINARY_DIR}/${LIB_TARGETS_FILE}"
)
install(FILES
"${PROJECT_BINARY_DIR}/${LIB_CONFIG_FILE}"
"${PROJECT_BINARY_DIR}/${LIB_VERSION_FILE}"
"${PROJECT_BINARY_DIR}/${LIB_MACROS_FILE}"
DESTINATION ${INSTALL_CONFIGDIR}
)
# Add bin dir to the prefix path such that plugins in the source tree
# can find albert using find_package
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_BINARY_DIR}")
message(STATUS "CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")
However when building the project in the build tree cmake complains about the missing targets file.
If I comment the add_subdirectory(plugins)
line the targets file is created.
Is add subdir preferred over export(EXPORT
?
What can I do to export the targets before the plugins are processed?
Or in general how can I achieve that the plugins are able to call find_package(…)
to find the interface library in the build tree or on the system?