install.rst 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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. Installing Directories
  384. ^^^^^^^^^^^^^^^^^^^^^^
  385. .. _`install(DIRECTORY)`:
  386. .. _DIRECTORY:
  387. .. code-block:: cmake
  388. install(DIRECTORY dirs...
  389. TYPE <type> | DESTINATION <dir>
  390. [FILE_PERMISSIONS permissions...]
  391. [DIRECTORY_PERMISSIONS permissions...]
  392. [USE_SOURCE_PERMISSIONS] [OPTIONAL] [MESSAGE_NEVER]
  393. [CONFIGURATIONS [Debug|Release|...]]
  394. [COMPONENT <component>] [EXCLUDE_FROM_ALL]
  395. [FILES_MATCHING]
  396. [[PATTERN <pattern> | REGEX <regex>]
  397. [EXCLUDE] [PERMISSIONS permissions...]] [...])
  398. The ``DIRECTORY`` form installs contents of one or more directories to a
  399. given destination. The directory structure is copied verbatim to the
  400. destination. The last component of each directory name is appended to
  401. the destination directory but a trailing slash may be used to avoid
  402. this because it leaves the last component empty. Directory names
  403. given as relative paths are interpreted with respect to the current
  404. source directory. If no input directory names are given the
  405. destination directory will be created but nothing will be installed
  406. into it. The ``FILE_PERMISSIONS`` and ``DIRECTORY_PERMISSIONS`` options
  407. specify permissions given to files and directories in the destination.
  408. If ``USE_SOURCE_PERMISSIONS`` is specified and ``FILE_PERMISSIONS`` is not,
  409. file permissions will be copied from the source directory structure.
  410. If no permissions are specified files will be given the default
  411. permissions specified in the ``FILES`` form of the command, and the
  412. directories will be given the default permissions specified in the
  413. ``PROGRAMS`` form of the command.
  414. .. versionadded:: 3.1
  415. The ``MESSAGE_NEVER`` option disables file installation status output.
  416. Installation of directories may be controlled with fine granularity
  417. using the ``PATTERN`` or ``REGEX`` options. These "match" options specify a
  418. globbing pattern or regular expression to match directories or files
  419. encountered within input directories. They may be used to apply
  420. certain options (see below) to a subset of the files and directories
  421. encountered. The full path to each input file or directory (with
  422. forward slashes) is matched against the expression. A ``PATTERN`` will
  423. match only complete file names: the portion of the full path matching
  424. the pattern must occur at the end of the file name and be preceded by
  425. a slash. A ``REGEX`` will match any portion of the full path but it may
  426. use ``/`` and ``$`` to simulate the ``PATTERN`` behavior. By default all
  427. files and directories are installed whether or not they are matched.
  428. The ``FILES_MATCHING`` option may be given before the first match option
  429. to disable installation of files (but not directories) not matched by
  430. any expression. For example, the code
  431. .. code-block:: cmake
  432. install(DIRECTORY src/ DESTINATION include/myproj
  433. FILES_MATCHING PATTERN "*.h")
  434. will extract and install header files from a source tree.
  435. Some options may follow a ``PATTERN`` or ``REGEX`` expression and are applied
  436. only to files or directories matching them. The ``EXCLUDE`` option will
  437. skip the matched file or directory. The ``PERMISSIONS`` option overrides
  438. the permissions setting for the matched file or directory. For
  439. example the code
  440. .. code-block:: cmake
  441. install(DIRECTORY icons scripts/ DESTINATION share/myproj
  442. PATTERN "CVS" EXCLUDE
  443. PATTERN "scripts/*"
  444. PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ
  445. GROUP_EXECUTE GROUP_READ)
  446. will install the ``icons`` directory to ``share/myproj/icons`` and the
  447. ``scripts`` directory to ``share/myproj``. The icons will get default
  448. file permissions, the scripts will be given specific permissions, and any
  449. ``CVS`` directories will be excluded.
  450. Either a ``TYPE`` or a ``DESTINATION`` must be provided, but not both.
  451. A ``TYPE`` argument specifies the generic file type of the files within the
  452. listed directories being installed. A destination will then be set
  453. automatically by taking the corresponding variable from
  454. :module:`GNUInstallDirs`, or by using a built-in default if that variable
  455. is not defined. See the table below for the supported file types and their
  456. corresponding variables and built-in defaults. Projects can provide a
  457. ``DESTINATION`` argument instead of a file type if they wish to explicitly
  458. define the install destination.
  459. ======================= ================================== =========================
  460. ``TYPE`` Argument GNUInstallDirs Variable Built-In Default
  461. ======================= ================================== =========================
  462. ``BIN`` ``${CMAKE_INSTALL_BINDIR}`` ``bin``
  463. ``SBIN`` ``${CMAKE_INSTALL_SBINDIR}`` ``sbin``
  464. ``LIB`` ``${CMAKE_INSTALL_LIBDIR}`` ``lib``
  465. ``INCLUDE`` ``${CMAKE_INSTALL_INCLUDEDIR}`` ``include``
  466. ``SYSCONF`` ``${CMAKE_INSTALL_SYSCONFDIR}`` ``etc``
  467. ``SHAREDSTATE`` ``${CMAKE_INSTALL_SHARESTATEDIR}`` ``com``
  468. ``LOCALSTATE`` ``${CMAKE_INSTALL_LOCALSTATEDIR}`` ``var``
  469. ``RUNSTATE`` ``${CMAKE_INSTALL_RUNSTATEDIR}`` ``<LOCALSTATE dir>/run``
  470. ``DATA`` ``${CMAKE_INSTALL_DATADIR}`` ``<DATAROOT dir>``
  471. ``INFO`` ``${CMAKE_INSTALL_INFODIR}`` ``<DATAROOT dir>/info``
  472. ``LOCALE`` ``${CMAKE_INSTALL_LOCALEDIR}`` ``<DATAROOT dir>/locale``
  473. ``MAN`` ``${CMAKE_INSTALL_MANDIR}`` ``<DATAROOT dir>/man``
  474. ``DOC`` ``${CMAKE_INSTALL_DOCDIR}`` ``<DATAROOT dir>/doc``
  475. ======================= ================================== =========================
  476. Note that some of the types' built-in defaults use the ``DATAROOT`` directory as
  477. a prefix. The ``DATAROOT`` prefix is calculated similarly to the types, with
  478. ``CMAKE_INSTALL_DATAROOTDIR`` as the variable and ``share`` as the built-in
  479. default. You cannot use ``DATAROOT`` as a ``TYPE`` parameter; please use
  480. ``DATA`` instead.
  481. To make packages compliant with distribution filesystem layout policies, if
  482. projects must specify a ``DESTINATION``, it is recommended that they use a
  483. path that begins with the appropriate :module:`GNUInstallDirs` variable.
  484. This allows package maintainers to control the install destination by setting
  485. the appropriate cache variables.
  486. .. versionadded:: 3.4
  487. An install destination given as a ``DESTINATION`` argument may
  488. use "generator expressions" with the syntax ``$<...>``. See the
  489. :manual:`cmake-generator-expressions(7)` manual for available expressions.
  490. .. versionadded:: 3.5
  491. The list of ``dirs...`` given to ``DIRECTORY`` may use
  492. "generator expressions" too.
  493. Custom Installation Logic
  494. ^^^^^^^^^^^^^^^^^^^^^^^^^
  495. .. _`install(CODE)`:
  496. .. _`install(SCRIPT)`:
  497. .. _CODE:
  498. .. _SCRIPT:
  499. .. code-block:: cmake
  500. install([[SCRIPT <file>] [CODE <code>]]
  501. [COMPONENT <component>] [EXCLUDE_FROM_ALL] [...])
  502. The ``SCRIPT`` form will invoke the given CMake script files during
  503. installation. If the script file name is a relative path it will be
  504. interpreted with respect to the current source directory. The ``CODE``
  505. form will invoke the given CMake code during installation. Code is
  506. specified as a single argument inside a double-quoted string. For
  507. example, the code
  508. .. code-block:: cmake
  509. install(CODE "MESSAGE(\"Sample install message.\")")
  510. will print a message during installation.
  511. .. versionadded:: 3.14
  512. ``<file>`` or ``<code>`` may use "generator expressions" with the syntax
  513. ``$<...>`` (in the case of ``<file>``, this refers to their use in the file
  514. name, not the file's contents). See the
  515. :manual:`cmake-generator-expressions(7)` manual for available expressions.
  516. Installing Exports
  517. ^^^^^^^^^^^^^^^^^^
  518. .. _`install(EXPORT)`:
  519. .. _EXPORT:
  520. .. code-block:: cmake
  521. install(EXPORT <export-name> DESTINATION <dir>
  522. [NAMESPACE <namespace>] [[FILE <name>.cmake]|
  523. [PERMISSIONS permissions...]
  524. [CONFIGURATIONS [Debug|Release|...]]
  525. [EXPORT_LINK_INTERFACE_LIBRARIES]
  526. [COMPONENT <component>]
  527. [EXCLUDE_FROM_ALL])
  528. install(EXPORT_ANDROID_MK <export-name> DESTINATION <dir> [...])
  529. The ``EXPORT`` form generates and installs a CMake file containing code to
  530. import targets from the installation tree into another project.
  531. Target installations are associated with the export ``<export-name>``
  532. using the ``EXPORT`` option of the `install(TARGETS)`_ signature
  533. documented above. The ``NAMESPACE`` option will prepend ``<namespace>`` to
  534. the target names as they are written to the import file. By default
  535. the generated file will be called ``<export-name>.cmake`` but the ``FILE``
  536. option may be used to specify a different name. The value given to
  537. the ``FILE`` option must be a file name with the ``.cmake`` extension.
  538. If a ``CONFIGURATIONS`` option is given then the file will only be installed
  539. when one of the named configurations is installed. Additionally, the
  540. generated import file will reference only the matching target
  541. configurations. The ``EXPORT_LINK_INTERFACE_LIBRARIES`` keyword, if
  542. present, causes the contents of the properties matching
  543. ``(IMPORTED_)?LINK_INTERFACE_LIBRARIES(_<CONFIG>)?`` to be exported, when
  544. policy :policy:`CMP0022` is ``NEW``.
  545. .. note::
  546. The installed ``<export-name>.cmake`` file may come with additional
  547. per-configuration ``<export-name>-*.cmake`` files to be loaded by
  548. globbing. Do not use an export name that is the same as the package
  549. name in combination with installing a ``<package-name>-config.cmake``
  550. file or the latter may be incorrectly matched by the glob and loaded.
  551. When a ``COMPONENT`` option is given, the listed ``<component>`` implicitly
  552. depends on all components mentioned in the export set. The exported
  553. ``<name>.cmake`` file will require each of the exported components to be
  554. present in order for dependent projects to build properly. For example, a
  555. project may define components ``Runtime`` and ``Development``, with shared
  556. libraries going into the ``Runtime`` component and static libraries and
  557. headers going into the ``Development`` component. The export set would also
  558. typically be part of the ``Development`` component, but it would export
  559. targets from both the ``Runtime`` and ``Development`` components. Therefore,
  560. the ``Runtime`` component would need to be installed if the ``Development``
  561. component was installed, but not vice versa. If the ``Development`` component
  562. was installed without the ``Runtime`` component, dependent projects that try
  563. to link against it would have build errors. Package managers, such as APT and
  564. RPM, typically handle this by listing the ``Runtime`` component as a dependency
  565. of the ``Development`` component in the package metadata, ensuring that the
  566. library is always installed if the headers and CMake export file are present.
  567. .. versionadded:: 3.7
  568. In addition to cmake language files, the ``EXPORT_ANDROID_MK`` mode maybe
  569. used to specify an export to the android ndk build system. This mode
  570. accepts the same options as the normal export mode. The Android
  571. NDK supports the use of prebuilt libraries, both static and shared. This
  572. allows cmake to build the libraries of a project and make them available
  573. to an ndk build system complete with transitive dependencies, include flags
  574. and defines required to use the libraries.
  575. The ``EXPORT`` form is useful to help outside projects use targets built
  576. and installed by the current project. For example, the code
  577. .. code-block:: cmake
  578. install(TARGETS myexe EXPORT myproj DESTINATION bin)
  579. install(EXPORT myproj NAMESPACE mp_ DESTINATION lib/myproj)
  580. install(EXPORT_ANDROID_MK myproj DESTINATION share/ndk-modules)
  581. will install the executable ``myexe`` to ``<prefix>/bin`` and code to import
  582. it in the file ``<prefix>/lib/myproj/myproj.cmake`` and
  583. ``<prefix>/share/ndk-modules/Android.mk``. An outside project
  584. may load this file with the include command and reference the ``myexe``
  585. executable from the installation tree using the imported target name
  586. ``mp_myexe`` as if the target were built in its own tree.
  587. .. note::
  588. This command supercedes the :command:`install_targets` command and
  589. the :prop_tgt:`PRE_INSTALL_SCRIPT` and :prop_tgt:`POST_INSTALL_SCRIPT`
  590. target properties. It also replaces the ``FILES`` forms of the
  591. :command:`install_files` and :command:`install_programs` commands.
  592. The processing order of these install rules relative to
  593. those generated by :command:`install_targets`,
  594. :command:`install_files`, and :command:`install_programs` commands
  595. is not defined.
  596. Generated Installation Script
  597. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  598. .. note::
  599. Use of this feature is not recommended. Please consider using the
  600. ``--install`` argument of :manual:`cmake(1)` instead.
  601. The ``install()`` command generates a file, ``cmake_install.cmake``, inside
  602. the build directory, which is used internally by the generated install target
  603. and by CPack. You can also invoke this script manually with ``cmake -P``. This
  604. script accepts several variables:
  605. ``COMPONENT``
  606. Set this variable to install only a single CPack component as opposed to all
  607. of them. For example, if you only want to install the ``Development``
  608. component, run ``cmake -DCOMPONENT=Development -P cmake_install.cmake``.
  609. ``BUILD_TYPE``
  610. Set this variable to change the build type if you are using a multi-config
  611. generator. For example, to install with the ``Debug`` configuration, run
  612. ``cmake -DBUILD_TYPE=Debug -P cmake_install.cmake``.
  613. ``DESTDIR``
  614. This is an environment variable rather than a CMake variable. It allows you
  615. to change the installation prefix on UNIX systems. See :envvar:`DESTDIR` for
  616. details.