forked from Eyescale/CMake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommonPackageConfig.cmake
75 lines (67 loc) · 3.19 KB
/
CommonPackageConfig.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Copyright (c) 2015 [email protected]
#
# Write ${PROJECT_NAME}ConfigVersion.cmake, ${PROJECT_NAME}Config.cmake, and
# ${PROJECT_NAME}Targets.cmake like CMake suggests to create a 'package' that
# can be found by downstream projects, either from the build tree (subproject)
# or from the install tree (package/module).
# http://www.cmake.org/cmake/help/v3.2/manual/cmake-packages.7.html#creating-packages
#
# Note that the install tree export target set creation is only supported if
# CMake 3 is used. On the other hand, a CMake 3 generated install tree can be
# consumed either by CMake 2 or 3.
#
# Uses:
# * ${UPPER_PROJECT_NAME}_DEPENDENT_LIBRARIES - list of 'leaking'/public
# dependent libraries that need to be 'found' first to provide targets that
# this project is depending on. Usually this matches the list of targets that
# are used PUBLIC in target_link_libraries().
# * ${PROJECT_NAME}Targets - the set of targets to export, added by
# CommonLibrary.cmake
# Generate ${PROJECT_NAME}ConfigVersion.cmake
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
VERSION ${VERSION} COMPATIBILITY SameMajorVersion
)
# Add find_package/find_dependency (CMake2/3) calls for leaking dependent
# libraries to ${PROJECT_NAME}Config.cmake
if(${UPPER_PROJECT_NAME}_DEPENDENT_LIBRARIES)
set(DEPENDENT_LIBRARIES
" # find dependent libraries which provide dependent targets\n"
" if(CMAKE_MAJOR_VERSION GREATER 2)\n"
" include(CMakeFindDependencyMacro)\n")
foreach(_dependent ${${UPPER_PROJECT_NAME}_DEPENDENT_LIBRARIES})
list(APPEND DEPENDENT_LIBRARIES " find_dependency(${_dependent})\n")
endforeach()
list(APPEND DEPENDENT_LIBRARIES " else()\n")
foreach(_dependent ${${UPPER_PROJECT_NAME}_DEPENDENT_LIBRARIES})
list(APPEND DEPENDENT_LIBRARIES " find_package(${_dependent} QUIET)\n")
endforeach()
list(APPEND DEPENDENT_LIBRARIES " endif()\n")
string(REGEX REPLACE ";" " " DEPENDENT_LIBRARIES ${DEPENDENT_LIBRARIES})
endif()
# Generate ${PROJECT_NAME}Config.cmake; is consumed by any find_package() call
# to this project. Does an inclusion of ${PROJECT_NAME}Targets.cmake if the
# target ${PROJECT_NAME} is not defined, which should only be the case when
# finding this project from the install tree.
configure_package_config_file("${CMAKE_CURRENT_LIST_DIR}/CommonConfig.cmake.in"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION ${CMAKE_MODULE_INSTALL_PATH}
)
install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION ${CMAKE_MODULE_INSTALL_PATH}
)
# No export(EXPORT) in CMake 2 or if common_library has not been used.
if(CMAKE_MAJOR_VERSION LESS 3 OR NOT TARGET ${PROJECT_NAME}_ALIAS)
return()
endif()
# Generate ${PROJECT_NAME}Targets.cmake; is written after the CMake run
# succeeds. Provides IMPORTED targets when using this project from the install
# tree.
export(EXPORT ${PROJECT_NAME}Targets
FILE "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake"
)
install(EXPORT ${PROJECT_NAME}Targets FILE ${PROJECT_NAME}Targets.cmake
DESTINATION ${CMAKE_MODULE_INSTALL_PATH}
)