file.rst 51 KB

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