CMakePackageConfigHelpers.cmake 12 KB

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