CTK  0.1.0
The Common Toolkit is a community effort to provide support code for medical image analysis, surgical navigation, and related projects.
CMakePackageConfigHelpers.cmake
Go to the documentation of this file.
1 #.rst:
2 # CMakePackageConfigHelpers
3 # -------------------------
4 #
5 # CONFIGURE_PACKAGE_CONFIG_FILE(), WRITE_BASIC_PACKAGE_VERSION_FILE()
6 #
7 #
8 #
9 # ::
10 #
11 # CONFIGURE_PACKAGE_CONFIG_FILE(<input> <output> INSTALL_DESTINATION <path>
12 # [PATH_VARS <var1> <var2> ... <varN>]
13 # [NO_SET_AND_CHECK_MACRO]
14 # [NO_CHECK_REQUIRED_COMPONENTS_MACRO]
15 # [NO_FIND_DEPENDENCY_MACRO])
16 #
17 #
18 #
19 # CONFIGURE_PACKAGE_CONFIG_FILE() should be used instead of the plain
20 # configure_file() command when creating the <Name>Config.cmake or
21 # <Name>-config.cmake file for installing a project or library. It
22 # helps making the resulting package relocatable by avoiding hardcoded
23 # paths in the installed Config.cmake file.
24 #
25 # In a FooConfig.cmake file there may be code like this to make the
26 # install destinations know to the using project:
27 #
28 # ::
29 #
30 # set(FOO_INCLUDE_DIR "@CMAKE_INSTALL_FULL_INCLUDEDIR@" )
31 # set(FOO_DATA_DIR "@CMAKE_INSTALL_PREFIX@/@RELATIVE_DATA_INSTALL_DIR@" )
32 # set(FOO_ICONS_DIR "@CMAKE_INSTALL_PREFIX@/share/icons" )
33 # ...logic to determine installedPrefix from the own location...
34 # set(FOO_CONFIG_DIR "${installedPrefix}/@CONFIG_INSTALL_DIR@" )
35 #
36 # All 4 options shown above are not sufficient, since the first 3
37 # hardcode the absolute directory locations, and the 4th case works only
38 # if the logic to determine the installedPrefix is correct, and if
39 # CONFIG_INSTALL_DIR contains a relative path, which in general cannot
40 # be guaranteed. This has the effect that the resulting FooConfig.cmake
41 # file would work poorly under Windows and OSX, where users are used to
42 # choose the install location of a binary package at install time,
43 # independent from how CMAKE_INSTALL_PREFIX was set at build/cmake time.
44 #
45 # Using CONFIGURE_PACKAGE_CONFIG_FILE() helps. If used correctly, it
46 # makes the resulting FooConfig.cmake file relocatable. Usage:
47 #
48 # ::
49 #
50 # 1. write a FooConfig.cmake.in file as you are used to
51 # 2. insert a line containing only the string "@PACKAGE_INIT@"
52 # 3. instead of set(FOO_DIR "@SOME_INSTALL_DIR@"), use set(FOO_DIR "@PACKAGE_SOME_INSTALL_DIR@")
53 # (this must be after the @PACKAGE_INIT@ line)
54 # 4. instead of using the normal configure_file(), use CONFIGURE_PACKAGE_CONFIG_FILE()
55 #
56 #
57 #
58 # The <input> and <output> arguments are the input and output file, the
59 # same way as in configure_file().
60 #
61 # The <path> given to INSTALL_DESTINATION must be the destination where
62 # the FooConfig.cmake file will be installed to. This can either be a
63 # relative or absolute path, both work.
64 #
65 # The variables <var1> to <varN> given as PATH_VARS are the variables
66 # which contain install destinations. For each of them the macro will
67 # create a helper variable PACKAGE_<var...>. These helper variables
68 # must be used in the FooConfig.cmake.in file for setting the installed
69 # location. They are calculated by CONFIGURE_PACKAGE_CONFIG_FILE() so
70 # that they are always relative to the installed location of the
71 # package. This works both for relative and also for absolute
72 # locations. For absolute locations it works only if the absolute
73 # location is a subdirectory of CMAKE_INSTALL_PREFIX.
74 #
75 # By default configure_package_config_file() also generates two helper
76 # macros, set_and_check() and check_required_components() into the
77 # FooConfig.cmake file.
78 #
79 # set_and_check() should be used instead of the normal set() command for
80 # setting directories and file locations. Additionally to setting the
81 # variable it also checks that the referenced file or directory actually
82 # exists and fails with a FATAL_ERROR otherwise. This makes sure that
83 # the created FooConfig.cmake file does not contain wrong references.
84 # When using the NO_SET_AND_CHECK_MACRO, this macro is not generated
85 # into the FooConfig.cmake file.
86 #
87 # check_required_components(<package_name>) should be called at the end
88 # of the FooConfig.cmake file if the package supports components. This
89 # macro checks whether all requested, non-optional components have been
90 # found, and if this is not the case, sets the Foo_FOUND variable to
91 # FALSE, so that the package is considered to be not found. It does
92 # that by testing the Foo_<Component>_FOUND variables for all requested
93 # required components. When using the NO_CHECK_REQUIRED_COMPONENTS
94 # option, this macro is not generated into the FooConfig.cmake file.
95 #
96 # For an example see below the documentation for
97 # WRITE_BASIC_PACKAGE_VERSION_FILE().
98 #
99 #
100 #
101 # ::
102 #
103 # WRITE_BASIC_PACKAGE_VERSION_FILE( filename VERSION major.minor.patch COMPATIBILITY (AnyNewerVersion|SameMajorVersion|ExactVersion) )
104 #
105 #
106 #
107 # Writes a file for use as <package>ConfigVersion.cmake file to
108 # <filename>. See the documentation of find_package() for details on
109 # this.
110 #
111 # ::
112 #
113 # filename is the output filename, it should be in the build tree.
114 # major.minor.patch is the version number of the project to be installed
115 #
116 # The COMPATIBILITY mode AnyNewerVersion means that the installed
117 # package version will be considered compatible if it is newer or
118 # exactly the same as the requested version. This mode should be used
119 # for packages which are fully backward compatible, also across major
120 # versions. If SameMajorVersion is used instead, then the behaviour
121 # differs from AnyNewerVersion in that the major version number must be
122 # the same as requested, e.g. version 2.0 will not be considered
123 # compatible if 1.0 is requested. This mode should be used for packages
124 # which guarantee backward compatibility within the same major version.
125 # If ExactVersion is used, then the package is only considered
126 # compatible if the requested version matches exactly its own version
127 # number (not considering the tweak version). For example, version
128 # 1.2.3 of a package is only considered compatible to requested version
129 # 1.2.3. This mode is for packages without compatibility guarantees.
130 # If your project has more elaborated version matching rules, you will
131 # need to write your own custom ConfigVersion.cmake file instead of
132 # using this macro.
133 #
134 # Internally, this macro executes configure_file() to create the
135 # resulting version file. Depending on the COMPATIBLITY, either the
136 # file BasicConfigVersion-SameMajorVersion.cmake.in or
137 # BasicConfigVersion-AnyNewerVersion.cmake.in is used. Please note that
138 # these two files are internal to CMake and you should not call
139 # configure_file() on them yourself, but they can be used as starting
140 # point to create more sophisticted custom ConfigVersion.cmake files.
141 #
142 #
143 #
144 # Example using both configure_package_config_file() and
145 # write_basic_package_version_file(): CMakeLists.txt:
146 #
147 # ::
148 #
149 # set(INCLUDE_INSTALL_DIR include/ ... CACHE )
150 # set(LIB_INSTALL_DIR lib/ ... CACHE )
151 # set(SYSCONFIG_INSTALL_DIR etc/foo/ ... CACHE )
152 # ...
153 # include(CMakePackageConfigHelpers)
154 # configure_package_config_file(FooConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/FooConfig.cmake
155 # INSTALL_DESTINATION ${LIB_INSTALL_DIR}/Foo/cmake
156 # PATH_VARS INCLUDE_INSTALL_DIR SYSCONFIG_INSTALL_DIR)
157 # write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/FooConfigVersion.cmake
158 # VERSION 1.2.3
159 # COMPATIBILITY SameMajorVersion )
160 # install(FILES ${CMAKE_CURRENT_BINARY_DIR}/FooConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/FooConfigVersion.cmake
161 # DESTINATION ${LIB_INSTALL_DIR}/Foo/cmake )
162 #
163 #
164 #
165 # With a FooConfig.cmake.in:
166 #
167 # ::
168 #
169 # set(FOO_VERSION x.y.z)
170 # ...
171 # @PACKAGE_INIT@
172 # ...
173 # set_and_check(FOO_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@")
174 # set_and_check(FOO_SYSCONFIG_DIR "@PACKAGE_SYSCONFIG_INSTALL_DIR@")
175 #
176 #
177 #
178 # ::
179 #
180 # check_required_components(Foo)
181 
182 
183 #=============================================================================
184 # Copyright 2012 Alexander Neundorf <neundorf@kde.org>
185 #
186 # Distributed under the OSI-approved BSD License (the "License");
187 # see accompanying file Copyright.txt for details.
188 #
189 # This software is distributed WITHOUT ANY WARRANTY; without even the
190 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
191 # See the License for more information.
192 #=============================================================================
193 # (To distribute this file outside of CMake, substitute the full
194 # License text for the above reference.)
195 
196 include(CMakeParseArguments)
197 
198 include(WriteBasicConfigVersionFile)
199 
200 macro(WRITE_BASIC_PACKAGE_VERSION_FILE)
201  write_basic_config_version_file(${ARGN})
202 endmacro()
203 
204 
205 function(CONFIGURE_PACKAGE_CONFIG_FILE _inputFile _outputFile)
206  set(options NO_SET_AND_CHECK_MACRO NO_CHECK_REQUIRED_COMPONENTS_MACRO NO_FIND_DEPENDENCY_MACRO)
207  set(oneValueArgs INSTALL_DESTINATION )
208  set(multiValueArgs PATH_VARS )
209 
210  cmake_parse_arguments(CCF "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
211 
212  if(CCF_UNPARSED_ARGUMENTS)
213  message(FATAL_ERROR "Unknown keywords given to CONFIGURE_PACKAGE_CONFIG_FILE(): \"${CCF_UNPARSED_ARGUMENTS}\"")
214  endif()
215 
216  if(NOT CCF_INSTALL_DESTINATION)
217  message(FATAL_ERROR "No INSTALL_DESTINATION given to CONFIGURE_PACKAGE_CONFIG_FILE()")
218  endif()
219 
220  if(IS_ABSOLUTE "${CCF_INSTALL_DESTINATION}")
221  set(absInstallDir "${CCF_INSTALL_DESTINATION}")
222  else()
223  set(absInstallDir "${CMAKE_INSTALL_PREFIX}/${CCF_INSTALL_DESTINATION}")
224  endif()
225 
226  file(RELATIVE_PATH PACKAGE_RELATIVE_PATH "${absInstallDir}" "${CMAKE_INSTALL_PREFIX}" )
227 
228  foreach(var ${CCF_PATH_VARS})
229  if(NOT DEFINED ${var})
230  message(FATAL_ERROR "Variable ${var} does not exist")
231  else()
232  if(IS_ABSOLUTE "${${var}}")
233  string(REPLACE "${CMAKE_INSTALL_PREFIX}" "\${PACKAGE_PREFIX_DIR}"
234  PACKAGE_${var} "${${var}}")
235  else()
236  set(PACKAGE_${var} "\${PACKAGE_PREFIX_DIR}/${${var}}")
237  endif()
238  endif()
239  endforeach()
240 
241  get_filename_component(inputFileName "${_inputFile}" NAME)
242 
243  set(PACKAGE_INIT "
244 ####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() #######
245 ####### Any changes to this file will be overwritten by the next CMake run ####
246 ####### The input file was ${inputFileName} ########
247 
248 get_filename_component(PACKAGE_PREFIX_DIR \"\${CMAKE_CURRENT_LIST_DIR}/${PACKAGE_RELATIVE_PATH}\" ABSOLUTE)
249 ")
250 
251  if("${absInstallDir}" MATCHES "^(/usr)?/lib(64)?/.+")
252  # Handle "/usr move" symlinks created by some Linux distros.
253  set(PACKAGE_INIT "${PACKAGE_INIT}
254 # Use original install prefix when loaded through a \"/usr move\"
255 # cross-prefix symbolic link such as /lib -> /usr/lib.
256 get_filename_component(_realCurr \"\${CMAKE_CURRENT_LIST_DIR}\" REALPATH)
257 get_filename_component(_realOrig \"${absInstallDir}\" REALPATH)
258 if(_realCurr STREQUAL _realOrig)
259  set(PACKAGE_PREFIX_DIR \"${CMAKE_INSTALL_PREFIX}\")
260 endif()
261 unset(_realOrig)
262 unset(_realCurr)
263 ")
264  endif()
265 
266  if(NOT CCF_NO_SET_AND_CHECK_MACRO)
267  set(PACKAGE_INIT "${PACKAGE_INIT}
268 macro(set_and_check _var _file)
269  set(\${_var} \"\${_file}\")
270  if(NOT EXISTS \"\${_file}\")
271  message(FATAL_ERROR \"File or directory \${_file} referenced by variable \${_var} does not exist !\")
272  endif()
273 endmacro()
274 ")
275  endif()
276 
277 
278  if(NOT CCF_NO_CHECK_REQUIRED_COMPONENTS_MACRO)
279  set(PACKAGE_INIT "${PACKAGE_INIT}
280 macro(check_required_components _NAME)
281  foreach(comp \${\${_NAME}_FIND_COMPONENTS})
282  if(NOT \${_NAME}_\${comp}_FOUND)
283  if(\${_NAME}_FIND_REQUIRED_\${comp})
284  set(\${_NAME}_FOUND FALSE)
285  endif()
286  endif()
287  endforeach()
288 endmacro()
289 ")
290  endif()
291 
292  if(NOT CCF_NO_FIND_DEPENDENCY_MACRO)
293  set(PACKAGE_INIT "${PACKAGE_INIT}
294 macro(find_dependency dep)
295  if (NOT \${dep}_FOUND)
296  if (\${ARGV1})
297  set(version \${ARGV1})
298  endif()
299  set(exact_arg)
300  if(\${CMAKE_FIND_PACKAGE_NAME}_FIND_VERSION_EXACT)
301  set(exact_arg EXACT)
302  endif()
303  set(quiet_arg)
304  if(\${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY)
305  set(quiet_arg QUIET)
306  endif()
307  set(required_arg)
308  if(\${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED)
309  set(required_arg REQUIRED)
310  endif()
311 
312  find_package(\${dep} \${version} \${exact_arg} \${quiet_arg} \${required_arg})
313  if (NOT \${dep}_FOUND)
314  set(\${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE \"\${CMAKE_FIND_PACKAGE_NAME} could not be found because dependency \${dep} could not be found.\")
315  set(\${CMAKE_FIND_PACKAGE_NAME}_FOUND False)
316  return()
317  endif()
318  set(required_arg)
319  set(quiet_arg)
320  set(exact_arg)
321  endif()
322 endmacro()
323 ")
324  endif()
325 
326  set(PACKAGE_INIT "${PACKAGE_INIT}
327 ####################################################################################")
328 
329  configure_file("${_inputFile}" "${_outputFile}" @ONLY)
330 
331 endfunction()