export.rst 9.5 KB

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