CMakePackageConfigHelpers.cmake 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. )
  171. Write a file that includes an Apple-platform-specific ``.cmake`` file,
  172. e.g., for use as ``<PackageName>Config.cmake``. This can be used in
  173. conjunction with the ``XCFRAMEWORK_LOCATION`` argument of
  174. :command:`export(SETUP)` to export packages in a way that a project
  175. built for any Apple platform can use them.
  176. ``INSTALL_DESTINATION <path>``
  177. Path to which the generated file will be installed by the caller, e.g.,
  178. via :command:`install(FILES)`. The path may be either relative to the
  179. ``INSTALL_PREFIX`` or absolute.
  180. ``INSTALL_PREFIX <path>``
  181. Path prefix to which the package will be installed by the caller.
  182. The ``<path>`` argument must be an absolute path. If this argument
  183. is not passed, the :variable:`CMAKE_INSTALL_PREFIX` variable will be
  184. used instead.
  185. ``MACOS_INCLUDE_FILE <file>``
  186. File to include if the platform is macOS.
  187. ``IOS_INCLUDE_FILE <file>``
  188. File to include if the platform is iOS.
  189. ``IOS_SIMULATOR_INCLUDE_FILE <file>``
  190. File to include if the platform is iOS Simulator.
  191. ``TVOS_INCLUDE_FILE <file>``
  192. File to include if the platform is tvOS.
  193. ``TVOS_SIMULATOR_INCLUDE_FILE <file>``
  194. File to include if the platform is tvOS Simulator.
  195. ``WATCHOS_INCLUDE_FILE <file>``
  196. File to include if the platform is watchOS.
  197. ``WATCHOS_SIMULATOR_INCLUDE_FILE <file>``
  198. File to include if the platform is watchOS Simulator.
  199. ``VISIONOS_INCLUDE_FILE <file>``
  200. File to include if the platform is visionOS.
  201. ``VISIONOS_SIMULATOR_INCLUDE_FILE <file>``
  202. File to include if the platform is visionOS Simulator.
  203. If any of the optional include files is not specified, and the consuming
  204. project is built for its corresponding platform, an error will be thrown
  205. when including the generated file.
  206. .. command:: generate_apple_architecture_selection_file
  207. .. versionadded:: 3.29
  208. Create an Apple architecture selection file:
  209. .. code-block:: cmake
  210. generate_apple_architecture_selection_file(<filename>
  211. INSTALL_DESTINATION <path>
  212. [INSTALL_PREFIX <path>]
  213. [SINGLE_ARCHITECTURES <arch>...
  214. SINGLE_ARCHITECTURE_INCLUDE_FILES <file>...]
  215. [UNIVERSAL_ARCHITECTURES <arch>...
  216. UNIVERSAL_INCLUDE_FILE <file>]
  217. )
  218. Write a file that includes an Apple-architecture-specific ``.cmake`` file
  219. based on :variable:`CMAKE_OSX_ARCHITECTURES`, e.g., for inclusion from an
  220. Apple-specific ``<PackageName>Config.cmake`` file.
  221. ``INSTALL_DESTINATION <path>``
  222. Path to which the generated file will be installed by the caller, e.g.,
  223. via :command:`install(FILES)`. The path may be either relative to the
  224. ``INSTALL_PREFIX`` or absolute.
  225. ``INSTALL_PREFIX <path>``
  226. Path prefix to which the package will be installed by the caller.
  227. The ``<path>`` argument must be an absolute path. If this argument
  228. is not passed, the :variable:`CMAKE_INSTALL_PREFIX` variable will be
  229. used instead.
  230. ``SINGLE_ARCHITECTURES <arch>...``
  231. Architectures provided by entries of ``SINGLE_ARCHITECTURE_INCLUDE_FILES``.
  232. ``SINGLE_ARCHITECTURE_INCLUDE_FILES <file>...``
  233. Architecture-specific files. One of them will be loaded
  234. when :variable:`CMAKE_OSX_ARCHITECTURES` contains a single
  235. architecture matching the corresponding entry of
  236. ``SINGLE_ARCHITECTURES``.
  237. ``UNIVERSAL_ARCHITECTURES <arch>...``
  238. Architectures provided by the ``UNIVERSAL_INCLUDE_FILE``.
  239. The list may include ``$(ARCHS_STANDARD)`` to support consumption using
  240. the :generator:`Xcode` generator, but the architectures should always
  241. be listed individually too.
  242. ``UNIVERSAL_INCLUDE_FILE <file>``
  243. A file to load when :variable:`CMAKE_OSX_ARCHITECTURES` contains
  244. a (non-strict) subset of the ``UNIVERSAL_ARCHITECTURES`` and
  245. does not match any one of the ``SINGLE_ARCHITECTURES``.
  246. Example Generating Package Files
  247. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  248. Example using both :command:`configure_package_config_file` and
  249. ``write_basic_package_version_file()``:
  250. ``CMakeLists.txt``:
  251. .. code-block:: cmake
  252. include(GNUInstallDirs)
  253. set(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR}/Foo
  254. CACHE PATH "Location of header files" )
  255. set(SYSCONFIG_INSTALL_DIR ${CMAKE_INSTALL_SYSCONFDIR}/foo
  256. CACHE PATH "Location of configuration files" )
  257. #...
  258. include(CMakePackageConfigHelpers)
  259. configure_package_config_file(FooConfig.cmake.in
  260. ${CMAKE_CURRENT_BINARY_DIR}/FooConfig.cmake
  261. INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Foo
  262. PATH_VARS INCLUDE_INSTALL_DIR SYSCONFIG_INSTALL_DIR)
  263. write_basic_package_version_file(
  264. ${CMAKE_CURRENT_BINARY_DIR}/FooConfigVersion.cmake
  265. VERSION 1.2.3
  266. COMPATIBILITY SameMajorVersion )
  267. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/FooConfig.cmake
  268. ${CMAKE_CURRENT_BINARY_DIR}/FooConfigVersion.cmake
  269. DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Foo )
  270. ``FooConfig.cmake.in``:
  271. ::
  272. set(FOO_VERSION x.y.z)
  273. ...
  274. @PACKAGE_INIT@
  275. ...
  276. set_and_check(FOO_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@")
  277. set_and_check(FOO_SYSCONFIG_DIR "@PACKAGE_SYSCONFIG_INSTALL_DIR@")
  278. check_required_components(Foo)
  279. #]=======================================================================]
  280. include(WriteBasicConfigVersionFile)
  281. macro(WRITE_BASIC_PACKAGE_VERSION_FILE)
  282. write_basic_config_version_file(${ARGN})
  283. endmacro()
  284. function(CONFIGURE_PACKAGE_CONFIG_FILE _inputFile _outputFile)
  285. set(options NO_SET_AND_CHECK_MACRO NO_CHECK_REQUIRED_COMPONENTS_MACRO)
  286. set(oneValueArgs INSTALL_DESTINATION INSTALL_PREFIX)
  287. set(multiValueArgs PATH_VARS )
  288. cmake_parse_arguments(CCF "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  289. if(CCF_UNPARSED_ARGUMENTS)
  290. message(FATAL_ERROR "Unknown keywords given to CONFIGURE_PACKAGE_CONFIG_FILE(): \"${CCF_UNPARSED_ARGUMENTS}\"")
  291. endif()
  292. if(NOT CCF_INSTALL_DESTINATION)
  293. message(FATAL_ERROR "No INSTALL_DESTINATION given to CONFIGURE_PACKAGE_CONFIG_FILE()")
  294. endif()
  295. if(DEFINED CCF_INSTALL_PREFIX)
  296. if(IS_ABSOLUTE "${CCF_INSTALL_PREFIX}")
  297. set(installPrefix "${CCF_INSTALL_PREFIX}")
  298. else()
  299. message(FATAL_ERROR "INSTALL_PREFIX must be an absolute path")
  300. endif()
  301. elseif(IS_ABSOLUTE "${CMAKE_INSTALL_PREFIX}")
  302. set(installPrefix "${CMAKE_INSTALL_PREFIX}")
  303. else()
  304. get_filename_component(installPrefix "${CMAKE_INSTALL_PREFIX}" ABSOLUTE)
  305. endif()
  306. if(IS_ABSOLUTE "${CCF_INSTALL_DESTINATION}")
  307. set(absInstallDir "${CCF_INSTALL_DESTINATION}")
  308. else()
  309. set(absInstallDir "${installPrefix}/${CCF_INSTALL_DESTINATION}")
  310. endif()
  311. file(RELATIVE_PATH PACKAGE_RELATIVE_PATH "${absInstallDir}" "${installPrefix}" )
  312. foreach(var ${CCF_PATH_VARS})
  313. if(NOT DEFINED ${var})
  314. message(FATAL_ERROR "Variable ${var} does not exist")
  315. else()
  316. if(IS_ABSOLUTE "${${var}}")
  317. string(REPLACE "${installPrefix}" "\${PACKAGE_PREFIX_DIR}"
  318. PACKAGE_${var} "${${var}}")
  319. else()
  320. set(PACKAGE_${var} "\${PACKAGE_PREFIX_DIR}/${${var}}")
  321. endif()
  322. endif()
  323. endforeach()
  324. get_filename_component(inputFileName "${_inputFile}" NAME)
  325. set(PACKAGE_INIT "
  326. ####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() #######
  327. ####### Any changes to this file will be overwritten by the next CMake run ####
  328. ####### The input file was ${inputFileName} ########
  329. get_filename_component(PACKAGE_PREFIX_DIR \"\${CMAKE_CURRENT_LIST_DIR}/${PACKAGE_RELATIVE_PATH}\" ABSOLUTE)
  330. ")
  331. if("${absInstallDir}" MATCHES "^(/usr)?/lib(64)?/.+")
  332. # Handle "/usr move" symlinks created by some Linux distros.
  333. string(APPEND PACKAGE_INIT "
  334. # Use original install prefix when loaded through a \"/usr move\"
  335. # cross-prefix symbolic link such as /lib -> /usr/lib.
  336. get_filename_component(_realCurr \"\${CMAKE_CURRENT_LIST_DIR}\" REALPATH)
  337. get_filename_component(_realOrig \"${absInstallDir}\" REALPATH)
  338. if(_realCurr STREQUAL _realOrig)
  339. set(PACKAGE_PREFIX_DIR \"${installPrefix}\")
  340. endif()
  341. unset(_realOrig)
  342. unset(_realCurr)
  343. ")
  344. endif()
  345. if(NOT CCF_NO_SET_AND_CHECK_MACRO)
  346. string(APPEND PACKAGE_INIT "
  347. macro(set_and_check _var _file)
  348. set(\${_var} \"\${_file}\")
  349. if(NOT EXISTS \"\${_file}\")
  350. message(FATAL_ERROR \"File or directory \${_file} referenced by variable \${_var} does not exist !\")
  351. endif()
  352. endmacro()
  353. ")
  354. endif()
  355. if(NOT CCF_NO_CHECK_REQUIRED_COMPONENTS_MACRO)
  356. string(APPEND PACKAGE_INIT "
  357. macro(check_required_components _NAME)
  358. foreach(comp \${\${_NAME}_FIND_COMPONENTS})
  359. if(NOT \${_NAME}_\${comp}_FOUND)
  360. if(\${_NAME}_FIND_REQUIRED_\${comp})
  361. set(\${_NAME}_FOUND FALSE)
  362. endif()
  363. endif()
  364. endforeach()
  365. endmacro()
  366. ")
  367. endif()
  368. string(APPEND PACKAGE_INIT "
  369. ####################################################################################")
  370. configure_file("${_inputFile}" "${_outputFile}" @ONLY)
  371. endfunction()
  372. function(generate_apple_platform_selection_file _output_file)
  373. set(_config_file_options
  374. MACOS_INCLUDE_FILE
  375. IOS_INCLUDE_FILE
  376. IOS_SIMULATOR_INCLUDE_FILE
  377. TVOS_INCLUDE_FILE
  378. TVOS_SIMULATOR_INCLUDE_FILE
  379. WATCHOS_INCLUDE_FILE
  380. WATCHOS_SIMULATOR_INCLUDE_FILE
  381. VISIONOS_INCLUDE_FILE
  382. VISIONOS_SIMULATOR_INCLUDE_FILE
  383. )
  384. set(_options)
  385. set(_single
  386. INSTALL_DESTINATION
  387. INSTALL_PREFIX
  388. ${_config_file_options}
  389. )
  390. set(_multi)
  391. cmake_parse_arguments(PARSE_ARGV 0 _gpsf "${_options}" "${_single}" "${_multi}")
  392. if(NOT _gpsf_INSTALL_DESTINATION)
  393. message(FATAL_ERROR "No INSTALL_DESTINATION given to generate_apple_platform_selection_file()")
  394. endif()
  395. if(_gpsf_INSTALL_PREFIX)
  396. set(maybe_INSTALL_PREFIX INSTALL_PREFIX ${_gpsf_INSTALL_PREFIX})
  397. else()
  398. set(maybe_INSTALL_PREFIX "")
  399. endif()
  400. set(_have_relative 0)
  401. foreach(_opt IN LISTS _config_file_options)
  402. if(_gpsf_${_opt})
  403. set(_config_file "${_gpsf_${_opt}}")
  404. if(NOT IS_ABSOLUTE "${_config_file}")
  405. string(PREPEND _config_file [[${PACKAGE_PREFIX_DIR}/]])
  406. set(_have_relative 1)
  407. endif()
  408. set(_branch_${_opt} "include(\"${_config_file}\")")
  409. else()
  410. set(_branch_${_opt} "message(FATAL_ERROR \"Platform not supported\")")
  411. endif()
  412. endforeach()
  413. configure_package_config_file("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Internal/ApplePlatformSelection.cmake.in" "${_output_file}"
  414. INSTALL_DESTINATION "${_gpsf_INSTALL_DESTINATION}"
  415. ${maybe_INSTALL_PREFIX}
  416. NO_SET_AND_CHECK_MACRO
  417. NO_CHECK_REQUIRED_COMPONENTS_MACRO
  418. )
  419. endfunction()
  420. function(generate_apple_architecture_selection_file _output_file)
  421. set(_options)
  422. set(_single
  423. INSTALL_DESTINATION
  424. INSTALL_PREFIX
  425. UNIVERSAL_INCLUDE_FILE
  426. )
  427. set(_multi
  428. SINGLE_ARCHITECTURES
  429. SINGLE_ARCHITECTURE_INCLUDE_FILES
  430. UNIVERSAL_ARCHITECTURES
  431. )
  432. cmake_parse_arguments(PARSE_ARGV 0 _gasf "${_options}" "${_single}" "${_multi}")
  433. if(NOT _gasf_INSTALL_DESTINATION)
  434. message(FATAL_ERROR "No INSTALL_DESTINATION given to generate_apple_platform_selection_file()")
  435. endif()
  436. if(_gasf_INSTALL_PREFIX)
  437. set(maybe_INSTALL_PREFIX INSTALL_PREFIX ${_gasf_INSTALL_PREFIX})
  438. else()
  439. set(maybe_INSTALL_PREFIX "")
  440. endif()
  441. list(LENGTH _gasf_SINGLE_ARCHITECTURES _gasf_SINGLE_ARCHITECTURES_len)
  442. list(LENGTH _gasf_SINGLE_ARCHITECTURE_INCLUDE_FILES _gasf_SINGLE_ARCHITECTURE_INCLUDE_FILES_len)
  443. if(NOT _gasf_SINGLE_ARCHITECTURES_len EQUAL _gasf_SINGLE_ARCHITECTURE_INCLUDE_FILES_len)
  444. message(FATAL_ERROR "SINGLE_ARCHITECTURES and SINGLE_ARCHITECTURE_INCLUDE_FILES do not have the same number of entries.")
  445. endif()
  446. set(_branch_code "")
  447. foreach(pair IN ZIP_LISTS _gasf_SINGLE_ARCHITECTURES _gasf_SINGLE_ARCHITECTURE_INCLUDE_FILES)
  448. set(arch "${pair_0}")
  449. set(config_file "${pair_1}")
  450. if(NOT IS_ABSOLUTE "${config_file}")
  451. string(PREPEND config_file [[${PACKAGE_PREFIX_DIR}/]])
  452. endif()
  453. string(APPEND _branch_code
  454. "\n"
  455. "if(CMAKE_OSX_ARCHITECTURES STREQUAL \"${arch}\")\n"
  456. " include(\"${config_file}\")\n"
  457. " return()\n"
  458. "endif()\n"
  459. )
  460. endforeach()
  461. if(_gasf_UNIVERSAL_ARCHITECTURES AND _gasf_UNIVERSAL_INCLUDE_FILE)
  462. string(JOIN " " universal_archs "${_gasf_UNIVERSAL_ARCHITECTURES}")
  463. set(config_file "${_gasf_UNIVERSAL_INCLUDE_FILE}")
  464. if(NOT IS_ABSOLUTE "${config_file}")
  465. string(PREPEND config_file [[${PACKAGE_PREFIX_DIR}/]])
  466. endif()
  467. string(APPEND _branch_code
  468. "\n"
  469. "set(_cmake_apple_archs \"\${CMAKE_OSX_ARCHITECTURES}\")\n"
  470. "list(REMOVE_ITEM _cmake_apple_archs ${universal_archs})\n"
  471. "if(NOT _cmake_apple_archs)\n"
  472. " include(\"${config_file}\")\n"
  473. " return()\n"
  474. "endif()\n"
  475. )
  476. elseif(_gasf_UNIVERSAL_ARCHITECTURES)
  477. message(FATAL_ERROR "UNIVERSAL_INCLUDE_FILE requires UNIVERSAL_ARCHITECTURES")
  478. elseif(_gasf_UNIVERSAL_INCLUDE_FILE)
  479. message(FATAL_ERROR "UNIVERSAL_ARCHITECTURES requires UNIVERSAL_INCLUDE_FILE")
  480. endif()
  481. configure_package_config_file("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Internal/AppleArchitectureSelection.cmake.in" "${_output_file}"
  482. INSTALL_DESTINATION "${_gasf_INSTALL_DESTINATION}"
  483. ${maybe_INSTALL_PREFIX}
  484. NO_SET_AND_CHECK_MACRO
  485. NO_CHECK_REQUIRED_COMPONENTS_MACRO
  486. )
  487. endfunction()