file.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. `Writing`_
  13. file({`WRITE`_ | `APPEND`_} <filename> <content>...)
  14. file({`TOUCH`_ | `TOUCH_NOCREATE`_} [<file>...])
  15. file(`GENERATE`_ OUTPUT <output-file> [...])
  16. `Filesystem`_
  17. file({`GLOB`_ | `GLOB_RECURSE`_} <out-var> [...] [<globbing-expr>...])
  18. file(`RENAME`_ <oldname> <newname>)
  19. file({`REMOVE`_ | `REMOVE_RECURSE`_ } [<files>...])
  20. file(`MAKE_DIRECTORY`_ [<dir>...])
  21. file({`COPY`_ | `INSTALL`_} <file>... DESTINATION <dir> [...])
  22. `Path Conversion`_
  23. file(`RELATIVE_PATH`_ <out-var> <directory> <file>)
  24. file({`TO_CMAKE_PATH`_ | `TO_NATIVE_PATH`_} <path> <out-var>)
  25. `Transfer`_
  26. file(`DOWNLOAD`_ <url> <file> [...])
  27. file(`UPLOAD`_ <file> <url> [...])
  28. `Locking`_
  29. file(`LOCK`_ <path> [...])
  30. Reading
  31. ^^^^^^^
  32. .. _READ:
  33. .. code-block:: cmake
  34. file(READ <filename> <variable>
  35. [OFFSET <offset>] [LIMIT <max-in>] [HEX])
  36. Read content from a file called ``<filename>`` and store it in a
  37. ``<variable>``. Optionally start from the given ``<offset>`` and
  38. read at most ``<max-in>`` bytes. The ``HEX`` option causes data to
  39. be converted to a hexadecimal representation (useful for binary data).
  40. .. _STRINGS:
  41. .. code-block:: cmake
  42. file(STRINGS <filename> <variable> [<options>...])
  43. Parse a list of ASCII strings from ``<filename>`` and store it in
  44. ``<variable>``. Binary data in the file are ignored. Carriage return
  45. (``\r``, CR) characters are ignored. The options are:
  46. ``LENGTH_MAXIMUM <max-len>``
  47. Consider only strings of at most a given length.
  48. ``LENGTH_MINIMUM <min-len>``
  49. Consider only strings of at least a given length.
  50. ``LIMIT_COUNT <max-num>``
  51. Limit the number of distinct strings to be extracted.
  52. ``LIMIT_INPUT <max-in>``
  53. Limit the number of input bytes to read from the file.
  54. ``LIMIT_OUTPUT <max-out>``
  55. Limit the number of total bytes to store in the ``<variable>``.
  56. ``NEWLINE_CONSUME``
  57. Treat newline characters (``\n``, LF) as part of string content
  58. instead of terminating at them.
  59. ``NO_HEX_CONVERSION``
  60. Intel Hex and Motorola S-record files are automatically converted to
  61. binary while reading unless this option is given.
  62. ``REGEX <regex>``
  63. Consider only strings that match the given regular expression.
  64. ``ENCODING <encoding-type>``
  65. Consider strings of a given encoding. Currently supported encodings are:
  66. UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. If the ENCODING option
  67. is not provided and the file has a Byte Order Mark, the ENCODING option
  68. will be defaulted to respect the Byte Order Mark.
  69. For example, the code
  70. .. code-block:: cmake
  71. file(STRINGS myfile.txt myfile)
  72. stores a list in the variable ``myfile`` in which each item is a line
  73. from the input file.
  74. .. _HASH:
  75. .. code-block:: cmake
  76. file(<HASH> <filename> <variable>)
  77. Compute a cryptographic hash of the content of ``<filename>`` and
  78. store it in a ``<variable>``. The supported ``<HASH>`` algorithm names
  79. are those listed by the :ref:`string(\<HASH\>) <Supported Hash Algorithms>`
  80. command.
  81. .. _TIMESTAMP:
  82. .. code-block:: cmake
  83. file(TIMESTAMP <filename> <variable> [<format>] [UTC])
  84. Compute a string representation of the modification time of ``<filename>``
  85. and store it in ``<variable>``. Should the command be unable to obtain a
  86. timestamp variable will be set to the empty string ("").
  87. See the :command:`string(TIMESTAMP)` command for documentation of
  88. the ``<format>`` and ``UTC`` options.
  89. Writing
  90. ^^^^^^^
  91. .. _WRITE:
  92. .. _APPEND:
  93. .. code-block:: cmake
  94. file(WRITE <filename> <content>...)
  95. file(APPEND <filename> <content>...)
  96. Write ``<content>`` into a file called ``<filename>``. If the file does
  97. not exist, it will be created. If the file already exists, ``WRITE``
  98. mode will overwrite it and ``APPEND`` mode will append to the end.
  99. Any directories in the path specified by ``<filename>`` that do not
  100. exist will be created.
  101. If the file is a build input, use the :command:`configure_file` command
  102. to update the file only when its content changes.
  103. .. _TOUCH:
  104. .. _TOUCH_NOCREATE:
  105. .. code-block:: cmake
  106. file(TOUCH [<files>...])
  107. file(TOUCH_NOCREATE [<files>...])
  108. Create a file with no content if it does not yet exist. If the file already
  109. exists, its access and/or modification will be updated to the time when the
  110. function call is executed.
  111. Use TOUCH_NOCREATE to touch a file if it exists but not create it. If a file
  112. does not exist it will be silently ignored.
  113. With TOUCH and TOUCH_NOCREATE the contents of an existing file will not be
  114. modified.
  115. .. _GENERATE:
  116. .. code-block:: cmake
  117. file(GENERATE OUTPUT output-file
  118. <INPUT input-file|CONTENT content>
  119. [CONDITION expression])
  120. Generate an output file for each build configuration supported by the current
  121. :manual:`CMake Generator <cmake-generators(7)>`. Evaluate
  122. :manual:`generator expressions <cmake-generator-expressions(7)>`
  123. from the input content to produce the output content. The options are:
  124. ``CONDITION <condition>``
  125. Generate the output file for a particular configuration only if
  126. the condition is true. The condition must be either ``0`` or ``1``
  127. after evaluating generator expressions.
  128. ``CONTENT <content>``
  129. Use the content given explicitly as input.
  130. ``INPUT <input-file>``
  131. Use the content from a given file as input.
  132. A relative path is treated with respect to the value of
  133. :variable:`CMAKE_CURRENT_SOURCE_DIR`. See policy :policy:`CMP0070`.
  134. ``OUTPUT <output-file>``
  135. Specify the output file name to generate. Use generator expressions
  136. such as ``$<CONFIG>`` to specify a configuration-specific output file
  137. name. Multiple configurations may generate the same output file only
  138. if the generated content is identical. Otherwise, the ``<output-file>``
  139. must evaluate to an unique name for each configuration.
  140. A relative path (after evaluating generator expressions) is treated
  141. with respect to the value of :variable:`CMAKE_CURRENT_BINARY_DIR`.
  142. See policy :policy:`CMP0070`.
  143. Exactly one ``CONTENT`` or ``INPUT`` option must be given. A specific
  144. ``OUTPUT`` file may be named by at most one invocation of ``file(GENERATE)``.
  145. Generated files are modified and their timestamp updated on subsequent cmake
  146. runs only if their content is changed.
  147. Note also that ``file(GENERATE)`` does not create the output file until the
  148. generation phase. The output file will not yet have been written when the
  149. ``file(GENERATE)`` command returns, it is written only after processing all
  150. of a project's ``CMakeLists.txt`` files.
  151. Filesystem
  152. ^^^^^^^^^^
  153. .. _GLOB:
  154. .. _GLOB_RECURSE:
  155. .. code-block:: cmake
  156. file(GLOB <variable>
  157. [LIST_DIRECTORIES true|false] [RELATIVE <path>] [CONFIGURE_DEPENDS]
  158. [<globbing-expressions>...])
  159. file(GLOB_RECURSE <variable> [FOLLOW_SYMLINKS]
  160. [LIST_DIRECTORIES true|false] [RELATIVE <path>] [CONFIGURE_DEPENDS]
  161. [<globbing-expressions>...])
  162. Generate a list of files that match the ``<globbing-expressions>`` and
  163. store it into the ``<variable>``. Globbing expressions are similar to
  164. regular expressions, but much simpler. If ``RELATIVE`` flag is
  165. specified, the results will be returned as relative paths to the given
  166. path. The results will be ordered lexicographically.
  167. If the ``CONFIGURE_DEPENDS`` flag is specified, CMake will add logic
  168. to the main build system check target to rerun the flagged ``GLOB`` commands
  169. at build time. If any of the outputs change, CMake will regenerate the build
  170. system.
  171. By default ``GLOB`` lists directories - directories are omitted in result if
  172. ``LIST_DIRECTORIES`` is set to false.
  173. .. note::
  174. We do not recommend using GLOB to collect a list of source files from
  175. your source tree. If no CMakeLists.txt file changes when a source is
  176. added or removed then the generated build system cannot know when to
  177. ask CMake to regenerate.
  178. The ``CONFIGURE_DEPENDS`` flag may not work reliably on all generators, or if
  179. a new generator is added in the future that cannot support it, projects using
  180. it will be stuck. Even if ``CONFIGURE_DEPENDS`` works reliably, there is
  181. still a cost to perform the check on every rebuild.
  182. Examples of globbing expressions include::
  183. *.cxx - match all files with extension cxx
  184. *.vt? - match all files with extension vta,...,vtz
  185. f[3-5].txt - match files f3.txt, f4.txt, f5.txt
  186. The ``GLOB_RECURSE`` mode will traverse all the subdirectories of the
  187. matched directory and match the files. Subdirectories that are symlinks
  188. are only traversed if ``FOLLOW_SYMLINKS`` is given or policy
  189. :policy:`CMP0009` is not set to ``NEW``.
  190. By default ``GLOB_RECURSE`` omits directories from result list - setting
  191. ``LIST_DIRECTORIES`` to true adds directories to result list.
  192. If ``FOLLOW_SYMLINKS`` is given or policy :policy:`CMP0009` is not set to
  193. ``OLD`` then ``LIST_DIRECTORIES`` treats symlinks as directories.
  194. Examples of recursive globbing include::
  195. /dir/*.py - match all python files in /dir and subdirectories
  196. .. _RENAME:
  197. .. code-block:: cmake
  198. file(RENAME <oldname> <newname>)
  199. Move a file or directory within a filesystem from ``<oldname>`` to
  200. ``<newname>``, replacing the destination atomically.
  201. .. _REMOVE:
  202. .. _REMOVE_RECURSE:
  203. .. code-block:: cmake
  204. file(REMOVE [<files>...])
  205. file(REMOVE_RECURSE [<files>...])
  206. Remove the given files. The ``REMOVE_RECURSE`` mode will remove the given
  207. files and directories, also non-empty directories. No error is emitted if a
  208. given file does not exist.
  209. .. _MAKE_DIRECTORY:
  210. .. code-block:: cmake
  211. file(MAKE_DIRECTORY [<directories>...])
  212. Create the given directories and their parents as needed.
  213. .. _COPY:
  214. .. _INSTALL:
  215. .. code-block:: cmake
  216. file(<COPY|INSTALL> <files>... DESTINATION <dir>
  217. [FILE_PERMISSIONS <permissions>...]
  218. [DIRECTORY_PERMISSIONS <permissions>...]
  219. [NO_SOURCE_PERMISSIONS] [USE_SOURCE_PERMISSIONS]
  220. [FILES_MATCHING]
  221. [[PATTERN <pattern> | REGEX <regex>]
  222. [EXCLUDE] [PERMISSIONS <permissions>...]] [...])
  223. The ``COPY`` signature copies files, directories, and symlinks to a
  224. destination folder. Relative input paths are evaluated with respect
  225. to the current source directory, and a relative destination is
  226. evaluated with respect to the current build directory. Copying
  227. preserves input file timestamps, and optimizes out a file if it exists
  228. at the destination with the same timestamp. Copying preserves input
  229. permissions unless explicit permissions or ``NO_SOURCE_PERMISSIONS``
  230. are given (default is ``USE_SOURCE_PERMISSIONS``).
  231. See the :command:`install(DIRECTORY)` command for documentation of
  232. permissions, ``FILES_MATCHING``, ``PATTERN``, ``REGEX``, and
  233. ``EXCLUDE`` options. Copying directories preserves the structure
  234. of their content even if options are used to select a subset of
  235. files.
  236. The ``INSTALL`` signature differs slightly from ``COPY``: it prints
  237. status messages (subject to the :variable:`CMAKE_INSTALL_MESSAGE` variable),
  238. and ``NO_SOURCE_PERMISSIONS`` is default.
  239. Installation scripts generated by the :command:`install` command
  240. use this signature (with some undocumented options for internal use).
  241. Path Conversion
  242. ^^^^^^^^^^^^^^^
  243. .. _RELATIVE_PATH:
  244. .. code-block:: cmake
  245. file(RELATIVE_PATH <variable> <directory> <file>)
  246. Compute the relative path from a ``<directory>`` to a ``<file>`` and
  247. store it in the ``<variable>``.
  248. .. _TO_CMAKE_PATH:
  249. .. _TO_NATIVE_PATH:
  250. .. code-block:: cmake
  251. file(TO_CMAKE_PATH "<path>" <variable>)
  252. file(TO_NATIVE_PATH "<path>" <variable>)
  253. The ``TO_CMAKE_PATH`` mode converts a native ``<path>`` into a cmake-style
  254. path with forward-slashes (``/``). The input can be a single path or a
  255. system search path like ``$ENV{PATH}``. A search path will be converted
  256. to a cmake-style list separated by ``;`` characters.
  257. The ``TO_NATIVE_PATH`` mode converts a cmake-style ``<path>`` into a native
  258. path with platform-specific slashes (``\`` on Windows and ``/`` elsewhere).
  259. Always use double quotes around the ``<path>`` to be sure it is treated
  260. as a single argument to this command.
  261. Transfer
  262. ^^^^^^^^
  263. .. _DOWNLOAD:
  264. .. _UPLOAD:
  265. .. code-block:: cmake
  266. file(DOWNLOAD <url> <file> [<options>...])
  267. file(UPLOAD <file> <url> [<options>...])
  268. The ``DOWNLOAD`` mode downloads the given ``<url>`` to a local ``<file>``.
  269. The ``UPLOAD`` mode uploads a local ``<file>`` to a given ``<url>``.
  270. Options to both ``DOWNLOAD`` and ``UPLOAD`` are:
  271. ``INACTIVITY_TIMEOUT <seconds>``
  272. Terminate the operation after a period of inactivity.
  273. ``LOG <variable>``
  274. Store a human-readable log of the operation in a variable.
  275. ``SHOW_PROGRESS``
  276. Print progress information as status messages until the operation is
  277. complete.
  278. ``STATUS <variable>``
  279. Store the resulting status of the operation in a variable.
  280. The status is a ``;`` separated list of length 2.
  281. The first element is the numeric return value for the operation,
  282. and the second element is a string value for the error.
  283. A ``0`` numeric error means no error in the operation.
  284. ``TIMEOUT <seconds>``
  285. Terminate the operation after a given total time has elapsed.
  286. ``USERPWD <username>:<password>``
  287. Set username and password for operation.
  288. ``HTTPHEADER <HTTP-header>``
  289. HTTP header for operation. Suboption can be repeated several times.
  290. ``NETRC <level>``
  291. Specify whether the .netrc file is to be used for operation. If this
  292. option is not specified, the value of the ``CMAKE_NETRC`` variable
  293. will be used instead.
  294. Valid levels are:
  295. ``IGNORED``
  296. The .netrc file is ignored.
  297. This is the default.
  298. ``OPTIONAL``
  299. The .netrc file is optional, and information in the URL is preferred.
  300. The file will be scanned to find which ever information is not specified
  301. in the URL.
  302. ``REQUIRED``
  303. The .netrc file is required, and information in the URL is ignored.
  304. ``NETRC_FILE <file>``
  305. Specify an alternative .netrc file to the one in your home directory,
  306. if the ``NETRC`` level is ``OPTIONAL`` or ``REQUIRED``. If this option
  307. is not specified, the value of the ``CMAKE_NETRC_FILE`` variable will
  308. be used instead.
  309. If neither ``NETRC`` option is given CMake will check variables
  310. ``CMAKE_NETRC`` and ``CMAKE_NETRC_FILE``, respectively.
  311. Additional options to ``DOWNLOAD`` are:
  312. ``EXPECTED_HASH ALGO=<value>``
  313. Verify that the downloaded content hash matches the expected value, where
  314. ``ALGO`` is one of the algorithms supported by ``file(<HASH>)``.
  315. If it does not match, the operation fails with an error.
  316. ``EXPECTED_MD5 <value>``
  317. Historical short-hand for ``EXPECTED_HASH MD5=<value>``.
  318. ``TLS_VERIFY <ON|OFF>``
  319. Specify whether to verify the server certificate for ``https://`` URLs.
  320. The default is to *not* verify.
  321. ``TLS_CAINFO <file>``
  322. Specify a custom Certificate Authority file for ``https://`` URLs.
  323. For ``https://`` URLs CMake must be built with OpenSSL support. ``TLS/SSL``
  324. certificates are not checked by default. Set ``TLS_VERIFY`` to ``ON`` to
  325. check certificates and/or use ``EXPECTED_HASH`` to verify downloaded content.
  326. If neither ``TLS`` option is given CMake will check variables
  327. ``CMAKE_TLS_VERIFY`` and ``CMAKE_TLS_CAINFO``, respectively.
  328. Locking
  329. ^^^^^^^
  330. .. _LOCK:
  331. .. code-block:: cmake
  332. file(LOCK <path> [DIRECTORY] [RELEASE]
  333. [GUARD <FUNCTION|FILE|PROCESS>]
  334. [RESULT_VARIABLE <variable>]
  335. [TIMEOUT <seconds>])
  336. Lock a file specified by ``<path>`` if no ``DIRECTORY`` option present and file
  337. ``<path>/cmake.lock`` otherwise. File will be locked for scope defined by
  338. ``GUARD`` option (default value is ``PROCESS``). ``RELEASE`` option can be used
  339. to unlock file explicitly. If option ``TIMEOUT`` is not specified CMake will
  340. wait until lock succeed or until fatal error occurs. If ``TIMEOUT`` is set to
  341. ``0`` lock will be tried once and result will be reported immediately. If
  342. ``TIMEOUT`` is not ``0`` CMake will try to lock file for the period specified
  343. by ``<seconds>`` value. Any errors will be interpreted as fatal if there is no
  344. ``RESULT_VARIABLE`` option. Otherwise result will be stored in ``<variable>``
  345. and will be ``0`` on success or error message on failure.
  346. Note that lock is advisory - there is no guarantee that other processes will
  347. respect this lock, i.e. lock synchronize two or more CMake instances sharing
  348. some modifiable resources. Similar logic applied to ``DIRECTORY`` option -
  349. locking parent directory doesn't prevent other ``LOCK`` commands to lock any
  350. child directory or file.
  351. Trying to lock file twice is not allowed. Any intermediate directories and
  352. file itself will be created if they not exist. ``GUARD`` and ``TIMEOUT``
  353. options ignored on ``RELEASE`` operation.