file.rst 39 KB

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