export.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. export
  2. ------
  3. Export targets or packages for outside projects to use them directly
  4. from the current project's build tree, without installation.
  5. See the :command:`install(EXPORT)` command to export targets from an
  6. install tree.
  7. Synopsis
  8. ^^^^^^^^
  9. .. parsed-literal::
  10. export(`TARGETS`_ <target>... [...])
  11. export(`EXPORT`_ <export-name> [...])
  12. export(`PACKAGE`_ <PackageName>)
  13. export(`SETUP`_ <export-name> [...])
  14. export(`PACKAGE_INFO`_ <package-name> [...])
  15. export(`SBOM`_ <sbom-name> [...])
  16. Exporting Targets
  17. ^^^^^^^^^^^^^^^^^
  18. .. signature::
  19. export(TARGETS <target>... [...])
  20. .. code-block:: cmake
  21. export(TARGETS <target>... [NAMESPACE <namespace>]
  22. [APPEND] FILE <filename> [EXPORT_LINK_INTERFACE_LIBRARIES]
  23. [CXX_MODULES_DIRECTORY <directory>])
  24. Creates a file ``<filename>`` that may be included by outside projects to
  25. import targets named by ``<target>...`` from the current project's build tree.
  26. This is useful during cross-compiling to build utility executables that can
  27. run on the host platform in one project and then import them into another
  28. project being compiled for the target platform.
  29. The file created by this command is specific to the build tree and
  30. should never be installed. See the :command:`install(EXPORT)` command to
  31. export targets from an install tree.
  32. The options are:
  33. ``NAMESPACE <namespace>``
  34. Prepend the ``<namespace>`` string to all target names written to the file.
  35. ``APPEND``
  36. Append to the file instead of overwriting it. This can be used to
  37. incrementally export multiple targets to the same file.
  38. ``EXPORT_LINK_INTERFACE_LIBRARIES``
  39. Include the contents of the properties named with the pattern
  40. ``(IMPORTED_)?LINK_INTERFACE_LIBRARIES(_<CONFIG>)?``
  41. in the export, even when policy :policy:`CMP0022` is NEW. This is useful
  42. to support consumers using CMake versions older than 2.8.12.
  43. ``CXX_MODULES_DIRECTORY <directory>``
  44. .. versionadded:: 3.28
  45. Export C++ module properties to files under the given directory. Each file
  46. will be named according to the target's export name (without any namespace).
  47. These files will automatically be included from the export file.
  48. This signature requires all targets to be listed explicitly. If a library
  49. target is included in the export, but a target to which it links is not
  50. included, the behavior is unspecified. See the :command:`export(EXPORT)` signature
  51. to automatically export the same targets from the build tree as
  52. :command:`install(EXPORT)` would from an install tree.
  53. .. note::
  54. :ref:`Object Libraries` under :generator:`Xcode` have special handling if
  55. multiple architectures are listed in :variable:`CMAKE_OSX_ARCHITECTURES`.
  56. In this case they will be exported as :ref:`Interface Libraries` with
  57. no object files available to clients. This is sufficient to satisfy
  58. transitive usage requirements of other targets that link to the
  59. object libraries in their implementation.
  60. This command exports all :ref:`build configurations` from the build tree.
  61. See the :variable:`CMAKE_MAP_IMPORTED_CONFIG_<CONFIG>` variable to map
  62. configurations of dependent projects to the exported configurations.
  63. Exporting Targets to Android.mk
  64. """""""""""""""""""""""""""""""
  65. .. code-block:: cmake
  66. export(TARGETS <target>... ANDROID_MK <filename>)
  67. .. versionadded:: 3.7
  68. This signature exports CMake built targets to the android ndk build system
  69. by creating an ``Android.mk`` file that references the prebuilt targets. The
  70. Android NDK supports the use of prebuilt libraries, both static and shared.
  71. This allows CMake to build the libraries of a project and make them available
  72. to an ndk build system complete with transitive dependencies, include flags
  73. and defines required to use the libraries. The signature takes a list of
  74. targets and puts them in the ``Android.mk`` file specified by the
  75. ``<filename>`` given. This signature can only be used if policy
  76. :policy:`CMP0022` is NEW for all targets given. A error will be issued if
  77. that policy is set to OLD for one of the targets.
  78. Exporting Targets matching install(EXPORT)
  79. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  80. .. signature::
  81. export(EXPORT <export-name> [...])
  82. .. code-block:: cmake
  83. export(EXPORT <export-name> [NAMESPACE <namespace>] [FILE <filename>]
  84. [CXX_MODULES_DIRECTORY <directory>] [EXPORT_PACKAGE_DEPENDENCIES])
  85. Creates a file ``<filename>`` that may be included by outside projects to
  86. import targets from the current project's build tree. This is the same
  87. as the :command:`export(TARGETS)` signature, except that the targets are not
  88. explicitly listed. Instead, it exports the targets associated with
  89. the installation export ``<export-name>``. Target installations may be
  90. associated with the export ``<export-name>`` using the ``EXPORT`` option
  91. of the :command:`install(TARGETS)` command.
  92. ``EXPORT_PACKAGE_DEPENDENCIES``
  93. .. note::
  94. Experimental. Gated by ``CMAKE_EXPERIMENTAL_EXPORT_PACKAGE_DEPENDENCIES``.
  95. Specify that :command:`find_dependency` calls should be exported. See
  96. :command:`install(EXPORT)` for details on how this works.
  97. Exporting Targets to the |CPS|
  98. """"""""""""""""""""""""""""""
  99. .. signature::
  100. export(PACKAGE_INFO <package-name> [...])
  101. .. code-block:: cmake
  102. export(PACKAGE_INFO <package-name> EXPORT <export-name>
  103. [PROJECT <project-name>|NO_PROJECT_METADATA]
  104. [APPENDIX <appendix-name>]
  105. [LOWER_CASE_FILE]
  106. [VERSION <version>
  107. [COMPAT_VERSION <version>]
  108. [VERSION_SCHEMA <string>]]
  109. [DEFAULT_TARGETS <target>...]
  110. [DEFAULT_CONFIGURATIONS <config>...]
  111. [LICENSE <license-string>]
  112. [DEFAULT_LICENSE <license-string>]
  113. [DESCRIPTION <description-string>]
  114. [HOMEPAGE_URL <url-string>]
  115. [CXX_MODULES_DIRECTORY <directory>])
  116. .. versionadded:: 4.3
  117. .. note::
  118. Experimental. Gated by ``CMAKE_EXPERIMENTAL_EXPORT_PACKAGE_INFO``.
  119. Creates a file in the |CPS|_ that may be included by outside projects to import
  120. targets named by ``<target>...`` from the current project's build tree. See
  121. the :command:`install(PACKAGE_INFO)` command to export targets from an install
  122. tree. The imported targets are implicitly in the namespace ``<package-name>``.
  123. The default file name is ``<package-name>[-<appendix-name>].cps``. If the
  124. ``LOWER_CASE_FILE`` option is given, the file name will use the package name
  125. converted to lower case.
  126. See :command:`install(PACKAGE_INFO)` for a description of the other options.
  127. Exporting Software Bill of Materials (SBOM) Documents
  128. """""""""""""""""""""""""""""""""""""""""""""""""""""
  129. .. signature::
  130. export(SBOM <sbom-name> [...])
  131. .. code-block:: cmake
  132. export(SBOM <sbom-name> EXPORT <export-name>
  133. [FORMAT <string>]
  134. [PROJECT <project-name>|NO_PROJECT_METADATA]
  135. [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
  136. [LICENSE <license-string>]
  137. [DESCRIPTION <description-string>]
  138. [HOMEPAGE_URL <url-string>]
  139. [PACKAGE_URL <url-string>])
  140. .. versionadded:: 4.3
  141. .. note::
  142. Experimental. Gated by ``CMAKE_EXPERIMENTAL_GENERATE_SBOM``.
  143. Generates a software bill of materials (SBOM) document describing the targets
  144. in the export ``<export-name>`` and their dependencies in the build tree
  145. See :command:`install(SBOM)` for details about the supported SBOM formats and a
  146. description of the other options.
  147. Exporting Packages
  148. ^^^^^^^^^^^^^^^^^^
  149. .. signature::
  150. export(PACKAGE <PackageName>)
  151. .. code-block:: cmake
  152. export(PACKAGE <PackageName>)
  153. Store the current build directory in the CMake user package registry
  154. for package ``<PackageName>``. The :command:`find_package` command may consider the
  155. directory while searching for package ``<PackageName>``. This helps dependent
  156. projects find and use a package from the current project's build tree
  157. without help from the user. Note that the entry in the package
  158. registry that this command creates works only in conjunction with a
  159. package configuration file (``<PackageName>Config.cmake``) that works with the
  160. build tree. In some cases, for example for packaging and for system
  161. wide installations, it is not desirable to write the user package
  162. registry.
  163. .. versionchanged:: 3.1
  164. If the :variable:`CMAKE_EXPORT_NO_PACKAGE_REGISTRY` variable
  165. is enabled, the ``export(PACKAGE)`` command will do nothing.
  166. .. versionchanged:: 3.15
  167. By default the ``export(PACKAGE)`` command does nothing (see policy
  168. :policy:`CMP0090`) because populating the user package registry has effects
  169. outside the source and build trees. Set the
  170. :variable:`CMAKE_EXPORT_PACKAGE_REGISTRY` variable to add build directories
  171. to the CMake user package registry.
  172. Configuring Exports
  173. ^^^^^^^^^^^^^^^^^^^
  174. .. signature::
  175. export(SETUP <export-name> [...])
  176. .. code-block:: cmake
  177. export(SETUP <export-name>
  178. [PACKAGE_DEPENDENCY <dep>
  179. [ENABLED (<bool-true>|<bool-false>|AUTO)]
  180. [EXTRA_ARGS <args>...]
  181. ] [...]
  182. [TARGET <target>
  183. [XCFRAMEWORK_LOCATION <location>]
  184. ] [...]
  185. )
  186. .. versionadded:: 3.29
  187. Configure the parameters of an export. The arguments are as follows:
  188. ``PACKAGE_DEPENDENCY <dep>``
  189. .. note::
  190. Experimental. Gated by ``CMAKE_EXPERIMENTAL_EXPORT_PACKAGE_DEPENDENCIES``.
  191. Specify a package dependency to configure. This changes how
  192. :command:`find_dependency` calls are written during
  193. :command:`export(EXPORT)` and :command:`install(EXPORT)`. ``<dep>`` is the
  194. name of a package to export. This argument accepts the following additional
  195. arguments:
  196. ``ENABLED``
  197. Manually control whether or not the dependency is exported. This accepts
  198. the following values:
  199. ``<bool-true>``
  200. Any value that CMake recognizes as "true". Always export the dependency,
  201. even if no exported targets depend on it. This can be used to manually
  202. add :command:`find_dependency` calls to the export.
  203. ``<bool-false>``
  204. Any value that CMake recognizes as "false". Never export the dependency,
  205. even if an exported target depends on it.
  206. ``AUTO``
  207. Only export the dependency if an exported target depends on it.
  208. ``EXTRA_ARGS <args>``
  209. Specify additional arguments to pass to :command:`find_dependency` after
  210. the ``REQUIRED`` argument.
  211. ``TARGET <target>``
  212. Specify a target to configure in this export. This argument accepts the
  213. following additional arguments:
  214. ``XCFRAMEWORK_LOCATION``
  215. Specify the location of an ``.xcframework`` which contains the library from
  216. this target. If specified, the generated code will check to see if the
  217. ``.xcframework`` exists, and if it does, it will use the ``.xcframework``
  218. as its imported location instead of the installed library.
  219. .. _CPS: https://cps-org.github.io/cps/
  220. .. |CPS| replace:: Common Package Specification