CMakePackageConfigHelpers.cmake 11 KB

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