file.rst 17 KB

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