CMakePackageConfigHelpers.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. # Copyright 2012 Alexander Neundorf <neundorf@kde.org>
  183. #
  184. # Distributed under the OSI-approved BSD License (the "License");
  185. # see accompanying file Copyright.txt for details.
  186. #
  187. # This software is distributed WITHOUT ANY WARRANTY; without even the
  188. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  189. # See the License for more information.
  190. #=============================================================================
  191. # (To distribute this file outside of CMake, substitute the full
  192. # License text for the above reference.)
  193. include(CMakeParseArguments)
  194. include(WriteBasicConfigVersionFile)
  195. macro(WRITE_BASIC_PACKAGE_VERSION_FILE)
  196. write_basic_config_version_file(${ARGN})
  197. endmacro()
  198. function(CONFIGURE_PACKAGE_CONFIG_FILE _inputFile _outputFile)
  199. set(options NO_SET_AND_CHECK_MACRO NO_CHECK_REQUIRED_COMPONENTS_MACRO NO_FIND_DEPENDENCY_MACRO)
  200. set(oneValueArgs INSTALL_DESTINATION )
  201. set(multiValueArgs PATH_VARS )
  202. cmake_parse_arguments(CCF "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  203. if(CCF_UNPARSED_ARGUMENTS)
  204. message(FATAL_ERROR "Unknown keywords given to CONFIGURE_PACKAGE_CONFIG_FILE(): \"${CCF_UNPARSED_ARGUMENTS}\"")
  205. endif()
  206. if(NOT CCF_INSTALL_DESTINATION)
  207. message(FATAL_ERROR "No INSTALL_DESTINATION given to CONFIGURE_PACKAGE_CONFIG_FILE()")
  208. endif()
  209. if(IS_ABSOLUTE "${CCF_INSTALL_DESTINATION}")
  210. set(absInstallDir "${CCF_INSTALL_DESTINATION}")
  211. else()
  212. set(absInstallDir "${CMAKE_INSTALL_PREFIX}/${CCF_INSTALL_DESTINATION}")
  213. endif()
  214. file(RELATIVE_PATH PACKAGE_RELATIVE_PATH "${absInstallDir}" "${CMAKE_INSTALL_PREFIX}" )
  215. foreach(var ${CCF_PATH_VARS})
  216. if(NOT DEFINED ${var})
  217. message(FATAL_ERROR "Variable ${var} does not exist")
  218. else()
  219. if(IS_ABSOLUTE "${${var}}")
  220. string(REPLACE "${CMAKE_INSTALL_PREFIX}" "\${PACKAGE_PREFIX_DIR}"
  221. PACKAGE_${var} "${${var}}")
  222. else()
  223. set(PACKAGE_${var} "\${PACKAGE_PREFIX_DIR}/${${var}}")
  224. endif()
  225. endif()
  226. endforeach()
  227. get_filename_component(inputFileName "${_inputFile}" NAME)
  228. set(PACKAGE_INIT "
  229. ####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() #######
  230. ####### Any changes to this file will be overwritten by the next CMake run ####
  231. ####### The input file was ${inputFileName} ########
  232. get_filename_component(PACKAGE_PREFIX_DIR \"\${CMAKE_CURRENT_LIST_DIR}/${PACKAGE_RELATIVE_PATH}\" ABSOLUTE)
  233. ")
  234. if("${absInstallDir}" MATCHES "^(/usr)?/lib(64)?/.+")
  235. # Handle "/usr move" symlinks created by some Linux distros.
  236. set(PACKAGE_INIT "${PACKAGE_INIT}
  237. # Use original install prefix when loaded through a \"/usr move\"
  238. # cross-prefix symbolic link such as /lib -> /usr/lib.
  239. get_filename_component(_realCurr \"\${CMAKE_CURRENT_LIST_DIR}\" REALPATH)
  240. get_filename_component(_realOrig \"${absInstallDir}\" REALPATH)
  241. if(_realCurr STREQUAL _realOrig)
  242. set(PACKAGE_PREFIX_DIR \"${CMAKE_INSTALL_PREFIX}\")
  243. endif()
  244. unset(_realOrig)
  245. unset(_realCurr)
  246. ")
  247. endif()
  248. if(NOT CCF_NO_SET_AND_CHECK_MACRO)
  249. set(PACKAGE_INIT "${PACKAGE_INIT}
  250. macro(set_and_check _var _file)
  251. set(\${_var} \"\${_file}\")
  252. if(NOT EXISTS \"\${_file}\")
  253. message(FATAL_ERROR \"File or directory \${_file} referenced by variable \${_var} does not exist !\")
  254. endif()
  255. endmacro()
  256. ")
  257. endif()
  258. if(NOT CCF_NO_CHECK_REQUIRED_COMPONENTS_MACRO)
  259. set(PACKAGE_INIT "${PACKAGE_INIT}
  260. macro(check_required_components _NAME)
  261. foreach(comp \${\${_NAME}_FIND_COMPONENTS})
  262. if(NOT \${_NAME}_\${comp}_FOUND)
  263. if(\${_NAME}_FIND_REQUIRED_\${comp})
  264. set(\${_NAME}_FOUND FALSE)
  265. endif()
  266. endif()
  267. endforeach()
  268. endmacro()
  269. ")
  270. endif()
  271. if(NOT CCF_NO_FIND_DEPENDENCY_MACRO)
  272. set(PACKAGE_INIT "${PACKAGE_INIT}
  273. macro(find_dependency dep)
  274. if (NOT \${dep}_FOUND)
  275. if (\${ARGV1})
  276. set(version \${ARGV1})
  277. endif()
  278. set(exact_arg)
  279. if(\${CMAKE_FIND_PACKAGE_NAME}_FIND_VERSION_EXACT)
  280. set(exact_arg EXACT)
  281. endif()
  282. set(quiet_arg)
  283. if(\${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY)
  284. set(quiet_arg QUIET)
  285. endif()
  286. set(required_arg)
  287. if(\${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED)
  288. set(required_arg REQUIRED)
  289. endif()
  290. find_package(\${dep} \${version} \${exact_arg} \${quiet_arg} \${required_arg})
  291. if (NOT \${dep}_FOUND)
  292. set(\${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE \"\${CMAKE_FIND_PACKAGE_NAME} could not be found because dependency \${dep} could not be found.\")
  293. set(\${CMAKE_FIND_PACKAGE_NAME}_FOUND False)
  294. return()
  295. endif()
  296. set(required_arg)
  297. set(quiet_arg)
  298. set(exact_arg)
  299. endif()
  300. endmacro()
  301. ")
  302. endif()
  303. set(PACKAGE_INIT "${PACKAGE_INIT}
  304. ####################################################################################")
  305. configure_file("${_inputFile}" "${_outputFile}" @ONLY)
  306. endfunction()