file.rst 49 KB

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