install.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. install
  2. -------
  3. .. only:: html
  4. .. contents::
  5. Specify rules to run at install time.
  6. Introduction
  7. ^^^^^^^^^^^^
  8. This command generates installation rules for a project. Rules
  9. specified by calls to this command within a source directory are
  10. executed in order during installation. The order across directories
  11. is not defined.
  12. There are multiple signatures for this command. Some of them define
  13. installation options for files and targets. Options common to
  14. multiple signatures are covered here but they are valid only for
  15. signatures that specify them. The common options are:
  16. ``DESTINATION``
  17. Specify the directory on disk to which a file will be installed.
  18. If a full path (with a leading slash or drive letter) is given
  19. it is used directly. If a relative path is given it is interpreted
  20. relative to the value of the :variable:`CMAKE_INSTALL_PREFIX` variable.
  21. The prefix can be relocated at install time using the ``DESTDIR``
  22. mechanism explained in the :variable:`CMAKE_INSTALL_PREFIX` variable
  23. documentation.
  24. ``PERMISSIONS``
  25. Specify permissions for installed files. Valid permissions are
  26. ``OWNER_READ``, ``OWNER_WRITE``, ``OWNER_EXECUTE``, ``GROUP_READ``,
  27. ``GROUP_WRITE``, ``GROUP_EXECUTE``, ``WORLD_READ``, ``WORLD_WRITE``,
  28. ``WORLD_EXECUTE``, ``SETUID``, and ``SETGID``. Permissions that do
  29. not make sense on certain platforms are ignored on those platforms.
  30. ``CONFIGURATIONS``
  31. Specify a list of build configurations for which the install rule
  32. applies (Debug, Release, etc.). Note that the values specified for
  33. this option only apply to options listed AFTER the ``CONFIGURATIONS``
  34. option. For example, to set separate install paths for the Debug and
  35. Release configurations, do the following:
  36. .. code-block:: cmake
  37. install(TARGETS target
  38. CONFIGURATIONS Debug
  39. RUNTIME DESTINATION Debug/bin)
  40. install(TARGETS target
  41. CONFIGURATIONS Release
  42. RUNTIME DESTINATION Release/bin)
  43. Note that ``CONFIGURATIONS`` appears BEFORE ``RUNTIME DESTINATION``.
  44. ``COMPONENT``
  45. Specify an installation component name with which the install rule
  46. is associated, such as "runtime" or "development". During
  47. component-specific installation only install rules associated with
  48. the given component name will be executed. During a full installation
  49. all components are installed unless marked with ``EXCLUDE_FROM_ALL``.
  50. If ``COMPONENT`` is not provided a default component "Unspecified" is
  51. created. The default component name may be controlled with the
  52. :variable:`CMAKE_INSTALL_DEFAULT_COMPONENT_NAME` variable.
  53. ``EXCLUDE_FROM_ALL``
  54. Specify that the file is excluded from a full installation and only
  55. installed as part of a component-specific installation
  56. ``RENAME``
  57. Specify a name for an installed file that may be different from the
  58. original file. Renaming is allowed only when a single file is
  59. installed by the command.
  60. ``OPTIONAL``
  61. Specify that it is not an error if the file to be installed does
  62. not exist.
  63. Command signatures that install files may print messages during
  64. installation. Use the :variable:`CMAKE_INSTALL_MESSAGE` variable
  65. to control which messages are printed.
  66. Installing Targets
  67. ^^^^^^^^^^^^^^^^^^
  68. ::
  69. install(TARGETS targets... [EXPORT <export-name>]
  70. [[ARCHIVE|LIBRARY|RUNTIME|OBJECTS|FRAMEWORK|BUNDLE|
  71. PRIVATE_HEADER|PUBLIC_HEADER|RESOURCE]
  72. [DESTINATION <dir>]
  73. [PERMISSIONS permissions...]
  74. [CONFIGURATIONS [Debug|Release|...]]
  75. [COMPONENT <component>]
  76. [OPTIONAL] [EXCLUDE_FROM_ALL]
  77. [NAMELINK_ONLY|NAMELINK_SKIP]
  78. ] [...]
  79. [INCLUDES DESTINATION [<dir> ...]]
  80. )
  81. The ``TARGETS`` form specifies rules for installing targets from a
  82. project. There are six kinds of target files that may be installed:
  83. ``ARCHIVE``, ``LIBRARY``, ``RUNTIME``, ``OBJECTS``, ``FRAMEWORK``, and
  84. ``BUNDLE``. Executables are treated as ``RUNTIME`` targets, except that
  85. those marked with the ``MACOSX_BUNDLE`` property are treated as ``BUNDLE``
  86. targets on OS X. Static libraries are treated as ``ARCHIVE`` targets,
  87. except that those marked with the ``FRAMEWORK`` property are treated
  88. as ``FRAMEWORK`` targets on OS X.
  89. Module libraries are always treated as ``LIBRARY`` targets.
  90. For non-DLL platforms shared libraries are treated as ``LIBRARY``
  91. targets, except that those marked with the ``FRAMEWORK`` property are
  92. treated as ``FRAMEWORK`` targets on OS X. For DLL platforms the DLL
  93. part of a shared library is treated as a ``RUNTIME`` target and the
  94. corresponding import library is treated as an ``ARCHIVE`` target.
  95. All Windows-based systems including Cygwin are DLL platforms. Object
  96. libraries are always treated as ``OBJECTS`` targets.
  97. The ``ARCHIVE``, ``LIBRARY``, ``RUNTIME``, ``OBJECTS``, and ``FRAMEWORK``
  98. arguments change the type of target to which the subsequent properties
  99. apply. If none is given the installation properties apply to all target
  100. types. If only one is given then only targets of that type will be
  101. installed (which can be used to install just a DLL or just an import
  102. library).
  103. The ``PRIVATE_HEADER``, ``PUBLIC_HEADER``, and ``RESOURCE`` arguments
  104. cause subsequent properties to be applied to installing a ``FRAMEWORK``
  105. shared library target's associated files on non-Apple platforms. Rules
  106. defined by these arguments are ignored on Apple platforms because the
  107. associated files are installed into the appropriate locations inside
  108. the framework folder. See documentation of the
  109. :prop_tgt:`PRIVATE_HEADER`, :prop_tgt:`PUBLIC_HEADER`, and
  110. :prop_tgt:`RESOURCE` target properties for details.
  111. Either ``NAMELINK_ONLY`` or ``NAMELINK_SKIP`` may be specified as a
  112. ``LIBRARY`` option. On some platforms a versioned shared library
  113. has a symbolic link such as::
  114. lib<name>.so -> lib<name>.so.1
  115. where ``lib<name>.so.1`` is the soname of the library and ``lib<name>.so``
  116. is a "namelink" allowing linkers to find the library when given
  117. ``-l<name>``. The ``NAMELINK_ONLY`` option causes installation of only the
  118. namelink when a library target is installed. The ``NAMELINK_SKIP`` option
  119. causes installation of library files other than the namelink when a
  120. library target is installed. When neither option is given both
  121. portions are installed. On platforms where versioned shared libraries
  122. do not have namelinks or when a library is not versioned the
  123. ``NAMELINK_SKIP`` option installs the library and the ``NAMELINK_ONLY``
  124. option installs nothing. See the :prop_tgt:`VERSION` and
  125. :prop_tgt:`SOVERSION` target properties for details on creating versioned
  126. shared libraries.
  127. The ``INCLUDES DESTINATION`` specifies a list of directories
  128. which will be added to the :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`
  129. target property of the ``<targets>`` when exported by the
  130. :command:`install(EXPORT)` command. If a relative path is
  131. specified, it is treated as relative to the ``$<INSTALL_PREFIX>``.
  132. This is independent of the rest of the argument groups and does
  133. not actually install anything.
  134. One or more groups of properties may be specified in a single call to
  135. the ``TARGETS`` form of this command. A target may be installed more than
  136. once to different locations. Consider hypothetical targets ``myExe``,
  137. ``mySharedLib``, and ``myStaticLib``. The code:
  138. .. code-block:: cmake
  139. install(TARGETS myExe mySharedLib myStaticLib
  140. RUNTIME DESTINATION bin
  141. LIBRARY DESTINATION lib
  142. ARCHIVE DESTINATION lib/static)
  143. install(TARGETS mySharedLib DESTINATION /some/full/path)
  144. will install ``myExe`` to ``<prefix>/bin`` and ``myStaticLib`` to
  145. ``<prefix>/lib/static``. On non-DLL platforms ``mySharedLib`` will be
  146. installed to ``<prefix>/lib`` and ``/some/full/path``. On DLL platforms
  147. the ``mySharedLib`` DLL will be installed to ``<prefix>/bin`` and
  148. ``/some/full/path`` and its import library will be installed to
  149. ``<prefix>/lib/static`` and ``/some/full/path``.
  150. The ``EXPORT`` option associates the installed target files with an
  151. export called ``<export-name>``. It must appear before any ``RUNTIME``,
  152. ``LIBRARY``, ``ARCHIVE``, or ``OBJECTS`` options. To actually install the
  153. export file itself, call ``install(EXPORT)``, documented below.
  154. Installing a target with the :prop_tgt:`EXCLUDE_FROM_ALL` target property
  155. set to ``TRUE`` has undefined behavior.
  156. The install destination given to the target install ``DESTINATION`` may
  157. use "generator expressions" with the syntax ``$<...>``. See the
  158. :manual:`cmake-generator-expressions(7)` manual for available expressions.
  159. Installing Files
  160. ^^^^^^^^^^^^^^^^
  161. ::
  162. install(<FILES|PROGRAMS> files... DESTINATION <dir>
  163. [PERMISSIONS permissions...]
  164. [CONFIGURATIONS [Debug|Release|...]]
  165. [COMPONENT <component>]
  166. [RENAME <name>] [OPTIONAL] [EXCLUDE_FROM_ALL])
  167. The ``FILES`` form specifies rules for installing files for a project.
  168. File names given as relative paths are interpreted with respect to the
  169. current source directory. Files installed by this form are by default
  170. given permissions ``OWNER_WRITE``, ``OWNER_READ``, ``GROUP_READ``, and
  171. ``WORLD_READ`` if no ``PERMISSIONS`` argument is given.
  172. The ``PROGRAMS`` form is identical to the ``FILES`` form except that the
  173. default permissions for the installed file also include ``OWNER_EXECUTE``,
  174. ``GROUP_EXECUTE``, and ``WORLD_EXECUTE``. This form is intended to install
  175. programs that are not targets, such as shell scripts. Use the ``TARGETS``
  176. form to install targets built within the project.
  177. The list of ``files...`` given to ``FILES`` or ``PROGRAMS`` may use
  178. "generator expressions" with the syntax ``$<...>``. See the
  179. :manual:`cmake-generator-expressions(7)` manual for available expressions.
  180. However, if any item begins in a generator expression it must evaluate
  181. to a full path.
  182. The install destination given to the files install ``DESTINATION`` may
  183. use "generator expressions" with the syntax ``$<...>``. See the
  184. :manual:`cmake-generator-expressions(7)` manual for available expressions.
  185. Installing Directories
  186. ^^^^^^^^^^^^^^^^^^^^^^
  187. ::
  188. install(DIRECTORY dirs... DESTINATION <dir>
  189. [FILE_PERMISSIONS permissions...]
  190. [DIRECTORY_PERMISSIONS permissions...]
  191. [USE_SOURCE_PERMISSIONS] [OPTIONAL] [MESSAGE_NEVER]
  192. [CONFIGURATIONS [Debug|Release|...]]
  193. [COMPONENT <component>] [EXCLUDE_FROM_ALL]
  194. [FILES_MATCHING]
  195. [[PATTERN <pattern> | REGEX <regex>]
  196. [EXCLUDE] [PERMISSIONS permissions...]] [...])
  197. The ``DIRECTORY`` form installs contents of one or more directories to a
  198. given destination. The directory structure is copied verbatim to the
  199. destination. The last component of each directory name is appended to
  200. the destination directory but a trailing slash may be used to avoid
  201. this because it leaves the last component empty. Directory names
  202. given as relative paths are interpreted with respect to the current
  203. source directory. If no input directory names are given the
  204. destination directory will be created but nothing will be installed
  205. into it. The ``FILE_PERMISSIONS`` and ``DIRECTORY_PERMISSIONS`` options
  206. specify permissions given to files and directories in the destination.
  207. If ``USE_SOURCE_PERMISSIONS`` is specified and ``FILE_PERMISSIONS`` is not,
  208. file permissions will be copied from the source directory structure.
  209. If no permissions are specified files will be given the default
  210. permissions specified in the ``FILES`` form of the command, and the
  211. directories will be given the default permissions specified in the
  212. ``PROGRAMS`` form of the command.
  213. The ``MESSAGE_NEVER`` option disables file installation status output.
  214. Installation of directories may be controlled with fine granularity
  215. using the ``PATTERN`` or ``REGEX`` options. These "match" options specify a
  216. globbing pattern or regular expression to match directories or files
  217. encountered within input directories. They may be used to apply
  218. certain options (see below) to a subset of the files and directories
  219. encountered. The full path to each input file or directory (with
  220. forward slashes) is matched against the expression. A ``PATTERN`` will
  221. match only complete file names: the portion of the full path matching
  222. the pattern must occur at the end of the file name and be preceded by
  223. a slash. A ``REGEX`` will match any portion of the full path but it may
  224. use ``/`` and ``$`` to simulate the ``PATTERN`` behavior. By default all
  225. files and directories are installed whether or not they are matched.
  226. The ``FILES_MATCHING`` option may be given before the first match option
  227. to disable installation of files (but not directories) not matched by
  228. any expression. For example, the code
  229. .. code-block:: cmake
  230. install(DIRECTORY src/ DESTINATION include/myproj
  231. FILES_MATCHING PATTERN "*.h")
  232. will extract and install header files from a source tree.
  233. Some options may follow a ``PATTERN`` or ``REGEX`` expression and are applied
  234. only to files or directories matching them. The ``EXCLUDE`` option will
  235. skip the matched file or directory. The ``PERMISSIONS`` option overrides
  236. the permissions setting for the matched file or directory. For
  237. example the code
  238. .. code-block:: cmake
  239. install(DIRECTORY icons scripts/ DESTINATION share/myproj
  240. PATTERN "CVS" EXCLUDE
  241. PATTERN "scripts/*"
  242. PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ
  243. GROUP_EXECUTE GROUP_READ)
  244. will install the ``icons`` directory to ``share/myproj/icons`` and the
  245. ``scripts`` directory to ``share/myproj``. The icons will get default
  246. file permissions, the scripts will be given specific permissions, and any
  247. ``CVS`` directories will be excluded.
  248. The list of ``dirs...`` given to ``DIRECTORY`` and the install destination
  249. given to the directory install ``DESTINATION`` may use "generator expressions"
  250. with the syntax ``$<...>``. See the :manual:`cmake-generator-expressions(7)`
  251. manual for available expressions.
  252. Custom Installation Logic
  253. ^^^^^^^^^^^^^^^^^^^^^^^^^
  254. ::
  255. install([[SCRIPT <file>] [CODE <code>]]
  256. [COMPONENT <component>] [EXCLUDE_FROM_ALL] [...])
  257. The ``SCRIPT`` form will invoke the given CMake script files during
  258. installation. If the script file name is a relative path it will be
  259. interpreted with respect to the current source directory. The ``CODE``
  260. form will invoke the given CMake code during installation. Code is
  261. specified as a single argument inside a double-quoted string. For
  262. example, the code
  263. .. code-block:: cmake
  264. install(CODE "MESSAGE(\"Sample install message.\")")
  265. will print a message during installation.
  266. Installing Exports
  267. ^^^^^^^^^^^^^^^^^^
  268. ::
  269. install(EXPORT <export-name> DESTINATION <dir>
  270. [NAMESPACE <namespace>] [[FILE <name>.cmake]|
  271. [EXPORT_ANDROID_MK <name>.mk]]
  272. [PERMISSIONS permissions...]
  273. [CONFIGURATIONS [Debug|Release|...]]
  274. [EXPORT_LINK_INTERFACE_LIBRARIES]
  275. [COMPONENT <component>]
  276. [EXCLUDE_FROM_ALL])
  277. The ``EXPORT`` form generates and installs a CMake file containing code to
  278. import targets from the installation tree into another project.
  279. Target installations are associated with the export ``<export-name>``
  280. using the ``EXPORT`` option of the ``install(TARGETS)`` signature
  281. documented above. The ``NAMESPACE`` option will prepend ``<namespace>`` to
  282. the target names as they are written to the import file. By default
  283. the generated file will be called ``<export-name>.cmake`` but the ``FILE``
  284. option may be used to specify a different name. The value given to
  285. the ``FILE`` option must be a file name with the ``.cmake`` extension.
  286. If a ``CONFIGURATIONS`` option is given then the file will only be installed
  287. when one of the named configurations is installed. Additionally, the
  288. generated import file will reference only the matching target
  289. configurations. The ``EXPORT_LINK_INTERFACE_LIBRARIES`` keyword, if
  290. present, causes the contents of the properties matching
  291. ``(IMPORTED_)?LINK_INTERFACE_LIBRARIES(_<CONFIG>)?`` to be exported, when
  292. policy :policy:`CMP0022` is ``NEW``. If a ``COMPONENT`` option is
  293. specified that does not match that given to the targets associated with
  294. ``<export-name>`` the behavior is undefined. If a library target is
  295. included in the export but a target to which it links is not included
  296. the behavior is unspecified.
  297. In addition to cmake language files, the ``EXPORT_ANDROID_MK`` option maybe
  298. used to specify an export to the android ndk build system. The Android
  299. NDK supports the use of prebuilt libraries, both static and shared. This
  300. allows cmake to build the libraries of a project and make them available
  301. to an ndk build system complete with transitive dependencies, include flags
  302. and defines required to use the libraries.
  303. The ``EXPORT`` form is useful to help outside projects use targets built
  304. and installed by the current project. For example, the code
  305. .. code-block:: cmake
  306. install(TARGETS myexe EXPORT myproj DESTINATION bin)
  307. install(EXPORT myproj NAMESPACE mp_ DESTINATION lib/myproj)
  308. install(EXPORT_ANDROID_MK myexp DESTINATION share/ndk-modules)
  309. will install the executable myexe to ``<prefix>/bin`` and code to import
  310. it in the file ``<prefix>/lib/myproj/myproj.cmake`` and
  311. ``<prefix>/lib/share/ndk-modules/Android.mk``. An outside project
  312. may load this file with the include command and reference the ``myexe``
  313. executable from the installation tree using the imported target name
  314. ``mp_myexe`` as if the target were built in its own tree.
  315. .. note::
  316. This command supercedes the :command:`install_targets` command and
  317. the :prop_tgt:`PRE_INSTALL_SCRIPT` and :prop_tgt:`POST_INSTALL_SCRIPT`
  318. target properties. It also replaces the ``FILES`` forms of the
  319. :command:`install_files` and :command:`install_programs` commands.
  320. The processing order of these install rules relative to
  321. those generated by :command:`install_targets`,
  322. :command:`install_files`, and :command:`install_programs` commands
  323. is not defined.