Debian Codegraph @

cmake/tribits/core/package_arch/TribitsProjectWriteConfigFile.cmake

raw
1 # @HEADER
2 # *****************************************************************************
3 # TriBITS: Tribal Build, Integrate, and Test System
4 #
5 # Copyright 2013-2016 NTESS and the TriBITS contributors.
6 # SPDX-License-Identifier: BSD-3-Clause
7 # *****************************************************************************
8 # @HEADER
9
10
11 ################################################################################
12 #
13 # Module TribitsInternalPackageWriteConfigFile.cmake
14 #
15 # This module contains code for generating <Project>Config.cmake files for
16 # TriBITS projects.
17 #
18 ################################################################################
19
20
21 include(TribitsInternalPackageWriteConfigFile)
22
23
24 # Write the outer TriBITS <Project>Config.cmake file
25 #
26 # If ${PROJECT_NAME}_VERSION is not set or is '' on input, then it will be set
27 # to 0.0.0 in order to create the ${PROJECT_NAME}ConfigVersion.cmake file.
28 #
29 # ToDo: Finish documentation!
30 #
31 function(tribits_write_project_client_export_files)
32
33 set(EXPORT_FILE_VAR_PREFIX ${PROJECT_NAME})
34
35 # Reversing the package list so that libraries will be produced in order of
36 # most dependent to least dependent.
37 set(PACKAGE_LIST ${${PROJECT_NAME}_DEFINED_INTERNAL_PACKAGES})
38 if (PACKAGE_LIST)
39 list(REVERSE PACKAGE_LIST)
40 endif()
41
42 # Loop over all packages to determine which were enabled. Then build a list
43 # of all their libraries/includes in the proper order for linking
44 set(FULL_PACKAGE_SET "")
45 set(FULL_LIBRARY_SET "")
46 foreach(TRIBITS_PACKAGE ${PACKAGE_LIST})
47 if(${PROJECT_NAME}_ENABLE_${TRIBITS_PACKAGE})
48 list(APPEND FULL_PACKAGE_SET ${TRIBITS_PACKAGE})
49 list(APPEND FULL_LIBRARY_SET ${${TRIBITS_PACKAGE}_LIBRARIES})
50 endif()
51 endforeach()
52
53 set(${PROJECT_NAME}_CONFIG_LIBRARIES ${FULL_LIBRARY_SET})
54
55 # Reversing the tpl list so that the list of tpls will be produced in
56 # order of most dependent to least dependent.
57 if (${PROJECT_NAME}_DEFINED_TPLS)
58 set(TPL_LIST ${${PROJECT_NAME}_DEFINED_TPLS})
59 list(REVERSE TPL_LIST)
60 endif()
61
62 # Loop over all TPLs to determine which were enabled. Then build a list
63 # of all their libraries/includes in the proper order for linking
64 set(FULL_TPL_SET "")
65 set(FULL_TPL_LIBRARY_SET "")
66 foreach(TPL ${TPL_LIST})
67 if(TPL_ENABLE_${TPL})
68 list(APPEND FULL_TPL_SET ${TPL})
69 list(APPEND FULL_TPL_LIBRARY_SET ${TPL_${TPL}_LIBRARIES})
70 endif()
71 endforeach()
72
73 set(${PROJECT_NAME}_CONFIG_TPL_LIBRARIES ${FULL_TPL_LIBRARY_SET})
74
75 #
76 # Configure two files for finding ${PROJECT_NAME}. One for the build tree
77 # and one for installing
78 #
79
80 # Generate a note discouraging editing of the <package>Config.cmake file
81 set(DISCOURAGE_EDITING "Do not edit: This file was generated automatically by CMake.")
82
83 # Write the specification of the rpath if necessary. This is only needed if
84 # we're building shared libraries.
85 if(BUILD_SHARED_LIBS)
86 string(REPLACE ";" ":" SHARED_LIB_RPATH_COMMAND
87 "${${PROJECT_NAME}_CONFIG_LIBRARY_DIRS}")
88 set(SHARED_LIB_RPATH_COMMAND ${CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG}${SHARED_LIB_RPATH_COMMAND})
89 endif()
90
91 # Custom code in configuration file.
92 set(PROJECT_CONFIG_CODE "")
93
94 # Appending the logic to include each package's config file.
95 set(LOAD_CODE "# Load configurations from enabled packages")
96 foreach(TRIBITS_PACKAGE ${FULL_PACKAGE_SET})
97 set(LOAD_CODE "${LOAD_CODE}
98 include(\"${${TRIBITS_PACKAGE}_BINARY_DIR}/${TRIBITS_PACKAGE}Config.cmake\")")
99 endforeach()
100 set(PROJECT_CONFIG_CODE "${PROJECT_CONFIG_CODE}\n${LOAD_CODE}")
101
102 tribits_set_compiler_vars_for_config_file(INSTALL_DIR)
103
104 if (${PROJECT_NAME}_ENABLE_INSTALL_CMAKE_CONFIG_FILES)
105 # In TribitsProjectConfigTemplate.cmake.in, we would like to preserve
106 # ${}-variables after the conversion to TribitsProjectConfigTemplate.cmake.
107 # To this end, one typically uses the @-syntax for variables. That doesn't
108 # support nested variables, however. Use ${PDOLLAR} as a workaround, cf.
109 # <http://www.cmake.org/pipermail/cmake/2013-April/054341.html>.
110 set(PDOLLAR "$")
111 set(tribitsInstallationDir
112 "${${PROJECT_NAME}_TRIBITS_DIR}/${TRIBITS_CMAKE_INSTALLATION_FILES_DIR}")
113 set(TRIBITS_PROJECT_INSTALL_INCLUDE_DIR "")
114 configure_file(
115 "${tribitsInstallationDir}/TribitsProjectConfigTemplate.cmake.in"
116 "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" )
117 endif()
118
119 ######
120 # Create a configure file for the install tree and set the install target for it. This
121 # file isn't generally useful inside the build tree. It will be placed in the base
122 # install directory for ${PROJECT_NAME} when installed.
123 ######
124
125 # Set the include and library directories relative to the location
126 # at which the ${PROJECT_NAME}Config.cmake file is going to be
127 # installed. Note the variable reference below is escaped so it
128 # won't be replaced until a client project attempts to locate
129 # directories using the installed config file. This is to deal with
130 # installers that allow relocation of the install tree at *install*
131 # time.
132 # The export files are typically installed in
133 # <install dir>/<lib path>/cmake/<package name>/.
134 # The relative path to the installation dir is hence k*(../) + ../../, where
135 # k is the number of components in <lib path>. Extract those here.
136 # This doesn't work if ${${PROJECT_NAME}_INSTALL_LIB_DIR} contains "./" or
137 # "../" components, but really, it never did. All of this should actually be
138 # handled by CMake's configure_package_config_file().
139 string(REPLACE "/" ";" PATH_LIST ${${PROJECT_NAME}_INSTALL_LIB_DIR})
140 set(RELATIVE_PATH "../..")
141 foreach(PATH ${PATH_LIST})
142 set(RELATIVE_PATH "${RELATIVE_PATH}/..")
143 endforeach()
144
145 # Write the specification of the rpath if necessary. This is only needed if
146 # we're building shared libraries.
147 if(BUILD_SHARED_LIBS)
148 set(SHARED_LIB_RPATH_COMMAND
149 "${CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG}${CMAKE_INSTALL_PREFIX}/${${PROJECT_NAME}_INSTALL_LIB_DIR}"
150 )
151 endif()
152
153 if (${PROJECT_NAME}_ENABLE_INSTALL_CMAKE_CONFIG_FILES)
154
155 tribits_set_compiler_vars_for_config_file(INSTALL_DIR)
156
157 # Custom code in configuration file.
158 set(PROJECT_CONFIG_CODE "")
159
160 set(PDOLLAR "$") # Hack used in configure file below
161
162 if (IS_ABSOLUTE "${${PROJECT_NAME}_INSTALL_INCLUDE_DIR}")
163 set(TRIBITS_PROJECT_INSTALL_INCLUDE_DIR "${${PROJECT_NAME}_INSTALL_INCLUDE_DIR}")
164 else()
165 set(TRIBITS_PROJECT_INSTALL_INCLUDE_DIR
166 "${CMAKE_INSTALL_PREFIX}/${${PROJECT_NAME}_INSTALL_INCLUDE_DIR}")
167 endif()
168
169 configure_file(
170 "${${PROJECT_NAME}_TRIBITS_DIR}/${TRIBITS_CMAKE_INSTALLATION_FILES_DIR}/TribitsProjectConfigTemplate.cmake.in"
171 "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config_install.cmake"
172 )
173
174 install(
175 FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config_install.cmake"
176 DESTINATION "${${PROJECT_NAME}_INSTALL_LIB_DIR}/cmake/${PROJECT_NAME}"
177 RENAME ${PROJECT_NAME}Config.cmake
178 )
179 endif()
180
181 #
182 # Configure the version file for ${PROJECT_NAME}
183 #
184 include(CMakePackageConfigHelpers)
185 if ("${${PROJECT_NAME}_VERSION}" STREQUAL "")
186 set(${PROJECT_NAME}_VERSION 0.0.0)
187 endif()
188 write_basic_package_version_file(
189 ${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
190 VERSION ${${PROJECT_NAME}_VERSION}
191 COMPATIBILITY SameMajorVersion
192 )
193 install(
194 FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
195 DESTINATION "${${PROJECT_NAME}_INSTALL_LIB_DIR}/cmake/${PROJECT_NAME}"
196 )
197
198 endfunction()