ECMPackageConfigHelpers.cmake 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #.rst:
  2. # ECMPackageConfigHelpers
  3. # -----------------------
  4. #
  5. # Helper macros for generating CMake package config files.
  6. #
  7. # ``write_basic_package_version_file()`` is the same as the one provided by the
  8. # `CMakePackageConfigHelpers
  9. # <https://www.cmake.org/cmake/help/v2.8.12/cmake.html#module:CMakePackageConfigHelpers>`_
  10. # module in CMake; see that module's documentation for
  11. # more information.
  12. #
  13. # ::
  14. #
  15. # ecm_configure_package_config_file(<input> <output>
  16. # INSTALL_DESTINATION <path>
  17. # [PATH_VARS <var1> [<var2> [...]]
  18. # [NO_SET_AND_CHECK_MACRO]
  19. # [NO_CHECK_REQUIRED_COMPONENTS_MACRO])
  20. #
  21. #
  22. # This behaves in the same way as configure_package_config_file() from CMake
  23. # 2.8.12, except that it adds an extra helper macro: find_dependency(). It is
  24. # highly recommended that you read the `documentation for
  25. # CMakePackageConfigHelpers
  26. # <https://www.cmake.org/cmake/help/v2.8.12/cmake.html#module:CMakePackageConfigHelpers>`_
  27. # for more information, particularly with regard to the PATH_VARS argument.
  28. #
  29. # Note that there is no argument that will disable the find_dependency() macro;
  30. # if you do not require this macro, you should use
  31. # ``configure_package_config_file`` from the CMakePackageConfigHelpers module.
  32. #
  33. # CMake 3.0 includes a CMakeFindDependencyMacro module that provides the
  34. # find_dependency() macro (which you can ``include()`` in your package config
  35. # file), so this file is only useful for projects wishing to provide config
  36. # files that will work with CMake 2.8.12.
  37. #
  38. # Additional Config File Macros
  39. # =============================
  40. #
  41. # ::
  42. #
  43. # find_dependency(<dep> [<version> [EXACT]])
  44. #
  45. # find_dependency() should be used instead of find_package() to find package
  46. # dependencies. It forwards the correct parameters for EXACT, QUIET and
  47. # REQUIRED which were passed to the original find_package() call. It also sets
  48. # an informative diagnostic message if the dependency could not be found.
  49. #
  50. # Since pre-1.0.0.
  51. #=============================================================================
  52. # SPDX-FileCopyrightText: 2014 Alex Merry <alex.merry@kdemail.net>
  53. # SPDX-FileCopyrightText: 2013 Stephen Kelly <steveire@gmail.com>
  54. #
  55. # SPDX-License-Identifier: BSD-3-Clause
  56. include(${CMAKE_ROOT}/Modules/CMakePackageConfigHelpers.cmake)
  57. set(_ecm_package_config_helpers_included TRUE)
  58. if(NOT CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.13)
  59. message(AUTHOR_WARNING "Your project already requires a version of CMake that includes the find_dependency macro via the CMakeFindDependencyMacro module. You should use CMakePackageConfigHelpers instead of ECMPackageConfigHelpers.")
  60. endif()
  61. function(ECM_CONFIGURE_PACKAGE_CONFIG_FILE _inputFile _outputFile)
  62. set(options NO_SET_AND_CHECK_MACRO NO_CHECK_REQUIRED_COMPONENTS_MACRO)
  63. set(oneValueArgs INSTALL_DESTINATION )
  64. set(multiValueArgs PATH_VARS )
  65. cmake_parse_arguments(CCF "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  66. if(CCF_UNPARSED_ARGUMENTS)
  67. message(FATAL_ERROR "Unknown keywords given to CONFIGURE_PACKAGE_CONFIG_FILE(): \"${CCF_UNPARSED_ARGUMENTS}\"")
  68. endif()
  69. if(NOT CCF_INSTALL_DESTINATION)
  70. message(FATAL_ERROR "No INSTALL_DESTINATION given to CONFIGURE_PACKAGE_CONFIG_FILE()")
  71. endif()
  72. if(IS_ABSOLUTE "${CCF_INSTALL_DESTINATION}")
  73. set(absInstallDir "${CCF_INSTALL_DESTINATION}")
  74. else()
  75. set(absInstallDir "${CMAKE_INSTALL_PREFIX}/${CCF_INSTALL_DESTINATION}")
  76. endif()
  77. file(RELATIVE_PATH PACKAGE_RELATIVE_PATH "${absInstallDir}" "${CMAKE_INSTALL_PREFIX}" )
  78. foreach(var ${CCF_PATH_VARS})
  79. if(NOT DEFINED ${var})
  80. message(FATAL_ERROR "Variable ${var} does not exist")
  81. else()
  82. if(IS_ABSOLUTE "${${var}}")
  83. string(REPLACE "${CMAKE_INSTALL_PREFIX}" "\${PACKAGE_PREFIX_DIR}"
  84. PACKAGE_${var} "${${var}}")
  85. else()
  86. set(PACKAGE_${var} "\${PACKAGE_PREFIX_DIR}/${${var}}")
  87. endif()
  88. endif()
  89. endforeach()
  90. get_filename_component(inputFileName "${_inputFile}" NAME)
  91. set(PACKAGE_INIT "
  92. ####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() (ECM variant) #######
  93. ####### Any changes to this file will be overwritten by the next CMake run #######
  94. ####### The input file was ${inputFileName} #######
  95. get_filename_component(PACKAGE_PREFIX_DIR \"\${CMAKE_CURRENT_LIST_DIR}/${PACKAGE_RELATIVE_PATH}\" ABSOLUTE)
  96. ")
  97. if("${absInstallDir}" MATCHES "^(/usr)?/lib(64)?/.+")
  98. # Handle "/usr move" symlinks created by some Linux distros.
  99. set(PACKAGE_INIT "${PACKAGE_INIT}
  100. # Use original install prefix when loaded through a \"/usr move\"
  101. # cross-prefix symbolic link such as /lib -> /usr/lib.
  102. get_filename_component(_realCurr \"\${CMAKE_CURRENT_LIST_DIR}\" REALPATH)
  103. get_filename_component(_realOrig \"${absInstallDir}\" REALPATH)
  104. if(_realCurr STREQUAL _realOrig)
  105. set(PACKAGE_PREFIX_DIR \"${CMAKE_INSTALL_PREFIX}\")
  106. endif()
  107. unset(_realOrig)
  108. unset(_realCurr)
  109. ")
  110. endif()
  111. if(NOT CCF_NO_SET_AND_CHECK_MACRO)
  112. set(PACKAGE_INIT "${PACKAGE_INIT}
  113. macro(set_and_check _var _file)
  114. set(\${_var} \"\${_file}\")
  115. if(NOT EXISTS \"\${_file}\")
  116. message(FATAL_ERROR \"File or directory \${_file} referenced by variable \${_var} does not exist !\")
  117. endif()
  118. endmacro()
  119. include(CMakeFindDependencyMacro OPTIONAL RESULT_VARIABLE _CMakeFindDependencyMacro_FOUND)
  120. if (NOT _CMakeFindDependencyMacro_FOUND)
  121. macro(find_dependency dep)
  122. if (NOT \${dep}_FOUND)
  123. set(ecm_fd_version)
  124. if (\${ARGC} GREATER 1)
  125. set(ecm_fd_version \${ARGV1})
  126. endif()
  127. set(ecm_fd_exact_arg)
  128. if(\${CMAKE_FIND_PACKAGE_NAME}_FIND_VERSION_EXACT)
  129. set(ecm_fd_exact_arg EXACT)
  130. endif()
  131. set(ecm_fd_quiet_arg)
  132. if(\${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY)
  133. set(ecm_fd_quiet_arg QUIET)
  134. endif()
  135. set(ecm_fd_required_arg)
  136. if(\${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED)
  137. set(ecm_fd_required_arg REQUIRED)
  138. endif()
  139. find_package(\${dep} \${ecm_fd_version}
  140. \${ecm_fd_exact_arg}
  141. \${ecm_fd_quiet_arg}
  142. \${ecm_fd_required_arg}
  143. )
  144. if (NOT \${dep}_FOUND)
  145. set(\${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE \"\${CMAKE_FIND_PACKAGE_NAME} could not be found because dependency \${dep} could not be found.\")
  146. set(\${CMAKE_FIND_PACKAGE_NAME}_FOUND False)
  147. return()
  148. endif()
  149. set(ecm_fd_version)
  150. set(ecm_fd_required_arg)
  151. set(ecm_fd_quiet_arg)
  152. set(ecm_fd_exact_arg)
  153. endif()
  154. endmacro()
  155. endif()
  156. ")
  157. endif()
  158. if(NOT CCF_NO_CHECK_REQUIRED_COMPONENTS_MACRO)
  159. set(PACKAGE_INIT "${PACKAGE_INIT}
  160. macro(check_required_components _NAME)
  161. foreach(comp \${\${_NAME}_FIND_COMPONENTS})
  162. if(NOT \${_NAME}_\${comp}_FOUND)
  163. if(\${_NAME}_FIND_REQUIRED_\${comp})
  164. set(\${_NAME}_FOUND FALSE)
  165. endif()
  166. endif()
  167. endforeach()
  168. endmacro()
  169. ")
  170. endif()
  171. set(PACKAGE_INIT "${PACKAGE_INIT}
  172. ####################################################################################")
  173. configure_file("${_inputFile}" "${_outputFile}" @ONLY)
  174. endfunction()