file.rst 38 KB

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