file.rst 44 KB

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