CMakePackageConfigHelpers.cmake 11 KB

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