file.rst 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  1. file
  2. ----
  3. File manipulation command.
  4. This command is dedicated to file and path manipulation requiring access to the
  5. filesystem.
  6. For other path manipulation, handling only syntactic aspects, see the
  7. :command:`cmake_path` command.
  8. .. note::
  9. The sub-commands `RELATIVE_PATH`_, `TO_CMAKE_PATH`_, and `TO_NATIVE_PATH`_
  10. have been superseded, respectively, by the sub-commands
  11. :command:`cmake_path(RELATIVE_PATH)`,
  12. :command:`cmake_path(CONVERT ... TO_CMAKE_PATH_LIST)`, and
  13. :command:`cmake_path(CONVERT ... TO_NATIVE_PATH_LIST)`.
  14. Synopsis
  15. ^^^^^^^^
  16. .. parsed-literal::
  17. `Reading`_
  18. file(`READ`_ <filename> <out-var> [...])
  19. file(`STRINGS`_ <filename> <out-var> [...])
  20. file(`\<HASH\>`_ <filename> <out-var>)
  21. file(`TIMESTAMP`_ <filename> <out-var> [...])
  22. `Writing`_
  23. file({`WRITE`_ | `APPEND`_} <filename> <content>...)
  24. file({`TOUCH`_ | `TOUCH_NOCREATE`_} <file>...)
  25. file(`GENERATE`_ OUTPUT <output-file> [...])
  26. file(`CONFIGURE`_ OUTPUT <output-file> CONTENT <content> [...])
  27. `Filesystem`_
  28. file({`GLOB`_ | `GLOB_RECURSE`_} <out-var> [...] <globbing-expr>...)
  29. file(`MAKE_DIRECTORY`_ <directories>... [...])
  30. file({`REMOVE`_ | `REMOVE_RECURSE`_ } <files>...)
  31. file(`RENAME`_ <oldname> <newname> [...])
  32. file(`COPY_FILE`_ <oldname> <newname> [...])
  33. file({`COPY`_ | `INSTALL`_} <file>... DESTINATION <dir> [...])
  34. file(`SIZE`_ <filename> <out-var>)
  35. file(`READ_SYMLINK`_ <linkname> <out-var>)
  36. file(`CREATE_LINK`_ <original> <linkname> [...])
  37. file(`CHMOD`_ <files>... <directories>... PERMISSIONS <permissions>... [...])
  38. file(`CHMOD_RECURSE`_ <files>... <directories>... PERMISSIONS <permissions>... [...])
  39. `Path Conversion`_
  40. file(`REAL_PATH`_ <path> <out-var> [BASE_DIRECTORY <dir>] [EXPAND_TILDE])
  41. file(`RELATIVE_PATH`_ <out-var> <directory> <file>)
  42. file({`TO_CMAKE_PATH`_ | `TO_NATIVE_PATH`_} <path> <out-var>)
  43. `Transfer`_
  44. file(`DOWNLOAD`_ <url> [<file>] [...])
  45. file(`UPLOAD`_ <file> <url> [...])
  46. `Locking`_
  47. file(`LOCK`_ <path> [...])
  48. `Archiving`_
  49. file(`ARCHIVE_CREATE`_ OUTPUT <archive> PATHS <paths>... [...])
  50. file(`ARCHIVE_EXTRACT`_ INPUT <archive> [...])
  51. `Handling Runtime Binaries`_
  52. file(`GET_RUNTIME_DEPENDENCIES`_ [...])
  53. Reading
  54. ^^^^^^^
  55. .. signature::
  56. file(READ <filename> <variable>
  57. [OFFSET <offset>] [LIMIT <max-in>] [HEX])
  58. Read content from a file called ``<filename>`` and store it in a
  59. ``<variable>``. Optionally start from the given ``<offset>`` and
  60. read at most ``<max-in>`` bytes. The ``HEX`` option causes data to
  61. be converted to a hexadecimal representation (useful for binary data).
  62. If the ``HEX`` option is specified, letters in the output
  63. (``a`` through ``f``) are in lowercase.
  64. .. signature::
  65. file(STRINGS <filename> <variable> <options>...)
  66. Parse a list of ASCII strings from ``<filename>`` and store it in
  67. ``<variable>``. Binary data in the file are ignored. Carriage return
  68. (``\r``, CR) characters are ignored. The options are:
  69. ``LENGTH_MAXIMUM <max-len>``
  70. Consider only strings of at most a given length.
  71. ``LENGTH_MINIMUM <min-len>``
  72. Consider only strings of at least a given length.
  73. ``LIMIT_COUNT <max-num>``
  74. Limit the number of distinct strings to be extracted.
  75. ``LIMIT_INPUT <max-in>``
  76. Limit the number of input bytes to read from the file.
  77. ``LIMIT_OUTPUT <max-out>``
  78. Limit the number of total bytes to store in the ``<variable>``.
  79. ``NEWLINE_CONSUME``
  80. Treat newline characters (``\n``, LF) as part of string content
  81. instead of terminating at them.
  82. ``NO_HEX_CONVERSION``
  83. Intel Hex and Motorola S-record files are automatically converted to
  84. binary while reading unless this option is given.
  85. ``REGEX <regex>``
  86. Consider only strings that match the given regular expression,
  87. as described under :ref:`string(REGEX) <Regex Specification>`.
  88. .. versionchanged:: 3.29
  89. Capture groups from the last match in the file are stored in
  90. :variable:`CMAKE_MATCH_<n>`, similar to
  91. :command:`string(REGEX MATCHALL)`. See policy :policy:`CMP0159`.
  92. ``ENCODING <encoding-type>``
  93. .. versionadded:: 3.1
  94. Consider strings of a given encoding. Currently supported encodings are:
  95. ``UTF-8``, ``UTF-16LE``, ``UTF-16BE``, ``UTF-32LE``, ``UTF-32BE``.
  96. If the ``ENCODING`` option is not provided and the file
  97. has a Byte Order Mark, the ``ENCODING`` option will be defaulted
  98. to respect the Byte Order Mark.
  99. .. versionadded:: 3.2
  100. Added the ``UTF-16LE``, ``UTF-16BE``, ``UTF-32LE``, ``UTF-32BE`` encodings.
  101. For example, the code
  102. .. code-block:: cmake
  103. file(STRINGS myfile.txt myfile)
  104. stores a list in the variable ``myfile`` in which each item is a line
  105. from the input file.
  106. .. signature::
  107. file(<HASH> <filename> <variable>)
  108. :target: <HASH>
  109. Compute a cryptographic hash of the content of ``<filename>`` and
  110. store it in a ``<variable>``. The supported ``<HASH>`` algorithm names
  111. are those listed by the :command:`string(<HASH>)` command.
  112. .. signature::
  113. file(TIMESTAMP <filename> <variable> [<format>] [UTC])
  114. Compute a string representation of the modification time of ``<filename>``
  115. and store it in ``<variable>``. Should the command be unable to obtain a
  116. timestamp variable will be set to the empty string ("").
  117. See the :command:`string(TIMESTAMP)` command for documentation of
  118. the ``<format>`` and ``UTC`` options.
  119. Writing
  120. ^^^^^^^
  121. .. signature::
  122. file(WRITE <filename> <content>...)
  123. file(APPEND <filename> <content>...)
  124. Write ``<content>`` into a file called ``<filename>``. If the file does
  125. not exist, it will be created. If the file already exists, ``WRITE``
  126. mode will overwrite it and ``APPEND`` mode will append to the end.
  127. Any directories in the path specified by ``<filename>`` that do not
  128. exist will be created.
  129. If the file is a build input, use the :command:`configure_file` command
  130. to update the file only when its content changes.
  131. .. signature::
  132. file(TOUCH <files>...)
  133. file(TOUCH_NOCREATE <files>...)
  134. .. versionadded:: 3.12
  135. Create a file with no content if it does not yet exist. If the file already
  136. exists, its access and/or modification will be updated to the time when the
  137. function call is executed.
  138. Use ``TOUCH_NOCREATE`` to touch a file if it exists but not create it.
  139. If a file does not exist it will be silently ignored.
  140. With ``TOUCH`` and ``TOUCH_NOCREATE``, the contents of an existing file
  141. will not be modified.
  142. .. versionchanged:: 3.30
  143. ``<files>`` can be an empty list. CMake 3.29 and earlier required
  144. at least one file to be given.
  145. .. signature::
  146. file(GENERATE [...])
  147. Generate an output file for each build configuration supported by the current
  148. :manual:`CMake Generator <cmake-generators(7)>`. Evaluate
  149. :manual:`generator expressions <cmake-generator-expressions(7)>`
  150. from the input content to produce the output content.
  151. .. code-block:: cmake
  152. file(GENERATE OUTPUT <output-file>
  153. <INPUT <input-file>|CONTENT <content>>
  154. [CONDITION <expression>] [TARGET <target>]
  155. [NO_SOURCE_PERMISSIONS | USE_SOURCE_PERMISSIONS |
  156. FILE_PERMISSIONS <permissions>...]
  157. [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF]])
  158. The options are:
  159. ``CONDITION <condition>``
  160. Generate the output file for a particular configuration only if
  161. the condition is true. The condition must be either ``0`` or ``1``
  162. after evaluating generator expressions.
  163. ``CONTENT <content>``
  164. Use the content given explicitly as input.
  165. ``INPUT <input-file>``
  166. Use the content from a given file as input.
  167. .. versionchanged:: 3.10
  168. A relative path is treated with respect to the value of
  169. :variable:`CMAKE_CURRENT_SOURCE_DIR`. See policy :policy:`CMP0070`.
  170. ``OUTPUT <output-file>``
  171. Specify the output file name to generate. Use generator expressions
  172. such as :genex:`$<CONFIG>` to specify a configuration-specific
  173. output file name. Multiple configurations may generate the same output
  174. file only if the generated content is identical. Otherwise, the
  175. ``<output-file>`` must evaluate to an unique name for each configuration.
  176. .. versionchanged:: 3.10
  177. A relative path (after evaluating generator expressions) is treated
  178. with respect to the value of :variable:`CMAKE_CURRENT_BINARY_DIR`.
  179. See policy :policy:`CMP0070`.
  180. ``TARGET <target>``
  181. .. versionadded:: 3.19
  182. Specify which target to use when evaluating generator expressions that
  183. require a target for evaluation (e.g.
  184. :genex:`$<COMPILE_FEATURES:...>`,
  185. :genex:`$<TARGET_PROPERTY:prop>`).
  186. ``NO_SOURCE_PERMISSIONS``
  187. .. versionadded:: 3.20
  188. The generated file permissions default to the standard 644 value
  189. (-rw-r--r--).
  190. ``USE_SOURCE_PERMISSIONS``
  191. .. versionadded:: 3.20
  192. Transfer the file permissions of the ``INPUT`` file to the generated
  193. file. This is already the default behavior if none of the three
  194. permissions-related keywords are given (``NO_SOURCE_PERMISSIONS``,
  195. ``USE_SOURCE_PERMISSIONS`` or ``FILE_PERMISSIONS``). The
  196. ``USE_SOURCE_PERMISSIONS`` keyword mostly serves as a way of making
  197. the intended behavior clearer at the call site. It is an error to
  198. specify this option without ``INPUT``.
  199. ``FILE_PERMISSIONS <permissions>...``
  200. .. versionadded:: 3.20
  201. Use the specified permissions for the generated file.
  202. ``NEWLINE_STYLE <style>``
  203. .. versionadded:: 3.20
  204. Specify the newline style for the generated file. Specify
  205. ``UNIX`` or ``LF`` for ``\n`` newlines, or specify
  206. ``DOS``, ``WIN32``, or ``CRLF`` for ``\r\n`` newlines.
  207. Exactly one ``CONTENT`` or ``INPUT`` option must be given. A specific
  208. ``OUTPUT`` file may be named by at most one invocation of ``file(GENERATE)``.
  209. Generated files are modified and their timestamp updated on subsequent cmake
  210. runs only if their content is changed.
  211. Note also that ``file(GENERATE)`` does not create the output file until the
  212. generation phase. The output file will not yet have been written when the
  213. ``file(GENERATE)`` command returns, it is written only after processing all
  214. of a project's ``CMakeLists.txt`` files.
  215. .. signature::
  216. file(CONFIGURE OUTPUT <output-file>
  217. CONTENT <content>
  218. [ESCAPE_QUOTES] [@ONLY]
  219. [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF]])
  220. :target: CONFIGURE
  221. .. versionadded:: 3.18
  222. Generate an output file using the input given by ``CONTENT`` and substitute
  223. variable values referenced as ``@VAR@`` or ``${VAR}`` contained therein. The
  224. substitution rules behave the same as the :command:`configure_file` command.
  225. In order to match :command:`configure_file`'s behavior, generator expressions
  226. are not supported for both ``OUTPUT`` and ``CONTENT``, and the output file
  227. is only modified and its timestamp updated if the content is changed or the
  228. file previously didn't exist.
  229. The arguments are:
  230. ``OUTPUT <output-file>``
  231. Specify the output file name to generate. A relative path is treated with
  232. respect to the value of :variable:`CMAKE_CURRENT_BINARY_DIR`.
  233. ``<output-file>`` does not support generator expressions.
  234. ``CONTENT <content>``
  235. Use the content given explicitly as input.
  236. ``<content>`` does not support generator expressions.
  237. ``ESCAPE_QUOTES``
  238. Escape any substituted quotes with backslashes (C-style).
  239. ``@ONLY``
  240. Restrict variable replacement to references of the form ``@VAR@``.
  241. This is useful for configuring scripts that use ``${VAR}`` syntax.
  242. ``NEWLINE_STYLE <style>``
  243. Specify the newline style for the output file. Specify
  244. ``UNIX`` or ``LF`` for ``\n`` newlines, or specify
  245. ``DOS``, ``WIN32``, or ``CRLF`` for ``\r\n`` newlines.
  246. Filesystem
  247. ^^^^^^^^^^
  248. .. signature::
  249. file(GLOB <variable>
  250. [LIST_DIRECTORIES true|false] [RELATIVE <path>] [CONFIGURE_DEPENDS]
  251. <globbing-expressions>...)
  252. file(GLOB_RECURSE <variable> [FOLLOW_SYMLINKS]
  253. [LIST_DIRECTORIES true|false] [RELATIVE <path>] [CONFIGURE_DEPENDS]
  254. <globbing-expressions>...)
  255. Generate a list of files that match the ``<globbing-expressions>`` and
  256. store it into the ``<variable>``. Globbing expressions are similar to
  257. regular expressions, but much simpler. If ``RELATIVE`` flag is
  258. specified, the results will be returned as relative paths to the given
  259. path.
  260. .. versionchanged:: 3.6
  261. The results will be ordered lexicographically.
  262. On Windows and macOS, globbing is case-insensitive even if the underlying
  263. filesystem is case-sensitive (both filenames and globbing expressions are
  264. converted to lowercase before matching). On other platforms, globbing is
  265. case-sensitive.
  266. .. versionadded:: 3.3
  267. By default ``GLOB`` lists directories. Directories are omitted in the
  268. result if ``LIST_DIRECTORIES`` is set to false.
  269. .. versionadded:: 3.12
  270. If the ``CONFIGURE_DEPENDS`` flag is specified, CMake will add logic
  271. to the main build system check target to rerun the flagged ``GLOB``
  272. commands at build time. If any of the outputs change, CMake will regenerate
  273. the build system.
  274. .. note::
  275. We do not recommend using GLOB to collect a list of source files from
  276. your source tree. If no CMakeLists.txt file changes when a source is
  277. added or removed then the generated build system cannot know when to
  278. ask CMake to regenerate.
  279. The ``CONFIGURE_DEPENDS`` flag may not work reliably on all generators, or
  280. if a new generator is added in the future that cannot support it, projects
  281. using it will be stuck. Even if ``CONFIGURE_DEPENDS`` works reliably, there
  282. is still a cost to perform the check on every rebuild.
  283. Examples of globbing expressions include:
  284. ============== ======================================================
  285. ``*.cxx`` match all files with extension ``cxx``
  286. ``*.vt?`` match all files with extension ``vta``, ..., ``vtz``
  287. ``f[3-5].txt`` match files ``f3.txt``, ``f4.txt``, ``f5.txt``
  288. ============== ======================================================
  289. The ``GLOB_RECURSE`` mode will traverse all the subdirectories of the
  290. matched directory and match the files. Subdirectories that are symlinks
  291. are only traversed if ``FOLLOW_SYMLINKS`` is given or policy
  292. :policy:`CMP0009` is not set to ``NEW``.
  293. .. versionadded:: 3.3
  294. By default ``GLOB_RECURSE`` omits directories from result list. Setting
  295. ``LIST_DIRECTORIES`` to true adds directories to result list.
  296. If ``FOLLOW_SYMLINKS`` is given or policy :policy:`CMP0009` is not set to
  297. ``NEW`` then ``LIST_DIRECTORIES`` treats symlinks as directories.
  298. Examples of recursive globbing include:
  299. ============== ======================================================
  300. ``/dir/*.py`` match all python files in ``/dir`` and subdirectories
  301. ============== ======================================================
  302. .. signature::
  303. file(MAKE_DIRECTORY <directories>... [RESULT <result>])
  304. Create the given directories and their parents as needed. Relative input
  305. paths are evaluated with respect to the current source directory.
  306. The options are:
  307. ``RESULT <result>``
  308. .. versionadded:: 3.31
  309. Set ``<result>`` variable to ``0`` on success or an error message
  310. otherwise. If ``RESULT`` is not specified and the operation fails,
  311. an error is emitted.
  312. .. versionchanged:: 3.30
  313. ``<directories>`` can be an empty list. CMake 3.29 and earlier required
  314. at least one directory to be given.
  315. .. signature::
  316. file(REMOVE <files>...)
  317. file(REMOVE_RECURSE <files>...)
  318. Remove the given files. The ``REMOVE_RECURSE`` mode will remove the given
  319. files and directories, including non-empty directories. No error is emitted
  320. if a given file does not exist. Relative input paths are evaluated with
  321. respect to the current source directory.
  322. .. versionchanged:: 3.15
  323. Empty input paths are ignored with a warning. Previous versions of CMake
  324. interpreted empty strings as a relative path with respect to the current
  325. directory and removed its contents.
  326. .. signature::
  327. file(RENAME <oldname> <newname> [RESULT <result>] [NO_REPLACE])
  328. Move a file or directory within a filesystem from ``<oldname>`` to
  329. ``<newname>``, replacing the destination atomically.
  330. The options are:
  331. ``RESULT <result>``
  332. .. versionadded:: 3.21
  333. Set ``<result>`` variable to ``0`` on success or an error message
  334. otherwise. If ``RESULT`` is not specified and the operation fails,
  335. an error is emitted.
  336. ``NO_REPLACE``
  337. .. versionadded:: 3.21
  338. If the ``<newname>`` path already exists, do not replace it.
  339. If ``RESULT <result>`` is used, the result variable will be
  340. set to ``NO_REPLACE``. Otherwise, an error is emitted.
  341. .. signature::
  342. file(COPY_FILE <oldname> <newname>
  343. [RESULT <result>]
  344. [ONLY_IF_DIFFERENT]
  345. [INPUT_MAY_BE_RECENT])
  346. .. versionadded:: 3.21
  347. Copy a file from ``<oldname>`` to ``<newname>``. Directories are not
  348. supported. Symlinks are ignored and ``<oldfile>``'s content is read and
  349. written to ``<newname>`` as a new file.
  350. The options are:
  351. ``RESULT <result>``
  352. Set ``<result>`` variable to ``0`` on success or an error message
  353. otherwise. If ``RESULT`` is not specified and the operation fails,
  354. an error is emitted.
  355. ``ONLY_IF_DIFFERENT``
  356. If the ``<newname>`` path already exists, do not replace it if the file's
  357. contents are already the same as ``<oldname>`` (this avoids updating
  358. ``<newname>``'s timestamp).
  359. ``INPUT_MAY_BE_RECENT``
  360. .. versionadded:: 3.26
  361. Tell CMake that the input file may have been recently created. This is
  362. meaningful only on Windows, where files may be inaccessible for a short
  363. time after they are created. With this option, if permission is denied,
  364. CMake will retry reading the input a few times.
  365. This sub-command has some similarities to :command:`configure_file`
  366. with the ``COPYONLY`` option. An important difference is that
  367. :command:`configure_file` creates a dependency on the source file,
  368. so CMake will be re-run if it changes. The ``file(COPY_FILE)``
  369. sub-command does not create such a dependency.
  370. See also the :command:`file(COPY)` sub-command just below which provides
  371. further file-copying capabilities.
  372. .. signature::
  373. file(COPY [...])
  374. file(INSTALL [...])
  375. The ``COPY`` signature copies files, directories, and symlinks to a
  376. destination folder. Relative input paths are evaluated with respect
  377. to the current source directory, and a relative destination is
  378. evaluated with respect to the current build directory. Copying
  379. preserves input file timestamps, and optimizes out a file if it exists
  380. at the destination with the same timestamp. Copying preserves input
  381. permissions unless explicit permissions or ``NO_SOURCE_PERMISSIONS``
  382. are given (default is ``USE_SOURCE_PERMISSIONS``).
  383. .. code-block:: cmake
  384. file(<COPY|INSTALL> <files>... DESTINATION <dir>
  385. [NO_SOURCE_PERMISSIONS | USE_SOURCE_PERMISSIONS]
  386. [FILE_PERMISSIONS <permissions>...]
  387. [DIRECTORY_PERMISSIONS <permissions>...]
  388. [FOLLOW_SYMLINK_CHAIN]
  389. [FILES_MATCHING]
  390. [[PATTERN <pattern> | REGEX <regex>]
  391. [EXCLUDE] [PERMISSIONS <permissions>...]] [...])
  392. .. note::
  393. For a simple file copying operation, the :command:`file(COPY_FILE)`
  394. sub-command just above may be easier to use.
  395. .. versionadded:: 3.15
  396. If ``FOLLOW_SYMLINK_CHAIN`` is specified, ``COPY`` will recursively resolve
  397. the symlinks at the paths given until a real file is found, and install
  398. a corresponding symlink in the destination for each symlink encountered.
  399. For each symlink that is installed, the resolution is stripped of the
  400. directory, leaving only the filename, meaning that the new symlink points
  401. to a file in the same directory as the symlink. This feature is useful on
  402. some Unix systems, where libraries are installed as a chain of symlinks
  403. with version numbers, with less specific versions pointing to more specific
  404. versions. ``FOLLOW_SYMLINK_CHAIN`` will install all of these symlinks and
  405. the library itself into the destination directory. For example, if you have
  406. the following directory structure:
  407. * ``/opt/foo/lib/libfoo.so.1.2.3``
  408. * ``/opt/foo/lib/libfoo.so.1.2 -> libfoo.so.1.2.3``
  409. * ``/opt/foo/lib/libfoo.so.1 -> libfoo.so.1.2``
  410. * ``/opt/foo/lib/libfoo.so -> libfoo.so.1``
  411. and you do:
  412. .. code-block:: cmake
  413. file(COPY /opt/foo/lib/libfoo.so DESTINATION lib FOLLOW_SYMLINK_CHAIN)
  414. This will install all of the symlinks and ``libfoo.so.1.2.3`` itself into
  415. ``lib``.
  416. See the :command:`install(DIRECTORY)` command for documentation of
  417. permissions, ``FILES_MATCHING``, ``PATTERN``, ``REGEX``, and
  418. ``EXCLUDE`` options. Copying directories preserves the structure
  419. of their content even if options are used to select a subset of
  420. files.
  421. The ``INSTALL`` signature differs slightly from ``COPY``: it prints
  422. status messages, and ``NO_SOURCE_PERMISSIONS`` is default. Installation
  423. scripts generated by the :command:`install` command use this signature
  424. (with some undocumented options for internal use).
  425. .. versionchanged:: 3.22
  426. The environment variable :envvar:`CMAKE_INSTALL_MODE` can override the
  427. default copying behavior of :command:`file(INSTALL)`.
  428. .. signature::
  429. file(SIZE <filename> <variable>)
  430. .. versionadded:: 3.14
  431. Determine the file size of the ``<filename>`` and put the result in
  432. ``<variable>`` variable. Requires that ``<filename>`` is a valid path
  433. pointing to a file and is readable.
  434. .. signature::
  435. file(READ_SYMLINK <linkname> <variable>)
  436. .. versionadded:: 3.14
  437. Query the symlink ``<linkname>`` and stores the path it points to
  438. in the result ``<variable>``. If ``<linkname>`` does not exist
  439. or is not a symlink, CMake issues a fatal error.
  440. Note that this command returns the raw symlink path and does not resolve
  441. a relative path. The following is an example of how to ensure that an
  442. absolute path is obtained:
  443. .. code-block:: cmake
  444. set(linkname "/path/to/foo.sym")
  445. file(READ_SYMLINK "${linkname}" result)
  446. if(NOT IS_ABSOLUTE "${result}")
  447. get_filename_component(dir "${linkname}" DIRECTORY)
  448. set(result "${dir}/${result}")
  449. endif()
  450. .. signature::
  451. file(CREATE_LINK <original> <linkname>
  452. [RESULT <result>] [COPY_ON_ERROR] [SYMBOLIC])
  453. .. versionadded:: 3.14
  454. Create a link ``<linkname>`` that points to ``<original>``.
  455. It will be a hard link by default, but providing the ``SYMBOLIC`` option
  456. results in a symbolic link instead. Hard links require that ``original``
  457. exists and is a file, not a directory. If ``<linkname>`` already exists,
  458. it will be overwritten.
  459. The ``<result>`` variable, if specified, receives the status of the
  460. operation. It is set to ``0`` upon success or an error message otherwise.
  461. If ``RESULT`` is not specified and the operation fails, a fatal error is
  462. emitted.
  463. Specifying ``COPY_ON_ERROR`` enables copying the file as a fallback if
  464. creating the link fails. It can be useful for handling situations such as
  465. ``<original>`` and ``<linkname>`` being on different drives or mount points,
  466. which would make them unable to support a hard link.
  467. If the source is a directory, the destination directory will be created if
  468. it does not exist. Contents of the source directory will be copied to the
  469. destination directory unless policy :policy:`CMP0205` is not set to ``NEW``.
  470. .. signature::
  471. file(CHMOD <files>... <directories>...
  472. [PERMISSIONS <permissions>...]
  473. [FILE_PERMISSIONS <permissions>...]
  474. [DIRECTORY_PERMISSIONS <permissions>...])
  475. .. versionadded:: 3.19
  476. Set the permissions for the ``<files>...`` and ``<directories>...``
  477. specified. Valid permissions are ``OWNER_READ``, ``OWNER_WRITE``,
  478. ``OWNER_EXECUTE``, ``GROUP_READ``, ``GROUP_WRITE``, ``GROUP_EXECUTE``,
  479. ``WORLD_READ``, ``WORLD_WRITE``, ``WORLD_EXECUTE``, ``SETUID``, ``SETGID``.
  480. Valid combination of keywords are:
  481. ``PERMISSIONS``
  482. All items are changed.
  483. ``FILE_PERMISSIONS``
  484. Only files are changed.
  485. ``DIRECTORY_PERMISSIONS``
  486. Only directories are changed.
  487. ``PERMISSIONS`` and ``FILE_PERMISSIONS``
  488. ``FILE_PERMISSIONS`` overrides ``PERMISSIONS`` for files.
  489. ``PERMISSIONS`` and ``DIRECTORY_PERMISSIONS``
  490. ``DIRECTORY_PERMISSIONS`` overrides ``PERMISSIONS`` for directories.
  491. ``FILE_PERMISSIONS`` and ``DIRECTORY_PERMISSIONS``
  492. Use ``FILE_PERMISSIONS`` for files and ``DIRECTORY_PERMISSIONS`` for
  493. directories.
  494. .. signature::
  495. file(CHMOD_RECURSE <files>... <directories>...
  496. [PERMISSIONS <permissions>...]
  497. [FILE_PERMISSIONS <permissions>...]
  498. [DIRECTORY_PERMISSIONS <permissions>...])
  499. .. versionadded:: 3.19
  500. Same as :cref:`CHMOD`, but change the permissions of files and directories
  501. present in the ``<directories>...`` recursively.
  502. Path Conversion
  503. ^^^^^^^^^^^^^^^
  504. .. signature::
  505. file(REAL_PATH <path> <out-var> [BASE_DIRECTORY <dir>] [EXPAND_TILDE])
  506. .. versionadded:: 3.19
  507. Compute the absolute path to an existing file or directory with symlinks
  508. resolved. The options are:
  509. ``BASE_DIRECTORY <dir>``
  510. If the provided ``<path>`` is a relative path, it is evaluated relative
  511. to the given base directory ``<dir>``. If no base directory is provided,
  512. the default base directory will be :variable:`CMAKE_CURRENT_SOURCE_DIR`.
  513. ``EXPAND_TILDE``
  514. .. versionadded:: 3.21
  515. If the ``<path>`` is ``~`` or starts with ``~/``, the ``~`` is replaced
  516. by the user's home directory. The path to the home directory is obtained
  517. from environment variables. On Windows, the ``USERPROFILE`` environment
  518. variable is used, falling back to the ``HOME`` environment variable
  519. if ``USERPROFILE`` is not defined. On all other platforms, only ``HOME``
  520. is used.
  521. .. versionchanged:: 3.28
  522. All symlinks are resolved before collapsing ``../`` components.
  523. See policy :policy:`CMP0152`.
  524. .. signature::
  525. file(RELATIVE_PATH <variable> <directory> <file>)
  526. Compute the relative path from a ``<directory>`` to a ``<file>`` and
  527. store it in the ``<variable>``.
  528. .. signature::
  529. file(TO_CMAKE_PATH "<path>" <variable>)
  530. file(TO_NATIVE_PATH "<path>" <variable>)
  531. The ``TO_CMAKE_PATH`` mode converts a native ``<path>`` into a cmake-style
  532. path with forward-slashes (``/``). The input can be a single path or a
  533. system search path like ``$ENV{PATH}``. A search path will be converted
  534. to a cmake-style list separated by ``;`` characters.
  535. The ``TO_NATIVE_PATH`` mode converts a cmake-style ``<path>`` into a native
  536. path with platform-specific slashes (``\`` on Windows hosts and ``/``
  537. elsewhere).
  538. Always use double quotes around the ``<path>`` to be sure it is treated
  539. as a single argument to this command.
  540. Transfer
  541. ^^^^^^^^
  542. .. signature::
  543. file(DOWNLOAD <url> [<file>] <options>...)
  544. file(UPLOAD <file> <url> <options>...)
  545. The ``DOWNLOAD`` subcommand downloads the given ``<url>`` to a local
  546. ``<file>``. The ``UPLOAD`` mode uploads a local ``<file>`` to a given
  547. ``<url>``.
  548. .. versionadded:: 3.19
  549. If ``<file>`` is not specified for ``file(DOWNLOAD)``, the file is not
  550. saved. This can be useful if you want to know if a file can be downloaded
  551. (for example, to check that it exists) without actually saving it anywhere.
  552. Options to both ``DOWNLOAD`` and ``UPLOAD`` are:
  553. ``INACTIVITY_TIMEOUT <seconds>``
  554. Terminate the operation after a period of inactivity.
  555. ``LOG <variable>``
  556. Store a human-readable log of the operation in a variable.
  557. ``SHOW_PROGRESS``
  558. Print progress information as status messages until the operation is
  559. complete.
  560. ``STATUS <variable>``
  561. Store the resulting status of the operation in a variable.
  562. The status is a ``;`` separated list of length 2.
  563. The first element is the numeric return value for the operation,
  564. and the second element is a string value for the error.
  565. A ``0`` numeric error means no error in the operation.
  566. ``TIMEOUT <seconds>``
  567. Terminate the operation after a given total time has elapsed.
  568. ``USERPWD <username>:<password>``
  569. .. versionadded:: 3.7
  570. Set username and password for operation.
  571. ``HTTPHEADER <HTTP-header>``
  572. .. versionadded:: 3.7
  573. HTTP header for ``DOWNLOAD`` and ``UPLOAD`` operations. ``HTTPHEADER``
  574. can be repeated for multiple options:
  575. .. code-block:: cmake
  576. file(DOWNLOAD <url>
  577. HTTPHEADER "Authorization: Bearer <auth-token>"
  578. HTTPHEADER "UserAgent: Mozilla/5.0")
  579. ``NETRC <level>``
  580. .. versionadded:: 3.11
  581. Specify whether the .netrc file is to be used for operation. If this
  582. option is not specified, the value of the :variable:`CMAKE_NETRC`
  583. variable will be used instead.
  584. Valid levels are:
  585. ``IGNORED``
  586. The .netrc file is ignored.
  587. This is the default.
  588. ``OPTIONAL``
  589. The .netrc file is optional, and information in the URL is preferred.
  590. The file will be scanned to find which ever information is not
  591. specified in the URL.
  592. ``REQUIRED``
  593. The .netrc file is required, and information in the URL is ignored.
  594. ``NETRC_FILE <file>``
  595. .. versionadded:: 3.11
  596. Specify an alternative .netrc file to the one in your home directory,
  597. if the ``NETRC`` level is ``OPTIONAL`` or ``REQUIRED``. If this option
  598. is not specified, the value of the :variable:`CMAKE_NETRC_FILE` variable
  599. will be used instead.
  600. ``TLS_VERSION <min>``
  601. .. versionadded:: 3.30
  602. Specify minimum TLS version for ``https://`` URLs.
  603. If this option is not specified, the value of the
  604. :variable:`CMAKE_TLS_VERSION` variable or :envvar:`CMAKE_TLS_VERSION`
  605. environment variable will be used instead.
  606. See :variable:`CMAKE_TLS_VERSION` for allowed values.
  607. .. versionchanged:: 3.31
  608. The default is TLS 1.2.
  609. Previously, no minimum version was enforced by default.
  610. ``TLS_VERIFY <ON|OFF>``
  611. Specify whether to verify the server certificate for ``https://`` URLs.
  612. If this option is not specified, the value of the
  613. :variable:`CMAKE_TLS_VERIFY` variable or :envvar:`CMAKE_TLS_VERIFY`
  614. environment variable will be used instead.
  615. If neither is set, the default is *on*.
  616. .. versionchanged:: 3.31
  617. The default is on. Previously, the default was off.
  618. Users may set the :envvar:`CMAKE_TLS_VERIFY` environment
  619. variable to ``0`` to restore the old default.
  620. .. versionadded:: 3.18
  621. Added support to ``file(UPLOAD)``.
  622. ``TLS_CAINFO <file>``
  623. Specify a custom Certificate Authority file for ``https://`` URLs.
  624. If this option is not specified, the value of the
  625. :variable:`CMAKE_TLS_CAINFO` variable will be used instead.
  626. .. versionadded:: 3.18
  627. Added support to ``file(UPLOAD)``.
  628. For ``https://`` URLs CMake must be built with SSL/TLS support.
  629. Additional options to ``DOWNLOAD`` are:
  630. ``EXPECTED_HASH <algorithm>=<value>``
  631. Verify that the downloaded content hash matches the expected value, where
  632. ``<algorithm>`` is one of the algorithms supported by :cref:`<HASH>`.
  633. If the file already exists and matches the hash, the download is skipped.
  634. If the file already exists and does not match the hash, the file is
  635. downloaded again. If after download the file does not match the hash, the
  636. operation fails with an error. It is an error to specify this option if
  637. ``DOWNLOAD`` is not given a ``<file>``.
  638. ``EXPECTED_MD5 <value>``
  639. Historical short-hand for ``EXPECTED_HASH MD5=<value>``. It is an error
  640. to specify this if ``DOWNLOAD`` is not given a ``<file>``.
  641. ``RANGE_START <value>``
  642. .. versionadded:: 3.24
  643. Offset of the start of the range in file in bytes. Could be omitted to
  644. download up to the specified ``RANGE_END``.
  645. ``RANGE_END <value>``
  646. .. versionadded:: 3.24
  647. Offset of the end of the range in file in bytes. Could be omitted to
  648. download everything from the specified ``RANGE_START`` to the end of
  649. file.
  650. Locking
  651. ^^^^^^^
  652. .. signature::
  653. file(LOCK <path> [DIRECTORY] [RELEASE]
  654. [GUARD <FUNCTION|FILE|PROCESS>]
  655. [RESULT_VARIABLE <variable>]
  656. [TIMEOUT <seconds>])
  657. .. versionadded:: 3.2
  658. Lock a file specified by ``<path>`` if no ``DIRECTORY`` option present and
  659. file ``<path>/cmake.lock`` otherwise. The file will be locked for the scope
  660. defined by the ``GUARD`` option (default value is ``PROCESS``). The
  661. ``RELEASE`` option can be used to unlock the file explicitly. If the
  662. ``TIMEOUT`` option is not specified, CMake will wait until the lock succeeds
  663. or until a fatal error occurs. If ``TIMEOUT`` is set to ``0``, locking will
  664. be tried once and the result will be reported immediately. If ``TIMEOUT``
  665. is not ``0``, CMake will try to lock the file for the period specified by
  666. the ``TIMEOUT <seconds>`` value. Any errors will be interpreted as fatal if
  667. there is no ``RESULT_VARIABLE`` option. Otherwise, the result will be stored
  668. in ``<variable>`` and will be ``0`` on success or an error message on
  669. failure.
  670. Note that lock is advisory; there is no guarantee that other processes will
  671. respect this lock, i.e. lock synchronize two or more CMake instances sharing
  672. some modifiable resources. Similar logic applies to the ``DIRECTORY`` option;
  673. locking a parent directory doesn't prevent other ``LOCK`` commands from
  674. locking any child directory or file.
  675. Trying to lock the same file twice is not allowed. Any intermediate
  676. directories and the file itself will be created if they not exist. The
  677. ``GUARD`` and ``TIMEOUT`` options are ignored on the ``RELEASE`` operation.
  678. Archiving
  679. ^^^^^^^^^
  680. .. signature::
  681. file(ARCHIVE_CREATE OUTPUT <archive>
  682. PATHS <paths>...
  683. [FORMAT <format>]
  684. [COMPRESSION <compression>
  685. [COMPRESSION_LEVEL <compression-level>]]
  686. [MTIME <mtime>]
  687. [WORKING_DIRECTORY <dir>]
  688. [VERBOSE])
  689. :target: ARCHIVE_CREATE
  690. :break: verbatim
  691. .. versionadded:: 3.18
  692. Creates the specified ``<archive>`` file with the files and directories
  693. listed in ``<paths>``. Note that ``<paths>`` must list actual files or
  694. directories; wildcards are not supported.
  695. The options are:
  696. ``FORMAT <format>``
  697. Specify the archive format. Supported values for ``<format>`` are
  698. ``7zip``, ``gnutar``, ``pax``, ``paxr``, ``raw`` and ``zip``.
  699. If ``FORMAT`` is not given, the default format is ``paxr``.
  700. ``COMPRESSION <compression>``
  701. Some archive formats allow the type of compression to be specified.
  702. The ``7zip`` and ``zip`` archive formats already imply a specific type of
  703. compression. The other formats use no compression by default, but can be
  704. directed to do so with the ``COMPRESSION`` option. Valid values for
  705. ``<compression>`` are ``None``, ``BZip2``, ``GZip``, ``XZ``, and ``Zstd``.
  706. .. note::
  707. With ``FORMAT`` set to ``raw``, only one file will be compressed
  708. with the compression type specified by ``COMPRESSION``.
  709. ``COMPRESSION_LEVEL <compression-level>``
  710. .. versionadded:: 3.19
  711. The compression level can be specified with the ``COMPRESSION_LEVEL``
  712. option. The ``<compression-level>`` should be between 0-9, with the
  713. default being 0. The ``COMPRESSION`` option must be present when
  714. ``COMPRESSION_LEVEL`` is given.
  715. .. versionadded:: 3.26
  716. The ``<compression-level>`` of the ``Zstd`` algorithm can be set
  717. between 0-19.
  718. ``MTIME <mtime>``
  719. Specify the modification time recorded in tarball entries.
  720. ``WORKING_DIRECTORY <dir>``
  721. .. versionadded:: 3.31
  722. Specify the directory in which the archive creation operation will
  723. be executed. Paths in the ``<paths>`` argument can be relative to
  724. this directory. If this option is not provided, the current working
  725. directory will be used by default.
  726. ``VERBOSE``
  727. Enable verbose output from the archive operation.
  728. .. signature::
  729. file(ARCHIVE_EXTRACT
  730. INPUT <archive>
  731. [DESTINATION <dir>]
  732. [PATTERNS <pattern>...]
  733. [LIST_ONLY]
  734. [VERBOSE]
  735. [TOUCH])
  736. :target: ARCHIVE_EXTRACT
  737. .. versionadded:: 3.18
  738. Extracts or lists the content of the specified ``<archive>``.
  739. The options are:
  740. ``DESTINATION <dir>``
  741. Specify the directory under which the content of the archive will be
  742. extracted. If the directory does not exist, it will be created.
  743. If ``DESTINATION`` is not given, the current binary directory will
  744. be used.
  745. ``PATTERNS <pattern>...``
  746. Extract/list only files and directories that match one of the given
  747. patterns. Wildcards are supported. If the ``PATTERNS`` option is
  748. not given, the entire archive will be listed or extracted.
  749. ``LIST_ONLY``
  750. List the files in the archive rather than extract them.
  751. ``TOUCH``
  752. .. versionadded:: 3.24
  753. Give extracted files a current local timestamp instead of extracting
  754. file timestamps from the archive.
  755. ``VERBOSE``
  756. Enable verbose output from the extraction operation.
  757. .. note::
  758. The working directory for this subcommand is the ``DESTINATION`` directory
  759. (provided or computed) except when ``LIST_ONLY`` is specified. Therefore,
  760. outside of script mode, it may be best to provide absolute paths to
  761. ``INPUT`` archives as they are unlikely to be extracted where a relative
  762. path works.
  763. Handling Runtime Binaries
  764. ^^^^^^^^^^^^^^^^^^^^^^^^^
  765. .. signature::
  766. file(GET_RUNTIME_DEPENDENCIES [...])
  767. .. versionadded:: 3.16
  768. Recursively get the list of libraries depended on by the given files:
  769. .. code-block:: cmake
  770. file(GET_RUNTIME_DEPENDENCIES
  771. [RESOLVED_DEPENDENCIES_VAR <deps_var>]
  772. [UNRESOLVED_DEPENDENCIES_VAR <unresolved_deps_var>]
  773. [CONFLICTING_DEPENDENCIES_PREFIX <conflicting_deps_prefix>]
  774. [EXECUTABLES <executable_files>...]
  775. [LIBRARIES <library_files>...]
  776. [MODULES <module_files>...]
  777. [DIRECTORIES <directories>...]
  778. [BUNDLE_EXECUTABLE <bundle_executable_file>]
  779. [PRE_INCLUDE_REGEXES <regexes>...]
  780. [PRE_EXCLUDE_REGEXES <regexes>...]
  781. [POST_INCLUDE_REGEXES <regexes>...]
  782. [POST_EXCLUDE_REGEXES <regexes>...]
  783. [POST_INCLUDE_FILES <files>...]
  784. [POST_EXCLUDE_FILES <files>...]
  785. )
  786. Please note that this sub-command is not intended to be used in project mode.
  787. It is intended for use at install time, either from code generated by the
  788. :command:`install(RUNTIME_DEPENDENCY_SET)` command, or from code provided by
  789. the project via :command:`install(CODE)` or :command:`install(SCRIPT)`.
  790. For example:
  791. .. code-block:: cmake
  792. install(CODE [[
  793. file(GET_RUNTIME_DEPENDENCIES
  794. # ...
  795. )
  796. ]])
  797. The arguments are as follows:
  798. ``RESOLVED_DEPENDENCIES_VAR <deps_var>``
  799. Name of the variable in which to store the list of resolved dependencies.
  800. ``UNRESOLVED_DEPENDENCIES_VAR <unresolved_deps_var>``
  801. Name of the variable in which to store the list of unresolved
  802. dependencies. If this variable is not specified, and there are any
  803. unresolved dependencies, an error is issued.
  804. ``CONFLICTING_DEPENDENCIES_PREFIX <conflicting_deps_prefix>``
  805. Variable prefix in which to store conflicting dependency information.
  806. Dependencies are conflicting if two files with the same name are found in
  807. two different directories. The list of filenames that conflict are stored
  808. in ``<conflicting_deps_prefix>_FILENAMES``. For each filename, the list
  809. of paths that were found for that filename are stored in
  810. ``<conflicting_deps_prefix>_<filename>``.
  811. ``EXECUTABLES <executable_files>...``
  812. List of executable files to read for dependencies. These are executables
  813. that are typically created with :command:`add_executable`, but they do
  814. not have to be created by CMake. On Apple platforms, the paths to these
  815. files determine the value of ``@executable_path`` when recursively
  816. resolving the libraries. Specifying any kind of library (``STATIC``,
  817. ``MODULE``, or ``SHARED``) here will result in undefined behavior.
  818. ``LIBRARIES <library_files>...``
  819. List of library files to read for dependencies. These are libraries that
  820. are typically created with :command:`add_library(SHARED)`, but they do
  821. not have to be created by CMake. Specifying ``STATIC`` libraries,
  822. ``MODULE`` libraries, or executables here will result in undefined
  823. behavior.
  824. ``MODULES <module_files>...``
  825. List of loadable module files to read for dependencies. These are modules
  826. that are typically created with :command:`add_library(MODULE)`, but they
  827. do not have to be created by CMake. They are typically used by calling
  828. ``dlopen()`` at runtime rather than linked at link time with ``ld -l``.
  829. Specifying ``STATIC`` libraries, ``SHARED`` libraries, or executables
  830. here will result in undefined behavior.
  831. ``DIRECTORIES <directories>...``
  832. List of additional directories to search for dependencies. On Linux
  833. platforms, these directories are searched if the dependency is not found
  834. in any of the other usual paths. If it is found in such a directory, a
  835. warning is issued, because it means that the file is incomplete (it does
  836. not list all of the directories that contain its dependencies).
  837. On Windows platforms, these directories are searched if the dependency
  838. is not found in any of the other search paths, but no warning is issued,
  839. because searching other paths is a normal part of Windows dependency
  840. resolution. On Apple platforms, this argument has no effect.
  841. ``BUNDLE_EXECUTABLE <bundle_executable_file>``
  842. Executable to treat as the "bundle executable" when resolving libraries.
  843. On Apple platforms, this argument determines the value of
  844. ``@executable_path`` when recursively resolving libraries for
  845. ``LIBRARIES`` and ``MODULES`` files. It has no effect on ``EXECUTABLES``
  846. files. On other platforms, it has no effect. This is typically (but not
  847. always) one of the executables in the ``EXECUTABLES`` argument which
  848. designates the "main" executable of the package.
  849. The following arguments specify filters for including or excluding libraries
  850. to be resolved. See below for a full description of how they work.
  851. Directory separators in file paths may be matched using forward
  852. slashes unless policy :policy:`CMP0207` is not set to ``NEW``.
  853. ``PRE_INCLUDE_REGEXES <regexes>...``
  854. List of pre-include regexes through which to filter the names of
  855. not-yet-resolved dependencies.
  856. ``PRE_EXCLUDE_REGEXES <regexes>...``
  857. List of pre-exclude regexes through which to filter the names of
  858. not-yet-resolved dependencies.
  859. ``POST_INCLUDE_REGEXES <regexes>...``
  860. List of post-include regexes through which to filter the names of
  861. resolved dependencies.
  862. ``POST_EXCLUDE_REGEXES <regexes>...``
  863. List of post-exclude regexes through which to filter the names of
  864. resolved dependencies.
  865. ``POST_INCLUDE_FILES <files>...``
  866. .. versionadded:: 3.21
  867. List of post-include filenames through which to filter the names of
  868. resolved dependencies. Symlinks are resolved when attempting to match
  869. these filenames.
  870. ``POST_EXCLUDE_FILES <files>...``
  871. .. versionadded:: 3.21
  872. List of post-exclude filenames through which to filter the names of
  873. resolved dependencies. Symlinks are resolved when attempting to match
  874. these filenames.
  875. These arguments can be used to exclude unwanted system libraries when
  876. resolving the dependencies, or to include libraries from a specific
  877. directory. The filtering works as follows:
  878. 1. If the not-yet-resolved dependency matches any of the
  879. ``PRE_INCLUDE_REGEXES``, steps 2 and 3 are skipped, and the dependency
  880. resolution proceeds to step 4.
  881. 2. If the not-yet-resolved dependency matches any of the
  882. ``PRE_EXCLUDE_REGEXES``, dependency resolution stops for that dependency.
  883. 3. Otherwise, dependency resolution proceeds.
  884. 4. ``file(GET_RUNTIME_DEPENDENCIES)`` searches for the dependency according
  885. to the linking rules of the platform (see below).
  886. 5. If the dependency is found, and its full path matches one of the
  887. ``POST_INCLUDE_REGEXES`` or ``POST_INCLUDE_FILES``, the full path is added
  888. to the resolved dependencies, and ``file(GET_RUNTIME_DEPENDENCIES)``
  889. recursively resolves that library's own dependencies. Otherwise, resolution
  890. proceeds to step 6.
  891. 6. If the dependency is found, but its full path matches one of the
  892. ``POST_EXCLUDE_REGEXES`` or ``POST_EXCLUDE_FILES``, it is not added to the
  893. resolved dependencies, and dependency resolution stops for that dependency.
  894. 7. If the dependency is found, and its full path does not match either
  895. ``POST_INCLUDE_REGEXES``, ``POST_INCLUDE_FILES``, ``POST_EXCLUDE_REGEXES``,
  896. or ``POST_EXCLUDE_FILES``, the full path is added to the resolved
  897. dependencies, and ``file(GET_RUNTIME_DEPENDENCIES)`` recursively resolves
  898. that library's own dependencies.
  899. Different platforms have different rules for how dependencies are resolved.
  900. These specifics are described here.
  901. On Linux platforms, library resolution works as follows:
  902. 1. If the depending file does not have any ``RUNPATH`` entries, and the
  903. library exists in one of the depending file's ``RPATH`` entries, or its
  904. parents', in that order, the dependency is resolved to that file.
  905. 2. Otherwise, if the depending file has any ``RUNPATH`` entries, and the
  906. library exists in one of those entries, the dependency is resolved to that
  907. file.
  908. 3. Otherwise, if the library exists in one of the directories listed by
  909. ``ldconfig``, the dependency is resolved to that file.
  910. 4. Otherwise, if the library exists in one of the ``DIRECTORIES`` entries,
  911. the dependency is resolved to that file. In this case, a warning is
  912. issued, because finding a file in one of the ``DIRECTORIES`` means that
  913. the depending file is not complete (it does not list all the directories
  914. from which it pulls dependencies).
  915. 5. Otherwise, the dependency is unresolved.
  916. .. versionchanged:: 3.31
  917. Resolution of each encountered library file name occurs at most once
  918. while processing a given root ELF file (executable or shared object).
  919. If a library file name is encountered again in the dependency tree,
  920. the original resolution is assumed. This behavior more closely matches
  921. the dynamic loader's behavior on Linux.
  922. On Windows platforms, library resolution works as follows:
  923. 1. DLL dependency names are converted to lowercase for matching filters.
  924. Windows DLL names are case-insensitive, and some linkers mangle the
  925. case of the DLL dependency names. However, this makes it more difficult
  926. for ``PRE_INCLUDE_REGEXES``, ``PRE_EXCLUDE_REGEXES``,
  927. ``POST_INCLUDE_REGEXES``, and ``POST_EXCLUDE_REGEXES`` to properly
  928. filter DLL names - every regex would have to check for both uppercase
  929. and lowercase letters. For example:
  930. .. code-block:: cmake
  931. file(GET_RUNTIME_DEPENDENCIES
  932. # ...
  933. PRE_INCLUDE_REGEXES "^[Mm][Yy][Ll][Ii][Bb][Rr][Aa][Rr][Yy]\\.[Dd][Ll][Ll]$"
  934. )
  935. Converting the DLL name to lowercase allows the regexes to only match
  936. lowercase names, thus simplifying the regex. For example:
  937. .. code-block:: cmake
  938. file(GET_RUNTIME_DEPENDENCIES
  939. # ...
  940. PRE_INCLUDE_REGEXES "^mylibrary\\.dll$"
  941. )
  942. This regex will match ``mylibrary.dll`` regardless of how it is cased,
  943. either on disk or in the depending file. (For example, it will match
  944. ``mylibrary.dll``, ``MyLibrary.dll``, and ``MYLIBRARY.DLL``.)
  945. .. versionchanged:: 3.27
  946. The conversion to lowercase only applies while matching filters.
  947. Results reported after filtering case-preserve each DLL name as it is
  948. found on disk, if resolved, and otherwise as it is referenced by the
  949. dependent binary.
  950. Prior to CMake 3.27, the results were reported with lowercase DLL
  951. file names, but the directory portion retained its casing.
  952. 2. (**Not yet implemented**) If the depending file is a Windows Store app,
  953. and the dependency is listed as a dependency in the application's package
  954. manifest, the dependency is resolved to that file.
  955. 3. Otherwise, if the library exists in the same directory as the depending
  956. file, the dependency is resolved to that file.
  957. 4. Otherwise, if the library exists in either the operating system's
  958. ``system32`` directory or the ``Windows`` directory, in that order, the
  959. dependency is resolved to that file.
  960. 5. Otherwise, if the library exists in one of the directories specified by
  961. ``DIRECTORIES``, in the order they are listed, the dependency is resolved
  962. to that file. In this case, a warning is not issued, because searching
  963. other directories is a normal part of Windows library resolution.
  964. 6. Otherwise, the dependency is unresolved.
  965. On Apple platforms, library resolution works as follows:
  966. 1. If the dependency starts with ``@executable_path/``, and an
  967. ``EXECUTABLES`` argument is in the process of being resolved, and
  968. replacing ``@executable_path/`` with the directory of the executable
  969. yields an existing file, the dependency is resolved to that file.
  970. 2. Otherwise, if the dependency starts with ``@executable_path/``, and there
  971. is a ``BUNDLE_EXECUTABLE`` argument, and replacing ``@executable_path/``
  972. with the directory of the bundle executable yields an existing file, the
  973. dependency is resolved to that file.
  974. 3. Otherwise, if the dependency starts with ``@loader_path/``, and replacing
  975. ``@loader_path/`` with the directory of the depending file yields an
  976. existing file, the dependency is resolved to that file.
  977. 4. Otherwise, if the dependency starts with ``@rpath/``, and replacing
  978. ``@rpath/`` with one of the ``RPATH`` entries of the depending file
  979. yields an existing file, the dependency is resolved to that file.
  980. Note that ``RPATH`` entries that start with ``@executable_path/`` or
  981. ``@loader_path/`` also have these items replaced with the appropriate
  982. path.
  983. 5. Otherwise, if the dependency is an absolute file that exists,
  984. the dependency is resolved to that file.
  985. 6. Otherwise, the dependency is unresolved.
  986. This function accepts several variables that determine which tool is used for
  987. dependency resolution:
  988. .. variable:: CMAKE_GET_RUNTIME_DEPENDENCIES_PLATFORM
  989. Determines which operating system and executable format the files are built
  990. for. This could be one of several values:
  991. * ``linux+elf``
  992. * ``windows+pe``
  993. * ``macos+macho``
  994. If this variable is not specified, it is determined automatically by system
  995. introspection.
  996. .. variable:: CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL
  997. Determines the tool to use for dependency resolution. It could be one of
  998. several values, depending on the value of
  999. :variable:`CMAKE_GET_RUNTIME_DEPENDENCIES_PLATFORM`:
  1000. ================================================= =============================================
  1001. ``CMAKE_GET_RUNTIME_DEPENDENCIES_PLATFORM`` ``CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL``
  1002. ================================================= =============================================
  1003. ``linux+elf`` ``objdump``
  1004. ``windows+pe`` ``objdump`` or ``dumpbin``
  1005. ``macos+macho`` ``otool``
  1006. ================================================= =============================================
  1007. If this variable is not specified, it is determined automatically by system
  1008. introspection.
  1009. .. variable:: CMAKE_GET_RUNTIME_DEPENDENCIES_COMMAND
  1010. Determines the path to the tool to use for dependency resolution. This is
  1011. the actual path to ``objdump``, ``dumpbin``, or ``otool``.
  1012. If this variable is not specified, it is determined by the value of
  1013. :variable:`CMAKE_OBJDUMP` variable if set, else by system introspection.
  1014. .. versionadded:: 3.18
  1015. Uses :variable:`CMAKE_OBJDUMP` if set.