install.rst 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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 with an
  279. `install(RUNTIME_DEPENDENCY_SET)`_ command.
  280. This keyword and the ``RUNTIME_DEPENDENCIES`` keyword 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 the following pair
  290. of calls:
  291. .. code-block:: cmake
  292. install(TARGETS ... RUNTIME_DEPENDENCY_SET <set-name>)
  293. install(RUNTIME_DEPENDENCY_SET <set-name> args...)
  294. where ``<set-name>`` will be a randomly generated set name.
  295. The ``args...`` may include any of the following keywords supported by
  296. the `install(RUNTIME_DEPENDENCY_SET)`_ command:
  297. * ``DIRECTORIES``
  298. * ``PRE_INCLUDE_REGEXES``
  299. * ``PRE_EXCLUDE_REGEXES``
  300. * ``POST_INCLUDE_REGEXES``
  301. * ``POST_EXCLUDE_REGEXES``
  302. * ``POST_INCLUDE_FILES``
  303. * ``POST_EXCLUDE_FILES``
  304. The ``RUNTIME_DEPENDENCIES`` and ``RUNTIME_DEPENDENCY_SET`` keywords are
  305. mutually exclusive.
  306. One or more groups of properties may be specified in a single call to
  307. the ``TARGETS`` form of this command. A target may be installed more than
  308. once to different locations. Consider hypothetical targets ``myExe``,
  309. ``mySharedLib``, and ``myStaticLib``. The code:
  310. .. code-block:: cmake
  311. install(TARGETS myExe mySharedLib myStaticLib
  312. RUNTIME DESTINATION bin
  313. LIBRARY DESTINATION lib
  314. ARCHIVE DESTINATION lib/static)
  315. install(TARGETS mySharedLib DESTINATION /some/full/path)
  316. will install ``myExe`` to ``<prefix>/bin`` and ``myStaticLib`` to
  317. ``<prefix>/lib/static``. On non-DLL platforms ``mySharedLib`` will be
  318. installed to ``<prefix>/lib`` and ``/some/full/path``. On DLL platforms
  319. the ``mySharedLib`` DLL will be installed to ``<prefix>/bin`` and
  320. ``/some/full/path`` and its import library will be installed to
  321. ``<prefix>/lib/static`` and ``/some/full/path``.
  322. :ref:`Interface Libraries` may be listed among the targets to install.
  323. They install no artifacts but will be included in an associated ``EXPORT``.
  324. If :ref:`Object Libraries` are listed but given no destination for their
  325. object files, they will be exported as :ref:`Interface Libraries`.
  326. This is sufficient to satisfy transitive usage requirements of other
  327. targets that link to the object libraries in their implementation.
  328. Installing a target with the :prop_tgt:`EXCLUDE_FROM_ALL` target property
  329. set to ``TRUE`` has undefined behavior.
  330. .. versionadded:: 3.3
  331. An install destination given as a ``DESTINATION`` argument may
  332. use "generator expressions" with the syntax ``$<...>``. See the
  333. :manual:`cmake-generator-expressions(7)` manual for available expressions.
  334. .. versionadded:: 3.13
  335. `install(TARGETS)`_ can install targets that were created in
  336. other directories. When using such cross-directory install rules, running
  337. ``make install`` (or similar) from a subdirectory will not guarantee that
  338. targets from other directories are up-to-date. You can use
  339. :command:`target_link_libraries` or :command:`add_dependencies`
  340. to ensure that such out-of-directory targets are built before the
  341. subdirectory-specific install rules are run.
  342. Installing Imported Runtime Artifacts
  343. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  344. .. _`install(IMPORTED_RUNTIME_ARTIFACTS)`:
  345. .. _IMPORTED_RUNTIME_ARTIFACTS:
  346. .. versionadded:: 3.21
  347. .. code-block:: cmake
  348. install(IMPORTED_RUNTIME_ARTIFACTS targets...
  349. [RUNTIME_DEPENDENCY_SET <set-name>]
  350. [[LIBRARY|RUNTIME|FRAMEWORK|BUNDLE]
  351. [DESTINATION <dir>]
  352. [PERMISSIONS permissions...]
  353. [CONFIGURATIONS [Debug|Release|...]]
  354. [COMPONENT <component>]
  355. [OPTIONAL] [EXCLUDE_FROM_ALL]
  356. ] [...]
  357. )
  358. The ``IMPORTED_RUNTIME_ARTIFACTS`` form specifies rules for installing the
  359. runtime artifacts of imported targets. Projects may do this if they want to
  360. bundle outside executables or modules inside their installation. The
  361. ``LIBRARY``, ``RUNTIME``, ``FRAMEWORK``, and ``BUNDLE`` arguments have the
  362. same semantics that they do in the `TARGETS`_ mode. Only the runtime artifacts
  363. of imported targets are installed (except in the case of :prop_tgt:`FRAMEWORK`
  364. libraries, :prop_tgt:`MACOSX_BUNDLE` executables, and :prop_tgt:`BUNDLE`
  365. CFBundles.) For example, headers and import libraries associated with DLLs are
  366. not installed. In the case of :prop_tgt:`FRAMEWORK` libraries,
  367. :prop_tgt:`MACOSX_BUNDLE` executables, and :prop_tgt:`BUNDLE` CFBundles, the
  368. entire directory is installed.
  369. The ``RUNTIME_DEPENDENCY_SET`` option causes the runtime artifacts of the
  370. imported executable, shared library, and module library ``targets`` to be
  371. added to the ``<set-name>`` runtime dependency set. This set can then be
  372. installed with an `install(RUNTIME_DEPENDENCY_SET)`_ command.
  373. Installing Files
  374. ^^^^^^^^^^^^^^^^
  375. .. _`install(FILES)`:
  376. .. _`install(PROGRAMS)`:
  377. .. _FILES:
  378. .. _PROGRAMS:
  379. .. code-block:: cmake
  380. install(<FILES|PROGRAMS> files...
  381. TYPE <type> | DESTINATION <dir>
  382. [PERMISSIONS permissions...]
  383. [CONFIGURATIONS [Debug|Release|...]]
  384. [COMPONENT <component>]
  385. [RENAME <name>] [OPTIONAL] [EXCLUDE_FROM_ALL])
  386. The ``FILES`` form specifies rules for installing files for a project.
  387. File names given as relative paths are interpreted with respect to the
  388. current source directory. Files installed by this form are by default
  389. given permissions ``OWNER_WRITE``, ``OWNER_READ``, ``GROUP_READ``, and
  390. ``WORLD_READ`` if no ``PERMISSIONS`` argument is given.
  391. The ``PROGRAMS`` form is identical to the ``FILES`` form except that the
  392. default permissions for the installed file also include ``OWNER_EXECUTE``,
  393. ``GROUP_EXECUTE``, and ``WORLD_EXECUTE``. This form is intended to install
  394. programs that are not targets, such as shell scripts. Use the ``TARGETS``
  395. form to install targets built within the project.
  396. The list of ``files...`` given to ``FILES`` or ``PROGRAMS`` may use
  397. "generator expressions" with the syntax ``$<...>``. See the
  398. :manual:`cmake-generator-expressions(7)` manual for available expressions.
  399. However, if any item begins in a generator expression it must evaluate
  400. to a full path.
  401. Either a ``TYPE`` or a ``DESTINATION`` must be provided, but not both.
  402. A ``TYPE`` argument specifies the generic file type of the files being
  403. installed. A destination will then be set automatically by taking the
  404. corresponding variable from :module:`GNUInstallDirs`, or by using a
  405. built-in default if that variable is not defined. See the table below for
  406. the supported file types and their corresponding variables and built-in
  407. defaults. Projects can provide a ``DESTINATION`` argument instead of a
  408. file type if they wish to explicitly define the install destination.
  409. ======================= ================================== =========================
  410. ``TYPE`` Argument GNUInstallDirs Variable Built-In Default
  411. ======================= ================================== =========================
  412. ``BIN`` ``${CMAKE_INSTALL_BINDIR}`` ``bin``
  413. ``SBIN`` ``${CMAKE_INSTALL_SBINDIR}`` ``sbin``
  414. ``LIB`` ``${CMAKE_INSTALL_LIBDIR}`` ``lib``
  415. ``INCLUDE`` ``${CMAKE_INSTALL_INCLUDEDIR}`` ``include``
  416. ``SYSCONF`` ``${CMAKE_INSTALL_SYSCONFDIR}`` ``etc``
  417. ``SHAREDSTATE`` ``${CMAKE_INSTALL_SHARESTATEDIR}`` ``com``
  418. ``LOCALSTATE`` ``${CMAKE_INSTALL_LOCALSTATEDIR}`` ``var``
  419. ``RUNSTATE`` ``${CMAKE_INSTALL_RUNSTATEDIR}`` ``<LOCALSTATE dir>/run``
  420. ``DATA`` ``${CMAKE_INSTALL_DATADIR}`` ``<DATAROOT dir>``
  421. ``INFO`` ``${CMAKE_INSTALL_INFODIR}`` ``<DATAROOT dir>/info``
  422. ``LOCALE`` ``${CMAKE_INSTALL_LOCALEDIR}`` ``<DATAROOT dir>/locale``
  423. ``MAN`` ``${CMAKE_INSTALL_MANDIR}`` ``<DATAROOT dir>/man``
  424. ``DOC`` ``${CMAKE_INSTALL_DOCDIR}`` ``<DATAROOT dir>/doc``
  425. ======================= ================================== =========================
  426. Projects wishing to follow the common practice of installing headers into a
  427. project-specific subdirectory will need to provide a destination rather than
  428. rely on the above.
  429. Note that some of the types' built-in defaults use the ``DATAROOT`` directory as
  430. a prefix. The ``DATAROOT`` prefix is calculated similarly to the types, with
  431. ``CMAKE_INSTALL_DATAROOTDIR`` as the variable and ``share`` as the built-in
  432. default. You cannot use ``DATAROOT`` as a ``TYPE`` parameter; please use
  433. ``DATA`` instead.
  434. To make packages compliant with distribution filesystem layout policies, if
  435. projects must specify a ``DESTINATION``, it is recommended that they use a
  436. path that begins with the appropriate :module:`GNUInstallDirs` variable.
  437. This allows package maintainers to control the install destination by setting
  438. the appropriate cache variables. The following example shows how to follow
  439. this advice while installing headers to a project-specific subdirectory:
  440. .. code-block:: cmake
  441. include(GNUInstallDirs)
  442. install(FILES mylib.h
  443. DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/myproj
  444. )
  445. .. versionadded:: 3.4
  446. An install destination given as a ``DESTINATION`` argument may
  447. use "generator expressions" with the syntax ``$<...>``. See the
  448. :manual:`cmake-generator-expressions(7)` manual for available expressions.
  449. .. versionadded:: 3.20
  450. An install rename given as a ``RENAME`` argument may
  451. use "generator expressions" with the syntax ``$<...>``. See the
  452. :manual:`cmake-generator-expressions(7)` manual for available expressions.
  453. Installing Directories
  454. ^^^^^^^^^^^^^^^^^^^^^^
  455. .. _`install(DIRECTORY)`:
  456. .. _DIRECTORY:
  457. .. code-block:: cmake
  458. install(DIRECTORY dirs...
  459. TYPE <type> | DESTINATION <dir>
  460. [FILE_PERMISSIONS permissions...]
  461. [DIRECTORY_PERMISSIONS permissions...]
  462. [USE_SOURCE_PERMISSIONS] [OPTIONAL] [MESSAGE_NEVER]
  463. [CONFIGURATIONS [Debug|Release|...]]
  464. [COMPONENT <component>] [EXCLUDE_FROM_ALL]
  465. [FILES_MATCHING]
  466. [[PATTERN <pattern> | REGEX <regex>]
  467. [EXCLUDE] [PERMISSIONS permissions...]] [...])
  468. The ``DIRECTORY`` form installs contents of one or more directories to a
  469. given destination. The directory structure is copied verbatim to the
  470. destination. The last component of each directory name is appended to
  471. the destination directory but a trailing slash may be used to avoid
  472. this because it leaves the last component empty. Directory names
  473. given as relative paths are interpreted with respect to the current
  474. source directory. If no input directory names are given the
  475. destination directory will be created but nothing will be installed
  476. into it. The ``FILE_PERMISSIONS`` and ``DIRECTORY_PERMISSIONS`` options
  477. specify permissions given to files and directories in the destination.
  478. If ``USE_SOURCE_PERMISSIONS`` is specified and ``FILE_PERMISSIONS`` is not,
  479. file permissions will be copied from the source directory structure.
  480. If no permissions are specified files will be given the default
  481. permissions specified in the ``FILES`` form of the command, and the
  482. directories will be given the default permissions specified in the
  483. ``PROGRAMS`` form of the command.
  484. .. versionadded:: 3.1
  485. The ``MESSAGE_NEVER`` option disables file installation status output.
  486. Installation of directories may be controlled with fine granularity
  487. using the ``PATTERN`` or ``REGEX`` options. These "match" options specify a
  488. globbing pattern or regular expression to match directories or files
  489. encountered within input directories. They may be used to apply
  490. certain options (see below) to a subset of the files and directories
  491. encountered. The full path to each input file or directory (with
  492. forward slashes) is matched against the expression. A ``PATTERN`` will
  493. match only complete file names: the portion of the full path matching
  494. the pattern must occur at the end of the file name and be preceded by
  495. a slash. A ``REGEX`` will match any portion of the full path but it may
  496. use ``/`` and ``$`` to simulate the ``PATTERN`` behavior. By default all
  497. files and directories are installed whether or not they are matched.
  498. The ``FILES_MATCHING`` option may be given before the first match option
  499. to disable installation of files (but not directories) not matched by
  500. any expression. For example, the code
  501. .. code-block:: cmake
  502. install(DIRECTORY src/ DESTINATION include/myproj
  503. FILES_MATCHING PATTERN "*.h")
  504. will extract and install header files from a source tree.
  505. Some options may follow a ``PATTERN`` or ``REGEX`` expression as described
  506. under :ref:`string(REGEX) <Regex Specification>` and are applied
  507. only to files or directories matching them. The ``EXCLUDE`` option will
  508. skip the matched file or directory. The ``PERMISSIONS`` option overrides
  509. the permissions setting for the matched file or directory. For
  510. example the code
  511. .. code-block:: cmake
  512. install(DIRECTORY icons scripts/ DESTINATION share/myproj
  513. PATTERN "CVS" EXCLUDE
  514. PATTERN "scripts/*"
  515. PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ
  516. GROUP_EXECUTE GROUP_READ)
  517. will install the ``icons`` directory to ``share/myproj/icons`` and the
  518. ``scripts`` directory to ``share/myproj``. The icons will get default
  519. file permissions, the scripts will be given specific permissions, and any
  520. ``CVS`` directories will be excluded.
  521. Either a ``TYPE`` or a ``DESTINATION`` must be provided, but not both.
  522. A ``TYPE`` argument specifies the generic file type of the files within the
  523. listed directories being installed. A destination will then be set
  524. automatically by taking the corresponding variable from
  525. :module:`GNUInstallDirs`, or by using a built-in default if that variable
  526. is not defined. See the table below for the supported file types and their
  527. corresponding variables and built-in defaults. Projects can provide a
  528. ``DESTINATION`` argument instead of a file type if they wish to explicitly
  529. define the install destination.
  530. ======================= ================================== =========================
  531. ``TYPE`` Argument GNUInstallDirs Variable Built-In Default
  532. ======================= ================================== =========================
  533. ``BIN`` ``${CMAKE_INSTALL_BINDIR}`` ``bin``
  534. ``SBIN`` ``${CMAKE_INSTALL_SBINDIR}`` ``sbin``
  535. ``LIB`` ``${CMAKE_INSTALL_LIBDIR}`` ``lib``
  536. ``INCLUDE`` ``${CMAKE_INSTALL_INCLUDEDIR}`` ``include``
  537. ``SYSCONF`` ``${CMAKE_INSTALL_SYSCONFDIR}`` ``etc``
  538. ``SHAREDSTATE`` ``${CMAKE_INSTALL_SHARESTATEDIR}`` ``com``
  539. ``LOCALSTATE`` ``${CMAKE_INSTALL_LOCALSTATEDIR}`` ``var``
  540. ``RUNSTATE`` ``${CMAKE_INSTALL_RUNSTATEDIR}`` ``<LOCALSTATE dir>/run``
  541. ``DATA`` ``${CMAKE_INSTALL_DATADIR}`` ``<DATAROOT dir>``
  542. ``INFO`` ``${CMAKE_INSTALL_INFODIR}`` ``<DATAROOT dir>/info``
  543. ``LOCALE`` ``${CMAKE_INSTALL_LOCALEDIR}`` ``<DATAROOT dir>/locale``
  544. ``MAN`` ``${CMAKE_INSTALL_MANDIR}`` ``<DATAROOT dir>/man``
  545. ``DOC`` ``${CMAKE_INSTALL_DOCDIR}`` ``<DATAROOT dir>/doc``
  546. ======================= ================================== =========================
  547. Note that some of the types' built-in defaults use the ``DATAROOT`` directory as
  548. a prefix. The ``DATAROOT`` prefix is calculated similarly to the types, with
  549. ``CMAKE_INSTALL_DATAROOTDIR`` as the variable and ``share`` as the built-in
  550. default. You cannot use ``DATAROOT`` as a ``TYPE`` parameter; please use
  551. ``DATA`` instead.
  552. To make packages compliant with distribution filesystem layout policies, if
  553. projects must specify a ``DESTINATION``, it is recommended that they use a
  554. path that begins with the appropriate :module:`GNUInstallDirs` variable.
  555. This allows package maintainers to control the install destination by setting
  556. the appropriate cache variables.
  557. .. versionadded:: 3.4
  558. An install destination given as a ``DESTINATION`` argument may
  559. use "generator expressions" with the syntax ``$<...>``. See the
  560. :manual:`cmake-generator-expressions(7)` manual for available expressions.
  561. .. versionadded:: 3.5
  562. The list of ``dirs...`` given to ``DIRECTORY`` may use
  563. "generator expressions" too.
  564. Custom Installation Logic
  565. ^^^^^^^^^^^^^^^^^^^^^^^^^
  566. .. _`install(CODE)`:
  567. .. _`install(SCRIPT)`:
  568. .. _CODE:
  569. .. _SCRIPT:
  570. .. code-block:: cmake
  571. install([[SCRIPT <file>] [CODE <code>]]
  572. [ALL_COMPONENTS | COMPONENT <component>]
  573. [EXCLUDE_FROM_ALL] [...])
  574. The ``SCRIPT`` form will invoke the given CMake script files during
  575. installation. If the script file name is a relative path it will be
  576. interpreted with respect to the current source directory. The ``CODE``
  577. form will invoke the given CMake code during installation. Code is
  578. specified as a single argument inside a double-quoted string. For
  579. example, the code
  580. .. code-block:: cmake
  581. install(CODE "MESSAGE(\"Sample install message.\")")
  582. will print a message during installation.
  583. .. versionadded:: 3.21
  584. When the ``ALL_COMPONENTS`` option is given, the custom installation
  585. script code will be executed for every component of a component-specific
  586. installation. This option is mutually exclusive with the ``COMPONENT``
  587. option.
  588. .. versionadded:: 3.14
  589. ``<file>`` or ``<code>`` may use "generator expressions" with the syntax
  590. ``$<...>`` (in the case of ``<file>``, this refers to their use in the file
  591. name, not the file's contents). See the
  592. :manual:`cmake-generator-expressions(7)` manual for available expressions.
  593. Installing Exports
  594. ^^^^^^^^^^^^^^^^^^
  595. .. _`install(EXPORT)`:
  596. .. _EXPORT:
  597. .. code-block:: cmake
  598. install(EXPORT <export-name> DESTINATION <dir>
  599. [NAMESPACE <namespace>] [[FILE <name>.cmake]|
  600. [PERMISSIONS permissions...]
  601. [CONFIGURATIONS [Debug|Release|...]]
  602. [EXPORT_LINK_INTERFACE_LIBRARIES]
  603. [COMPONENT <component>]
  604. [EXCLUDE_FROM_ALL])
  605. install(EXPORT_ANDROID_MK <export-name> DESTINATION <dir> [...])
  606. The ``EXPORT`` form generates and installs a CMake file containing code to
  607. import targets from the installation tree into another project.
  608. Target installations are associated with the export ``<export-name>``
  609. using the ``EXPORT`` option of the `install(TARGETS)`_ signature
  610. documented above. The ``NAMESPACE`` option will prepend ``<namespace>`` to
  611. the target names as they are written to the import file. By default
  612. the generated file will be called ``<export-name>.cmake`` but the ``FILE``
  613. option may be used to specify a different name. The value given to
  614. the ``FILE`` option must be a file name with the ``.cmake`` extension.
  615. If a ``CONFIGURATIONS`` option is given then the file will only be installed
  616. when one of the named configurations is installed. Additionally, the
  617. generated import file will reference only the matching target
  618. configurations. The ``EXPORT_LINK_INTERFACE_LIBRARIES`` keyword, if
  619. present, causes the contents of the properties matching
  620. ``(IMPORTED_)?LINK_INTERFACE_LIBRARIES(_<CONFIG>)?`` to be exported, when
  621. policy :policy:`CMP0022` is ``NEW``.
  622. .. note::
  623. The installed ``<export-name>.cmake`` file may come with additional
  624. per-configuration ``<export-name>-*.cmake`` files to be loaded by
  625. globbing. Do not use an export name that is the same as the package
  626. name in combination with installing a ``<package-name>-config.cmake``
  627. file or the latter may be incorrectly matched by the glob and loaded.
  628. When a ``COMPONENT`` option is given, the listed ``<component>`` implicitly
  629. depends on all components mentioned in the export set. The exported
  630. ``<name>.cmake`` file will require each of the exported components to be
  631. present in order for dependent projects to build properly. For example, a
  632. project may define components ``Runtime`` and ``Development``, with shared
  633. libraries going into the ``Runtime`` component and static libraries and
  634. headers going into the ``Development`` component. The export set would also
  635. typically be part of the ``Development`` component, but it would export
  636. targets from both the ``Runtime`` and ``Development`` components. Therefore,
  637. the ``Runtime`` component would need to be installed if the ``Development``
  638. component was installed, but not vice versa. If the ``Development`` component
  639. was installed without the ``Runtime`` component, dependent projects that try
  640. to link against it would have build errors. Package managers, such as APT and
  641. RPM, typically handle this by listing the ``Runtime`` component as a dependency
  642. of the ``Development`` component in the package metadata, ensuring that the
  643. library is always installed if the headers and CMake export file are present.
  644. .. versionadded:: 3.7
  645. In addition to cmake language files, the ``EXPORT_ANDROID_MK`` mode maybe
  646. used to specify an export to the android ndk build system. This mode
  647. accepts the same options as the normal export mode. The Android
  648. NDK supports the use of prebuilt libraries, both static and shared. This
  649. allows cmake to build the libraries of a project and make them available
  650. to an ndk build system complete with transitive dependencies, include flags
  651. and defines required to use the libraries.
  652. The ``EXPORT`` form is useful to help outside projects use targets built
  653. and installed by the current project. For example, the code
  654. .. code-block:: cmake
  655. install(TARGETS myexe EXPORT myproj DESTINATION bin)
  656. install(EXPORT myproj NAMESPACE mp_ DESTINATION lib/myproj)
  657. install(EXPORT_ANDROID_MK myproj DESTINATION share/ndk-modules)
  658. will install the executable ``myexe`` to ``<prefix>/bin`` and code to import
  659. it in the file ``<prefix>/lib/myproj/myproj.cmake`` and
  660. ``<prefix>/share/ndk-modules/Android.mk``. An outside project
  661. may load this file with the include command and reference the ``myexe``
  662. executable from the installation tree using the imported target name
  663. ``mp_myexe`` as if the target were built in its own tree.
  664. .. note::
  665. This command supersedes the :command:`install_targets` command and
  666. the :prop_tgt:`PRE_INSTALL_SCRIPT` and :prop_tgt:`POST_INSTALL_SCRIPT`
  667. target properties. It also replaces the ``FILES`` forms of the
  668. :command:`install_files` and :command:`install_programs` commands.
  669. The processing order of these install rules relative to
  670. those generated by :command:`install_targets`,
  671. :command:`install_files`, and :command:`install_programs` commands
  672. is not defined.
  673. Installing Runtime Dependencies
  674. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  675. .. _`install(RUNTIME_DEPENDENCY_SET)`:
  676. .. _RUNTIME_DEPENDENCY_SET:
  677. .. versionadded:: 3.21
  678. .. code-block:: cmake
  679. install(RUNTIME_DEPENDENCY_SET <set-name>
  680. [[LIBRARY|RUNTIME|FRAMEWORK]
  681. [DESTINATION <dir>]
  682. [PERMISSIONS permissions...]
  683. [CONFIGURATIONS [Debug|Release|...]]
  684. [COMPONENT <component>]
  685. [NAMELINK_COMPONENT <component>]
  686. [OPTIONAL] [EXCLUDE_FROM_ALL]
  687. ] [...]
  688. [PRE_INCLUDE_REGEXES regexes...]
  689. [PRE_EXCLUDE_REGEXES regexes...]
  690. [POST_INCLUDE_REGEXES regexes...]
  691. [POST_EXCLUDE_REGEXES regexes...]
  692. [POST_INCLUDE_FILES files...]
  693. [POST_EXCLUDE_FILES files...]
  694. [DIRECTORIES directories...]
  695. )
  696. Installs a runtime dependency set previously created by one or more
  697. `install(TARGETS)`_ or `install(IMPORTED_RUNTIME_ARTIFACTS)`_ commands. The
  698. dependencies of targets belonging to a runtime dependency set are installed in
  699. the ``RUNTIME`` destination and component on DLL platforms, and in the
  700. ``LIBRARY`` destination and component on non-DLL platforms. macOS frameworks
  701. are installed in the ``FRAMEWORK`` destination and component.
  702. Targets built within the build tree will never be installed as runtime
  703. dependencies, nor will their own dependencies, unless the targets themselves
  704. are installed with `install(TARGETS)`_.
  705. The generated install script calls :command:`file(GET_RUNTIME_DEPENDENCIES)`
  706. on the build-tree files to calculate the runtime dependencies. The build-tree
  707. executable files are passed as the ``EXECUTABLES`` argument, the build-tree
  708. shared libraries as the ``LIBRARIES`` argument, and the build-tree modules as
  709. the ``MODULES`` argument. On macOS, if one of the executables is a
  710. :prop_tgt:`MACOSX_BUNDLE`, that executable is passed as the
  711. ``BUNDLE_EXECUTABLE`` argument. At most one such bundle executable may be in
  712. the runtime dependency set on macOS. The :prop_tgt:`MACOSX_BUNDLE` property
  713. has no effect on other platforms. Note that
  714. :command:`file(GET_RUNTIME_DEPENDENCIES)` only supports collecting the runtime
  715. dependencies for Windows, Linux and macOS platforms, so
  716. ``install(RUNTIME_DEPENDENCY_SET)`` has the same limitation.
  717. The following sub-arguments are forwarded through as the corresponding
  718. arguments to :command:`file(GET_RUNTIME_DEPENDENCIES)` (for those that provide
  719. a non-empty list of directories, regular expressions or files). They all
  720. support :manual:`generator expressions <cmake-generator-expressions(7)>`.
  721. * ``DIRECTORIES <directories>``
  722. * ``PRE_INCLUDE_REGEXES <regexes>``
  723. * ``PRE_EXCLUDE_REGEXES <regexes>``
  724. * ``POST_INCLUDE_REGEXES <regexes>``
  725. * ``POST_EXCLUDE_REGEXES <regexes>``
  726. * ``POST_INCLUDE_FILES <files>``
  727. * ``POST_EXCLUDE_FILES <files>``
  728. Generated Installation Script
  729. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  730. .. note::
  731. Use of this feature is not recommended. Please consider using the
  732. ``--install`` argument of :manual:`cmake(1)` instead.
  733. The ``install()`` command generates a file, ``cmake_install.cmake``, inside
  734. the build directory, which is used internally by the generated install target
  735. and by CPack. You can also invoke this script manually with ``cmake -P``. This
  736. script accepts several variables:
  737. ``COMPONENT``
  738. Set this variable to install only a single CPack component as opposed to all
  739. of them. For example, if you only want to install the ``Development``
  740. component, run ``cmake -DCOMPONENT=Development -P cmake_install.cmake``.
  741. ``BUILD_TYPE``
  742. Set this variable to change the build type if you are using a multi-config
  743. generator. For example, to install with the ``Debug`` configuration, run
  744. ``cmake -DBUILD_TYPE=Debug -P cmake_install.cmake``.
  745. ``DESTDIR``
  746. This is an environment variable rather than a CMake variable. It allows you
  747. to change the installation prefix on UNIX systems. See :envvar:`DESTDIR` for
  748. details.