install.rst 40 KB

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