file.rst 44 KB

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