GenerateConfig.cmake
Go to the documentation of this file.
1 # ============================================================================
2 # Copyright (c) 2011-2012 University of Pennsylvania
3 # Copyright (c) 2013-2016 Andreas Schuh
4 # All rights reserved.
5 #
6 # See COPYING file for license information or visit
7 # https://cmake-basis.github.io/download.html#license
8 # ============================================================================
9 
10 ##############################################################################
11 # @file GenerateConfig.cmake
12 # @brief Generates package configuration files.
13 #
14 # This CMake script configures the \<package\>Config.cmake et al. files,
15 # once for the build tree and once for the install tree. Variables with a
16 # _CONFIG suffix are replaced in the default template file by either the
17 # value for the build or the install tree, respectively.
18 #
19 # If present, this script includes the @c PROJECT_CONFIG_DIR/ConfigBuild.cmake
20 # and/or @c PROJECT_CONFIG_DIR/ConfigInstall.cmake file before configuring the
21 # Config.cmake.in template. If a file @c PROJECT_CONFIG_DIR/Config.cmake.in
22 # exists, it is used as template. Otherwise, the default template file is used.
23 #
24 # Similarly, if the file @c PROJECT_CONFIG_DIR/ConfigVersion.cmake.in exists,
25 # it is used as template for the \<package\>ConfigVersion.cmake file. The same
26 # applies to ConfigUse.cmake.in.
27 #
28 # @ingroup CMakeTools
29 ##############################################################################
30 
31 basis_sanitize_for_regex (CMAKE_CURRENT_LIST_DIR_RE "${CMAKE_CURRENT_LIST_DIR}")
32 
33 # ============================================================================
34 # names of output files
35 # ============================================================================
36 
37 # Attention: This has to be done before configuring any files such that these
38 # variables can be used by the template files.
39 
40 ## @addtogroup CMakeUtilities
41 # @{
42 
43 
44 ## @brief Package name.
45 set (CONFIG_PREFIX "${PROJECT_PACKAGE_CONFIG_PREFIX}")
46 ## @brief Name of the CMake package configuration file.
47 set (CONFIG_FILE "${CONFIG_PREFIX}Config.cmake")
48 ## @brief Name of the CMake package version file.
49 set (VERSION_FILE "${CONFIG_PREFIX}ConfigVersion.cmake")
50 ## @brief Name of the CMake package use file.
51 set (USE_FILE "${CONFIG_PREFIX}Use.cmake")
52 ## @brief Name of the CMake target exports file.
53 set (EXPORTS_FILE "${CONFIG_PREFIX}${BASIS_EXPORT_SUFFIX}.cmake")
54 ## @brief Name of the CMake target exports file for custom targets.
55 set (CUSTOM_EXPORTS_FILE "${CONFIG_PREFIX}Custom${BASIS_EXPORT_SUFFIX}.cmake")
56 
57 
58 ## @}
59 # end of Doxygen group
60 
61 # ============================================================================
62 # export build targets
63 # ============================================================================
64 
67  FILE "${EXPORTS_FILE}"
68  CUSTOM_FILE "${CUSTOM_EXPORTS_FILE}"
69  )
70 endif ()
71 
72 # ============================================================================
73 # namespace
74 # ============================================================================
75 
76 # code used at top of package configuration and use files to set package
77 # namespace prefix used for configuration variables
78 set (BASIS_NS
79 "# prefix used for variable names
80 set (NS \"${PROJECT_CONFIG_PREFIX}_\")
81 
82 # allow caller to change namespace - used by projects with modules
83 if (\${NS}CONFIG_PREFIX)
84  set (NS \"\${\${NS}CONFIG_PREFIX}_\")
85 endif ()"
86 )
87 
88 # ============================================================================
89 # project configuration file
90 # ============================================================================
91 
92 # ----------------------------------------------------------------------------
93 # choose template
94 
95 if (EXISTS "${PROJECT_CONFIG_DIR}/Config.cmake.in")
96  set (TEMPLATE "${PROJECT_CONFIG_DIR}/Config.cmake.in")
97 elseif (PROJECT_IS_MODULE OR PROJECT_IS_SUBMODULE OR PROJECT_IS_SUBPROJECT)
98  set (TEMPLATE "${CMAKE_CURRENT_LIST_DIR}/ModuleConfig.cmake.in")
99 else ()
100  set (TEMPLATE "${CMAKE_CURRENT_LIST_DIR}/Config.cmake.in")
101 endif ()
102 
103 # ----------------------------------------------------------------------------
104 # provide code of BASIS config file as variable
105 
106 if (NOT TEMPLATE MATCHES "^${CMAKE_CURRENT_LIST_DIR_RE}/")
107  if (PROJECT_IS_MODULE OR PROJECT_IS_SUBMODULE OR PROJECT_IS_SUBPROJECT)
108  file (READ "${CMAKE_CURRENT_LIST_DIR}/ModuleConfig.cmake.in" BASIS_TEMPLATE)
109  else ()
110  file (READ "${CMAKE_CURRENT_LIST_DIR}/Config.cmake.in" BASIS_TEMPLATE)
111  endif ()
112  # remove file header
113  string (REGEX REPLACE "^########.*########" "" BASIS_TEMPLATE "${BASIS_TEMPLATE}")
114  string (STRIP "${BASIS_TEMPLATE}" BASIS_TEMPLATE)
115 else ()
116  set (BASIS_TEMPLATE "")
117 endif ()
118 
119 # ----------------------------------------------------------------------------
120 # build tree related configuration
121 
123 include ("${CMAKE_CURRENT_LIST_DIR}/BasisConfigSettings.cmake")
124 include ("${PROJECT_CONFIG_DIR}/ConfigSettings.cmake" OPTIONAL)
125 
126 # ----------------------------------------------------------------------------
127 # configure project configuration file for build tree
128 
129 string (CONFIGURE "${BASIS_TEMPLATE}" BASIS_CONFIG @ONLY)
130 configure_file ("${TEMPLATE}" "${BINARY_LIBCONF_DIR}/${CONFIG_FILE}" @ONLY)
131 
132 if (NOT BASIS_BUILD_ONLY)
133 
134  # --------------------------------------------------------------------------
135  # install tree related configuration
136 
137  set (BUILD_CONFIG_SETTINGS 0)
138  include ("${CMAKE_CURRENT_LIST_DIR}/BasisConfigSettings.cmake")
139  include ("${PROJECT_CONFIG_DIR}/ConfigSettings.cmake" OPTIONAL)
140 
141  # --------------------------------------------------------------------------
142  # configure project configuration file for install tree
143 
144  string (CONFIGURE "${BASIS_TEMPLATE}" BASIS_CONFIG @ONLY)
145  configure_file ("${TEMPLATE}" "${BINARY_CONFIG_DIR}/${CONFIG_FILE}" @ONLY)
146 
147  # --------------------------------------------------------------------------
148  # install project configuration file
149 
150  install (
151  FILES "${BINARY_CONFIG_DIR}/${CONFIG_FILE}"
152  DESTINATION "${INSTALL_CONFIG_DIR}"
153  )
154 
155 endif ()
156 
157 # ============================================================================
158 # project version file
159 # ============================================================================
160 if (NOT PROJECT_IS_SUBMODULE)
161 
162  # --------------------------------------------------------------------------
163  # choose template
164 
165  if (EXISTS "${PROJECT_CONFIG_DIR}/ConfigVersion.cmake.in")
166  set (TEMPLATE "${PROJECT_CONFIG_DIR}/ConfigVersion.cmake.in")
167  else ()
168  set (TEMPLATE "${CMAKE_CURRENT_LIST_DIR}/ConfigVersion.cmake.in")
169  endif ()
170 
171  # --------------------------------------------------------------------------
172  # configure project configuration version file
173 
174  configure_file ("${TEMPLATE}" "${BINARY_LIBCONF_DIR}/${VERSION_FILE}" @ONLY)
175 
176  # --------------------------------------------------------------------------
177  # install project configuration version file
178 
179  if (NOT BASIS_BUILD_ONLY)
180  install (
181  FILES "${BINARY_LIBCONF_DIR}/${VERSION_FILE}"
182  DESTINATION "${INSTALL_CONFIG_DIR}"
183  )
184  endif ()
185 
186 endif ()
187 # ============================================================================
188 # project use file
189 # ============================================================================
190 
191 # ----------------------------------------------------------------------------
192 # choose template
193 
194 if (EXISTS "${PROJECT_CONFIG_DIR}/ConfigUse.cmake.in")
195  set (TEMPLATE "${PROJECT_CONFIG_DIR}/ConfigUse.cmake.in")
196 elseif (EXISTS "${PROJECT_CONFIG_DIR}/Use.cmake.in")
197  # backwards compatibility to version <= 0.1.8 of BASIS
198  set (TEMPLATE "${PROJECT_CONFIG_DIR}/Use.cmake.in")
199 elseif (PROJECT_IS_MODULE OR PROJECT_IS_SUBMODULE OR PROJECT_IS_SUBPROJECT)
200  set (TEMPLATE "${CMAKE_CURRENT_LIST_DIR}/ModuleConfigUse.cmake.in")
201 else ()
202  set (TEMPLATE "${CMAKE_CURRENT_LIST_DIR}/ConfigUse.cmake.in")
203 endif ()
204 
205 # ----------------------------------------------------------------------------
206 # provide code of BASIS use file as variable
207 if (NOT TEMPLATE MATCHES "^${CMAKE_CURRENT_LIST_DIR_RE}/")
208  if (PROJECT_IS_MODULE OR PROJECT_IS_SUBMODULE OR PROJECT_IS_SUBPROJECT)
209  file (READ "${CMAKE_CURRENT_LIST_DIR}/ModuleConfigUse.cmake.in" BASIS_USE)
210  else ()
211  file (READ "${CMAKE_CURRENT_LIST_DIR}/ConfigUse.cmake.in" BASIS_USE)
212  endif ()
213  # remove file header
214  string (REGEX REPLACE "^########.*########" "" BASIS_USE "${BASIS_USE}")
215  string (STRIP "${BASIS_USE}" BASIS_USE)
216 else ()
217  set (BASIS_USE "")
218 endif ()
219 
220 # ----------------------------------------------------------------------------
221 # configure project use file
222 
223 string (CONFIGURE "${BASIS_USE}" BASIS_USE @ONLY)
224 configure_file ("${TEMPLATE}" "${BINARY_LIBCONF_DIR}/${USE_FILE}" @ONLY)
225 
226 # ----------------------------------------------------------------------------
227 # install project use file
228 
229 if (NOT BASIS_BUILD_ONLY)
230  install (
231  FILES "${BINARY_LIBCONF_DIR}/${USE_FILE}"
232  DESTINATION "${INSTALL_CONFIG_DIR}"
233  )
234 endif ()
235 
236 unset (BASIS_NS)
237 unset (BASIS_TEMPLATE)
238 unset (BASIS_CONFIG)
239 unset (BASIS_USE)
240 unset (CMAKE_CURRENT_LIST_DIR_RE)
string OPTIONAL
Definition: argparse.py:128
cmake EXPORTS_FILE
Name of the CMake target exports file.
cmake USE_FILE
Name of the CMake package use file.
macro basis_sanitize_for_regex(out OUT, in STR)
Sanitize string variable for use in regular expression.
function basis_export_targets()
Export all targets added by basis_add_* commands.
cmake BUILD_CONFIG_SETTINGS
cmake BASIS_TEMPLATE
cmake TEMPLATE
cmake CONFIG_FILE
Name of the CMake package configuration file.
cmake FILE
cmake BASIS_EXPORT_ENABLED
Whether to create "<Package><ExportSuffix>.cmake" file so other projects can import the exported targ...
cmake CONFIG_PREFIX
Package name.
cmake VERSION_FILE
Name of the CMake package version file.
option BASIS_BUILD_ONLY
Request configuration of software build only, skipping steps related to packaging and installation...
cmake CUSTOM_EXPORTS_FILE
Name of the CMake target exports file for custom targets.
cmake BASIS_USE
if(oldcoutbuf)