install.rst 32 KB

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