install.rst 42 KB

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