file.rst 49 KB

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