file.rst 43 KB

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