CMakePackageConfigHelpers.cmake 25 KB

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