install.rst 34 KB

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