file.rst 32 KB

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