file.rst 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. file
  2. ----
  3. File manipulation command.
  4. Synopsis
  5. ^^^^^^^^
  6. .. parsed-literal::
  7. `Reading`_
  8. file(`READ`_ <filename> <out-var> [...])
  9. file(`STRINGS`_ <filename> <out-var> [...])
  10. file(`\<HASH\> <HASH_>`_ <filename> <out-var>)
  11. file(`TIMESTAMP`_ <filename> <out-var> [...])
  12. file(`GET_RUNTIME_DEPENDENCIES`_ [...])
  13. `Writing`_
  14. file({`WRITE`_ | `APPEND`_} <filename> <content>...)
  15. file({`TOUCH`_ | `TOUCH_NOCREATE`_} [<file>...])
  16. file(`GENERATE`_ OUTPUT <output-file> [...])
  17. file(`CONFIGURE`_ OUTPUT <output-file> CONTENT <content> [...])
  18. `Filesystem`_
  19. file({`GLOB`_ | `GLOB_RECURSE`_} <out-var> [...] [<globbing-expr>...])
  20. file(`RENAME`_ <oldname> <newname>)
  21. file({`REMOVE`_ | `REMOVE_RECURSE`_ } [<files>...])
  22. file(`MAKE_DIRECTORY`_ [<dir>...])
  23. file({`COPY`_ | `INSTALL`_} <file>... DESTINATION <dir> [...])
  24. file(`SIZE`_ <filename> <out-var>)
  25. file(`READ_SYMLINK`_ <linkname> <out-var>)
  26. file(`CREATE_LINK`_ <original> <linkname> [...])
  27. file(`CHMOD`_ <files>... <directories>... PERMISSIONS <permissions>... [...])
  28. file(`CHMOD_RECURSE`_ <files>... <directories>... PERMISSIONS <permissions>... [...])
  29. `Path Conversion`_
  30. file(`REAL_PATH`_ <path> <out-var> [BASE_DIRECTORY <dir>])
  31. file(`RELATIVE_PATH`_ <out-var> <directory> <file>)
  32. file({`TO_CMAKE_PATH`_ | `TO_NATIVE_PATH`_} <path> <out-var>)
  33. `Transfer`_
  34. file(`DOWNLOAD`_ <url> [<file>] [...])
  35. file(`UPLOAD`_ <file> <url> [...])
  36. `Locking`_
  37. file(`LOCK`_ <path> [...])
  38. `Archiving`_
  39. file(`ARCHIVE_CREATE`_ OUTPUT <archive> PATHS <paths>... [...])
  40. file(`ARCHIVE_EXTRACT`_ INPUT <archive> [...])
  41. Reading
  42. ^^^^^^^
  43. .. _READ:
  44. .. code-block:: cmake
  45. file(READ <filename> <variable>
  46. [OFFSET <offset>] [LIMIT <max-in>] [HEX])
  47. Read content from a file called ``<filename>`` and store it in a
  48. ``<variable>``. Optionally start from the given ``<offset>`` and
  49. read at most ``<max-in>`` bytes. The ``HEX`` option causes data to
  50. be converted to a hexadecimal representation (useful for binary data). If the
  51. ``HEX`` option is specified, letters in the output (``a`` through ``f``) are in
  52. lowercase.
  53. .. _STRINGS:
  54. .. code-block:: cmake
  55. file(STRINGS <filename> <variable> [<options>...])
  56. Parse a list of ASCII strings from ``<filename>`` and store it in
  57. ``<variable>``. Binary data in the file are ignored. Carriage return
  58. (``\r``, CR) characters are ignored. The options are:
  59. ``LENGTH_MAXIMUM <max-len>``
  60. Consider only strings of at most a given length.
  61. ``LENGTH_MINIMUM <min-len>``
  62. Consider only strings of at least a given length.
  63. ``LIMIT_COUNT <max-num>``
  64. Limit the number of distinct strings to be extracted.
  65. ``LIMIT_INPUT <max-in>``
  66. Limit the number of input bytes to read from the file.
  67. ``LIMIT_OUTPUT <max-out>``
  68. Limit the number of total bytes to store in the ``<variable>``.
  69. ``NEWLINE_CONSUME``
  70. Treat newline characters (``\n``, LF) as part of string content
  71. instead of terminating at them.
  72. ``NO_HEX_CONVERSION``
  73. Intel Hex and Motorola S-record files are automatically converted to
  74. binary while reading unless this option is given.
  75. ``REGEX <regex>``
  76. Consider only strings that match the given regular expression.
  77. ``ENCODING <encoding-type>``
  78. Consider strings of a given encoding. Currently supported encodings are:
  79. UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. If the ENCODING option
  80. is not provided and the file has a Byte Order Mark, the ENCODING option
  81. will be defaulted to respect the Byte Order Mark.
  82. For example, the code
  83. .. code-block:: cmake
  84. file(STRINGS myfile.txt myfile)
  85. stores a list in the variable ``myfile`` in which each item is a line
  86. from the input file.
  87. .. _HASH:
  88. .. code-block:: cmake
  89. file(<HASH> <filename> <variable>)
  90. Compute a cryptographic hash of the content of ``<filename>`` and
  91. store it in a ``<variable>``. The supported ``<HASH>`` algorithm names
  92. are those listed by the :ref:`string(\<HASH\>) <Supported Hash Algorithms>`
  93. command.
  94. .. _TIMESTAMP:
  95. .. code-block:: cmake
  96. file(TIMESTAMP <filename> <variable> [<format>] [UTC])
  97. Compute a string representation of the modification time of ``<filename>``
  98. and store it in ``<variable>``. Should the command be unable to obtain a
  99. timestamp variable will be set to the empty string ("").
  100. See the :command:`string(TIMESTAMP)` command for documentation of
  101. the ``<format>`` and ``UTC`` options.
  102. .. _GET_RUNTIME_DEPENDENCIES:
  103. .. code-block:: cmake
  104. file(GET_RUNTIME_DEPENDENCIES
  105. [RESOLVED_DEPENDENCIES_VAR <deps_var>]
  106. [UNRESOLVED_DEPENDENCIES_VAR <unresolved_deps_var>]
  107. [CONFLICTING_DEPENDENCIES_PREFIX <conflicting_deps_prefix>]
  108. [EXECUTABLES [<executable_files>...]]
  109. [LIBRARIES [<library_files>...]]
  110. [MODULES [<module_files>...]]
  111. [DIRECTORIES [<directories>...]]
  112. [BUNDLE_EXECUTABLE <bundle_executable_file>]
  113. [PRE_INCLUDE_REGEXES [<regexes>...]]
  114. [PRE_EXCLUDE_REGEXES [<regexes>...]]
  115. [POST_INCLUDE_REGEXES [<regexes>...]]
  116. [POST_EXCLUDE_REGEXES [<regexes>...]]
  117. )
  118. Recursively get the list of libraries depended on by the given files.
  119. Please note that this sub-command is not intended to be used in project mode.
  120. Instead, use it in an :command:`install(CODE)` or :command:`install(SCRIPT)`
  121. block. For example:
  122. .. code-block:: cmake
  123. install(CODE [[
  124. file(GET_RUNTIME_DEPENDENCIES
  125. # ...
  126. )
  127. ]])
  128. The arguments are as follows:
  129. ``RESOLVED_DEPENDENCIES_VAR <deps_var>``
  130. Name of the variable in which to store the list of resolved dependencies.
  131. ``UNRESOLVED_DEPENDENCIES_VAR <unresolved_deps_var>``
  132. Name of the variable in which to store the list of unresolved dependencies.
  133. If this variable is not specified, and there are any unresolved dependencies,
  134. an error is issued.
  135. ``CONFLICTING_DEPENDENCIES_PREFIX <conflicting_deps_prefix>``
  136. Variable prefix in which to store conflicting dependency information.
  137. Dependencies are conflicting if two files with the same name are found in
  138. two different directories. The list of filenames that conflict are stored in
  139. ``<conflicting_deps_prefix>_FILENAMES``. For each filename, the list of paths
  140. that were found for that filename are stored in
  141. ``<conflicting_deps_prefix>_<filename>``.
  142. ``EXECUTABLES <executable_files>``
  143. List of executable files to read for dependencies. These are executables that
  144. are typically created with :command:`add_executable`, but they do not have to
  145. be created by CMake. On Apple platforms, the paths to these files determine
  146. the value of ``@executable_path`` when recursively resolving the libraries.
  147. Specifying any kind of library (``STATIC``, ``MODULE``, or ``SHARED``) here
  148. will result in undefined behavior.
  149. ``LIBRARIES <library_files>``
  150. List of library files to read for dependencies. These are libraries that are
  151. typically created with :command:`add_library(SHARED)`, but they do not have
  152. to be created by CMake. Specifying ``STATIC`` libraries, ``MODULE``
  153. libraries, or executables here will result in undefined behavior.
  154. ``MODULES <module_files>``
  155. List of loadable module files to read for dependencies. These are modules
  156. that are typically created with :command:`add_library(MODULE)`, but they do
  157. not have to be created by CMake. They are typically used by calling
  158. ``dlopen()`` at runtime rather than linked at link time with ``ld -l``.
  159. Specifying ``STATIC`` libraries, ``SHARED`` libraries, or executables here
  160. will result in undefined behavior.
  161. ``DIRECTORIES <directories>``
  162. List of additional directories to search for dependencies. On Linux
  163. platforms, these directories are searched if the dependency is not found in
  164. any of the other usual paths. If it is found in such a directory, a warning
  165. is issued, because it means that the file is incomplete (it does not list all
  166. of the directories that contain its dependencies). On Windows platforms,
  167. these directories are searched if the dependency is not found in any of the
  168. other search paths, but no warning is issued, because searching other paths
  169. is a normal part of Windows dependency resolution. On Apple platforms, this
  170. argument has no effect.
  171. ``BUNDLE_EXECUTABLE <bundle_executable_file>``
  172. Executable to treat as the "bundle executable" when resolving libraries. On
  173. Apple platforms, this argument determines the value of ``@executable_path``
  174. when recursively resolving libraries for ``LIBRARIES`` and ``MODULES`` files.
  175. It has no effect on ``EXECUTABLES`` files. On other platforms, it has no
  176. effect. This is typically (but not always) one of the executables in the
  177. ``EXECUTABLES`` argument which designates the "main" executable of the
  178. package.
  179. The following arguments specify filters for including or excluding libraries to
  180. be resolved. See below for a full description of how they work.
  181. ``PRE_INCLUDE_REGEXES <regexes>``
  182. List of pre-include regexes through which to filter the names of
  183. not-yet-resolved dependencies.
  184. ``PRE_EXCLUDE_REGEXES <regexes>``
  185. List of pre-exclude regexes through which to filter the names of
  186. not-yet-resolved dependencies.
  187. ``POST_INCLUDE_REGEXES <regexes>``
  188. List of post-include regexes through which to filter the names of resolved
  189. dependencies.
  190. ``POST_EXCLUDE_REGEXES <regexes>``
  191. List of post-exclude regexes through which to filter the names of resolved
  192. dependencies.
  193. These arguments can be used to exclude unwanted system libraries when
  194. resolving the dependencies, or to include libraries from a specific
  195. directory. The filtering works as follows:
  196. 1. If the not-yet-resolved dependency matches any of the
  197. ``PRE_INCLUDE_REGEXES``, steps 2 and 3 are skipped, and the dependency
  198. resolution proceeds to step 4.
  199. 2. If the not-yet-resolved dependency matches any of the
  200. ``PRE_EXCLUDE_REGEXES``, dependency resolution stops for that dependency.
  201. 3. Otherwise, dependency resolution proceeds.
  202. 4. ``file(GET_RUNTIME_DEPENDENCIES)`` searches for the dependency according to
  203. the linking rules of the platform (see below).
  204. 5. If the dependency is found, and its full path matches one of the
  205. ``POST_INCLUDE_REGEXES``, the full path is added to the resolved
  206. dependencies, and ``file(GET_RUNTIME_DEPENDENCIES)`` recursively resolves
  207. that library's own dependencies. Otherwise, resolution proceeds to step 6.
  208. 6. If the dependency is found, but its full path matches one of the
  209. ``POST_EXCLUDE_REGEXES``, it is not added to the resolved dependencies, and
  210. dependency resolution stops for that dependency.
  211. 7. If the dependency is found, and its full path does not match either
  212. ``POST_INCLUDE_REGEXES`` or ``POST_EXCLUDE_REGEXES``, the full path is added
  213. to the resolved dependencies, and ``file(GET_RUNTIME_DEPENDENCIES)``
  214. recursively resolves that library's own dependencies.
  215. Different platforms have different rules for how dependencies are resolved.
  216. These specifics are described here.
  217. On Linux platforms, library resolution works as follows:
  218. 1. If the depending file does not have any ``RUNPATH`` entries, and the library
  219. exists in one of the depending file's ``RPATH`` entries, or its parents', in
  220. that order, the dependency is resolved to that file.
  221. 2. Otherwise, if the depending file has any ``RUNPATH`` entries, and the
  222. library exists in one of those entries, the dependency is resolved to that
  223. file.
  224. 3. Otherwise, if the library exists in one of the directories listed by
  225. ``ldconfig``, the dependency is resolved to that file.
  226. 4. Otherwise, if the library exists in one of the ``DIRECTORIES`` entries, the
  227. dependency is resolved to that file. In this case, a warning is issued,
  228. because finding a file in one of the ``DIRECTORIES`` means that the
  229. depending file is not complete (it does not list all the directories from
  230. which it pulls dependencies).
  231. 5. Otherwise, the dependency is unresolved.
  232. On Windows platforms, library resolution works as follows:
  233. 1. The dependent DLL name is converted to lowercase. Windows DLL names are
  234. case-insensitive, and some linkers mangle the case of the DLL dependency
  235. names. However, this makes it more difficult for ``PRE_INCLUDE_REGEXES``,
  236. ``PRE_EXCLUDE_REGEXES``, ``POST_INCLUDE_REGEXES``, and
  237. ``POST_EXCLUDE_REGEXES`` to properly filter DLL names - every regex would
  238. have to check for both uppercase and lowercase letters. For example:
  239. .. code-block:: cmake
  240. file(GET_RUNTIME_DEPENDENCIES
  241. # ...
  242. PRE_INCLUDE_REGEXES "^[Mm][Yy][Ll][Ii][Bb][Rr][Aa][Rr][Yy]\\.[Dd][Ll][Ll]$"
  243. )
  244. Converting the DLL name to lowercase allows the regexes to only match
  245. lowercase names, thus simplifying the regex. For example:
  246. .. code-block:: cmake
  247. file(GET_RUNTIME_DEPENDENCIES
  248. # ...
  249. PRE_INCLUDE_REGEXES "^mylibrary\\.dll$"
  250. )
  251. This regex will match ``mylibrary.dll`` regardless of how it is cased,
  252. either on disk or in the depending file. (For example, it will match
  253. ``mylibrary.dll``, ``MyLibrary.dll``, and ``MYLIBRARY.DLL``.)
  254. Please note that the directory portion of any resolved DLLs retains its
  255. casing and is not converted to lowercase. Only the filename portion is
  256. converted.
  257. 2. (**Not yet implemented**) If the depending file is a Windows Store app, and
  258. the dependency is listed as a dependency in the application's package
  259. manifest, the dependency is resolved to that file.
  260. 3. Otherwise, if the library exists in the same directory as the depending
  261. file, the dependency is resolved to that file.
  262. 4. Otherwise, if the library exists in either the operating system's
  263. ``system32`` directory or the ``Windows`` directory, in that order, the
  264. dependency is resolved to that file.
  265. 5. Otherwise, if the library exists in one of the directories specified by
  266. ``DIRECTORIES``, in the order they are listed, the dependency is resolved to
  267. that file. In this case, a warning is not issued, because searching other
  268. directories is a normal part of Windows library resolution.
  269. 6. Otherwise, the dependency is unresolved.
  270. On Apple platforms, library resolution works as follows:
  271. 1. If the dependency starts with ``@executable_path/``, and an ``EXECUTABLES``
  272. argument is in the process of being resolved, and replacing
  273. ``@executable_path/`` with the directory of the executable yields an
  274. existing file, the dependency is resolved to that file.
  275. 2. Otherwise, if the dependency starts with ``@executable_path/``, and there is
  276. a ``BUNDLE_EXECUTABLE`` argument, and replacing ``@executable_path/`` with
  277. the directory of the bundle executable yields an existing file, the
  278. dependency is resolved to that file.
  279. 3. Otherwise, if the dependency starts with ``@loader_path/``, and replacing
  280. ``@loader_path/`` with the directory of the depending file yields an
  281. existing file, the dependency is resolved to that file.
  282. 4. Otherwise, if the dependency starts with ``@rpath/``, and replacing
  283. ``@rpath/`` with one of the ``RPATH`` entries of the depending file yields
  284. an existing file, the dependency is resolved to that file. Note that
  285. ``RPATH`` entries that start with ``@executable_path/`` or ``@loader_path/``
  286. also have these items replaced with the appropriate path.
  287. 5. Otherwise, if the dependency is an absolute file that exists, the dependency
  288. is resolved to that file.
  289. 6. Otherwise, the dependency is unresolved.
  290. This function accepts several variables that determine which tool is used for
  291. dependency resolution:
  292. .. variable:: CMAKE_GET_RUNTIME_DEPENDENCIES_PLATFORM
  293. Determines which operating system and executable format the files are built
  294. for. This could be one of several values:
  295. * ``linux+elf``
  296. * ``windows+pe``
  297. * ``macos+macho``
  298. If this variable is not specified, it is determined automatically by system
  299. introspection.
  300. .. variable:: CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL
  301. Determines the tool to use for dependency resolution. It could be one of
  302. several values, depending on the value of
  303. :variable:`CMAKE_GET_RUNTIME_DEPENDENCIES_PLATFORM`:
  304. ================================================= =============================================
  305. ``CMAKE_GET_RUNTIME_DEPENDENCIES_PLATFORM`` ``CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL``
  306. ================================================= =============================================
  307. ``linux+elf`` ``objdump``
  308. ``windows+pe`` ``dumpbin``
  309. ``windows+pe`` ``objdump``
  310. ``macos+macho`` ``otool``
  311. ================================================= =============================================
  312. If this variable is not specified, it is determined automatically by system
  313. introspection.
  314. .. variable:: CMAKE_GET_RUNTIME_DEPENDENCIES_COMMAND
  315. Determines the path to the tool to use for dependency resolution. This is the
  316. actual path to ``objdump``, ``dumpbin``, or ``otool``.
  317. If this variable is not specified, it is determined by the value of
  318. ``CMAKE_OBJDUMP`` if set, else by system introspection.
  319. Writing
  320. ^^^^^^^
  321. .. _WRITE:
  322. .. _APPEND:
  323. .. code-block:: cmake
  324. file(WRITE <filename> <content>...)
  325. file(APPEND <filename> <content>...)
  326. Write ``<content>`` into a file called ``<filename>``. If the file does
  327. not exist, it will be created. If the file already exists, ``WRITE``
  328. mode will overwrite it and ``APPEND`` mode will append to the end.
  329. Any directories in the path specified by ``<filename>`` that do not
  330. exist will be created.
  331. If the file is a build input, use the :command:`configure_file` command
  332. to update the file only when its content changes.
  333. .. _TOUCH:
  334. .. _TOUCH_NOCREATE:
  335. .. code-block:: cmake
  336. file(TOUCH [<files>...])
  337. file(TOUCH_NOCREATE [<files>...])
  338. Create a file with no content if it does not yet exist. If the file already
  339. exists, its access and/or modification will be updated to the time when the
  340. function call is executed.
  341. Use TOUCH_NOCREATE to touch a file if it exists but not create it. If a file
  342. does not exist it will be silently ignored.
  343. With TOUCH and TOUCH_NOCREATE the contents of an existing file will not be
  344. modified.
  345. .. _GENERATE:
  346. .. code-block:: cmake
  347. file(GENERATE OUTPUT output-file
  348. <INPUT input-file|CONTENT content>
  349. [CONDITION expression] [TARGET target])
  350. Generate an output file for each build configuration supported by the current
  351. :manual:`CMake Generator <cmake-generators(7)>`. Evaluate
  352. :manual:`generator expressions <cmake-generator-expressions(7)>`
  353. from the input content to produce the output content. The options are:
  354. ``CONDITION <condition>``
  355. Generate the output file for a particular configuration only if
  356. the condition is true. The condition must be either ``0`` or ``1``
  357. after evaluating generator expressions.
  358. ``CONTENT <content>``
  359. Use the content given explicitly as input.
  360. ``INPUT <input-file>``
  361. Use the content from a given file as input.
  362. A relative path is treated with respect to the value of
  363. :variable:`CMAKE_CURRENT_SOURCE_DIR`. See policy :policy:`CMP0070`.
  364. ``OUTPUT <output-file>``
  365. Specify the output file name to generate. Use generator expressions
  366. such as ``$<CONFIG>`` to specify a configuration-specific output file
  367. name. Multiple configurations may generate the same output file only
  368. if the generated content is identical. Otherwise, the ``<output-file>``
  369. must evaluate to an unique name for each configuration.
  370. A relative path (after evaluating generator expressions) is treated
  371. with respect to the value of :variable:`CMAKE_CURRENT_BINARY_DIR`.
  372. See policy :policy:`CMP0070`.
  373. ``TARGET <target>``
  374. Specify which target to use when evaluating generator expressions that
  375. require a target for evaluation (e.g. ``$<COMPILE_FEATURES:...>``,
  376. ``$<TARGET_PROPERTY:prop>``).
  377. Exactly one ``CONTENT`` or ``INPUT`` option must be given. A specific
  378. ``OUTPUT`` file may be named by at most one invocation of ``file(GENERATE)``.
  379. Generated files are modified and their timestamp updated on subsequent cmake
  380. runs only if their content is changed.
  381. Note also that ``file(GENERATE)`` does not create the output file until the
  382. generation phase. The output file will not yet have been written when the
  383. ``file(GENERATE)`` command returns, it is written only after processing all
  384. of a project's ``CMakeLists.txt`` files.
  385. .. _CONFIGURE:
  386. .. code-block:: cmake
  387. file(CONFIGURE OUTPUT output-file
  388. CONTENT content
  389. [ESCAPE_QUOTES] [@ONLY]
  390. [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ])
  391. Generate an output file using the input given by ``CONTENT`` and substitute
  392. variable values referenced as ``@VAR@`` or ``${VAR}`` contained therein. The
  393. substitution rules behave the same as the :command:`configure_file` command.
  394. In order to match :command:`configure_file`'s behavior, generator expressions
  395. are not supported for both ``OUTPUT`` and ``CONTENT``.
  396. The arguments are:
  397. ``OUTPUT <output-file>``
  398. Specify the output file name to generate. A relative path is treated with
  399. respect to the value of :variable:`CMAKE_CURRENT_BINARY_DIR`.
  400. ``<output-file>`` does not support generator expressions.
  401. ``CONTENT <content>``
  402. Use the content given explicitly as input.
  403. ``<content>`` does not support generator expressions.
  404. ``ESCAPE_QUOTES``
  405. Escape any substituted quotes with backslashes (C-style).
  406. ``@ONLY``
  407. Restrict variable replacement to references of the form ``@VAR@``.
  408. This is useful for configuring scripts that use ``${VAR}`` syntax.
  409. ``NEWLINE_STYLE <style>``
  410. Specify the newline style for the output file. Specify
  411. ``UNIX`` or ``LF`` for ``\n`` newlines, or specify
  412. ``DOS``, ``WIN32``, or ``CRLF`` for ``\r\n`` newlines.
  413. Filesystem
  414. ^^^^^^^^^^
  415. .. _GLOB:
  416. .. _GLOB_RECURSE:
  417. .. code-block:: cmake
  418. file(GLOB <variable>
  419. [LIST_DIRECTORIES true|false] [RELATIVE <path>] [CONFIGURE_DEPENDS]
  420. [<globbing-expressions>...])
  421. file(GLOB_RECURSE <variable> [FOLLOW_SYMLINKS]
  422. [LIST_DIRECTORIES true|false] [RELATIVE <path>] [CONFIGURE_DEPENDS]
  423. [<globbing-expressions>...])
  424. Generate a list of files that match the ``<globbing-expressions>`` and
  425. store it into the ``<variable>``. Globbing expressions are similar to
  426. regular expressions, but much simpler. If ``RELATIVE`` flag is
  427. specified, the results will be returned as relative paths to the given
  428. path. The results will be ordered lexicographically.
  429. On Windows and macOS, globbing is case-insensitive even if the underlying
  430. filesystem is case-sensitive (both filenames and globbing expressions are
  431. converted to lowercase before matching). On other platforms, globbing is
  432. case-sensitive.
  433. If the ``CONFIGURE_DEPENDS`` flag is specified, CMake will add logic
  434. to the main build system check target to rerun the flagged ``GLOB`` commands
  435. at build time. If any of the outputs change, CMake will regenerate the build
  436. system.
  437. By default ``GLOB`` lists directories - directories are omitted in result if
  438. ``LIST_DIRECTORIES`` is set to false.
  439. .. note::
  440. We do not recommend using GLOB to collect a list of source files from
  441. your source tree. If no CMakeLists.txt file changes when a source is
  442. added or removed then the generated build system cannot know when to
  443. ask CMake to regenerate.
  444. The ``CONFIGURE_DEPENDS`` flag may not work reliably on all generators, or if
  445. a new generator is added in the future that cannot support it, projects using
  446. it will be stuck. Even if ``CONFIGURE_DEPENDS`` works reliably, there is
  447. still a cost to perform the check on every rebuild.
  448. Examples of globbing expressions include::
  449. *.cxx - match all files with extension cxx
  450. *.vt? - match all files with extension vta,...,vtz
  451. f[3-5].txt - match files f3.txt, f4.txt, f5.txt
  452. The ``GLOB_RECURSE`` mode will traverse all the subdirectories of the
  453. matched directory and match the files. Subdirectories that are symlinks
  454. are only traversed if ``FOLLOW_SYMLINKS`` is given or policy
  455. :policy:`CMP0009` is not set to ``NEW``.
  456. By default ``GLOB_RECURSE`` omits directories from result list - setting
  457. ``LIST_DIRECTORIES`` to true adds directories to result list.
  458. If ``FOLLOW_SYMLINKS`` is given or policy :policy:`CMP0009` is not set to
  459. ``NEW`` then ``LIST_DIRECTORIES`` treats symlinks as directories.
  460. Examples of recursive globbing include::
  461. /dir/*.py - match all python files in /dir and subdirectories
  462. .. _RENAME:
  463. .. code-block:: cmake
  464. file(RENAME <oldname> <newname>)
  465. Move a file or directory within a filesystem from ``<oldname>`` to
  466. ``<newname>``, replacing the destination atomically.
  467. .. _REMOVE:
  468. .. _REMOVE_RECURSE:
  469. .. code-block:: cmake
  470. file(REMOVE [<files>...])
  471. file(REMOVE_RECURSE [<files>...])
  472. Remove the given files. The ``REMOVE_RECURSE`` mode will remove the given
  473. files and directories, also non-empty directories. No error is emitted if a
  474. given file does not exist. Relative input paths are evaluated with respect
  475. to the current source directory. Empty input paths are ignored with a warning.
  476. .. _MAKE_DIRECTORY:
  477. .. code-block:: cmake
  478. file(MAKE_DIRECTORY [<directories>...])
  479. Create the given directories and their parents as needed.
  480. .. _COPY:
  481. .. _INSTALL:
  482. .. code-block:: cmake
  483. file(<COPY|INSTALL> <files>... DESTINATION <dir>
  484. [FILE_PERMISSIONS <permissions>...]
  485. [DIRECTORY_PERMISSIONS <permissions>...]
  486. [NO_SOURCE_PERMISSIONS] [USE_SOURCE_PERMISSIONS]
  487. [FOLLOW_SYMLINK_CHAIN]
  488. [FILES_MATCHING]
  489. [[PATTERN <pattern> | REGEX <regex>]
  490. [EXCLUDE] [PERMISSIONS <permissions>...]] [...])
  491. The ``COPY`` signature copies files, directories, and symlinks to a
  492. destination folder. Relative input paths are evaluated with respect
  493. to the current source directory, and a relative destination is
  494. evaluated with respect to the current build directory. Copying
  495. preserves input file timestamps, and optimizes out a file if it exists
  496. at the destination with the same timestamp. Copying preserves input
  497. permissions unless explicit permissions or ``NO_SOURCE_PERMISSIONS``
  498. are given (default is ``USE_SOURCE_PERMISSIONS``).
  499. If ``FOLLOW_SYMLINK_CHAIN`` is specified, ``COPY`` will recursively resolve
  500. the symlinks at the paths given until a real file is found, and install
  501. a corresponding symlink in the destination for each symlink encountered. For
  502. each symlink that is installed, the resolution is stripped of the directory,
  503. leaving only the filename, meaning that the new symlink points to a file in
  504. the same directory as the symlink. This feature is useful on some Unix systems,
  505. where libraries are installed as a chain of symlinks with version numbers, with
  506. less specific versions pointing to more specific versions.
  507. ``FOLLOW_SYMLINK_CHAIN`` will install all of these symlinks and the library
  508. itself into the destination directory. For example, if you have the following
  509. directory structure:
  510. * ``/opt/foo/lib/libfoo.so.1.2.3``
  511. * ``/opt/foo/lib/libfoo.so.1.2 -> libfoo.so.1.2.3``
  512. * ``/opt/foo/lib/libfoo.so.1 -> libfoo.so.1.2``
  513. * ``/opt/foo/lib/libfoo.so -> libfoo.so.1``
  514. and you do:
  515. .. code-block:: cmake
  516. file(COPY /opt/foo/lib/libfoo.so DESTINATION lib FOLLOW_SYMLINK_CHAIN)
  517. This will install all of the symlinks and ``libfoo.so.1.2.3`` itself into
  518. ``lib``.
  519. See the :command:`install(DIRECTORY)` command for documentation of
  520. permissions, ``FILES_MATCHING``, ``PATTERN``, ``REGEX``, and
  521. ``EXCLUDE`` options. Copying directories preserves the structure
  522. of their content even if options are used to select a subset of
  523. files.
  524. The ``INSTALL`` signature differs slightly from ``COPY``: it prints
  525. status messages (subject to the :variable:`CMAKE_INSTALL_MESSAGE` variable),
  526. and ``NO_SOURCE_PERMISSIONS`` is default.
  527. Installation scripts generated by the :command:`install` command
  528. use this signature (with some undocumented options for internal use).
  529. .. _SIZE:
  530. .. code-block:: cmake
  531. file(SIZE <filename> <variable>)
  532. Determine the file size of the ``<filename>`` and put the result in
  533. ``<variable>`` variable. Requires that ``<filename>`` is a valid path
  534. pointing to a file and is readable.
  535. .. _READ_SYMLINK:
  536. .. code-block:: cmake
  537. file(READ_SYMLINK <linkname> <variable>)
  538. This subcommand queries the symlink ``<linkname>`` and stores the path it
  539. points to in the result ``<variable>``. If ``<linkname>`` does not exist or
  540. is not a symlink, CMake issues a fatal error.
  541. Note that this command returns the raw symlink path and does not resolve
  542. a relative path. The following is an example of how to ensure that an
  543. absolute path is obtained:
  544. .. code-block:: cmake
  545. set(linkname "/path/to/foo.sym")
  546. file(READ_SYMLINK "${linkname}" result)
  547. if(NOT IS_ABSOLUTE "${result}")
  548. get_filename_component(dir "${linkname}" DIRECTORY)
  549. set(result "${dir}/${result}")
  550. endif()
  551. .. _CREATE_LINK:
  552. .. code-block:: cmake
  553. file(CREATE_LINK <original> <linkname>
  554. [RESULT <result>] [COPY_ON_ERROR] [SYMBOLIC])
  555. Create a link ``<linkname>`` that points to ``<original>``.
  556. It will be a hard link by default, but providing the ``SYMBOLIC`` option
  557. results in a symbolic link instead. Hard links require that ``original``
  558. exists and is a file, not a directory. If ``<linkname>`` already exists,
  559. it will be overwritten.
  560. The ``<result>`` variable, if specified, receives the status of the operation.
  561. It is set to ``0`` upon success or an error message otherwise. If ``RESULT``
  562. is not specified and the operation fails, a fatal error is emitted.
  563. Specifying ``COPY_ON_ERROR`` enables copying the file as a fallback if
  564. creating the link fails. It can be useful for handling situations such as
  565. ``<original>`` and ``<linkname>`` being on different drives or mount points,
  566. which would make them unable to support a hard link.
  567. .. _CHMOD:
  568. .. code-block:: cmake
  569. file(CHMOD <files>... <directories>...
  570. [PERMISSIONS <permissions>...]
  571. [FILE_PERMISSIONS <permissions>...]
  572. [DIRECTORY_PERMISSIONS <permissions>...])
  573. Set the permissions for the ``<files>...`` and ``<directories>...`` specified.
  574. Valid permissions are ``OWNER_READ``, ``OWNER_WRITE``, ``OWNER_EXECUTE``,
  575. ``GROUP_READ``, ``GROUP_WRITE``, ``GROUP_EXECUTE``, ``WORLD_READ``,
  576. ``WORLD_WRITE``, ``WORLD_EXECUTE``.
  577. Valid combination of keywords are:
  578. ``PERMISSIONS``
  579. All items are changed.
  580. ``FILE_PERMISSIONS``
  581. Only files are changed.
  582. ``DIRECTORY_PERMISSIONS``
  583. Only directories are changed.
  584. ``PERMISSIONS`` and ``FILE_PERMISSIONS``
  585. ``FILE_PERMISSIONS`` overrides ``PERMISSIONS`` for files.
  586. ``PERMISSIONS`` and ``DIRECTORY_PERMISSIONS``
  587. ``DIRECTORY_PERMISSIONS`` overrides ``PERMISSIONS`` for directories.
  588. ``FILE_PERMISSIONS`` and ``DIRECTORY_PERMISSIONS``
  589. Use ``FILE_PERMISSIONS`` for files and ``DIRECTORY_PERMISSIONS`` for
  590. directories.
  591. .. _CHMOD_RECURSE:
  592. .. code-block:: cmake
  593. file(CHMOD_RECURSE <files>... <directories>...
  594. [PERMISSIONS <permissions>...]
  595. [FILE_PERMISSIONS <permissions>...]
  596. [DIRECTORY_PERMISSIONS <permissions>...])
  597. Same as `CHMOD`_, but change the permissions of files and directories present in
  598. the ``<directories>...`` recursively.
  599. Path Conversion
  600. ^^^^^^^^^^^^^^^
  601. .. _REAL_PATH:
  602. .. code-block:: cmake
  603. file(REAL_PATH <path> <out-var> [BASE_DIRECTORY <dir>])
  604. Compute the absolute path to an existing file or directory with symlinks
  605. resolved.
  606. If the provided ``<path>`` is a relative path, it is evaluated relative to the
  607. given base directory ``<dir>``. If no base directory is provided, the default
  608. base directory will be :variable:`CMAKE_CURRENT_SOURCE_DIR`.
  609. .. _RELATIVE_PATH:
  610. .. code-block:: cmake
  611. file(RELATIVE_PATH <variable> <directory> <file>)
  612. Compute the relative path from a ``<directory>`` to a ``<file>`` and
  613. store it in the ``<variable>``.
  614. .. _TO_CMAKE_PATH:
  615. .. _TO_NATIVE_PATH:
  616. .. code-block:: cmake
  617. file(TO_CMAKE_PATH "<path>" <variable>)
  618. file(TO_NATIVE_PATH "<path>" <variable>)
  619. The ``TO_CMAKE_PATH`` mode converts a native ``<path>`` into a cmake-style
  620. path with forward-slashes (``/``). The input can be a single path or a
  621. system search path like ``$ENV{PATH}``. A search path will be converted
  622. to a cmake-style list separated by ``;`` characters.
  623. The ``TO_NATIVE_PATH`` mode converts a cmake-style ``<path>`` into a native
  624. path with platform-specific slashes (``\`` on Windows and ``/`` elsewhere).
  625. Always use double quotes around the ``<path>`` to be sure it is treated
  626. as a single argument to this command.
  627. Transfer
  628. ^^^^^^^^
  629. .. _DOWNLOAD:
  630. .. _UPLOAD:
  631. .. code-block:: cmake
  632. file(DOWNLOAD <url> [<file>] [<options>...])
  633. file(UPLOAD <file> <url> [<options>...])
  634. The ``DOWNLOAD`` subcommand downloads the given ``<url>`` to a local ``<file>``.
  635. If ``<file>`` is not specified for ``file(DOWNLOAD)``, the file is not saved.
  636. This can be useful if you want to know if a file can be downloaded (for example,
  637. to check that it exists) without actually saving it anywhere. The ``UPLOAD``
  638. mode uploads a local ``<file>`` to a given ``<url>``.
  639. Options to both ``DOWNLOAD`` and ``UPLOAD`` are:
  640. ``INACTIVITY_TIMEOUT <seconds>``
  641. Terminate the operation after a period of inactivity.
  642. ``LOG <variable>``
  643. Store a human-readable log of the operation in a variable.
  644. ``SHOW_PROGRESS``
  645. Print progress information as status messages until the operation is
  646. complete.
  647. ``STATUS <variable>``
  648. Store the resulting status of the operation in a variable.
  649. The status is a ``;`` separated list of length 2.
  650. The first element is the numeric return value for the operation,
  651. and the second element is a string value for the error.
  652. A ``0`` numeric error means no error in the operation.
  653. ``TIMEOUT <seconds>``
  654. Terminate the operation after a given total time has elapsed.
  655. ``USERPWD <username>:<password>``
  656. Set username and password for operation.
  657. ``HTTPHEADER <HTTP-header>``
  658. HTTP header for operation. Suboption can be repeated several times.
  659. ``NETRC <level>``
  660. Specify whether the .netrc file is to be used for operation. If this
  661. option is not specified, the value of the ``CMAKE_NETRC`` variable
  662. will be used instead.
  663. Valid levels are:
  664. ``IGNORED``
  665. The .netrc file is ignored.
  666. This is the default.
  667. ``OPTIONAL``
  668. The .netrc file is optional, and information in the URL is preferred.
  669. The file will be scanned to find which ever information is not specified
  670. in the URL.
  671. ``REQUIRED``
  672. The .netrc file is required, and information in the URL is ignored.
  673. ``NETRC_FILE <file>``
  674. Specify an alternative .netrc file to the one in your home directory,
  675. if the ``NETRC`` level is ``OPTIONAL`` or ``REQUIRED``. If this option
  676. is not specified, the value of the ``CMAKE_NETRC_FILE`` variable will
  677. be used instead.
  678. If neither ``NETRC`` option is given CMake will check variables
  679. ``CMAKE_NETRC`` and ``CMAKE_NETRC_FILE``, respectively.
  680. ``TLS_VERIFY <ON|OFF>``
  681. Specify whether to verify the server certificate for ``https://`` URLs.
  682. The default is to *not* verify.
  683. ``TLS_CAINFO <file>``
  684. Specify a custom Certificate Authority file for ``https://`` URLs.
  685. For ``https://`` URLs CMake must be built with OpenSSL support. ``TLS/SSL``
  686. certificates are not checked by default. Set ``TLS_VERIFY`` to ``ON`` to
  687. check certificates. If neither ``TLS`` option is given CMake will check
  688. variables ``CMAKE_TLS_VERIFY`` and ``CMAKE_TLS_CAINFO``, respectively.
  689. Additional options to ``DOWNLOAD`` are:
  690. ``EXPECTED_HASH ALGO=<value>``
  691. Verify that the downloaded content hash matches the expected value, where
  692. ``ALGO`` is one of the algorithms supported by ``file(<HASH>)``.
  693. If it does not match, the operation fails with an error. It is an error to
  694. specify this if ``DOWNLOAD`` is not given a ``<file>``.
  695. ``EXPECTED_MD5 <value>``
  696. Historical short-hand for ``EXPECTED_HASH MD5=<value>``. It is an error to
  697. specify this if ``DOWNLOAD`` is not given a ``<file>``.
  698. Locking
  699. ^^^^^^^
  700. .. _LOCK:
  701. .. code-block:: cmake
  702. file(LOCK <path> [DIRECTORY] [RELEASE]
  703. [GUARD <FUNCTION|FILE|PROCESS>]
  704. [RESULT_VARIABLE <variable>]
  705. [TIMEOUT <seconds>])
  706. Lock a file specified by ``<path>`` if no ``DIRECTORY`` option present and file
  707. ``<path>/cmake.lock`` otherwise. File will be locked for scope defined by
  708. ``GUARD`` option (default value is ``PROCESS``). ``RELEASE`` option can be used
  709. to unlock file explicitly. If option ``TIMEOUT`` is not specified CMake will
  710. wait until lock succeed or until fatal error occurs. If ``TIMEOUT`` is set to
  711. ``0`` lock will be tried once and result will be reported immediately. If
  712. ``TIMEOUT`` is not ``0`` CMake will try to lock file for the period specified
  713. by ``<seconds>`` value. Any errors will be interpreted as fatal if there is no
  714. ``RESULT_VARIABLE`` option. Otherwise result will be stored in ``<variable>``
  715. and will be ``0`` on success or error message on failure.
  716. Note that lock is advisory - there is no guarantee that other processes will
  717. respect this lock, i.e. lock synchronize two or more CMake instances sharing
  718. some modifiable resources. Similar logic applied to ``DIRECTORY`` option -
  719. locking parent directory doesn't prevent other ``LOCK`` commands to lock any
  720. child directory or file.
  721. Trying to lock file twice is not allowed. Any intermediate directories and
  722. file itself will be created if they not exist. ``GUARD`` and ``TIMEOUT``
  723. options ignored on ``RELEASE`` operation.
  724. Archiving
  725. ^^^^^^^^^
  726. .. _ARCHIVE_CREATE:
  727. .. code-block:: cmake
  728. file(ARCHIVE_CREATE OUTPUT <archive>
  729. PATHS <paths>...
  730. [FORMAT <format>]
  731. [COMPRESSION <compression> [COMPRESSION_LEVEL <compression-level>]]
  732. [MTIME <mtime>]
  733. [VERBOSE])
  734. Creates the specified ``<archive>`` file with the files and directories
  735. listed in ``<paths>``. Note that ``<paths>`` must list actual files or
  736. directories, wildcards are not supported.
  737. Use the ``FORMAT`` option to specify the archive format. Supported values
  738. for ``<format>`` are ``7zip``, ``gnutar``, ``pax``, ``paxr``, ``raw`` and
  739. ``zip``. If ``FORMAT`` is not given, the default format is ``paxr``.
  740. Some archive formats allow the type of compression to be specified.
  741. The ``7zip`` and ``zip`` archive formats already imply a specific type of
  742. compression. The other formats use no compression by default, but can be
  743. directed to do so with the ``COMPRESSION`` option. Valid values for
  744. ``<compression>`` are ``None``, ``BZip2``, ``GZip``, ``XZ``, and ``Zstd``.
  745. The compression level can be specified with the ``COMPRESSION_LEVEL`` option.
  746. The ``<compression-level>`` should be between 0-9, with the default being 0.
  747. The ``COMPRESSION`` option must be present when ``COMPRESSION_LEVEL`` is given.
  748. .. note::
  749. With ``FORMAT`` set to ``raw`` only one file will be compressed with the
  750. compression type specified by ``COMPRESSION``.
  751. The ``VERBOSE`` option enables verbose output for the archive operation.
  752. To specify the modification time recorded in tarball entries, use
  753. the ``MTIME`` option.
  754. .. _ARCHIVE_EXTRACT:
  755. .. code-block:: cmake
  756. file(ARCHIVE_EXTRACT INPUT <archive>
  757. [DESTINATION <dir>]
  758. [PATTERNS <patterns>...]
  759. [LIST_ONLY]
  760. [VERBOSE])
  761. Extracts or lists the content of the specified ``<archive>``.
  762. The directory where the content of the archive will be extracted to can
  763. be specified using the ``DESTINATION`` option. If the directory does not
  764. exist, it will be created. If ``DESTINATION`` is not given, the current
  765. binary directory will be used.
  766. If required, you may select which files and directories to list or extract
  767. from the archive using the specified ``<patterns>``. Wildcards are supported.
  768. If the ``PATTERNS`` option is not given, the entire archive will be listed or
  769. extracted.
  770. ``LIST_ONLY`` will list the files in the archive rather than extract them.
  771. With ``VERBOSE``, the command will produce verbose output.