install.rst 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. install
  2. -------
  3. Specify rules to run at install time.
  4. Synopsis
  5. ^^^^^^^^
  6. .. parsed-literal::
  7. install(`TARGETS`_ <target>... [...])
  8. install(`IMPORTED_RUNTIME_ARTIFACTS`_ <target>... [...])
  9. install({`FILES`_ | `PROGRAMS`_} <file>... [...])
  10. install(`DIRECTORY`_ <dir>... [...])
  11. install(`SCRIPT`_ <file> [...])
  12. install(`CODE`_ <code> [...])
  13. install(`EXPORT`_ <export-name> [...])
  14. Introduction
  15. ^^^^^^^^^^^^
  16. This command generates installation rules for a project. Install rules
  17. specified by calls to the ``install()`` command within a source directory
  18. are executed in order during installation.
  19. .. versionchanged:: 3.14
  20. Install rules in subdirectories
  21. added by calls to the :command:`add_subdirectory` command are interleaved
  22. with those in the parent directory to run in the order declared (see
  23. policy :policy:`CMP0082`).
  24. There are multiple signatures for this command. Some of them define
  25. installation options for files and targets. Options common to
  26. multiple signatures are covered here but they are valid only for
  27. signatures that specify them. The common options are:
  28. ``DESTINATION``
  29. Specify the directory on disk to which a file will be installed.
  30. Arguments can be relative or absolute paths.
  31. If a relative path is given it is interpreted relative to the value
  32. of the :variable:`CMAKE_INSTALL_PREFIX` variable.
  33. The prefix can be relocated at install time using the ``DESTDIR``
  34. mechanism explained in the :variable:`CMAKE_INSTALL_PREFIX` variable
  35. documentation.
  36. If an absolute path (with a leading slash or drive letter) is given
  37. it is used verbatim.
  38. As absolute paths are not supported by :manual:`cpack <cpack(1)>` installer
  39. generators, it is preferable to use relative paths throughout.
  40. In particular, there is no need to make paths absolute by prepending
  41. :variable:`CMAKE_INSTALL_PREFIX`; this prefix is used by default if
  42. the DESTINATION is a relative path.
  43. ``PERMISSIONS``
  44. Specify permissions for installed files. Valid permissions are
  45. ``OWNER_READ``, ``OWNER_WRITE``, ``OWNER_EXECUTE``, ``GROUP_READ``,
  46. ``GROUP_WRITE``, ``GROUP_EXECUTE``, ``WORLD_READ``, ``WORLD_WRITE``,
  47. ``WORLD_EXECUTE``, ``SETUID``, and ``SETGID``. Permissions that do
  48. not make sense on certain platforms are ignored on those platforms.
  49. ``CONFIGURATIONS``
  50. Specify a list of build configurations for which the install rule
  51. applies (Debug, Release, etc.). Note that the values specified for
  52. this option only apply to options listed AFTER the ``CONFIGURATIONS``
  53. option. For example, to set separate install paths for the Debug and
  54. Release configurations, do the following:
  55. .. code-block:: cmake
  56. install(TARGETS target
  57. CONFIGURATIONS Debug
  58. RUNTIME DESTINATION Debug/bin)
  59. install(TARGETS target
  60. CONFIGURATIONS Release
  61. RUNTIME DESTINATION Release/bin)
  62. Note that ``CONFIGURATIONS`` appears BEFORE ``RUNTIME DESTINATION``.
  63. ``COMPONENT``
  64. Specify an installation component name with which the install rule
  65. is associated, such as "runtime" or "development". During
  66. component-specific installation only install rules associated with
  67. the given component name will be executed. During a full installation
  68. all components are installed unless marked with ``EXCLUDE_FROM_ALL``.
  69. If ``COMPONENT`` is not provided a default component "Unspecified" is
  70. created. The default component name may be controlled with the
  71. :variable:`CMAKE_INSTALL_DEFAULT_COMPONENT_NAME` variable.
  72. ``EXCLUDE_FROM_ALL``
  73. .. versionadded:: 3.6
  74. Specify that the file is excluded from a full installation and only
  75. installed as part of a component-specific installation
  76. ``RENAME``
  77. Specify a name for an installed file that may be different from the
  78. original file. Renaming is allowed only when a single file is
  79. installed by the command.
  80. ``OPTIONAL``
  81. Specify that it is not an error if the file to be installed does
  82. not exist.
  83. .. versionadded:: 3.1
  84. Command signatures that install files may print messages during
  85. installation. Use the :variable:`CMAKE_INSTALL_MESSAGE` variable
  86. to control which messages are printed.
  87. .. versionadded:: 3.11
  88. Many of the ``install()`` variants implicitly create the directories
  89. containing the installed files. If
  90. :variable:`CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS` is set, these
  91. directories will be created with the permissions specified. Otherwise,
  92. they will be created according to the uname rules on Unix-like platforms.
  93. Windows platforms are unaffected.
  94. Installing Targets
  95. ^^^^^^^^^^^^^^^^^^
  96. .. _`install(TARGETS)`:
  97. .. _TARGETS:
  98. .. code-block:: cmake
  99. install(TARGETS targets... [EXPORT <export-name>]
  100. [[ARCHIVE|LIBRARY|RUNTIME|OBJECTS|FRAMEWORK|BUNDLE|
  101. PRIVATE_HEADER|PUBLIC_HEADER|RESOURCE]
  102. [DESTINATION <dir>]
  103. [PERMISSIONS permissions...]
  104. [CONFIGURATIONS [Debug|Release|...]]
  105. [COMPONENT <component>]
  106. [NAMELINK_COMPONENT <component>]
  107. [OPTIONAL] [EXCLUDE_FROM_ALL]
  108. [NAMELINK_ONLY|NAMELINK_SKIP]
  109. ] [...]
  110. [INCLUDES DESTINATION [<dir> ...]]
  111. )
  112. The ``TARGETS`` form specifies rules for installing targets from a
  113. project. There are several kinds of target :ref:`Output Artifacts`
  114. that may be installed:
  115. ``ARCHIVE``
  116. Target artifacts of this kind include:
  117. * *Static libraries*
  118. (except on macOS when marked as ``FRAMEWORK``, see below);
  119. * *DLL import libraries*
  120. (on all Windows-based systems including Cygwin; they have extension
  121. ``.lib``, in contrast to the ``.dll`` libraries that go to ``RUNTIME``);
  122. * On AIX, the *linker import file* created for executables with
  123. :prop_tgt:`ENABLE_EXPORTS` enabled.
  124. ``LIBRARY``
  125. Target artifacts of this kind include:
  126. * *Shared libraries*, except
  127. - DLLs (these go to ``RUNTIME``, see below),
  128. - on macOS when marked as ``FRAMEWORK`` (see below).
  129. ``RUNTIME``
  130. Target artifacts of this kind include:
  131. * *Executables*
  132. (except on macOS when marked as ``MACOSX_BUNDLE``, see ``BUNDLE`` below);
  133. * DLLs (on all Windows-based systems including Cygwin; note that the
  134. accompanying import libraries are of kind ``ARCHIVE``).
  135. ``OBJECTS``
  136. .. versionadded:: 3.9
  137. Object files associated with *object libraries*.
  138. ``FRAMEWORK``
  139. Both static and shared libraries marked with the ``FRAMEWORK``
  140. property are treated as ``FRAMEWORK`` targets on macOS.
  141. ``BUNDLE``
  142. Executables marked with the :prop_tgt:`MACOSX_BUNDLE` property are treated as
  143. ``BUNDLE`` targets on macOS.
  144. ``PUBLIC_HEADER``
  145. Any :prop_tgt:`PUBLIC_HEADER` files associated with a library are installed in
  146. the destination specified by the ``PUBLIC_HEADER`` argument on non-Apple
  147. platforms. Rules defined by this argument are ignored for :prop_tgt:`FRAMEWORK`
  148. libraries on Apple platforms because the associated files are installed
  149. into the appropriate locations inside the framework folder. See
  150. :prop_tgt:`PUBLIC_HEADER` for details.
  151. ``PRIVATE_HEADER``
  152. Similar to ``PUBLIC_HEADER``, but for ``PRIVATE_HEADER`` files. See
  153. :prop_tgt:`PRIVATE_HEADER` for details.
  154. ``RESOURCE``
  155. Similar to ``PUBLIC_HEADER`` and ``PRIVATE_HEADER``, but for
  156. ``RESOURCE`` files. See :prop_tgt:`RESOURCE` for details.
  157. For each of these arguments given, the arguments following them only apply
  158. to the target or file type specified in the argument. If none is given, the
  159. installation properties apply to all target types. If only one is given then
  160. only targets of that type will be installed (which can be used to install
  161. just a DLL or just an import library.)
  162. For regular executables, static libraries and shared libraries, the
  163. ``DESTINATION`` argument is not required. For these target types, when
  164. ``DESTINATION`` is omitted, a default destination will be taken from the
  165. appropriate variable from :module:`GNUInstallDirs`, or set to a built-in
  166. default value if that variable is not defined. The same is true for the
  167. public and private headers associated with the installed targets through the
  168. :prop_tgt:`PUBLIC_HEADER` and :prop_tgt:`PRIVATE_HEADER` target properties.
  169. A destination must always be provided for module libraries, Apple bundles and
  170. frameworks. A destination can be omitted for interface and object libraries,
  171. but they are handled differently (see the discussion of this topic toward the
  172. end of this section).
  173. The following table shows the target types with their associated variables and
  174. built-in defaults that apply when no destination is given:
  175. ================== =============================== ======================
  176. Target Type GNUInstallDirs Variable Built-In Default
  177. ================== =============================== ======================
  178. ``RUNTIME`` ``${CMAKE_INSTALL_BINDIR}`` ``bin``
  179. ``LIBRARY`` ``${CMAKE_INSTALL_LIBDIR}`` ``lib``
  180. ``ARCHIVE`` ``${CMAKE_INSTALL_LIBDIR}`` ``lib``
  181. ``PRIVATE_HEADER`` ``${CMAKE_INSTALL_INCLUDEDIR}`` ``include``
  182. ``PUBLIC_HEADER`` ``${CMAKE_INSTALL_INCLUDEDIR}`` ``include``
  183. ================== =============================== ======================
  184. Projects wishing to follow the common practice of installing headers into a
  185. project-specific subdirectory will need to provide a destination rather than
  186. rely on the above.
  187. To make packages compliant with distribution filesystem layout policies, if
  188. projects must specify a ``DESTINATION``, it is recommended that they use a
  189. path that begins with the appropriate :module:`GNUInstallDirs` variable.
  190. This allows package maintainers to control the install destination by setting
  191. the appropriate cache variables. The following example shows a static library
  192. being installed to the default destination provided by
  193. :module:`GNUInstallDirs`, but with its headers installed to a project-specific
  194. subdirectory that follows the above recommendation:
  195. .. code-block:: cmake
  196. add_library(mylib STATIC ...)
  197. set_target_properties(mylib PROPERTIES PUBLIC_HEADER mylib.h)
  198. include(GNUInstallDirs)
  199. install(TARGETS mylib
  200. PUBLIC_HEADER
  201. DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/myproj
  202. )
  203. In addition to the common options listed above, each target can accept
  204. the following additional arguments:
  205. ``NAMELINK_COMPONENT``
  206. .. versionadded:: 3.12
  207. On some platforms a versioned shared library has a symbolic link such
  208. as::
  209. lib<name>.so -> lib<name>.so.1
  210. where ``lib<name>.so.1`` is the soname of the library and ``lib<name>.so``
  211. is a "namelink" allowing linkers to find the library when given
  212. ``-l<name>``. The ``NAMELINK_COMPONENT`` option is similar to the
  213. ``COMPONENT`` option, but it changes the installation component of a shared
  214. library namelink if one is generated. If not specified, this defaults to the
  215. value of ``COMPONENT``. It is an error to use this parameter outside of a
  216. ``LIBRARY`` block.
  217. Consider the following example:
  218. .. code-block:: cmake
  219. install(TARGETS mylib
  220. LIBRARY
  221. COMPONENT Libraries
  222. NAMELINK_COMPONENT Development
  223. PUBLIC_HEADER
  224. COMPONENT Development
  225. )
  226. In this scenario, if you choose to install only the ``Development``
  227. component, both the headers and namelink will be installed without the
  228. library. (If you don't also install the ``Libraries`` component, the
  229. namelink will be a dangling symlink, and projects that link to the library
  230. will have build errors.) If you install only the ``Libraries`` component,
  231. only the library will be installed, without the headers and namelink.
  232. This option is typically used for package managers that have separate
  233. runtime and development packages. For example, on Debian systems, the
  234. library is expected to be in the runtime package, and the headers and
  235. namelink are expected to be in the development package.
  236. See the :prop_tgt:`VERSION` and :prop_tgt:`SOVERSION` target properties for
  237. details on creating versioned shared libraries.
  238. ``NAMELINK_ONLY``
  239. This option causes the installation of only the namelink when a library
  240. target is installed. On platforms where versioned shared libraries do not
  241. have namelinks or when a library is not versioned, the ``NAMELINK_ONLY``
  242. option installs nothing. It is an error to use this parameter outside of a
  243. ``LIBRARY`` block.
  244. When ``NAMELINK_ONLY`` is given, either ``NAMELINK_COMPONENT`` or
  245. ``COMPONENT`` may be used to specify the installation component of the
  246. namelink, but ``COMPONENT`` should generally be preferred.
  247. ``NAMELINK_SKIP``
  248. Similar to ``NAMELINK_ONLY``, but it has the opposite effect: it causes the
  249. installation of library files other than the namelink when a library target
  250. is installed. When neither ``NAMELINK_ONLY`` or ``NAMELINK_SKIP`` are given,
  251. both portions are installed. On platforms where versioned shared libraries
  252. do not have symlinks or when a library is not versioned, ``NAMELINK_SKIP``
  253. installs the library. It is an error to use this parameter outside of a
  254. ``LIBRARY`` block.
  255. If ``NAMELINK_SKIP`` is specified, ``NAMELINK_COMPONENT`` has no effect. It
  256. is not recommended to use ``NAMELINK_SKIP`` in conjunction with
  257. ``NAMELINK_COMPONENT``.
  258. The `install(TARGETS)`_ command can also accept the following options at the
  259. top level:
  260. ``EXPORT``
  261. This option associates the installed target files with an export called
  262. ``<export-name>``. It must appear before any target options. To actually
  263. install the export file itself, call `install(EXPORT)`_, documented below.
  264. See documentation of the :prop_tgt:`EXPORT_NAME` target property to change
  265. the name of the exported target.
  266. ``INCLUDES DESTINATION``
  267. This option specifies a list of directories which will be added to the
  268. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` target property of the
  269. ``<targets>`` when exported by the `install(EXPORT)`_ command. If a
  270. relative path is specified, it is treated as relative to the
  271. ``$<INSTALL_PREFIX>``.
  272. One or more groups of properties may be specified in a single call to
  273. the ``TARGETS`` form of this command. A target may be installed more than
  274. once to different locations. Consider hypothetical targets ``myExe``,
  275. ``mySharedLib``, and ``myStaticLib``. The code:
  276. .. code-block:: cmake
  277. install(TARGETS myExe mySharedLib myStaticLib
  278. RUNTIME DESTINATION bin
  279. LIBRARY DESTINATION lib
  280. ARCHIVE DESTINATION lib/static)
  281. install(TARGETS mySharedLib DESTINATION /some/full/path)
  282. will install ``myExe`` to ``<prefix>/bin`` and ``myStaticLib`` to
  283. ``<prefix>/lib/static``. On non-DLL platforms ``mySharedLib`` will be
  284. installed to ``<prefix>/lib`` and ``/some/full/path``. On DLL platforms
  285. the ``mySharedLib`` DLL will be installed to ``<prefix>/bin`` and
  286. ``/some/full/path`` and its import library will be installed to
  287. ``<prefix>/lib/static`` and ``/some/full/path``.
  288. :ref:`Interface Libraries` may be listed among the targets to install.
  289. They install no artifacts but will be included in an associated ``EXPORT``.
  290. If :ref:`Object Libraries` are listed but given no destination for their
  291. object files, they will be exported as :ref:`Interface Libraries`.
  292. This is sufficient to satisfy transitive usage requirements of other
  293. targets that link to the object libraries in their implementation.
  294. Installing a target with the :prop_tgt:`EXCLUDE_FROM_ALL` target property
  295. set to ``TRUE`` has undefined behavior.
  296. .. versionadded:: 3.3
  297. An install destination given as a ``DESTINATION`` argument may
  298. use "generator expressions" with the syntax ``$<...>``. See the
  299. :manual:`cmake-generator-expressions(7)` manual for available expressions.
  300. .. versionadded:: 3.13
  301. `install(TARGETS)`_ can install targets that were created in
  302. other directories. When using such cross-directory install rules, running
  303. ``make install`` (or similar) from a subdirectory will not guarantee that
  304. targets from other directories are up-to-date. You can use
  305. :command:`target_link_libraries` or :command:`add_dependencies`
  306. to ensure that such out-of-directory targets are built before the
  307. subdirectory-specific install rules are run.
  308. Installing Imported Runtime Artifacts
  309. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  310. .. _`install(IMPORTED_RUNTIME_ARTIFACTS)`:
  311. .. _IMPORTED_RUNTIME_ARTIFACTS:
  312. .. versionadded:: 3.21
  313. .. code-block:: cmake
  314. install(IMPORTED_RUNTIME_ARTIFACTS targets...
  315. [[LIBRARY|RUNTIME|FRAMEWORK|BUNDLE]
  316. [DESTINATION <dir>]
  317. [PERMISSIONS permissions...]
  318. [CONFIGURATIONS [Debug|Release|...]]
  319. [COMPONENT <component>]
  320. [OPTIONAL] [EXCLUDE_FROM_ALL]
  321. ] [...]
  322. )
  323. The ``IMPORTED_RUNTIME_ARTIFACTS`` form specifies rules for installing the
  324. runtime artifacts of imported targets. Projects may do this if they want to
  325. bundle outside executables or modules inside their installation. The
  326. ``LIBRARY``, ``RUNTIME``, ``FRAMEWORK``, and ``BUNDLE`` arguments have the
  327. same semantics that they do in the `TARGETS`_ mode. Only the runtime artifacts
  328. of imported targets are installed (except in the case of :prop_tgt:`FRAMEWORK`
  329. libraries, :prop_tgt:`MACOSX_BUNDLE` executables, and :prop_tgt:`BUNDLE`
  330. CFBundles.) For example, headers and import libraries associated with DLLs are
  331. not installed. In the case of :prop_tgt:`FRAMEWORK` libraries,
  332. :prop_tgt:`MACOSX_BUNDLE` executables, and :prop_tgt:`BUNDLE` CFBundles, the
  333. entire directory is installed.
  334. Installing Files
  335. ^^^^^^^^^^^^^^^^
  336. .. _`install(FILES)`:
  337. .. _`install(PROGRAMS)`:
  338. .. _FILES:
  339. .. _PROGRAMS:
  340. .. code-block:: cmake
  341. install(<FILES|PROGRAMS> files...
  342. TYPE <type> | DESTINATION <dir>
  343. [PERMISSIONS permissions...]
  344. [CONFIGURATIONS [Debug|Release|...]]
  345. [COMPONENT <component>]
  346. [RENAME <name>] [OPTIONAL] [EXCLUDE_FROM_ALL])
  347. The ``FILES`` form specifies rules for installing files for a project.
  348. File names given as relative paths are interpreted with respect to the
  349. current source directory. Files installed by this form are by default
  350. given permissions ``OWNER_WRITE``, ``OWNER_READ``, ``GROUP_READ``, and
  351. ``WORLD_READ`` if no ``PERMISSIONS`` argument is given.
  352. The ``PROGRAMS`` form is identical to the ``FILES`` form except that the
  353. default permissions for the installed file also include ``OWNER_EXECUTE``,
  354. ``GROUP_EXECUTE``, and ``WORLD_EXECUTE``. This form is intended to install
  355. programs that are not targets, such as shell scripts. Use the ``TARGETS``
  356. form to install targets built within the project.
  357. The list of ``files...`` given to ``FILES`` or ``PROGRAMS`` may use
  358. "generator expressions" with the syntax ``$<...>``. See the
  359. :manual:`cmake-generator-expressions(7)` manual for available expressions.
  360. However, if any item begins in a generator expression it must evaluate
  361. to a full path.
  362. Either a ``TYPE`` or a ``DESTINATION`` must be provided, but not both.
  363. A ``TYPE`` argument specifies the generic file type of the files being
  364. installed. A destination will then be set automatically by taking the
  365. corresponding variable from :module:`GNUInstallDirs`, or by using a
  366. built-in default if that variable is not defined. See the table below for
  367. the supported file types and their corresponding variables and built-in
  368. defaults. Projects can provide a ``DESTINATION`` argument instead of a
  369. file type if they wish to explicitly define the install destination.
  370. ======================= ================================== =========================
  371. ``TYPE`` Argument GNUInstallDirs Variable Built-In Default
  372. ======================= ================================== =========================
  373. ``BIN`` ``${CMAKE_INSTALL_BINDIR}`` ``bin``
  374. ``SBIN`` ``${CMAKE_INSTALL_SBINDIR}`` ``sbin``
  375. ``LIB`` ``${CMAKE_INSTALL_LIBDIR}`` ``lib``
  376. ``INCLUDE`` ``${CMAKE_INSTALL_INCLUDEDIR}`` ``include``
  377. ``SYSCONF`` ``${CMAKE_INSTALL_SYSCONFDIR}`` ``etc``
  378. ``SHAREDSTATE`` ``${CMAKE_INSTALL_SHARESTATEDIR}`` ``com``
  379. ``LOCALSTATE`` ``${CMAKE_INSTALL_LOCALSTATEDIR}`` ``var``
  380. ``RUNSTATE`` ``${CMAKE_INSTALL_RUNSTATEDIR}`` ``<LOCALSTATE dir>/run``
  381. ``DATA`` ``${CMAKE_INSTALL_DATADIR}`` ``<DATAROOT dir>``
  382. ``INFO`` ``${CMAKE_INSTALL_INFODIR}`` ``<DATAROOT dir>/info``
  383. ``LOCALE`` ``${CMAKE_INSTALL_LOCALEDIR}`` ``<DATAROOT dir>/locale``
  384. ``MAN`` ``${CMAKE_INSTALL_MANDIR}`` ``<DATAROOT dir>/man``
  385. ``DOC`` ``${CMAKE_INSTALL_DOCDIR}`` ``<DATAROOT dir>/doc``
  386. ======================= ================================== =========================
  387. Projects wishing to follow the common practice of installing headers into a
  388. project-specific subdirectory will need to provide a destination rather than
  389. rely on the above.
  390. Note that some of the types' built-in defaults use the ``DATAROOT`` directory as
  391. a prefix. The ``DATAROOT`` prefix is calculated similarly to the types, with
  392. ``CMAKE_INSTALL_DATAROOTDIR`` as the variable and ``share`` as the built-in
  393. default. You cannot use ``DATAROOT`` as a ``TYPE`` parameter; please use
  394. ``DATA`` instead.
  395. To make packages compliant with distribution filesystem layout policies, if
  396. projects must specify a ``DESTINATION``, it is recommended that they use a
  397. path that begins with the appropriate :module:`GNUInstallDirs` variable.
  398. This allows package maintainers to control the install destination by setting
  399. the appropriate cache variables. The following example shows how to follow
  400. this advice while installing headers to a project-specific subdirectory:
  401. .. code-block:: cmake
  402. include(GNUInstallDirs)
  403. install(FILES mylib.h
  404. DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/myproj
  405. )
  406. .. versionadded:: 3.4
  407. An install destination given as a ``DESTINATION`` argument may
  408. use "generator expressions" with the syntax ``$<...>``. See the
  409. :manual:`cmake-generator-expressions(7)` manual for available expressions.
  410. .. versionadded:: 3.20
  411. An install rename given as a ``RENAME`` argument may
  412. use "generator expressions" with the syntax ``$<...>``. See the
  413. :manual:`cmake-generator-expressions(7)` manual for available expressions.
  414. Installing Directories
  415. ^^^^^^^^^^^^^^^^^^^^^^
  416. .. _`install(DIRECTORY)`:
  417. .. _DIRECTORY:
  418. .. code-block:: cmake
  419. install(DIRECTORY dirs...
  420. TYPE <type> | DESTINATION <dir>
  421. [FILE_PERMISSIONS permissions...]
  422. [DIRECTORY_PERMISSIONS permissions...]
  423. [USE_SOURCE_PERMISSIONS] [OPTIONAL] [MESSAGE_NEVER]
  424. [CONFIGURATIONS [Debug|Release|...]]
  425. [COMPONENT <component>] [EXCLUDE_FROM_ALL]
  426. [FILES_MATCHING]
  427. [[PATTERN <pattern> | REGEX <regex>]
  428. [EXCLUDE] [PERMISSIONS permissions...]] [...])
  429. The ``DIRECTORY`` form installs contents of one or more directories to a
  430. given destination. The directory structure is copied verbatim to the
  431. destination. The last component of each directory name is appended to
  432. the destination directory but a trailing slash may be used to avoid
  433. this because it leaves the last component empty. Directory names
  434. given as relative paths are interpreted with respect to the current
  435. source directory. If no input directory names are given the
  436. destination directory will be created but nothing will be installed
  437. into it. The ``FILE_PERMISSIONS`` and ``DIRECTORY_PERMISSIONS`` options
  438. specify permissions given to files and directories in the destination.
  439. If ``USE_SOURCE_PERMISSIONS`` is specified and ``FILE_PERMISSIONS`` is not,
  440. file permissions will be copied from the source directory structure.
  441. If no permissions are specified files will be given the default
  442. permissions specified in the ``FILES`` form of the command, and the
  443. directories will be given the default permissions specified in the
  444. ``PROGRAMS`` form of the command.
  445. .. versionadded:: 3.1
  446. The ``MESSAGE_NEVER`` option disables file installation status output.
  447. Installation of directories may be controlled with fine granularity
  448. using the ``PATTERN`` or ``REGEX`` options. These "match" options specify a
  449. globbing pattern or regular expression to match directories or files
  450. encountered within input directories. They may be used to apply
  451. certain options (see below) to a subset of the files and directories
  452. encountered. The full path to each input file or directory (with
  453. forward slashes) is matched against the expression. A ``PATTERN`` will
  454. match only complete file names: the portion of the full path matching
  455. the pattern must occur at the end of the file name and be preceded by
  456. a slash. A ``REGEX`` will match any portion of the full path but it may
  457. use ``/`` and ``$`` to simulate the ``PATTERN`` behavior. By default all
  458. files and directories are installed whether or not they are matched.
  459. The ``FILES_MATCHING`` option may be given before the first match option
  460. to disable installation of files (but not directories) not matched by
  461. any expression. For example, the code
  462. .. code-block:: cmake
  463. install(DIRECTORY src/ DESTINATION include/myproj
  464. FILES_MATCHING PATTERN "*.h")
  465. will extract and install header files from a source tree.
  466. Some options may follow a ``PATTERN`` or ``REGEX`` expression as described
  467. under :ref:`string(REGEX) <Regex Specification>` and are applied
  468. only to files or directories matching them. The ``EXCLUDE`` option will
  469. skip the matched file or directory. The ``PERMISSIONS`` option overrides
  470. the permissions setting for the matched file or directory. For
  471. example the code
  472. .. code-block:: cmake
  473. install(DIRECTORY icons scripts/ DESTINATION share/myproj
  474. PATTERN "CVS" EXCLUDE
  475. PATTERN "scripts/*"
  476. PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ
  477. GROUP_EXECUTE GROUP_READ)
  478. will install the ``icons`` directory to ``share/myproj/icons`` and the
  479. ``scripts`` directory to ``share/myproj``. The icons will get default
  480. file permissions, the scripts will be given specific permissions, and any
  481. ``CVS`` directories will be excluded.
  482. Either a ``TYPE`` or a ``DESTINATION`` must be provided, but not both.
  483. A ``TYPE`` argument specifies the generic file type of the files within the
  484. listed directories being installed. A destination will then be set
  485. automatically by taking the corresponding variable from
  486. :module:`GNUInstallDirs`, or by using a built-in default if that variable
  487. is not defined. See the table below for the supported file types and their
  488. corresponding variables and built-in defaults. Projects can provide a
  489. ``DESTINATION`` argument instead of a file type if they wish to explicitly
  490. define the install destination.
  491. ======================= ================================== =========================
  492. ``TYPE`` Argument GNUInstallDirs Variable Built-In Default
  493. ======================= ================================== =========================
  494. ``BIN`` ``${CMAKE_INSTALL_BINDIR}`` ``bin``
  495. ``SBIN`` ``${CMAKE_INSTALL_SBINDIR}`` ``sbin``
  496. ``LIB`` ``${CMAKE_INSTALL_LIBDIR}`` ``lib``
  497. ``INCLUDE`` ``${CMAKE_INSTALL_INCLUDEDIR}`` ``include``
  498. ``SYSCONF`` ``${CMAKE_INSTALL_SYSCONFDIR}`` ``etc``
  499. ``SHAREDSTATE`` ``${CMAKE_INSTALL_SHARESTATEDIR}`` ``com``
  500. ``LOCALSTATE`` ``${CMAKE_INSTALL_LOCALSTATEDIR}`` ``var``
  501. ``RUNSTATE`` ``${CMAKE_INSTALL_RUNSTATEDIR}`` ``<LOCALSTATE dir>/run``
  502. ``DATA`` ``${CMAKE_INSTALL_DATADIR}`` ``<DATAROOT dir>``
  503. ``INFO`` ``${CMAKE_INSTALL_INFODIR}`` ``<DATAROOT dir>/info``
  504. ``LOCALE`` ``${CMAKE_INSTALL_LOCALEDIR}`` ``<DATAROOT dir>/locale``
  505. ``MAN`` ``${CMAKE_INSTALL_MANDIR}`` ``<DATAROOT dir>/man``
  506. ``DOC`` ``${CMAKE_INSTALL_DOCDIR}`` ``<DATAROOT dir>/doc``
  507. ======================= ================================== =========================
  508. Note that some of the types' built-in defaults use the ``DATAROOT`` directory as
  509. a prefix. The ``DATAROOT`` prefix is calculated similarly to the types, with
  510. ``CMAKE_INSTALL_DATAROOTDIR`` as the variable and ``share`` as the built-in
  511. default. You cannot use ``DATAROOT`` as a ``TYPE`` parameter; please use
  512. ``DATA`` instead.
  513. To make packages compliant with distribution filesystem layout policies, if
  514. projects must specify a ``DESTINATION``, it is recommended that they use a
  515. path that begins with the appropriate :module:`GNUInstallDirs` variable.
  516. This allows package maintainers to control the install destination by setting
  517. the appropriate cache variables.
  518. .. versionadded:: 3.4
  519. An install destination given as a ``DESTINATION`` argument may
  520. use "generator expressions" with the syntax ``$<...>``. See the
  521. :manual:`cmake-generator-expressions(7)` manual for available expressions.
  522. .. versionadded:: 3.5
  523. The list of ``dirs...`` given to ``DIRECTORY`` may use
  524. "generator expressions" too.
  525. Custom Installation Logic
  526. ^^^^^^^^^^^^^^^^^^^^^^^^^
  527. .. _`install(CODE)`:
  528. .. _`install(SCRIPT)`:
  529. .. _CODE:
  530. .. _SCRIPT:
  531. .. code-block:: cmake
  532. install([[SCRIPT <file>] [CODE <code>]]
  533. [COMPONENT <component>] [EXCLUDE_FROM_ALL] [ALL_COMPONENTS] [...])
  534. The ``SCRIPT`` form will invoke the given CMake script files during
  535. installation. If the script file name is a relative path it will be
  536. interpreted with respect to the current source directory. The ``CODE``
  537. form will invoke the given CMake code during installation. Code is
  538. specified as a single argument inside a double-quoted string. For
  539. example, the code
  540. .. code-block:: cmake
  541. install(CODE "MESSAGE(\"Sample install message.\")")
  542. will print a message during installation.
  543. The option ``ALL_COMPONENTS``
  544. .. versionadded:: 3.21
  545. Run the custom installation script code for every component of a
  546. component-specific installation.
  547. .. versionadded:: 3.14
  548. ``<file>`` or ``<code>`` may use "generator expressions" with the syntax
  549. ``$<...>`` (in the case of ``<file>``, this refers to their use in the file
  550. name, not the file's contents). See the
  551. :manual:`cmake-generator-expressions(7)` manual for available expressions.
  552. Installing Exports
  553. ^^^^^^^^^^^^^^^^^^
  554. .. _`install(EXPORT)`:
  555. .. _EXPORT:
  556. .. code-block:: cmake
  557. install(EXPORT <export-name> DESTINATION <dir>
  558. [NAMESPACE <namespace>] [[FILE <name>.cmake]|
  559. [PERMISSIONS permissions...]
  560. [CONFIGURATIONS [Debug|Release|...]]
  561. [EXPORT_LINK_INTERFACE_LIBRARIES]
  562. [COMPONENT <component>]
  563. [EXCLUDE_FROM_ALL])
  564. install(EXPORT_ANDROID_MK <export-name> DESTINATION <dir> [...])
  565. The ``EXPORT`` form generates and installs a CMake file containing code to
  566. import targets from the installation tree into another project.
  567. Target installations are associated with the export ``<export-name>``
  568. using the ``EXPORT`` option of the `install(TARGETS)`_ signature
  569. documented above. The ``NAMESPACE`` option will prepend ``<namespace>`` to
  570. the target names as they are written to the import file. By default
  571. the generated file will be called ``<export-name>.cmake`` but the ``FILE``
  572. option may be used to specify a different name. The value given to
  573. the ``FILE`` option must be a file name with the ``.cmake`` extension.
  574. If a ``CONFIGURATIONS`` option is given then the file will only be installed
  575. when one of the named configurations is installed. Additionally, the
  576. generated import file will reference only the matching target
  577. configurations. The ``EXPORT_LINK_INTERFACE_LIBRARIES`` keyword, if
  578. present, causes the contents of the properties matching
  579. ``(IMPORTED_)?LINK_INTERFACE_LIBRARIES(_<CONFIG>)?`` to be exported, when
  580. policy :policy:`CMP0022` is ``NEW``.
  581. .. note::
  582. The installed ``<export-name>.cmake`` file may come with additional
  583. per-configuration ``<export-name>-*.cmake`` files to be loaded by
  584. globbing. Do not use an export name that is the same as the package
  585. name in combination with installing a ``<package-name>-config.cmake``
  586. file or the latter may be incorrectly matched by the glob and loaded.
  587. When a ``COMPONENT`` option is given, the listed ``<component>`` implicitly
  588. depends on all components mentioned in the export set. The exported
  589. ``<name>.cmake`` file will require each of the exported components to be
  590. present in order for dependent projects to build properly. For example, a
  591. project may define components ``Runtime`` and ``Development``, with shared
  592. libraries going into the ``Runtime`` component and static libraries and
  593. headers going into the ``Development`` component. The export set would also
  594. typically be part of the ``Development`` component, but it would export
  595. targets from both the ``Runtime`` and ``Development`` components. Therefore,
  596. the ``Runtime`` component would need to be installed if the ``Development``
  597. component was installed, but not vice versa. If the ``Development`` component
  598. was installed without the ``Runtime`` component, dependent projects that try
  599. to link against it would have build errors. Package managers, such as APT and
  600. RPM, typically handle this by listing the ``Runtime`` component as a dependency
  601. of the ``Development`` component in the package metadata, ensuring that the
  602. library is always installed if the headers and CMake export file are present.
  603. .. versionadded:: 3.7
  604. In addition to cmake language files, the ``EXPORT_ANDROID_MK`` mode maybe
  605. used to specify an export to the android ndk build system. This mode
  606. accepts the same options as the normal export mode. The Android
  607. NDK supports the use of prebuilt libraries, both static and shared. This
  608. allows cmake to build the libraries of a project and make them available
  609. to an ndk build system complete with transitive dependencies, include flags
  610. and defines required to use the libraries.
  611. The ``EXPORT`` form is useful to help outside projects use targets built
  612. and installed by the current project. For example, the code
  613. .. code-block:: cmake
  614. install(TARGETS myexe EXPORT myproj DESTINATION bin)
  615. install(EXPORT myproj NAMESPACE mp_ DESTINATION lib/myproj)
  616. install(EXPORT_ANDROID_MK myproj DESTINATION share/ndk-modules)
  617. will install the executable ``myexe`` to ``<prefix>/bin`` and code to import
  618. it in the file ``<prefix>/lib/myproj/myproj.cmake`` and
  619. ``<prefix>/share/ndk-modules/Android.mk``. An outside project
  620. may load this file with the include command and reference the ``myexe``
  621. executable from the installation tree using the imported target name
  622. ``mp_myexe`` as if the target were built in its own tree.
  623. .. note::
  624. This command supersedes the :command:`install_targets` command and
  625. the :prop_tgt:`PRE_INSTALL_SCRIPT` and :prop_tgt:`POST_INSTALL_SCRIPT`
  626. target properties. It also replaces the ``FILES`` forms of the
  627. :command:`install_files` and :command:`install_programs` commands.
  628. The processing order of these install rules relative to
  629. those generated by :command:`install_targets`,
  630. :command:`install_files`, and :command:`install_programs` commands
  631. is not defined.
  632. Generated Installation Script
  633. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  634. .. note::
  635. Use of this feature is not recommended. Please consider using the
  636. ``--install`` argument of :manual:`cmake(1)` instead.
  637. The ``install()`` command generates a file, ``cmake_install.cmake``, inside
  638. the build directory, which is used internally by the generated install target
  639. and by CPack. You can also invoke this script manually with ``cmake -P``. This
  640. script accepts several variables:
  641. ``COMPONENT``
  642. Set this variable to install only a single CPack component as opposed to all
  643. of them. For example, if you only want to install the ``Development``
  644. component, run ``cmake -DCOMPONENT=Development -P cmake_install.cmake``.
  645. ``BUILD_TYPE``
  646. Set this variable to change the build type if you are using a multi-config
  647. generator. For example, to install with the ``Debug`` configuration, run
  648. ``cmake -DBUILD_TYPE=Debug -P cmake_install.cmake``.
  649. ``DESTDIR``
  650. This is an environment variable rather than a CMake variable. It allows you
  651. to change the installation prefix on UNIX systems. See :envvar:`DESTDIR` for
  652. details.