Provide ability to export all libraries and headers of theCore to use in external projects
Suppose that someone needs to use theCore functionality, but he has already made project written using make/scons etc. Extracting headers and libraries in that case would be a problem.
Usually it done by simply building and installing dependency (theCore in our case) in some dir and then using contents of that dir when building top-level project.
install() cmake command can be used for such purpose.
Preprocessor definitions that are generated during theCore build can be exported to a header using configure_file() command
It means that all preprocessor public definitions (i.e. such that used within public library headers) must be present in core_config_${library}.h rather than defined from command line
install() has problems with exporting sources, instead following snippet can be used:
macro(theCore_export_target)
cmake_parse_arguments(
EXPORT
"INTERFACE"
"TARGET"
""
${ARGN}
)
set(CORE_EXPORT_INC_DIR ${CORE_BIN_DIR}/export/inc/)
set(CORE_EXPORT_SRC_DIR ${CORE_BIN_DIR}/export/src/)
file(MAKE_DIRECTORY ${CORE_EXPORT_INC_DIR} ${CORE_EXPORT_SRC_DIR})
# TODO: take into account interface libraries
if(EXPORT_INTERFACE)
get_target_property(TARGET_INC ${EXPORT_TARGET} INTERFACE_INCLUDE_DIRECTORIES)
message(">> ${TARGET_INC}")
else()
get_target_property(TARGET_SRCS ${EXPORT_TARGET} SOURCES)
get_target_property(TARGET_INC ${EXPORT_TARGET} INCLUDE_DIRECTORIES)
set(EXPORT_CMD
COMMAND
${CMAKE_COMMAND} -E copy_if_different
${TARGET_SRCS}
${CORE_EXPORT_SRC_DIR}
)
endif()
set(EXPORT_CMD ${EXPORT_CMD}
COMMAND ${CMAKE_COMMAND} -E copy_directory
${TARGET_INC}
${CORE_EXPORT_INC_DIR}
)
add_custom_target(${EXPORT_TARGET}_export
${EXPORT_CMD}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
# Tie all exporting together
add_dependencies(export ${EXPORT_TARGET}_export)
add_dependencies(${EXPORT_TARGET}_export ${EXPORT_TARGET})
unset(CORE_EXPORT_INC_DIR)
unset(CORE_EXPORT_SRC_DIR)
endmacro()
It can be extended with additional parameters:
-
ADD_INCLUDE_FILES- additional files to be exported as includes -
OVERRIDE_INCLUDE_FILES- only copy specified files as includes, do not export entire dir