file.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. file
  2. ----
  3. File manipulation command.
  4. ------------------------------------------------------------------------------
  5. ::
  6. file(WRITE <filename> <content>...)
  7. file(APPEND <filename> <content>...)
  8. Write ``<content>`` into a file called ``<filename>``. If the file does
  9. not exist, it will be created. If the file already exists, ``WRITE``
  10. mode will overwrite it and ``APPEND`` mode will append to the end.
  11. (If the file is a build input, use the :command:`configure_file` command
  12. to update the file only when its content changes.)
  13. ------------------------------------------------------------------------------
  14. ::
  15. file(READ <filename> <variable>
  16. [OFFSET <offset>] [LIMIT <max-in>] [HEX])
  17. Read content from a file called ``<filename>`` and store it in a
  18. ``<variable>``. Optionally start from the given ``<offset>`` and
  19. read at most ``<max-in>`` bytes. The ``HEX`` option causes data to
  20. be converted to a hexadecimal representation (useful for binary data).
  21. ------------------------------------------------------------------------------
  22. ::
  23. file(STRINGS <filename> <variable> [<options>...])
  24. Parse a list of ASCII strings from ``<filename>`` and store it in
  25. ``<variable>``. Binary data in the file are ignored. Carriage return
  26. (``\r``, CR) characters are ignored. The options are:
  27. ``LENGTH_MAXIMUM <max-len>``
  28. Consider only strings of at most a given length.
  29. ``LENGTH_MINIMUM <min-len>``
  30. Consider only strings of at least a given length.
  31. ``LIMIT_COUNT <max-num>``
  32. Limit the number of distinct strings to be extracted.
  33. ``LIMIT_INPUT <max-in>``
  34. Limit the number of input bytes to read from the file.
  35. ``LIMIT_OUTPUT <max-out>``
  36. Limit the number of total bytes to store in the ``<variable>``.
  37. ``NEWLINE_CONSUME``
  38. Treat newline characters (``\n``, LF) as part of string content
  39. instead of terminating at them.
  40. ``NO_HEX_CONVERSION``
  41. Intel Hex and Motorola S-record files are automatically converted to
  42. binary while reading unless this option is given.
  43. ``REGEX <regex>``
  44. Consider only strings that match the given regular expression.
  45. ``ENCODING <encoding-type>``
  46. Consider strings of a given encoding. Currently supported encodings are:
  47. UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. If the ENCODING option
  48. is not provided and the file has a Byte Order Mark, the ENCODING option
  49. will be defaulted to respect the Byte Order Mark.
  50. For example, the code
  51. .. code-block:: cmake
  52. file(STRINGS myfile.txt myfile)
  53. stores a list in the variable ``myfile`` in which each item is a line
  54. from the input file.
  55. ------------------------------------------------------------------------------
  56. ::
  57. file(<HASH> <filename> <variable>)
  58. Compute a cryptographic hash of the content of ``<filename>`` and
  59. store it in a ``<variable>``. The supported ``<HASH>`` algorithm names
  60. are those listed by the :ref:`string(\<HASH\>) <Supported Hash Algorithms>`
  61. command.
  62. ------------------------------------------------------------------------------
  63. ::
  64. file(GLOB <variable>
  65. [LIST_DIRECTORIES true|false] [RELATIVE <path>]
  66. [<globbing-expressions>...])
  67. file(GLOB_RECURSE <variable> [FOLLOW_SYMLINKS]
  68. [LIST_DIRECTORIES true|false] [RELATIVE <path>]
  69. [<globbing-expressions>...])
  70. Generate a list of files that match the ``<globbing-expressions>`` and
  71. store it into the ``<variable>``. Globbing expressions are similar to
  72. regular expressions, but much simpler. If ``RELATIVE`` flag is
  73. specified, the results will be returned as relative paths to the given
  74. path. No specific order of results is defined other than that it is
  75. deterministic. If order is important then sort the list explicitly
  76. (e.g. using the :command:`list(SORT)` command).
  77. By default ``GLOB`` lists directories - directories are omited in result if
  78. ``LIST_DIRECTORIES`` is set to false.
  79. .. note::
  80. We do not recommend using GLOB to collect a list of source files from
  81. your source tree. If no CMakeLists.txt file changes when a source is
  82. added or removed then the generated build system cannot know when to
  83. ask CMake to regenerate.
  84. Examples of globbing expressions include::
  85. *.cxx - match all files with extension cxx
  86. *.vt? - match all files with extension vta,...,vtz
  87. f[3-5].txt - match files f3.txt, f4.txt, f5.txt
  88. The ``GLOB_RECURSE`` mode will traverse all the subdirectories of the
  89. matched directory and match the files. Subdirectories that are symlinks
  90. are only traversed if ``FOLLOW_SYMLINKS`` is given or policy
  91. :policy:`CMP0009` is not set to ``NEW``.
  92. By default ``GLOB_RECURSE`` omits directories from result list - setting
  93. ``LIST_DIRECTORIES`` to true adds directories to result list.
  94. If ``FOLLOW_SYMLINKS`` is given or policy :policy:`CMP0009` is not set to
  95. ``OLD`` then ``LIST_DIRECTORIES`` treats symlinks as directories.
  96. Examples of recursive globbing include::
  97. /dir/*.py - match all python files in /dir and subdirectories
  98. ------------------------------------------------------------------------------
  99. ::
  100. file(RENAME <oldname> <newname>)
  101. Move a file or directory within a filesystem from ``<oldname>`` to
  102. ``<newname>``, replacing the destination atomically.
  103. ------------------------------------------------------------------------------
  104. ::
  105. file(REMOVE [<files>...])
  106. file(REMOVE_RECURSE [<files>...])
  107. Remove the given files. The ``REMOVE_RECURSE`` mode will remove the given
  108. files and directories, also non-empty directories. No error is emitted if a
  109. given file does not exist.
  110. ------------------------------------------------------------------------------
  111. ::
  112. file(MAKE_DIRECTORY [<directories>...])
  113. Create the given directories and their parents as needed.
  114. ------------------------------------------------------------------------------
  115. ::
  116. file(RELATIVE_PATH <variable> <directory> <file>)
  117. Compute the relative path from a ``<directory>`` to a ``<file>`` and
  118. store it in the ``<variable>``.
  119. ------------------------------------------------------------------------------
  120. ::
  121. file(TO_CMAKE_PATH "<path>" <variable>)
  122. file(TO_NATIVE_PATH "<path>" <variable>)
  123. The ``TO_CMAKE_PATH`` mode converts a native ``<path>`` into a cmake-style
  124. path with forward-slashes (``/``). The input can be a single path or a
  125. system search path like ``$ENV{PATH}``. A search path will be converted
  126. to a cmake-style list separated by ``;`` characters.
  127. The ``TO_NATIVE_PATH`` mode converts a cmake-style ``<path>`` into a native
  128. path with platform-specific slashes (``\`` on Windows and ``/`` elsewhere).
  129. Always use double quotes around the ``<path>`` to be sure it is treated
  130. as a single argument to this command.
  131. ------------------------------------------------------------------------------
  132. ::
  133. file(DOWNLOAD <url> <file> [<options>...])
  134. file(UPLOAD <file> <url> [<options>...])
  135. The ``DOWNLOAD`` mode downloads the given ``<url>`` to a local ``<file>``.
  136. The ``UPLOAD`` mode uploads a local ``<file>`` to a given ``<url>``.
  137. Options to both ``DOWNLOAD`` and ``UPLOAD`` are:
  138. ``INACTIVITY_TIMEOUT <seconds>``
  139. Terminate the operation after a period of inactivity.
  140. ``LOG <variable>``
  141. Store a human-readable log of the operation in a variable.
  142. ``SHOW_PROGRESS``
  143. Print progress information as status messages until the operation is
  144. complete.
  145. ``STATUS <variable>``
  146. Store the resulting status of the operation in a variable.
  147. The status is a ``;`` separated list of length 2.
  148. The first element is the numeric return value for the operation,
  149. and the second element is a string value for the error.
  150. A ``0`` numeric error means no error in the operation.
  151. ``TIMEOUT <seconds>``
  152. Terminate the operation after a given total time has elapsed.
  153. ``USERPWD <username>:<password>``
  154. Set username and password for operation.
  155. ``HTTPHEADER <HTTP-header>``
  156. HTTP header for operation. Suboption can be repeated several times.
  157. Additional options to ``DOWNLOAD`` are:
  158. ``EXPECTED_HASH ALGO=<value>``
  159. Verify that the downloaded content hash matches the expected value, where
  160. ``ALGO`` is one of the algorithms supported by ``file(<HASH>)``.
  161. If it does not match, the operation fails with an error.
  162. ``EXPECTED_MD5 <value>``
  163. Historical short-hand for ``EXPECTED_HASH MD5=<value>``.
  164. ``TLS_VERIFY <ON|OFF>``
  165. Specify whether to verify the server certificate for ``https://`` URLs.
  166. The default is to *not* verify.
  167. ``TLS_CAINFO <file>``
  168. Specify a custom Certificate Authority file for ``https://`` URLs.
  169. For ``https://`` URLs CMake must be built with OpenSSL support. ``TLS/SSL``
  170. certificates are not checked by default. Set ``TLS_VERIFY`` to ``ON`` to
  171. check certificates and/or use ``EXPECTED_HASH`` to verify downloaded content.
  172. If neither ``TLS`` option is given CMake will check variables
  173. ``CMAKE_TLS_VERIFY`` and ``CMAKE_TLS_CAINFO``, respectively.
  174. ------------------------------------------------------------------------------
  175. ::
  176. file(TIMESTAMP <filename> <variable> [<format>] [UTC])
  177. Compute a string representation of the modification time of ``<filename>``
  178. and store it in ``<variable>``. Should the command be unable to obtain a
  179. timestamp variable will be set to the empty string ("").
  180. See the :command:`string(TIMESTAMP)` command for documentation of
  181. the ``<format>`` and ``UTC`` options.
  182. ------------------------------------------------------------------------------
  183. ::
  184. file(GENERATE OUTPUT output-file
  185. <INPUT input-file|CONTENT content>
  186. [CONDITION expression])
  187. Generate an output file for each build configuration supported by the current
  188. :manual:`CMake Generator <cmake-generators(7)>`. Evaluate
  189. :manual:`generator expressions <cmake-generator-expressions(7)>`
  190. from the input content to produce the output content. The options are:
  191. ``CONDITION <condition>``
  192. Generate the output file for a particular configuration only if
  193. the condition is true. The condition must be either ``0`` or ``1``
  194. after evaluating generator expressions.
  195. ``CONTENT <content>``
  196. Use the content given explicitly as input.
  197. ``INPUT <input-file>``
  198. Use the content from a given file as input.
  199. ``OUTPUT <output-file>``
  200. Specify the output file name to generate. Use generator expressions
  201. such as ``$<CONFIG>`` to specify a configuration-specific output file
  202. name. Multiple configurations may generate the same output file only
  203. if the generated content is identical. Otherwise, the ``<output-file>``
  204. must evaluate to an unique name for each configuration.
  205. Exactly one ``CONTENT`` or ``INPUT`` option must be given. A specific
  206. ``OUTPUT`` file may be named by at most one invocation of ``file(GENERATE)``.
  207. Generated files are modified on subsequent cmake runs only if their content
  208. is changed.
  209. ------------------------------------------------------------------------------
  210. ::
  211. file(<COPY|INSTALL> <files>... DESTINATION <dir>
  212. [FILE_PERMISSIONS <permissions>...]
  213. [DIRECTORY_PERMISSIONS <permissions>...]
  214. [NO_SOURCE_PERMISSIONS] [USE_SOURCE_PERMISSIONS]
  215. [FILES_MATCHING]
  216. [[PATTERN <pattern> | REGEX <regex>]
  217. [EXCLUDE] [PERMISSIONS <permissions>...]] [...])
  218. The ``COPY`` signature copies files, directories, and symlinks to a
  219. destination folder. Relative input paths are evaluated with respect
  220. to the current source directory, and a relative destination is
  221. evaluated with respect to the current build directory. Copying
  222. preserves input file timestamps, and optimizes out a file if it exists
  223. at the destination with the same timestamp. Copying preserves input
  224. permissions unless explicit permissions or ``NO_SOURCE_PERMISSIONS``
  225. are given (default is ``USE_SOURCE_PERMISSIONS``).
  226. See the :command:`install(DIRECTORY)` command for documentation of
  227. permissions, ``FILES_MATCHING``, ``PATTERN``, ``REGEX``, and
  228. ``EXCLUDE`` options. Copying directories preserves the structure
  229. of their content even if options are used to select a subset of
  230. files.
  231. The ``INSTALL`` signature differs slightly from ``COPY``: it prints
  232. status messages (subject to the :variable:`CMAKE_INSTALL_MESSAGE` variable),
  233. and ``NO_SOURCE_PERMISSIONS`` is default.
  234. Installation scripts generated by the :command:`install` command
  235. use this signature (with some undocumented options for internal use).
  236. ------------------------------------------------------------------------------
  237. ::
  238. file(LOCK <path> [DIRECTORY] [RELEASE]
  239. [GUARD <FUNCTION|FILE|PROCESS>]
  240. [RESULT_VARIABLE <variable>]
  241. [TIMEOUT <seconds>])
  242. Lock a file specified by ``<path>`` if no ``DIRECTORY`` option present and file
  243. ``<path>/cmake.lock`` otherwise. File will be locked for scope defined by
  244. ``GUARD`` option (default value is ``PROCESS``). ``RELEASE`` option can be used
  245. to unlock file explicitly. If option ``TIMEOUT`` is not specified CMake will
  246. wait until lock succeed or until fatal error occurs. If ``TIMEOUT`` is set to
  247. ``0`` lock will be tried once and result will be reported immediately. If
  248. ``TIMEOUT`` is not ``0`` CMake will try to lock file for the period specified
  249. by ``<seconds>`` value. Any errors will be interpreted as fatal if there is no
  250. ``RESULT_VARIABLE`` option. Otherwise result will be stored in ``<variable>``
  251. and will be ``0`` on success or error message on failure.
  252. Note that lock is advisory - there is no guarantee that other processes will
  253. respect this lock, i.e. lock synchronize two or more CMake instances sharing
  254. some modifiable resources. Similar logic applied to ``DIRECTORY`` option -
  255. locking parent directory doesn't prevent other ``LOCK`` commands to lock any
  256. child directory or file.
  257. Trying to lock file twice is not allowed. Any intermediate directories and
  258. file itself will be created if they not exist. ``GUARD`` and ``TIMEOUT``
  259. options ignored on ``RELEASE`` operation.