install.rst 41 KB

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