file.rst 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  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. If the source is a directory, the destination
  465. directory will be created if it does not exist, but no files will be copied
  466. the from source one. It can be useful for handling situations such as
  467. ``<original>`` and ``<linkname>`` being on different drives or mount points,
  468. which would make them unable to support a hard link.
  469. .. signature::
  470. file(CHMOD <files>... <directories>...
  471. [PERMISSIONS <permissions>...]
  472. [FILE_PERMISSIONS <permissions>...]
  473. [DIRECTORY_PERMISSIONS <permissions>...])
  474. .. versionadded:: 3.19
  475. Set the permissions for the ``<files>...`` and ``<directories>...``
  476. specified. Valid permissions are ``OWNER_READ``, ``OWNER_WRITE``,
  477. ``OWNER_EXECUTE``, ``GROUP_READ``, ``GROUP_WRITE``, ``GROUP_EXECUTE``,
  478. ``WORLD_READ``, ``WORLD_WRITE``, ``WORLD_EXECUTE``, ``SETUID``, ``SETGID``.
  479. Valid combination of keywords are:
  480. ``PERMISSIONS``
  481. All items are changed.
  482. ``FILE_PERMISSIONS``
  483. Only files are changed.
  484. ``DIRECTORY_PERMISSIONS``
  485. Only directories are changed.
  486. ``PERMISSIONS`` and ``FILE_PERMISSIONS``
  487. ``FILE_PERMISSIONS`` overrides ``PERMISSIONS`` for files.
  488. ``PERMISSIONS`` and ``DIRECTORY_PERMISSIONS``
  489. ``DIRECTORY_PERMISSIONS`` overrides ``PERMISSIONS`` for directories.
  490. ``FILE_PERMISSIONS`` and ``DIRECTORY_PERMISSIONS``
  491. Use ``FILE_PERMISSIONS`` for files and ``DIRECTORY_PERMISSIONS`` for
  492. directories.
  493. .. signature::
  494. file(CHMOD_RECURSE <files>... <directories>...
  495. [PERMISSIONS <permissions>...]
  496. [FILE_PERMISSIONS <permissions>...]
  497. [DIRECTORY_PERMISSIONS <permissions>...])
  498. .. versionadded:: 3.19
  499. Same as :cref:`CHMOD`, but change the permissions of files and directories
  500. present in the ``<directories>...`` recursively.
  501. Path Conversion
  502. ^^^^^^^^^^^^^^^
  503. .. signature::
  504. file(REAL_PATH <path> <out-var> [BASE_DIRECTORY <dir>] [EXPAND_TILDE])
  505. .. versionadded:: 3.19
  506. Compute the absolute path to an existing file or directory with symlinks
  507. resolved. The options are:
  508. ``BASE_DIRECTORY <dir>``
  509. If the provided ``<path>`` is a relative path, it is evaluated relative
  510. to the given base directory ``<dir>``. If no base directory is provided,
  511. the default base directory will be :variable:`CMAKE_CURRENT_SOURCE_DIR`.
  512. ``EXPAND_TILDE``
  513. .. versionadded:: 3.21
  514. If the ``<path>`` is ``~`` or starts with ``~/``, the ``~`` is replaced
  515. by the user's home directory. The path to the home directory is obtained
  516. from environment variables. On Windows, the ``USERPROFILE`` environment
  517. variable is used, falling back to the ``HOME`` environment variable
  518. if ``USERPROFILE`` is not defined. On all other platforms, only ``HOME``
  519. is used.
  520. .. versionchanged:: 3.28
  521. All symlinks are resolved before collapsing ``../`` components.
  522. See policy :policy:`CMP0152`.
  523. .. signature::
  524. file(RELATIVE_PATH <variable> <directory> <file>)
  525. Compute the relative path from a ``<directory>`` to a ``<file>`` and
  526. store it in the ``<variable>``.
  527. .. signature::
  528. file(TO_CMAKE_PATH "<path>" <variable>)
  529. file(TO_NATIVE_PATH "<path>" <variable>)
  530. The ``TO_CMAKE_PATH`` mode converts a native ``<path>`` into a cmake-style
  531. path with forward-slashes (``/``). The input can be a single path or a
  532. system search path like ``$ENV{PATH}``. A search path will be converted
  533. to a cmake-style list separated by ``;`` characters.
  534. The ``TO_NATIVE_PATH`` mode converts a cmake-style ``<path>`` into a native
  535. path with platform-specific slashes (``\`` on Windows hosts and ``/``
  536. elsewhere).
  537. Always use double quotes around the ``<path>`` to be sure it is treated
  538. as a single argument to this command.
  539. Transfer
  540. ^^^^^^^^
  541. .. signature::
  542. file(DOWNLOAD <url> [<file>] <options>...)
  543. file(UPLOAD <file> <url> <options>...)
  544. The ``DOWNLOAD`` subcommand downloads the given ``<url>`` to a local
  545. ``<file>``. The ``UPLOAD`` mode uploads a local ``<file>`` to a given
  546. ``<url>``.
  547. .. versionadded:: 3.19
  548. If ``<file>`` is not specified for ``file(DOWNLOAD)``, the file is not
  549. saved. This can be useful if you want to know if a file can be downloaded
  550. (for example, to check that it exists) without actually saving it anywhere.
  551. Options to both ``DOWNLOAD`` and ``UPLOAD`` are:
  552. ``INACTIVITY_TIMEOUT <seconds>``
  553. Terminate the operation after a period of inactivity.
  554. ``LOG <variable>``
  555. Store a human-readable log of the operation in a variable.
  556. ``SHOW_PROGRESS``
  557. Print progress information as status messages until the operation is
  558. complete.
  559. ``STATUS <variable>``
  560. Store the resulting status of the operation in a variable.
  561. The status is a ``;`` separated list of length 2.
  562. The first element is the numeric return value for the operation,
  563. and the second element is a string value for the error.
  564. A ``0`` numeric error means no error in the operation.
  565. ``TIMEOUT <seconds>``
  566. Terminate the operation after a given total time has elapsed.
  567. ``USERPWD <username>:<password>``
  568. .. versionadded:: 3.7
  569. Set username and password for operation.
  570. ``HTTPHEADER <HTTP-header>``
  571. .. versionadded:: 3.7
  572. HTTP header for ``DOWNLOAD`` and ``UPLOAD`` operations. ``HTTPHEADER``
  573. can be repeated for multiple options:
  574. .. code-block:: cmake
  575. file(DOWNLOAD <url>
  576. HTTPHEADER "Authorization: Bearer <auth-token>"
  577. HTTPHEADER "UserAgent: Mozilla/5.0")
  578. ``NETRC <level>``
  579. .. versionadded:: 3.11
  580. Specify whether the .netrc file is to be used for operation. If this
  581. option is not specified, the value of the :variable:`CMAKE_NETRC`
  582. variable will be used instead.
  583. Valid levels are:
  584. ``IGNORED``
  585. The .netrc file is ignored.
  586. This is the default.
  587. ``OPTIONAL``
  588. The .netrc file is optional, and information in the URL is preferred.
  589. The file will be scanned to find which ever information is not
  590. specified in the URL.
  591. ``REQUIRED``
  592. The .netrc file is required, and information in the URL is ignored.
  593. ``NETRC_FILE <file>``
  594. .. versionadded:: 3.11
  595. Specify an alternative .netrc file to the one in your home directory,
  596. if the ``NETRC`` level is ``OPTIONAL`` or ``REQUIRED``. If this option
  597. is not specified, the value of the :variable:`CMAKE_NETRC_FILE` variable
  598. will be used instead.
  599. ``TLS_VERSION <min>``
  600. .. versionadded:: 3.30
  601. Specify minimum TLS version for ``https://`` URLs.
  602. If this option is not specified, the value of the
  603. :variable:`CMAKE_TLS_VERSION` variable or :envvar:`CMAKE_TLS_VERSION`
  604. environment variable will be used instead.
  605. See :variable:`CMAKE_TLS_VERSION` for allowed values.
  606. .. versionchanged:: 3.31
  607. The default is TLS 1.2.
  608. Previously, no minimum version was enforced by default.
  609. ``TLS_VERIFY <ON|OFF>``
  610. Specify whether to verify the server certificate for ``https://`` URLs.
  611. If this option is not specified, the value of the
  612. :variable:`CMAKE_TLS_VERIFY` variable or :envvar:`CMAKE_TLS_VERIFY`
  613. environment variable will be used instead.
  614. If neither is set, the default is *on*.
  615. .. versionchanged:: 3.31
  616. The default is on. Previously, the default was off.
  617. Users may set the :envvar:`CMAKE_TLS_VERIFY` environment
  618. variable to ``0`` to restore the old default.
  619. .. versionadded:: 3.18
  620. Added support to ``file(UPLOAD)``.
  621. ``TLS_CAINFO <file>``
  622. Specify a custom Certificate Authority file for ``https://`` URLs.
  623. If this option is not specified, the value of the
  624. :variable:`CMAKE_TLS_CAINFO` variable will be used instead.
  625. .. versionadded:: 3.18
  626. Added support to ``file(UPLOAD)``.
  627. For ``https://`` URLs CMake must be built with SSL/TLS support.
  628. Additional options to ``DOWNLOAD`` are:
  629. ``EXPECTED_HASH <algorithm>=<value>``
  630. Verify that the downloaded content hash matches the expected value, where
  631. ``<algorithm>`` is one of the algorithms supported by :cref:`<HASH>`.
  632. If the file already exists and matches the hash, the download is skipped.
  633. If the file already exists and does not match the hash, the file is
  634. downloaded again. If after download the file does not match the hash, the
  635. operation fails with an error. It is an error to specify this option if
  636. ``DOWNLOAD`` is not given a ``<file>``.
  637. ``EXPECTED_MD5 <value>``
  638. Historical short-hand for ``EXPECTED_HASH MD5=<value>``. It is an error
  639. to specify this if ``DOWNLOAD`` is not given a ``<file>``.
  640. ``RANGE_START <value>``
  641. .. versionadded:: 3.24
  642. Offset of the start of the range in file in bytes. Could be omitted to
  643. download up to the specified ``RANGE_END``.
  644. ``RANGE_END <value>``
  645. .. versionadded:: 3.24
  646. Offset of the end of the range in file in bytes. Could be omitted to
  647. download everything from the specified ``RANGE_START`` to the end of
  648. file.
  649. Locking
  650. ^^^^^^^
  651. .. signature::
  652. file(LOCK <path> [DIRECTORY] [RELEASE]
  653. [GUARD <FUNCTION|FILE|PROCESS>]
  654. [RESULT_VARIABLE <variable>]
  655. [TIMEOUT <seconds>])
  656. .. versionadded:: 3.2
  657. Lock a file specified by ``<path>`` if no ``DIRECTORY`` option present and
  658. file ``<path>/cmake.lock`` otherwise. The file will be locked for the scope
  659. defined by the ``GUARD`` option (default value is ``PROCESS``). The
  660. ``RELEASE`` option can be used to unlock the file explicitly. If the
  661. ``TIMEOUT`` option is not specified, CMake will wait until the lock succeeds
  662. or until a fatal error occurs. If ``TIMEOUT`` is set to ``0``, locking will
  663. be tried once and the result will be reported immediately. If ``TIMEOUT``
  664. is not ``0``, CMake will try to lock the file for the period specified by
  665. the ``TIMEOUT <seconds>`` value. Any errors will be interpreted as fatal if
  666. there is no ``RESULT_VARIABLE`` option. Otherwise, the result will be stored
  667. in ``<variable>`` and will be ``0`` on success or an error message on
  668. failure.
  669. Note that lock is advisory; there is no guarantee that other processes will
  670. respect this lock, i.e. lock synchronize two or more CMake instances sharing
  671. some modifiable resources. Similar logic applies to the ``DIRECTORY`` option;
  672. locking a parent directory doesn't prevent other ``LOCK`` commands from
  673. locking any child directory or file.
  674. Trying to lock the same file twice is not allowed. Any intermediate
  675. directories and the file itself will be created if they not exist. The
  676. ``GUARD`` and ``TIMEOUT`` options are ignored on the ``RELEASE`` operation.
  677. Archiving
  678. ^^^^^^^^^
  679. .. signature::
  680. file(ARCHIVE_CREATE OUTPUT <archive>
  681. PATHS <paths>...
  682. [FORMAT <format>]
  683. [COMPRESSION <compression>
  684. [COMPRESSION_LEVEL <compression-level>]]
  685. [MTIME <mtime>]
  686. [WORKING_DIRECTORY <dir>]
  687. [VERBOSE])
  688. :target: ARCHIVE_CREATE
  689. :break: verbatim
  690. .. versionadded:: 3.18
  691. Creates the specified ``<archive>`` file with the files and directories
  692. listed in ``<paths>``. Note that ``<paths>`` must list actual files or
  693. directories; wildcards are not supported.
  694. The options are:
  695. ``FORMAT <format>``
  696. Specify the archive format. Supported values for ``<format>`` are
  697. ``7zip``, ``gnutar``, ``pax``, ``paxr``, ``raw`` and ``zip``.
  698. If ``FORMAT`` is not given, the default format is ``paxr``.
  699. ``COMPRESSION <compression>``
  700. Some archive formats allow the type of compression to be specified.
  701. The ``7zip`` and ``zip`` archive formats already imply a specific type of
  702. compression. The other formats use no compression by default, but can be
  703. directed to do so with the ``COMPRESSION`` option. Valid values for
  704. ``<compression>`` are ``None``, ``BZip2``, ``GZip``, ``XZ``, and ``Zstd``.
  705. .. note::
  706. With ``FORMAT`` set to ``raw``, only one file will be compressed
  707. with the compression type specified by ``COMPRESSION``.
  708. ``COMPRESSION_LEVEL <compression-level>``
  709. .. versionadded:: 3.19
  710. The compression level can be specified with the ``COMPRESSION_LEVEL``
  711. option. The ``<compression-level>`` should be between 0-9, with the
  712. default being 0. The ``COMPRESSION`` option must be present when
  713. ``COMPRESSION_LEVEL`` is given.
  714. .. versionadded:: 3.26
  715. The ``<compression-level>`` of the ``Zstd`` algorithm can be set
  716. between 0-19.
  717. ``MTIME <mtime>``
  718. Specify the modification time recorded in tarball entries.
  719. ``WORKING_DIRECTORY <dir>``
  720. .. versionadded:: 3.31
  721. Specify the directory in which the archive creation operation will
  722. be executed. Paths in the ``<paths>`` argument can be relative to
  723. this directory. If this option is not provided, the current working
  724. directory will be used by default.
  725. ``VERBOSE``
  726. Enable verbose output from the archive operation.
  727. .. signature::
  728. file(ARCHIVE_EXTRACT
  729. INPUT <archive>
  730. [DESTINATION <dir>]
  731. [PATTERNS <pattern>...]
  732. [LIST_ONLY]
  733. [VERBOSE]
  734. [TOUCH])
  735. :target: ARCHIVE_EXTRACT
  736. .. versionadded:: 3.18
  737. Extracts or lists the content of the specified ``<archive>``.
  738. The options are:
  739. ``DESTINATION <dir>``
  740. Specify the directory under which the content of the archive will be
  741. extracted. If the directory does not exist, it will be created.
  742. If ``DESTINATION`` is not given, the current binary directory will
  743. be used.
  744. ``PATTERNS <pattern>...``
  745. Extract/list only files and directories that match one of the given
  746. patterns. Wildcards are supported. If the ``PATTERNS`` option is
  747. not given, the entire archive will be listed or extracted.
  748. ``LIST_ONLY``
  749. List the files in the archive rather than extract them.
  750. ``TOUCH``
  751. .. versionadded:: 3.24
  752. Give extracted files a current local timestamp instead of extracting
  753. file timestamps from the archive.
  754. ``VERBOSE``
  755. Enable verbose output from the extraction operation.
  756. .. note::
  757. The working directory for this subcommand is the ``DESTINATION`` directory
  758. (provided or computed) except when ``LIST_ONLY`` is specified. Therefore,
  759. outside of script mode, it may be best to provide absolute paths to
  760. ``INPUT`` archives as they are unlikely to be extracted where a relative
  761. path works.
  762. Handling Runtime Binaries
  763. ^^^^^^^^^^^^^^^^^^^^^^^^^
  764. .. signature::
  765. file(GET_RUNTIME_DEPENDENCIES [...])
  766. .. versionadded:: 3.16
  767. Recursively get the list of libraries depended on by the given files:
  768. .. code-block:: cmake
  769. file(GET_RUNTIME_DEPENDENCIES
  770. [RESOLVED_DEPENDENCIES_VAR <deps_var>]
  771. [UNRESOLVED_DEPENDENCIES_VAR <unresolved_deps_var>]
  772. [CONFLICTING_DEPENDENCIES_PREFIX <conflicting_deps_prefix>]
  773. [EXECUTABLES <executable_files>...]
  774. [LIBRARIES <library_files>...]
  775. [MODULES <module_files>...]
  776. [DIRECTORIES <directories>...]
  777. [BUNDLE_EXECUTABLE <bundle_executable_file>]
  778. [PRE_INCLUDE_REGEXES <regexes>...]
  779. [PRE_EXCLUDE_REGEXES <regexes>...]
  780. [POST_INCLUDE_REGEXES <regexes>...]
  781. [POST_EXCLUDE_REGEXES <regexes>...]
  782. [POST_INCLUDE_FILES <files>...]
  783. [POST_EXCLUDE_FILES <files>...]
  784. )
  785. Please note that this sub-command is not intended to be used in project mode.
  786. It is intended for use at install time, either from code generated by the
  787. :command:`install(RUNTIME_DEPENDENCY_SET)` command, or from code provided by
  788. the project via :command:`install(CODE)` or :command:`install(SCRIPT)`.
  789. For example:
  790. .. code-block:: cmake
  791. install(CODE [[
  792. file(GET_RUNTIME_DEPENDENCIES
  793. # ...
  794. )
  795. ]])
  796. The arguments are as follows:
  797. ``RESOLVED_DEPENDENCIES_VAR <deps_var>``
  798. Name of the variable in which to store the list of resolved dependencies.
  799. ``UNRESOLVED_DEPENDENCIES_VAR <unresolved_deps_var>``
  800. Name of the variable in which to store the list of unresolved
  801. dependencies. If this variable is not specified, and there are any
  802. unresolved dependencies, an error is issued.
  803. ``CONFLICTING_DEPENDENCIES_PREFIX <conflicting_deps_prefix>``
  804. Variable prefix in which to store conflicting dependency information.
  805. Dependencies are conflicting if two files with the same name are found in
  806. two different directories. The list of filenames that conflict are stored
  807. in ``<conflicting_deps_prefix>_FILENAMES``. For each filename, the list
  808. of paths that were found for that filename are stored in
  809. ``<conflicting_deps_prefix>_<filename>``.
  810. ``EXECUTABLES <executable_files>...``
  811. List of executable files to read for dependencies. These are executables
  812. that are typically created with :command:`add_executable`, but they do
  813. not have to be created by CMake. On Apple platforms, the paths to these
  814. files determine the value of ``@executable_path`` when recursively
  815. resolving the libraries. Specifying any kind of library (``STATIC``,
  816. ``MODULE``, or ``SHARED``) here will result in undefined behavior.
  817. ``LIBRARIES <library_files>...``
  818. List of library files to read for dependencies. These are libraries that
  819. are typically created with :command:`add_library(SHARED)`, but they do
  820. not have to be created by CMake. Specifying ``STATIC`` libraries,
  821. ``MODULE`` libraries, or executables here will result in undefined
  822. behavior.
  823. ``MODULES <module_files>...``
  824. List of loadable module files to read for dependencies. These are modules
  825. that are typically created with :command:`add_library(MODULE)`, but they
  826. do not have to be created by CMake. They are typically used by calling
  827. ``dlopen()`` at runtime rather than linked at link time with ``ld -l``.
  828. Specifying ``STATIC`` libraries, ``SHARED`` libraries, or executables
  829. here will result in undefined behavior.
  830. ``DIRECTORIES <directories>...``
  831. List of additional directories to search for dependencies. On Linux
  832. platforms, these directories are searched if the dependency is not found
  833. in any of the other usual paths. If it is found in such a directory, a
  834. warning is issued, because it means that the file is incomplete (it does
  835. not list all of the directories that contain its dependencies).
  836. On Windows platforms, these directories are searched if the dependency
  837. is not found in any of the other search paths, but no warning is issued,
  838. because searching other paths is a normal part of Windows dependency
  839. resolution. On Apple platforms, this argument has no effect.
  840. ``BUNDLE_EXECUTABLE <bundle_executable_file>``
  841. Executable to treat as the "bundle executable" when resolving libraries.
  842. On Apple platforms, this argument determines the value of
  843. ``@executable_path`` when recursively resolving libraries for
  844. ``LIBRARIES`` and ``MODULES`` files. It has no effect on ``EXECUTABLES``
  845. files. On other platforms, it has no effect. This is typically (but not
  846. always) one of the executables in the ``EXECUTABLES`` argument which
  847. designates the "main" executable of the package.
  848. The following arguments specify filters for including or excluding libraries
  849. to be resolved. See below for a full description of how they work.
  850. ``PRE_INCLUDE_REGEXES <regexes>...``
  851. List of pre-include regexes through which to filter the names of
  852. not-yet-resolved dependencies.
  853. ``PRE_EXCLUDE_REGEXES <regexes>...``
  854. List of pre-exclude regexes through which to filter the names of
  855. not-yet-resolved dependencies.
  856. ``POST_INCLUDE_REGEXES <regexes>...``
  857. List of post-include regexes through which to filter the names of
  858. resolved dependencies.
  859. ``POST_EXCLUDE_REGEXES <regexes>...``
  860. List of post-exclude regexes through which to filter the names of
  861. resolved dependencies.
  862. ``POST_INCLUDE_FILES <files>...``
  863. .. versionadded:: 3.21
  864. List of post-include filenames through which to filter the names of
  865. resolved dependencies. Symlinks are resolved when attempting to match
  866. these filenames.
  867. ``POST_EXCLUDE_FILES <files>...``
  868. .. versionadded:: 3.21
  869. List of post-exclude filenames through which to filter the names of
  870. resolved dependencies. Symlinks are resolved when attempting to match
  871. these filenames.
  872. These arguments can be used to exclude unwanted system libraries when
  873. resolving the dependencies, or to include libraries from a specific
  874. directory. The filtering works as follows:
  875. 1. If the not-yet-resolved dependency matches any of the
  876. ``PRE_INCLUDE_REGEXES``, steps 2 and 3 are skipped, and the dependency
  877. resolution proceeds to step 4.
  878. 2. If the not-yet-resolved dependency matches any of the
  879. ``PRE_EXCLUDE_REGEXES``, dependency resolution stops for that dependency.
  880. 3. Otherwise, dependency resolution proceeds.
  881. 4. ``file(GET_RUNTIME_DEPENDENCIES)`` searches for the dependency according
  882. to the linking rules of the platform (see below).
  883. 5. If the dependency is found, and its full path matches one of the
  884. ``POST_INCLUDE_REGEXES`` or ``POST_INCLUDE_FILES``, the full path is added
  885. to the resolved dependencies, and ``file(GET_RUNTIME_DEPENDENCIES)``
  886. recursively resolves that library's own dependencies. Otherwise, resolution
  887. proceeds to step 6.
  888. 6. If the dependency is found, but its full path matches one of the
  889. ``POST_EXCLUDE_REGEXES`` or ``POST_EXCLUDE_FILES``, it is not added to the
  890. resolved dependencies, and dependency resolution stops for that dependency.
  891. 7. If the dependency is found, and its full path does not match either
  892. ``POST_INCLUDE_REGEXES``, ``POST_INCLUDE_FILES``, ``POST_EXCLUDE_REGEXES``,
  893. or ``POST_EXCLUDE_FILES``, the full path is added to the resolved
  894. dependencies, and ``file(GET_RUNTIME_DEPENDENCIES)`` recursively resolves
  895. that library's own dependencies.
  896. Different platforms have different rules for how dependencies are resolved.
  897. These specifics are described here.
  898. On Linux platforms, library resolution works as follows:
  899. 1. If the depending file does not have any ``RUNPATH`` entries, and the
  900. library exists in one of the depending file's ``RPATH`` entries, or its
  901. parents', in that order, the dependency is resolved to that file.
  902. 2. Otherwise, if the depending file has any ``RUNPATH`` entries, and the
  903. library exists in one of those entries, the dependency is resolved to that
  904. file.
  905. 3. Otherwise, if the library exists in one of the directories listed by
  906. ``ldconfig``, the dependency is resolved to that file.
  907. 4. Otherwise, if the library exists in one of the ``DIRECTORIES`` entries,
  908. the dependency is resolved to that file. In this case, a warning is
  909. issued, because finding a file in one of the ``DIRECTORIES`` means that
  910. the depending file is not complete (it does not list all the directories
  911. from which it pulls dependencies).
  912. 5. Otherwise, the dependency is unresolved.
  913. .. versionchanged:: 3.31
  914. Resolution of each encountered library file name occurs at most once
  915. while processing a given root ELF file (executable or shared object).
  916. If a library file name is encountered again in the dependency tree,
  917. the original resolution is assumed. This behavior more closely matches
  918. the dynamic loader's behavior on Linux.
  919. On Windows platforms, library resolution works as follows:
  920. 1. DLL dependency names are converted to lowercase for matching filters.
  921. Windows DLL names are case-insensitive, and some linkers mangle the
  922. case of the DLL dependency names. However, this makes it more difficult
  923. for ``PRE_INCLUDE_REGEXES``, ``PRE_EXCLUDE_REGEXES``,
  924. ``POST_INCLUDE_REGEXES``, and ``POST_EXCLUDE_REGEXES`` to properly
  925. filter DLL names - every regex would have to check for both uppercase
  926. and lowercase letters. For example:
  927. .. code-block:: cmake
  928. file(GET_RUNTIME_DEPENDENCIES
  929. # ...
  930. PRE_INCLUDE_REGEXES "^[Mm][Yy][Ll][Ii][Bb][Rr][Aa][Rr][Yy]\\.[Dd][Ll][Ll]$"
  931. )
  932. Converting the DLL name to lowercase allows the regexes to only match
  933. lowercase names, thus simplifying the regex. For example:
  934. .. code-block:: cmake
  935. file(GET_RUNTIME_DEPENDENCIES
  936. # ...
  937. PRE_INCLUDE_REGEXES "^mylibrary\\.dll$"
  938. )
  939. This regex will match ``mylibrary.dll`` regardless of how it is cased,
  940. either on disk or in the depending file. (For example, it will match
  941. ``mylibrary.dll``, ``MyLibrary.dll``, and ``MYLIBRARY.DLL``.)
  942. .. versionchanged:: 3.27
  943. The conversion to lowercase only applies while matching filters.
  944. Results reported after filtering case-preserve each DLL name as it is
  945. found on disk, if resolved, and otherwise as it is referenced by the
  946. dependent binary.
  947. Prior to CMake 3.27, the results were reported with lowercase DLL
  948. file names, but the directory portion retained its casing.
  949. 2. (**Not yet implemented**) If the depending file is a Windows Store app,
  950. and the dependency is listed as a dependency in the application's package
  951. manifest, the dependency is resolved to that file.
  952. 3. Otherwise, if the library exists in the same directory as the depending
  953. file, the dependency is resolved to that file.
  954. 4. Otherwise, if the library exists in either the operating system's
  955. ``system32`` directory or the ``Windows`` directory, in that order, the
  956. dependency is resolved to that file.
  957. 5. Otherwise, if the library exists in one of the directories specified by
  958. ``DIRECTORIES``, in the order they are listed, the dependency is resolved
  959. to that file. In this case, a warning is not issued, because searching
  960. other directories is a normal part of Windows library resolution.
  961. 6. Otherwise, the dependency is unresolved.
  962. On Apple platforms, library resolution works as follows:
  963. 1. If the dependency starts with ``@executable_path/``, and an
  964. ``EXECUTABLES`` argument is in the process of being resolved, and
  965. replacing ``@executable_path/`` with the directory of the executable
  966. yields an existing file, the dependency is resolved to that file.
  967. 2. Otherwise, if the dependency starts with ``@executable_path/``, and there
  968. is a ``BUNDLE_EXECUTABLE`` argument, and replacing ``@executable_path/``
  969. with the directory of the bundle executable yields an existing file, the
  970. dependency is resolved to that file.
  971. 3. Otherwise, if the dependency starts with ``@loader_path/``, and replacing
  972. ``@loader_path/`` with the directory of the depending file yields an
  973. existing file, the dependency is resolved to that file.
  974. 4. Otherwise, if the dependency starts with ``@rpath/``, and replacing
  975. ``@rpath/`` with one of the ``RPATH`` entries of the depending file
  976. yields an existing file, the dependency is resolved to that file.
  977. Note that ``RPATH`` entries that start with ``@executable_path/`` or
  978. ``@loader_path/`` also have these items replaced with the appropriate
  979. path.
  980. 5. Otherwise, if the dependency is an absolute file that exists,
  981. the dependency is resolved to that file.
  982. 6. Otherwise, the dependency is unresolved.
  983. This function accepts several variables that determine which tool is used for
  984. dependency resolution:
  985. .. variable:: CMAKE_GET_RUNTIME_DEPENDENCIES_PLATFORM
  986. Determines which operating system and executable format the files are built
  987. for. This could be one of several values:
  988. * ``linux+elf``
  989. * ``windows+pe``
  990. * ``macos+macho``
  991. If this variable is not specified, it is determined automatically by system
  992. introspection.
  993. .. variable:: CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL
  994. Determines the tool to use for dependency resolution. It could be one of
  995. several values, depending on the value of
  996. :variable:`CMAKE_GET_RUNTIME_DEPENDENCIES_PLATFORM`:
  997. ================================================= =============================================
  998. ``CMAKE_GET_RUNTIME_DEPENDENCIES_PLATFORM`` ``CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL``
  999. ================================================= =============================================
  1000. ``linux+elf`` ``objdump``
  1001. ``windows+pe`` ``objdump`` or ``dumpbin``
  1002. ``macos+macho`` ``otool``
  1003. ================================================= =============================================
  1004. If this variable is not specified, it is determined automatically by system
  1005. introspection.
  1006. .. variable:: CMAKE_GET_RUNTIME_DEPENDENCIES_COMMAND
  1007. Determines the path to the tool to use for dependency resolution. This is
  1008. the actual path to ``objdump``, ``dumpbin``, or ``otool``.
  1009. If this variable is not specified, it is determined by the value of
  1010. :variable:`CMAKE_OBJDUMP` variable if set, else by system introspection.
  1011. .. versionadded:: 3.18
  1012. Uses :variable:`CMAKE_OBJDUMP` if set.