CMakePackageConfigHelpers.cmake 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CMakePackageConfigHelpers
  5. -------------------------
  6. Helper functions for creating config files that can be included by other
  7. projects to find and use a package.
  8. Generating a Package Configuration File
  9. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  10. .. command:: configure_package_config_file
  11. Create a config file for a project:
  12. .. code-block:: cmake
  13. configure_package_config_file(<input> <output>
  14. INSTALL_DESTINATION <path>
  15. [PATH_VARS <var1> <var2> ... <varN>]
  16. [NO_SET_AND_CHECK_MACRO]
  17. [NO_CHECK_REQUIRED_COMPONENTS_MACRO]
  18. [INSTALL_PREFIX <path>]
  19. )
  20. ``configure_package_config_file()`` should be used instead of the plain
  21. :command:`configure_file()` command when creating the ``<PackageName>Config.cmake``
  22. or ``<PackageName>-config.cmake`` file for installing a project or library.
  23. It helps make the resulting package relocatable by avoiding hardcoded paths
  24. in the installed ``<PackageName>Config.cmake`` file.
  25. In a ``FooConfig.cmake`` file there may be code like this to make the install
  26. destinations known to the using project:
  27. .. code-block:: cmake
  28. set(FOO_INCLUDE_DIR "@CMAKE_INSTALL_FULL_INCLUDEDIR@" )
  29. set(FOO_DATA_DIR "@CMAKE_INSTALL_PREFIX@/@RELATIVE_DATA_INSTALL_DIR@" )
  30. set(FOO_ICONS_DIR "@CMAKE_INSTALL_PREFIX@/share/icons" )
  31. #...logic to determine installedPrefix from the own location...
  32. set(FOO_CONFIG_DIR "${installedPrefix}/@CONFIG_INSTALL_DIR@" )
  33. All four options shown above are not sufficient The first three hardcode the
  34. absolute directory locations. The fourth case works only if the logic to
  35. determine the ``installedPrefix`` is correct, and if ``CONFIG_INSTALL_DIR``
  36. contains a relative path, which in general cannot be guaranteed. This has the
  37. effect that the resulting ``FooConfig.cmake`` file would work poorly under
  38. Windows and macOS, where users are used to choosing the install location of a
  39. binary package at install time, independent from how
  40. :variable:`CMAKE_INSTALL_PREFIX` was set at build/cmake time.
  41. Using ``configure_package_config_file()`` helps. If used correctly, it makes
  42. the resulting ``FooConfig.cmake`` file relocatable. Usage:
  43. 1. Write a ``FooConfig.cmake.in`` file as you are used to.
  44. 2. Insert a line at the top containing only the string ``@PACKAGE_INIT@``.
  45. 3. Instead of ``set(FOO_DIR "@SOME_INSTALL_DIR@")``, use
  46. ``set(FOO_DIR "@PACKAGE_SOME_INSTALL_DIR@")`` (this must be after the
  47. ``@PACKAGE_INIT@`` line).
  48. 4. Instead of using the normal :command:`configure_file()` command, use
  49. ``configure_package_config_file()``.
  50. The ``<input>`` and ``<output>`` arguments are the input and output file, the
  51. same way as in :command:`configure_file()`.
  52. The ``<path>`` given to ``INSTALL_DESTINATION`` must be the destination where
  53. the ``FooConfig.cmake`` file will be installed to. This path can either be
  54. absolute, or relative to the ``INSTALL_PREFIX`` path.
  55. The variables ``<var1>`` to ``<varN>`` given as ``PATH_VARS`` are the
  56. variables which contain install destinations. For each of them, the macro will
  57. create a helper variable ``PACKAGE_<var...>``. These helper variables must be
  58. used in the ``FooConfig.cmake.in`` file for setting the installed location.
  59. They are calculated by ``configure_package_config_file()`` so that they are
  60. always relative to the installed location of the package. This works both for
  61. relative and also for absolute locations. For absolute locations, it works
  62. only if the absolute location is a subdirectory of ``INSTALL_PREFIX``.
  63. .. versionadded:: 3.30
  64. The variable ``PACKAGE_PREFIX_DIR`` will always be defined after the
  65. ``@PACKAGE_INIT@`` line. It will hold the value of the base install
  66. location. In general, variables defined via the ``PATH_VARS`` mechanism
  67. should be used instead, but ``PACKAGE_PREFIX_DIR`` can be used for those
  68. cases not easily handled by ``PATH_VARS``, such as for files installed
  69. directly to the base install location rather than a subdirectory of it.
  70. .. note::
  71. When consumers of the generated file use CMake 3.29 or older, the value
  72. of ``PACKAGE_PREFIX_DIR`` can be changed by a call to
  73. :command:`find_dependency` or :command:`find_package`.
  74. If a project relies on ``PACKAGE_PREFIX_DIR``, it is the project's
  75. responsibility to ensure that the value of ``PACKAGE_PREFIX_DIR`` is
  76. preserved across any such calls, or any other calls which might include
  77. another file generated by ``configure_package_config_file()``.
  78. .. versionadded:: 3.1
  79. If the ``INSTALL_PREFIX`` argument is passed, this is used as the base path to
  80. calculate all the relative paths. The ``<path>`` argument must be an absolute
  81. path. If this argument is not passed, the :variable:`CMAKE_INSTALL_PREFIX`
  82. variable will be used instead. The default value is good when generating a
  83. ``FooConfig.cmake`` file to use your package from the install tree. When
  84. generating a ``FooConfig.cmake`` file to use your package from the build tree,
  85. this option should be used.
  86. By default, ``configure_package_config_file()`` also generates two helper
  87. macros, ``set_and_check()`` and ``check_required_components()``, into the
  88. ``FooConfig.cmake`` file.
  89. ``set_and_check()`` should be used instead of the normal :command:`set` command
  90. for setting directories and file locations. In addition to setting the
  91. variable, it also checks that the referenced file or directory actually exists
  92. and fails with a fatal error if it doesn't. This ensures that the generated
  93. ``FooConfig.cmake`` file does not contain wrong references.
  94. Add the ``NO_SET_AND_CHECK_MACRO`` option to prevent the generation of the
  95. ``set_and_check()`` macro in the ``FooConfig.cmake`` file.
  96. ``check_required_components(<PackageName>)`` should be called at the end of
  97. the ``FooConfig.cmake`` file. This macro checks whether all requested,
  98. non-optional components have been found, and if this is not the case, it sets
  99. the ``Foo_FOUND`` variable to ``FALSE`` so that the package is considered to
  100. be not found. It does that by testing the ``Foo_<Component>_FOUND``
  101. variables for all requested required components. This macro should be
  102. called even if the package doesn't provide any components to make sure
  103. users are not specifying components erroneously. Add the
  104. ``NO_CHECK_REQUIRED_COMPONENTS_MACRO`` option to prevent the generation of the
  105. ``check_required_components()`` macro in the ``FooConfig.cmake`` file.
  106. See also :ref:`CMakePackageConfigHelpers Examples`.
  107. Generating a Package Version File
  108. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  109. .. command:: write_basic_package_version_file
  110. Create a version file for a project:
  111. .. code-block:: cmake
  112. write_basic_package_version_file(<filename>
  113. [VERSION <major.minor.patch>]
  114. COMPATIBILITY <AnyNewerVersion|SameMajorVersion|SameMinorVersion|ExactVersion>
  115. [ARCH_INDEPENDENT] )
  116. Writes a file for use as a ``<PackageName>ConfigVersion.cmake`` file to
  117. ``<filename>``. See the documentation of :command:`find_package()` for
  118. details on such files.
  119. ``<filename>`` is the output filename, which should be in the build tree.
  120. ``<major.minor.patch>`` is the version number of the project to be installed.
  121. If no ``VERSION`` is given, the :variable:`PROJECT_VERSION` variable is used.
  122. If this hasn't been set, it errors out.
  123. The ``COMPATIBILITY`` mode ``AnyNewerVersion`` means that the installed
  124. package version will be considered compatible if it is newer or exactly the
  125. same as the requested version. This mode should be used for packages which
  126. are fully backward compatible, also across major versions.
  127. If ``SameMajorVersion`` is used instead, then the behavior differs from
  128. ``AnyNewerVersion`` in that the major version number must be the same as
  129. requested, e.g. version 2.0 will not be considered compatible if 1.0 is
  130. requested. This mode should be used for packages which guarantee backward
  131. compatibility within the same major version.
  132. If ``SameMinorVersion`` is used, the behavior is the same as
  133. ``SameMajorVersion``, but both major and minor version must be the same as
  134. requested, e.g version 0.2 will not be compatible if 0.1 is requested.
  135. If ``ExactVersion`` is used, then the package is only considered compatible if
  136. the requested version matches exactly its own version number (not considering
  137. the tweak version). For example, version 1.2.3 of a package is only
  138. considered compatible to requested version 1.2.3. This mode is for packages
  139. without compatibility guarantees.
  140. If your project has more elaborate version matching rules, you will need to
  141. write your own custom ``<PackageName>ConfigVersion.cmake`` file instead of
  142. using this macro.
  143. .. versionadded:: 3.11
  144. The ``SameMinorVersion`` compatibility mode.
  145. .. versionadded:: 3.14
  146. If ``ARCH_INDEPENDENT`` is given, the installed package version will be
  147. considered compatible even if it was built for a different architecture than
  148. the requested architecture. Otherwise, an architecture check will be performed,
  149. and the package will be considered compatible only if the architecture matches
  150. exactly. For example, if the package is built for a 32-bit architecture, the
  151. package is only considered compatible if it is used on a 32-bit architecture,
  152. unless ``ARCH_INDEPENDENT`` is given, in which case the package is considered
  153. compatible on any architecture.
  154. .. note:: ``ARCH_INDEPENDENT`` is intended for header-only libraries or
  155. similar packages with no binaries.
  156. .. versionadded:: 3.19
  157. The version file generated by ``AnyNewerVersion``, ``SameMajorVersion`` and
  158. ``SameMinorVersion`` arguments of ``COMPATIBILITY`` handle the version range,
  159. if one is specified (see :command:`find_package` command for the details).
  160. ``ExactVersion`` mode is incompatible with version ranges and will display an
  161. author warning if one is specified.
  162. Internally, this macro executes :command:`configure_file()` to create the
  163. resulting version file. Depending on the ``COMPATIBILITY``, the corresponding
  164. ``BasicConfigVersion-<COMPATIBILITY>.cmake.in`` file is used.
  165. Please note that these files are internal to CMake and you should not call
  166. :command:`configure_file()` on them yourself, but they can be used as a starting
  167. point to create more sophisticated custom ``<PackageName>ConfigVersion.cmake``
  168. files.
  169. Generating an Apple Platform Selection File
  170. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  171. .. command:: generate_apple_platform_selection_file
  172. .. versionadded:: 3.29
  173. Create an Apple platform selection file:
  174. .. code-block:: cmake
  175. generate_apple_platform_selection_file(<filename>
  176. INSTALL_DESTINATION <path>
  177. [INSTALL_PREFIX <path>]
  178. [MACOS_INCLUDE_FILE <file>]
  179. [IOS_INCLUDE_FILE <file>]
  180. [IOS_SIMULATOR_INCLUDE_FILE <file>]
  181. [IOS_CATALYST_INCLUDE_FILE <file>]
  182. [TVOS_INCLUDE_FILE <file>]
  183. [TVOS_SIMULATOR_INCLUDE_FILE <file>]
  184. [WATCHOS_INCLUDE_FILE <file>]
  185. [WATCHOS_SIMULATOR_INCLUDE_FILE <file>]
  186. [VISIONOS_INCLUDE_FILE <file>]
  187. [VISIONOS_SIMULATOR_INCLUDE_FILE <file>]
  188. [ERROR_VARIABLE <variable>]
  189. )
  190. Write a file that includes an Apple-platform-specific ``.cmake`` file,
  191. e.g., for use as ``<PackageName>Config.cmake``. This can be used in
  192. conjunction with the ``XCFRAMEWORK_LOCATION`` argument of
  193. :command:`export(SETUP)` to export packages in a way that a project
  194. built for any Apple platform can use them.
  195. ``INSTALL_DESTINATION <path>``
  196. Path to which the generated file will be installed by the caller, e.g.,
  197. via :command:`install(FILES)`. The path may be either relative to the
  198. ``INSTALL_PREFIX`` or absolute.
  199. ``INSTALL_PREFIX <path>``
  200. Path prefix to which the package will be installed by the caller.
  201. The ``<path>`` argument must be an absolute path. If this argument
  202. is not passed, the :variable:`CMAKE_INSTALL_PREFIX` variable will be
  203. used instead.
  204. ``MACOS_INCLUDE_FILE <file>``
  205. File to include if the platform is macOS.
  206. ``IOS_INCLUDE_FILE <file>``
  207. File to include if the platform is iOS.
  208. ``IOS_SIMULATOR_INCLUDE_FILE <file>``
  209. File to include if the platform is iOS Simulator.
  210. ``IOS_CATALYST_INCLUDE_FILE <file>``
  211. .. versionadded:: 3.31
  212. File to include if the platform is iOS Catalyst.
  213. ``TVOS_INCLUDE_FILE <file>``
  214. File to include if the platform is tvOS.
  215. ``TVOS_SIMULATOR_INCLUDE_FILE <file>``
  216. File to include if the platform is tvOS Simulator.
  217. ``WATCHOS_INCLUDE_FILE <file>``
  218. File to include if the platform is watchOS.
  219. ``WATCHOS_SIMULATOR_INCLUDE_FILE <file>``
  220. File to include if the platform is watchOS Simulator.
  221. ``VISIONOS_INCLUDE_FILE <file>``
  222. File to include if the platform is visionOS.
  223. ``VISIONOS_SIMULATOR_INCLUDE_FILE <file>``
  224. File to include if the platform is visionOS Simulator.
  225. ``ERROR_VARIABLE <variable>``
  226. If the consuming project is built for an unsupported platform,
  227. set ``<variable>`` to an error message. The includer may use this
  228. information to pretend the package was not found. If this option
  229. is not given, the default behavior is to issue a fatal error.
  230. If any of the optional include files is not specified, and the consuming
  231. project is built for its corresponding platform, the generated file will
  232. consider the platform to be unsupported. The behavior is determined
  233. by the ``ERROR_VARIABLE`` option.
  234. Generating an Apple Architecture Selection File
  235. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  236. .. command:: generate_apple_architecture_selection_file
  237. .. versionadded:: 3.29
  238. Create an Apple architecture selection file:
  239. .. code-block:: cmake
  240. generate_apple_architecture_selection_file(<filename>
  241. INSTALL_DESTINATION <path>
  242. [INSTALL_PREFIX <path>]
  243. [SINGLE_ARCHITECTURES <arch>...
  244. SINGLE_ARCHITECTURE_INCLUDE_FILES <file>...]
  245. [UNIVERSAL_ARCHITECTURES <arch>...
  246. UNIVERSAL_INCLUDE_FILE <file>]
  247. [ERROR_VARIABLE <variable>]
  248. )
  249. Write a file that includes an Apple-architecture-specific ``.cmake`` file
  250. based on :variable:`CMAKE_OSX_ARCHITECTURES`, e.g., for inclusion from an
  251. Apple-specific ``<PackageName>Config.cmake`` file.
  252. ``INSTALL_DESTINATION <path>``
  253. Path to which the generated file will be installed by the caller, e.g.,
  254. via :command:`install(FILES)`. The path may be either relative to the
  255. ``INSTALL_PREFIX`` or absolute.
  256. ``INSTALL_PREFIX <path>``
  257. Path prefix to which the package will be installed by the caller.
  258. The ``<path>`` argument must be an absolute path. If this argument
  259. is not passed, the :variable:`CMAKE_INSTALL_PREFIX` variable will be
  260. used instead.
  261. ``SINGLE_ARCHITECTURES <arch>...``
  262. Architectures provided by entries of ``SINGLE_ARCHITECTURE_INCLUDE_FILES``.
  263. ``SINGLE_ARCHITECTURE_INCLUDE_FILES <file>...``
  264. Architecture-specific files. One of them will be loaded
  265. when :variable:`CMAKE_OSX_ARCHITECTURES` contains a single
  266. architecture matching the corresponding entry of
  267. ``SINGLE_ARCHITECTURES``.
  268. ``UNIVERSAL_ARCHITECTURES <arch>...``
  269. Architectures provided by the ``UNIVERSAL_INCLUDE_FILE``.
  270. The list may include ``$(ARCHS_STANDARD)`` to support consumption using
  271. the :generator:`Xcode` generator, but the architectures should always
  272. be listed individually too.
  273. ``UNIVERSAL_INCLUDE_FILE <file>``
  274. A file to load when :variable:`CMAKE_OSX_ARCHITECTURES` contains
  275. a (non-strict) subset of the ``UNIVERSAL_ARCHITECTURES`` and
  276. does not match any one of the ``SINGLE_ARCHITECTURES``.
  277. ``ERROR_VARIABLE <variable>``
  278. If the consuming project is built for an unsupported architecture,
  279. set ``<variable>`` to an error message. The includer may use this
  280. information to pretend the package was not found. If this option
  281. is not given, the default behavior is to issue a fatal error.
  282. .. _`CMakePackageConfigHelpers Examples`:
  283. Example Generating Package Files
  284. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  285. Example using both the :command:`configure_package_config_file` and
  286. :command:`write_basic_package_version_file()` commands:
  287. .. code-block:: cmake
  288. :caption: ``CMakeLists.txt``
  289. include(GNUInstallDirs)
  290. set(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR}/Foo
  291. CACHE PATH "Location of header files" )
  292. set(SYSCONFIG_INSTALL_DIR ${CMAKE_INSTALL_SYSCONFDIR}/foo
  293. CACHE PATH "Location of configuration files" )
  294. #...
  295. include(CMakePackageConfigHelpers)
  296. configure_package_config_file(FooConfig.cmake.in
  297. ${CMAKE_CURRENT_BINARY_DIR}/FooConfig.cmake
  298. INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Foo
  299. PATH_VARS INCLUDE_INSTALL_DIR SYSCONFIG_INSTALL_DIR)
  300. write_basic_package_version_file(
  301. ${CMAKE_CURRENT_BINARY_DIR}/FooConfigVersion.cmake
  302. VERSION 1.2.3
  303. COMPATIBILITY SameMajorVersion )
  304. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/FooConfig.cmake
  305. ${CMAKE_CURRENT_BINARY_DIR}/FooConfigVersion.cmake
  306. DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Foo )
  307. .. code-block:: cmake
  308. :caption: ``FooConfig.cmake.in``
  309. :force:
  310. set(FOO_VERSION x.y.z)
  311. ...
  312. @PACKAGE_INIT@
  313. ...
  314. set_and_check(FOO_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@")
  315. set_and_check(FOO_SYSCONFIG_DIR "@PACKAGE_SYSCONFIG_INSTALL_DIR@")
  316. check_required_components(Foo)
  317. #]=======================================================================]
  318. include(WriteBasicConfigVersionFile)
  319. macro(WRITE_BASIC_PACKAGE_VERSION_FILE)
  320. write_basic_config_version_file(${ARGN})
  321. endmacro()
  322. function(CONFIGURE_PACKAGE_CONFIG_FILE _inputFile _outputFile)
  323. set(options NO_SET_AND_CHECK_MACRO NO_CHECK_REQUIRED_COMPONENTS_MACRO)
  324. set(oneValueArgs INSTALL_DESTINATION INSTALL_PREFIX)
  325. set(multiValueArgs PATH_VARS )
  326. cmake_parse_arguments(CCF "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  327. if(CCF_UNPARSED_ARGUMENTS)
  328. message(FATAL_ERROR "Unknown keywords given to CONFIGURE_PACKAGE_CONFIG_FILE(): \"${CCF_UNPARSED_ARGUMENTS}\"")
  329. endif()
  330. if(NOT CCF_INSTALL_DESTINATION)
  331. message(FATAL_ERROR "No INSTALL_DESTINATION given to CONFIGURE_PACKAGE_CONFIG_FILE()")
  332. endif()
  333. if(DEFINED CCF_INSTALL_PREFIX)
  334. if(IS_ABSOLUTE "${CCF_INSTALL_PREFIX}")
  335. set(installPrefix "${CCF_INSTALL_PREFIX}")
  336. else()
  337. message(FATAL_ERROR "INSTALL_PREFIX must be an absolute path")
  338. endif()
  339. elseif(IS_ABSOLUTE "${CMAKE_INSTALL_PREFIX}")
  340. set(installPrefix "${CMAKE_INSTALL_PREFIX}")
  341. else()
  342. get_filename_component(installPrefix "${CMAKE_INSTALL_PREFIX}" ABSOLUTE)
  343. endif()
  344. if(IS_ABSOLUTE "${CCF_INSTALL_DESTINATION}")
  345. set(absInstallDir "${CCF_INSTALL_DESTINATION}")
  346. else()
  347. set(absInstallDir "${installPrefix}/${CCF_INSTALL_DESTINATION}")
  348. endif()
  349. file(RELATIVE_PATH PACKAGE_RELATIVE_PATH "${absInstallDir}" "${installPrefix}" )
  350. foreach(var ${CCF_PATH_VARS})
  351. if(NOT DEFINED ${var})
  352. message(FATAL_ERROR "Variable ${var} does not exist")
  353. else()
  354. if(IS_ABSOLUTE "${${var}}")
  355. string(REPLACE "${installPrefix}" "\${PACKAGE_PREFIX_DIR}"
  356. PACKAGE_${var} "${${var}}")
  357. else()
  358. set(PACKAGE_${var} "\${PACKAGE_PREFIX_DIR}/${${var}}")
  359. endif()
  360. endif()
  361. endforeach()
  362. get_filename_component(inputFileName "${_inputFile}" NAME)
  363. set(PACKAGE_INIT "
  364. ####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() #######
  365. ####### Any changes to this file will be overwritten by the next CMake run ####
  366. ####### The input file was ${inputFileName} ########
  367. get_filename_component(PACKAGE_PREFIX_DIR \"\${CMAKE_CURRENT_LIST_DIR}/${PACKAGE_RELATIVE_PATH}\" ABSOLUTE)
  368. ")
  369. if("${absInstallDir}" MATCHES "^(/usr)?/lib(64)?/.+")
  370. # Handle "/usr move" symlinks created by some Linux distros.
  371. string(APPEND PACKAGE_INIT "
  372. # Use original install prefix when loaded through a \"/usr move\"
  373. # cross-prefix symbolic link such as /lib -> /usr/lib.
  374. get_filename_component(_realCurr \"\${CMAKE_CURRENT_LIST_DIR}\" REALPATH)
  375. get_filename_component(_realOrig \"${absInstallDir}\" REALPATH)
  376. if(_realCurr STREQUAL _realOrig)
  377. set(PACKAGE_PREFIX_DIR \"${installPrefix}\")
  378. endif()
  379. unset(_realOrig)
  380. unset(_realCurr)
  381. ")
  382. endif()
  383. if(NOT CCF_NO_SET_AND_CHECK_MACRO)
  384. string(APPEND PACKAGE_INIT "
  385. macro(set_and_check _var _file)
  386. set(\${_var} \"\${_file}\")
  387. if(NOT EXISTS \"\${_file}\")
  388. message(FATAL_ERROR \"File or directory \${_file} referenced by variable \${_var} does not exist !\")
  389. endif()
  390. endmacro()
  391. ")
  392. endif()
  393. if(NOT CCF_NO_CHECK_REQUIRED_COMPONENTS_MACRO)
  394. string(APPEND PACKAGE_INIT "
  395. macro(check_required_components _NAME)
  396. foreach(comp \${\${_NAME}_FIND_COMPONENTS})
  397. if(NOT \${_NAME}_\${comp}_FOUND)
  398. if(\${_NAME}_FIND_REQUIRED_\${comp})
  399. set(\${_NAME}_FOUND FALSE)
  400. endif()
  401. endif()
  402. endforeach()
  403. endmacro()
  404. ")
  405. endif()
  406. string(APPEND PACKAGE_INIT "
  407. ####################################################################################")
  408. configure_file("${_inputFile}" "${_outputFile}" @ONLY)
  409. endfunction()
  410. function(generate_apple_platform_selection_file _output_file)
  411. set(_config_file_options
  412. MACOS_INCLUDE_FILE
  413. IOS_INCLUDE_FILE
  414. IOS_SIMULATOR_INCLUDE_FILE
  415. IOS_CATALYST_INCLUDE_FILE
  416. TVOS_INCLUDE_FILE
  417. TVOS_SIMULATOR_INCLUDE_FILE
  418. WATCHOS_INCLUDE_FILE
  419. WATCHOS_SIMULATOR_INCLUDE_FILE
  420. VISIONOS_INCLUDE_FILE
  421. VISIONOS_SIMULATOR_INCLUDE_FILE
  422. )
  423. set(_options)
  424. set(_single
  425. INSTALL_DESTINATION
  426. INSTALL_PREFIX
  427. ${_config_file_options}
  428. ERROR_VARIABLE
  429. )
  430. set(_multi)
  431. cmake_parse_arguments(PARSE_ARGV 0 _gpsf "${_options}" "${_single}" "${_multi}")
  432. if(NOT _gpsf_INSTALL_DESTINATION)
  433. message(FATAL_ERROR "No INSTALL_DESTINATION given to generate_apple_platform_selection_file()")
  434. endif()
  435. if(_gpsf_INSTALL_PREFIX)
  436. set(maybe_INSTALL_PREFIX INSTALL_PREFIX ${_gpsf_INSTALL_PREFIX})
  437. else()
  438. set(maybe_INSTALL_PREFIX "")
  439. endif()
  440. if(_gpsf_ERROR_VARIABLE)
  441. set(_branch_INIT "set(\"${_gpsf_ERROR_VARIABLE}\" \"\")")
  442. else()
  443. set(_branch_INIT "")
  444. endif()
  445. set(_else ELSE)
  446. foreach(_opt IN LISTS _config_file_options _else)
  447. if(_gpsf_${_opt})
  448. set(_config_file "${_gpsf_${_opt}}")
  449. if(NOT IS_ABSOLUTE "${_config_file}")
  450. string(PREPEND _config_file [[${PACKAGE_PREFIX_DIR}/]])
  451. endif()
  452. set(_branch_${_opt} "include(\"${_config_file}\")")
  453. elseif(_gpsf_ERROR_VARIABLE)
  454. set(_branch_${_opt} "set(\"${_gpsf_ERROR_VARIABLE}\" \"Platform not supported\")")
  455. else()
  456. set(_branch_${_opt} "message(FATAL_ERROR \"Platform not supported\")")
  457. endif()
  458. endforeach()
  459. configure_package_config_file("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Internal/ApplePlatformSelection.cmake.in" "${_output_file}"
  460. INSTALL_DESTINATION "${_gpsf_INSTALL_DESTINATION}"
  461. ${maybe_INSTALL_PREFIX}
  462. NO_SET_AND_CHECK_MACRO
  463. NO_CHECK_REQUIRED_COMPONENTS_MACRO
  464. )
  465. endfunction()
  466. function(generate_apple_architecture_selection_file _output_file)
  467. set(_options)
  468. set(_single
  469. INSTALL_DESTINATION
  470. INSTALL_PREFIX
  471. UNIVERSAL_INCLUDE_FILE
  472. ERROR_VARIABLE
  473. )
  474. set(_multi
  475. SINGLE_ARCHITECTURES
  476. SINGLE_ARCHITECTURE_INCLUDE_FILES
  477. UNIVERSAL_ARCHITECTURES
  478. )
  479. cmake_parse_arguments(PARSE_ARGV 0 _gasf "${_options}" "${_single}" "${_multi}")
  480. if(NOT _gasf_INSTALL_DESTINATION)
  481. message(FATAL_ERROR "No INSTALL_DESTINATION given to generate_apple_platform_selection_file()")
  482. endif()
  483. if(_gasf_INSTALL_PREFIX)
  484. set(maybe_INSTALL_PREFIX INSTALL_PREFIX ${_gasf_INSTALL_PREFIX})
  485. else()
  486. set(maybe_INSTALL_PREFIX "")
  487. endif()
  488. list(LENGTH _gasf_SINGLE_ARCHITECTURES _gasf_SINGLE_ARCHITECTURES_len)
  489. list(LENGTH _gasf_SINGLE_ARCHITECTURE_INCLUDE_FILES _gasf_SINGLE_ARCHITECTURE_INCLUDE_FILES_len)
  490. if(NOT _gasf_SINGLE_ARCHITECTURES_len EQUAL _gasf_SINGLE_ARCHITECTURE_INCLUDE_FILES_len)
  491. message(FATAL_ERROR "SINGLE_ARCHITECTURES and SINGLE_ARCHITECTURE_INCLUDE_FILES do not have the same number of entries.")
  492. endif()
  493. set(_branch_code "")
  494. if(_gasf_ERROR_VARIABLE)
  495. string(APPEND _branch_code
  496. "set(\"${_gasf_ERROR_VARIABLE}\" \"\")\n"
  497. )
  498. endif()
  499. string(APPEND _branch_code
  500. "\n"
  501. "if(NOT CMAKE_OSX_ARCHITECTURES)\n"
  502. )
  503. if(_gasf_ERROR_VARIABLE)
  504. string(APPEND _branch_code
  505. " set(\"${_gasf_ERROR_VARIABLE}\" \"CMAKE_OSX_ARCHITECTURES must be explicitly set for this package\")\n"
  506. " return()\n"
  507. )
  508. else()
  509. string(APPEND _branch_code
  510. " message(FATAL_ERROR \"CMAKE_OSX_ARCHITECTURES must be explicitly set for this package\")\n"
  511. )
  512. endif()
  513. string(APPEND _branch_code
  514. "endif()\n\n"
  515. "set(_cmake_apple_archs \"\${CMAKE_OSX_ARCHITECTURES}\")\n"
  516. )
  517. if(NOT "${_gasf_UNIVERSAL_ARCHITECTURES}" STREQUAL "")
  518. string(APPEND _branch_code "list(REMOVE_ITEM _cmake_apple_archs ${_gasf_UNIVERSAL_ARCHITECTURES})\n")
  519. endif()
  520. string(APPEND _branch_code "\n")
  521. set(maybe_else "")
  522. foreach(pair IN ZIP_LISTS _gasf_SINGLE_ARCHITECTURES _gasf_SINGLE_ARCHITECTURE_INCLUDE_FILES)
  523. set(arch "${pair_0}")
  524. set(config_file "${pair_1}")
  525. if(NOT IS_ABSOLUTE "${config_file}")
  526. string(PREPEND config_file [[${PACKAGE_PREFIX_DIR}/]])
  527. endif()
  528. string(APPEND _branch_code
  529. "${maybe_else}if(CMAKE_OSX_ARCHITECTURES STREQUAL \"${arch}\")\n"
  530. " include(\"${config_file}\")\n"
  531. )
  532. set(maybe_else else)
  533. endforeach()
  534. if(_gasf_UNIVERSAL_ARCHITECTURES AND _gasf_UNIVERSAL_INCLUDE_FILE)
  535. set(config_file "${_gasf_UNIVERSAL_INCLUDE_FILE}")
  536. if(NOT IS_ABSOLUTE "${config_file}")
  537. string(PREPEND config_file [[${PACKAGE_PREFIX_DIR}/]])
  538. endif()
  539. string(APPEND _branch_code
  540. "${maybe_else}if(NOT _cmake_apple_archs)\n"
  541. " include(\"${config_file}\")\n"
  542. )
  543. set(maybe_else else)
  544. elseif(_gasf_UNIVERSAL_ARCHITECTURES)
  545. message(FATAL_ERROR "UNIVERSAL_INCLUDE_FILE requires UNIVERSAL_ARCHITECTURES")
  546. elseif(_gasf_UNIVERSAL_INCLUDE_FILE)
  547. message(FATAL_ERROR "UNIVERSAL_ARCHITECTURES requires UNIVERSAL_INCLUDE_FILE")
  548. endif()
  549. if(maybe_else)
  550. string(APPEND _branch_code "else()\n")
  551. set(_indent " ")
  552. else()
  553. set(_indent "")
  554. endif()
  555. if(_gasf_ERROR_VARIABLE)
  556. string(APPEND _branch_code
  557. "${_indent}set(\"${_gasf_ERROR_VARIABLE}\" \"Architecture not supported\")\n"
  558. )
  559. else()
  560. string(APPEND _branch_code
  561. "${_indent}message(FATAL_ERROR \"Architecture not supported\")\n"
  562. )
  563. endif()
  564. if(maybe_else)
  565. string(APPEND _branch_code "endif()\n")
  566. endif()
  567. configure_package_config_file("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Internal/AppleArchitectureSelection.cmake.in" "${_output_file}"
  568. INSTALL_DESTINATION "${_gasf_INSTALL_DESTINATION}"
  569. ${maybe_INSTALL_PREFIX}
  570. NO_SET_AND_CHECK_MACRO
  571. NO_CHECK_REQUIRED_COMPONENTS_MACRO
  572. )
  573. endfunction()