install.rst 33 KB

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