cmake_path.rst 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. cmake_path
  2. ----------
  3. .. versionadded:: 3.20
  4. This command is for the manipulation of paths. Only syntactic aspects of
  5. paths are handled, there is no interaction of any kind with any underlying
  6. file system. The path may represent a non-existing path or even one that
  7. is not allowed to exist on the current file system or platform.
  8. For operations that do interact with the filesystem, see the :command:`file`
  9. command.
  10. .. note::
  11. The ``cmake_path`` command handles paths in the format of the build system
  12. (i.e. the host platform), not the target system. When cross-compiling,
  13. if the path contains elements that are not representable on the host
  14. platform (e.g. a drive letter when the host is not Windows), the results
  15. will be unpredictable.
  16. Synopsis
  17. ^^^^^^^^
  18. .. parsed-literal::
  19. `Conventions`_
  20. `Path Structure And Terminology`_
  21. `Normalization`_
  22. `Decomposition`_
  23. cmake_path(`GET`_ <path-var> `ROOT_NAME <GET ... ROOT_NAME_>`_ <out-var>)
  24. cmake_path(`GET`_ <path-var> `ROOT_DIRECTORY <GET ... ROOT_DIRECTORY_>`_ <out-var>)
  25. cmake_path(`GET`_ <path-var> `ROOT_PATH <GET ... ROOT_PATH_>`_ <out-var>)
  26. cmake_path(`GET`_ <path-var> `FILENAME <GET ... FILENAME_>`_ <out-var>)
  27. cmake_path(`GET`_ <path-var> `EXTENSION <GET ... EXTENSION_>`_ [LAST_ONLY] <out-var>)
  28. cmake_path(`GET`_ <path-var> `STEM <GET ... STEM_>`_ [LAST_ONLY] <out-var>)
  29. cmake_path(`GET`_ <path-var> `RELATIVE_PART <GET ... RELATIVE_PART_>`_ <out-var>)
  30. cmake_path(`GET`_ <path-var> `PARENT_PATH <GET ... PARENT_PATH_>`_ <out-var>)
  31. `Query`_
  32. cmake_path(`HAS_ROOT_NAME`_ <path-var> <out-var>)
  33. cmake_path(`HAS_ROOT_DIRECTORY`_ <path-var> <out-var>)
  34. cmake_path(`HAS_ROOT_PATH`_ <path-var> <out-var>)
  35. cmake_path(`HAS_FILENAME`_ <path-var> <out-var>)
  36. cmake_path(`HAS_EXTENSION`_ <path-var> <out-var>)
  37. cmake_path(`HAS_STEM`_ <path-var> <out-var>)
  38. cmake_path(`HAS_RELATIVE_PART`_ <path-var> <out-var>)
  39. cmake_path(`HAS_PARENT_PATH`_ <path-var> <out-var>)
  40. cmake_path(`IS_ABSOLUTE`_ <path-var> <out-var>)
  41. cmake_path(`IS_RELATIVE`_ <path-var> <out-var>)
  42. cmake_path(`IS_PREFIX`_ <path-var> <input> [NORMALIZE] <out-var>)
  43. `Comparison`_
  44. cmake_path(`COMPARE`_ <input1> <op> <input2> <out-var>)
  45. `Modification`_
  46. cmake_path(`SET`_ <path-var> [NORMALIZE] <input>)
  47. cmake_path(`APPEND`_ <path-var> [<input>...] [OUTPUT_VARIABLE <out-var>])
  48. cmake_path(`APPEND_STRING`_ <path-var> [<input>...] [OUTPUT_VARIABLE <out-var>])
  49. cmake_path(`REMOVE_FILENAME`_ <path-var> [OUTPUT_VARIABLE <out-var>])
  50. cmake_path(`REPLACE_FILENAME`_ <path-var> <input> [OUTPUT_VARIABLE <out-var>])
  51. cmake_path(`REMOVE_EXTENSION`_ <path-var> [LAST_ONLY] [OUTPUT_VARIABLE <out-var>])
  52. cmake_path(`REPLACE_EXTENSION`_ <path-var> [LAST_ONLY] <input> [OUTPUT_VARIABLE <out-var>])
  53. `Generation`_
  54. cmake_path(`NORMAL_PATH`_ <path-var> [OUTPUT_VARIABLE <out-var>])
  55. cmake_path(`RELATIVE_PATH`_ <path-var> [BASE_DIRECTORY <input>] [OUTPUT_VARIABLE <out-var>])
  56. cmake_path(`ABSOLUTE_PATH`_ <path-var> [BASE_DIRECTORY <input>] [NORMALIZE] [OUTPUT_VARIABLE <out-var>])
  57. `Native Conversion`_
  58. cmake_path(`NATIVE_PATH`_ <path-var> [NORMALIZE] <out-var>)
  59. cmake_path(`CONVERT`_ <input> `TO_CMAKE_PATH_LIST <CONVERT ... TO_CMAKE_PATH_LIST_>`_ <out-var> [NORMALIZE])
  60. cmake_path(`CONVERT`_ <input> `TO_NATIVE_PATH_LIST <CONVERT ... TO_NATIVE_PATH_LIST_>`_ <out-var> [NORMALIZE])
  61. `Hashing`_
  62. cmake_path(`HASH`_ <path-var> <out-var>)
  63. Conventions
  64. ^^^^^^^^^^^
  65. The following conventions are used in this command's documentation:
  66. ``<path-var>``
  67. Always the name of a variable. For commands that expect a ``<path-var>``
  68. as input, the variable must exist and it is expected to hold a single path.
  69. ``<input>``
  70. A string literal which may contain a path, path fragment, or multiple paths
  71. with a special separator depending on the command. See the description of
  72. each command to see how this is interpreted.
  73. ``<input>...``
  74. Zero or more string literal arguments.
  75. ``<out-var>``
  76. The name of a variable into which the result of a command will be written.
  77. .. _Path Structure And Terminology:
  78. Path Structure And Terminology
  79. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  80. A path has the following structure (all components are optional, with some
  81. constraints):
  82. ::
  83. root-name root-directory-separator (item-name directory-separator)* filename
  84. ``root-name``
  85. Identifies the root on a filesystem with multiple roots (such as ``"C:"``
  86. or ``"//myserver"``). It is optional.
  87. ``root-directory-separator``
  88. A directory separator that, if present, indicates that this path is
  89. absolute. If it is missing and the first element other than the
  90. ``root-name`` is an ``item-name``, then the path is relative.
  91. ``item-name``
  92. A sequence of characters that aren't directory separators. This name may
  93. identify a file, a hard link, a symbolic link, or a directory. Two special
  94. cases are recognized:
  95. * The item name consisting of a single dot character ``.`` is a
  96. directory name that refers to the current directory.
  97. * The item name consisting of two dot characters ``..`` is a
  98. directory name that refers to the parent directory.
  99. The ``(...)*`` pattern shown above is to indicate that there can be zero
  100. or more item names, with multiple items separated by a
  101. ``directory-separator``. The ``()*`` characters are not part of the path.
  102. ``directory-separator``
  103. The only recognized directory separator is a forward slash character ``/``.
  104. If this character is repeated, it is treated as a single directory
  105. separator. In other words, ``/usr///////lib`` is the same as ``/usr/lib``.
  106. .. _FILENAME_DEF:
  107. .. _EXTENSION_DEF:
  108. .. _STEM_DEF:
  109. ``filename``
  110. A path has a ``filename`` if it does not end with a ``directory-separator``.
  111. The ``filename`` is effectively the last ``item-name`` of the path, so it
  112. can also be a hard link, symbolic link or a directory.
  113. A ``filename`` can have an *extension*. By default, the extension is
  114. defined as the sub-string beginning at the left-most period (including
  115. the period) and until the end of the ``filename``. In commands that
  116. accept a ``LAST_ONLY`` keyword, ``LAST_ONLY`` changes the interpretation
  117. to the sub-string beginning at the right-most period.
  118. The following exceptions apply to the above interpretation:
  119. * If the first character in the ``filename`` is a period, that period is
  120. ignored (i.e. a ``filename`` like ``".profile"`` is treated as having
  121. no extension).
  122. * If the ``filename`` is either ``.`` or ``..``, it has no extension.
  123. The *stem* is the part of the ``filename`` before the extension.
  124. Some commands refer to a ``root-path``. This is the concatenation of
  125. ``root-name`` and ``root-directory-separator``, either or both of which can
  126. be empty. A ``relative-part`` refers to the full path with any ``root-path``
  127. removed.
  128. .. _Creating a Path Variable:
  129. Creating A Path Variable
  130. ^^^^^^^^^^^^^^^^^^^^^^^^
  131. While a path can be created with care using an ordinary :command:`set`
  132. command, it is recommended to use :command:`cmake_path(SET)` instead, as it
  133. automatically converts the path to the required form where required. The
  134. :command:`cmake_path(APPEND)` subcommand may be another suitable alternative
  135. where a path needs to be constructed by joining fragments. The following
  136. example compares the three methods for constructing the same path:
  137. .. code-block:: cmake
  138. set(path1 "${CMAKE_CURRENT_SOURCE_DIR}/data")
  139. cmake_path(SET path2 "${CMAKE_CURRENT_SOURCE_DIR}/data")
  140. cmake_path(APPEND path3 "${CMAKE_CURRENT_SOURCE_DIR}" "data")
  141. `Modification`_ and `Generation`_ sub-commands can either store the result
  142. in-place, or in a separate variable named after an ``OUTPUT_VARIABLE``
  143. keyword. All other sub-commands store the result in a mandatory ``<out-var>``
  144. variable.
  145. .. note::
  146. The ``cmake_path`` command can be used directly with variables provided by
  147. CMake representing paths (e.g., :variable:`CMAKE_SOURCE_DIR`). However, since
  148. the command can modify these variables, and many of them have undefined
  149. behavior when modified, they should never be specified as an ``<out-var>``,
  150. and only as the ``<path-var>`` when a separate ``<out-var>`` is specified.
  151. See :manual:`cmake-variables(7)` for the list of CMake variables and
  152. identifiers reserved by CMake.
  153. .. _Normalization:
  154. Normalization
  155. ^^^^^^^^^^^^^
  156. Some sub-commands support *normalizing* a path. The algorithm used to
  157. normalize a path is as follows:
  158. 1. If the path is empty, stop (the normalized form of an empty path is
  159. also an empty path).
  160. 2. Replace each ``directory-separator``, which may consist of multiple
  161. separators, with a single ``/`` (``/a///b --> /a/b``).
  162. 3. Remove each solitary period (``.``) and any immediately following
  163. ``directory-separator`` (``/a/./b/. --> /a/b``).
  164. 4. Remove each ``item-name`` (other than ``..``) that is immediately
  165. followed by a ``directory-separator`` and a ``..``, along with any
  166. immediately following ``directory-separator`` (``/a/b/../c --> a/c``).
  167. 5. If there is a ``root-directory``, remove any ``..`` and any
  168. ``directory-separators`` immediately following them. The parent of the
  169. root directory is treated as still the root directory (``/../a --> /a``).
  170. 6. If the last ``item-name`` is ``..``, remove any trailing
  171. ``directory-separator`` (``../ --> ..``).
  172. 7. If the path is empty by this stage, add a ``dot`` (normal form of ``./``
  173. is ``.``).
  174. .. _Path Decomposition:
  175. Decomposition
  176. ^^^^^^^^^^^^^
  177. .. _GET:
  178. The following forms of the ``GET`` subcommand each retrieve a different
  179. component or group of components from a path. See
  180. `Path Structure And Terminology`_ for the meaning of each path component.
  181. .. signature::
  182. cmake_path(GET <path-var> ROOT_NAME <out-var>)
  183. cmake_path(GET <path-var> ROOT_DIRECTORY <out-var>)
  184. cmake_path(GET <path-var> ROOT_PATH <out-var>)
  185. cmake_path(GET <path-var> FILENAME <out-var>)
  186. cmake_path(GET <path-var> EXTENSION [LAST_ONLY] <out-var>)
  187. cmake_path(GET <path-var> STEM [LAST_ONLY] <out-var>)
  188. cmake_path(GET <path-var> RELATIVE_PART <out-var>)
  189. cmake_path(GET <path-var> PARENT_PATH <out-var>)
  190. :target:
  191. GET ... ROOT_NAME
  192. GET ... ROOT_DIRECTORY
  193. GET ... ROOT_PATH
  194. GET ... FILENAME
  195. GET ... EXTENSION
  196. GET ... STEM
  197. GET ... RELATIVE_PART
  198. GET ... PARENT_PATH
  199. If a requested component is not present in the path, an empty string will be
  200. stored in ``<out-var>``. For example, only Windows systems have the concept
  201. of a ``root-name``, so when the host machine is non-Windows, the ``ROOT_NAME``
  202. subcommand will always return an empty string.
  203. For ``PARENT_PATH``, if the :cref:`HAS_RELATIVE_PART` sub-command returns
  204. false, the result is a copy of ``<path-var>``. Note that this implies that a
  205. root directory is considered to have a parent, with that parent being itself.
  206. Where :cref:`HAS_RELATIVE_PART` returns true, the result will essentially be
  207. ``<path-var>`` with one less element.
  208. Root examples
  209. """""""""""""
  210. .. code-block:: cmake
  211. set(path "c:/a")
  212. cmake_path(GET path ROOT_NAME rootName)
  213. cmake_path(GET path ROOT_DIRECTORY rootDir)
  214. cmake_path(GET path ROOT_PATH rootPath)
  215. message("Root name is \"${rootName}\"")
  216. message("Root directory is \"${rootDir}\"")
  217. message("Root path is \"${rootPath}\"")
  218. ::
  219. Root name is "c:"
  220. Root directory is "/"
  221. Root path is "c:/"
  222. Filename examples
  223. """""""""""""""""
  224. .. code-block:: cmake
  225. set(path "/a/b")
  226. cmake_path(GET path FILENAME filename)
  227. message("First filename is \"${filename}\"")
  228. # Trailing slash means filename is empty
  229. set(path "/a/b/")
  230. cmake_path(GET path FILENAME filename)
  231. message("Second filename is \"${filename}\"")
  232. ::
  233. First filename is "b"
  234. Second filename is ""
  235. Extension and stem examples
  236. """""""""""""""""""""""""""
  237. .. code-block:: cmake
  238. set(path "name.ext1.ext2")
  239. cmake_path(GET path EXTENSION fullExt)
  240. cmake_path(GET path STEM fullStem)
  241. message("Full extension is \"${fullExt}\"")
  242. message("Full stem is \"${fullStem}\"")
  243. # Effect of LAST_ONLY
  244. cmake_path(GET path EXTENSION LAST_ONLY lastExt)
  245. cmake_path(GET path STEM LAST_ONLY lastStem)
  246. message("Last extension is \"${lastExt}\"")
  247. message("Last stem is \"${lastStem}\"")
  248. # Special cases
  249. set(dotPath "/a/.")
  250. set(dotDotPath "/a/..")
  251. set(someMorePath "/a/.some.more")
  252. cmake_path(GET dotPath EXTENSION dotExt)
  253. cmake_path(GET dotPath STEM dotStem)
  254. cmake_path(GET dotDotPath EXTENSION dotDotExt)
  255. cmake_path(GET dotDotPath STEM dotDotStem)
  256. cmake_path(GET someMorePath EXTENSION someMoreExt)
  257. cmake_path(GET someMorePath STEM someMoreStem)
  258. message("Dot extension is \"${dotExt}\"")
  259. message("Dot stem is \"${dotStem}\"")
  260. message("Dot-dot extension is \"${dotDotExt}\"")
  261. message("Dot-dot stem is \"${dotDotStem}\"")
  262. message(".some.more extension is \"${someMoreExt}\"")
  263. message(".some.more stem is \"${someMoreStem}\"")
  264. ::
  265. Full extension is ".ext1.ext2"
  266. Full stem is "name"
  267. Last extension is ".ext2"
  268. Last stem is "name.ext1"
  269. Dot extension is ""
  270. Dot stem is "."
  271. Dot-dot extension is ""
  272. Dot-dot stem is ".."
  273. .some.more extension is ".more"
  274. .some.more stem is ".some"
  275. Relative part examples
  276. """"""""""""""""""""""
  277. .. code-block:: cmake
  278. set(path "c:/a/b")
  279. cmake_path(GET path RELATIVE_PART result)
  280. message("Relative part is \"${result}\"")
  281. set(path "c/d")
  282. cmake_path(GET path RELATIVE_PART result)
  283. message("Relative part is \"${result}\"")
  284. set(path "/")
  285. cmake_path(GET path RELATIVE_PART result)
  286. message("Relative part is \"${result}\"")
  287. ::
  288. Relative part is "a/b"
  289. Relative part is "c/d"
  290. Relative part is ""
  291. Path traversal examples
  292. """""""""""""""""""""""
  293. .. code-block:: cmake
  294. set(path "c:/a/b")
  295. cmake_path(GET path PARENT_PATH result)
  296. message("Parent path is \"${result}\"")
  297. set(path "c:/")
  298. cmake_path(GET path PARENT_PATH result)
  299. message("Parent path is \"${result}\"")
  300. ::
  301. Parent path is "c:/a"
  302. Parent path is "c:/"
  303. .. _Path Query:
  304. Query
  305. ^^^^^
  306. Each of the :cref:`GET` subcommands has a corresponding
  307. ``HAS_...`` subcommand which can be used to discover whether a particular path
  308. component is present. See `Path Structure And Terminology`_ for the
  309. meaning of each path component.
  310. .. signature::
  311. cmake_path(HAS_ROOT_NAME <path-var> <out-var>)
  312. cmake_path(HAS_ROOT_DIRECTORY <path-var> <out-var>)
  313. cmake_path(HAS_ROOT_PATH <path-var> <out-var>)
  314. cmake_path(HAS_FILENAME <path-var> <out-var>)
  315. cmake_path(HAS_EXTENSION <path-var> <out-var>)
  316. cmake_path(HAS_STEM <path-var> <out-var>)
  317. cmake_path(HAS_RELATIVE_PART <path-var> <out-var>)
  318. cmake_path(HAS_PARENT_PATH <path-var> <out-var>)
  319. Each of the above follows the predictable pattern of setting ``<out-var>``
  320. to true if the path has the associated component, or false otherwise.
  321. Note the following special cases:
  322. * For ``HAS_ROOT_PATH``, a true result will only be returned if at least one
  323. of ``root-name`` or ``root-directory`` is non-empty.
  324. * For ``HAS_PARENT_PATH``, the root directory is also considered to have a
  325. parent, which will be itself. The result is true except if the path
  326. consists of just a :ref:`filename <FILENAME_DEF>`.
  327. .. signature::
  328. cmake_path(IS_ABSOLUTE <path-var> <out-var>)
  329. Sets ``<out-var>`` to true if ``<path-var>`` is absolute. An absolute path
  330. is a path that unambiguously identifies the location of a file without
  331. reference to an additional starting location. On Windows, this means the
  332. path must have both a ``root-name`` and a ``root-directory-separator`` to be
  333. considered absolute. On other platforms, just a ``root-directory-separator``
  334. is sufficient. Note that this means on Windows, ``IS_ABSOLUTE`` can be
  335. false while :cref:`HAS_ROOT_DIRECTORY` can be true.
  336. .. signature::
  337. cmake_path(IS_RELATIVE <path-var> <out-var>)
  338. This will store the opposite of :cref:`IS_ABSOLUTE` in ``<out-var>``.
  339. .. signature::
  340. cmake_path(IS_PREFIX <path-var> <input> [NORMALIZE] <out-var>)
  341. Checks if ``<path-var>`` is the prefix of ``<input>``.
  342. When the ``NORMALIZE`` option is specified, ``<path-var>`` and ``<input>``
  343. are :ref:`normalized <Normalization>` before the check.
  344. .. code-block:: cmake
  345. set(path "/a/b/c")
  346. cmake_path(IS_PREFIX path "/a/b/c/d" result) # result = true
  347. cmake_path(IS_PREFIX path "/a/b" result) # result = false
  348. cmake_path(IS_PREFIX path "/x/y/z" result) # result = false
  349. set(path "/a/b")
  350. cmake_path(IS_PREFIX path "/a/c/../b" NORMALIZE result) # result = true
  351. .. _Path Comparison:
  352. Comparison
  353. ^^^^^^^^^^
  354. .. _COMPARE:
  355. .. signature::
  356. cmake_path(COMPARE <input1> EQUAL <input2> <out-var>)
  357. cmake_path(COMPARE <input1> NOT_EQUAL <input2> <out-var>)
  358. :target:
  359. COMPARE ... EQUAL
  360. COMPARE ... NOT_EQUAL
  361. Compares the lexical representations of two paths provided as string literals.
  362. No normalization is performed on either path, except multiple consecutive
  363. directory separators are effectively collapsed into a single separator.
  364. Equality is determined according to the following pseudo-code logic:
  365. ::
  366. if(NOT <input1>.root_name() STREQUAL <input2>.root_name())
  367. return FALSE
  368. if(<input1>.has_root_directory() XOR <input2>.has_root_directory())
  369. return FALSE
  370. Return FALSE if a relative portion of <input1> is not lexicographically
  371. equal to the relative portion of <input2>. This comparison is performed path
  372. component-wise. If all of the components compare equal, then return TRUE.
  373. .. note::
  374. Unlike most other ``cmake_path()`` subcommands, the ``COMPARE`` subcommand
  375. takes literal strings as input, not the names of variables.
  376. .. _Path Modification:
  377. Modification
  378. ^^^^^^^^^^^^
  379. .. signature::
  380. cmake_path(SET <path-var> [NORMALIZE] <input>)
  381. Assigns the ``<input>`` path to ``<path-var>``. If ``<input>`` is a native
  382. path, it is converted into a CMake-style path with forward-slashes
  383. (``/``). On Windows, the long filename marker is taken into account.
  384. When the ``NORMALIZE`` option is specified, the path is :ref:`normalized
  385. <Normalization>` after the conversion.
  386. For example:
  387. .. code-block:: cmake
  388. set(native_path "c:\\a\\b/..\\c")
  389. cmake_path(SET path "${native_path}")
  390. message("CMake path is \"${path}\"")
  391. cmake_path(SET path NORMALIZE "${native_path}")
  392. message("Normalized CMake path is \"${path}\"")
  393. Output::
  394. CMake path is "c:/a/b/../c"
  395. Normalized CMake path is "c:/a/c"
  396. .. signature::
  397. cmake_path(APPEND <path-var> [<input>...] [OUTPUT_VARIABLE <out-var>])
  398. Appends all the ``<input>`` arguments to the ``<path-var>`` using ``/`` as
  399. the ``directory-separator``. Depending on the ``<input>``, the previous
  400. contents of ``<path-var>`` may be discarded. For each ``<input>`` argument,
  401. the following algorithm (pseudo-code) applies:
  402. ::
  403. # <path> is the contents of <path-var>
  404. if(<input>.is_absolute() OR
  405. (<input>.has_root_name() AND
  406. NOT <input>.root_name() STREQUAL <path>.root_name()))
  407. replace <path> with <input>
  408. return()
  409. endif()
  410. if(<input>.has_root_directory())
  411. remove any root-directory and the entire relative path from <path>
  412. elseif(<path>.has_filename() OR
  413. (NOT <path-var>.has_root_directory() OR <path>.is_absolute()))
  414. append directory-separator to <path>
  415. endif()
  416. append <input> omitting any root-name to <path>
  417. See :ref:`Creating a Path Variable` for details on the output variable.
  418. .. signature::
  419. cmake_path(APPEND_STRING <path-var> [<input>...] [OUTPUT_VARIABLE <out-var>])
  420. Appends all the ``<input>`` arguments to the ``<path-var>`` without adding any
  421. ``directory-separator``.
  422. See :ref:`Creating a Path Variable` for details on the output variable.
  423. .. signature::
  424. cmake_path(REMOVE_FILENAME <path-var> [OUTPUT_VARIABLE <out-var>])
  425. Removes the :ref:`filename <FILENAME_DEF>` component (as returned by
  426. :cref:`GET ... FILENAME`) from ``<path-var>``. After removal, any trailing
  427. ``directory-separator`` is left alone, if present.
  428. :cref:`HAS_FILENAME` will return false for the result, whether stored in
  429. ``<out-var>`` or ``<path-var>``.
  430. For example:
  431. .. code-block:: cmake
  432. set(path "/a/b")
  433. cmake_path(REMOVE_FILENAME path)
  434. message("First path is \"${path}\"")
  435. # filename is now already empty, the following removes nothing
  436. cmake_path(REMOVE_FILENAME path)
  437. message("Second path is \"${path}\"")
  438. Output::
  439. First path is "/a/"
  440. Second path is "/a/"
  441. See :ref:`Creating a Path Variable` for details on the output variable.
  442. .. signature::
  443. cmake_path(REPLACE_FILENAME <path-var> <input> [OUTPUT_VARIABLE <out-var>])
  444. Replaces the :ref:`filename <FILENAME_DEF>` component from ``<path-var>``
  445. with ``<input>``. If ``<path-var>`` has no filename component (i.e.
  446. :cref:`HAS_FILENAME` returns false), the path is unchanged. The operation is
  447. equivalent to the following:
  448. .. code-block:: cmake
  449. cmake_path(HAS_FILENAME path has_filename)
  450. if(has_filename)
  451. cmake_path(REMOVE_FILENAME path)
  452. cmake_path(APPEND path "${input}")
  453. endif()
  454. See :ref:`Creating a Path Variable` for details on the output variable.
  455. .. signature::
  456. cmake_path(REMOVE_EXTENSION <path-var> [LAST_ONLY]
  457. [OUTPUT_VARIABLE <out-var>])
  458. Removes the :ref:`extension <EXTENSION_DEF>`, if any, from ``<path-var>``.
  459. See :ref:`Creating a Path Variable` for details on the output variable.
  460. .. signature::
  461. cmake_path(REPLACE_EXTENSION <path-var> [LAST_ONLY] <input>
  462. [OUTPUT_VARIABLE <out-var>])
  463. Replaces the :ref:`extension <EXTENSION_DEF>` with ``<input>``. Its effect
  464. is equivalent to the following:
  465. .. code-block:: cmake
  466. cmake_path(REMOVE_EXTENSION path)
  467. if(NOT input MATCHES "^\\.")
  468. cmake_path(APPEND_STRING path ".")
  469. endif()
  470. cmake_path(APPEND_STRING path "${input}")
  471. See :ref:`Creating a Path Variable` for details on the output variable.
  472. .. _Path Generation:
  473. Generation
  474. ^^^^^^^^^^
  475. .. signature::
  476. cmake_path(NORMAL_PATH <path-var> [OUTPUT_VARIABLE <out-var>])
  477. Normalizes ``<path-var>`` according the steps described in
  478. :ref:`Normalization`.
  479. See :ref:`Creating a Path Variable` for details on the output variable.
  480. .. signature::
  481. cmake_path(RELATIVE_PATH <path-var> [BASE_DIRECTORY <input>]
  482. [OUTPUT_VARIABLE <out-var>])
  483. Modifies ``<path-var>`` to make it relative to the ``BASE_DIRECTORY`` argument.
  484. If ``BASE_DIRECTORY`` is not specified, the default base directory will be
  485. :variable:`CMAKE_CURRENT_SOURCE_DIR`.
  486. For reference, the algorithm used to compute the relative path is the same
  487. as that used by C++
  488. `std::filesystem::path::lexically_relative
  489. <https://en.cppreference.com/w/cpp/filesystem/path/lexically_normal>`_.
  490. See :ref:`Creating a Path Variable` for details on the output variable.
  491. .. signature::
  492. cmake_path(ABSOLUTE_PATH <path-var> [BASE_DIRECTORY <input>] [NORMALIZE]
  493. [OUTPUT_VARIABLE <out-var>])
  494. If ``<path-var>`` is a relative path (:cref:`IS_RELATIVE` is true), it is
  495. evaluated relative to the given base directory specified by ``BASE_DIRECTORY``
  496. option. If ``BASE_DIRECTORY`` is not specified, the default base directory
  497. will be :variable:`CMAKE_CURRENT_SOURCE_DIR`.
  498. When the ``NORMALIZE`` option is specified, the path is :ref:`normalized
  499. <Normalization>` after the path computation.
  500. Because ``cmake_path()`` does not access the filesystem, symbolic links are
  501. not resolved and any leading tilde is not expanded. To compute a real path
  502. with symbolic links resolved and leading tildes expanded, use the
  503. :command:`file(REAL_PATH)` command instead.
  504. See :ref:`Creating a Path Variable` for details on the output variable.
  505. Native Conversion
  506. ^^^^^^^^^^^^^^^^^
  507. For commands in this section, *native* refers to the host platform, not the
  508. target platform when cross-compiling.
  509. .. signature::
  510. cmake_path(NATIVE_PATH <path-var> [NORMALIZE] <out-var>)
  511. Converts a CMake-style ``<path-var>`` into a native path with
  512. platform-specific slashes (``\`` on Windows hosts and ``/`` elsewhere).
  513. When the ``NORMALIZE`` option is specified, the path is :ref:`normalized
  514. <Normalization>` before the conversion.
  515. See :ref:`Creating a Path Variable` for details on the output variable.
  516. .. _CONVERT:
  517. .. signature::
  518. cmake_path(CONVERT <input> TO_CMAKE_PATH_LIST <out-var> [NORMALIZE])
  519. :target:
  520. CONVERT ... TO_CMAKE_PATH_LIST
  521. Converts a native ``<input>`` path into a CMake-style path with forward
  522. slashes (``/``).
  523. On Windows hosts, the long filename marker is taken into
  524. account. The input can be a single path or a system search path like
  525. ``$ENV{PATH}``. A search path will be converted to a
  526. :ref:`semicolon-separated list <CMake Language Lists>` (on non-Windows
  527. platforms, this essentially means ``:`` separators are replaced with ``;``).
  528. When the ``NORMALIZE`` option is specified, the path is :ref:`normalized
  529. <Normalization>` before the conversion.
  530. See :ref:`Creating a Path Variable` for details on the output variable.
  531. .. note::
  532. Unlike most other ``cmake_path()`` subcommands, the ``CONVERT`` subcommand
  533. takes a literal string as input, not the name of a variable.
  534. .. signature::
  535. cmake_path(CONVERT <input> TO_NATIVE_PATH_LIST <out-var> [NORMALIZE])
  536. :target:
  537. CONVERT ... TO_NATIVE_PATH_LIST
  538. Converts a CMake-style ``<input>`` path into a native path with
  539. platform-specific slashes (``\`` on Windows hosts and ``/`` elsewhere).
  540. The input can be a single path or a :ref:`semicolon-separated list
  541. <CMake Language Lists>`. A list will be converted into a native search path
  542. (``;``-separated on Windows, ``:``-separated on other platforms).
  543. When the ``NORMALIZE`` option is specified, the path is :ref:`normalized
  544. <Normalization>` before the conversion.
  545. See :ref:`Creating a Path Variable` for details on the output variable.
  546. .. note::
  547. Unlike most other ``cmake_path()`` subcommands, the ``CONVERT`` subcommand
  548. takes a literal string as input, not the name of a variable.
  549. For example:
  550. .. code-block:: cmake
  551. set(paths "/a/b/c" "/x/y/z")
  552. cmake_path(CONVERT "${paths}" TO_NATIVE_PATH_LIST native_paths)
  553. message("Native path list is \"${native_paths}\"")
  554. Output on Windows::
  555. Native path list is "\a\b\c;\x\y\z"
  556. Output on all other platforms::
  557. Native path list is "/a/b/c:/x/y/z"
  558. Hashing
  559. ^^^^^^^
  560. .. signature::
  561. cmake_path(HASH <path-var> <out-var>)
  562. Computes a hash value of ``<path-var>`` such that for two paths ``p1`` and
  563. ``p2`` that compare equal (:cref:`COMPARE ... EQUAL`), the hash value of
  564. ``p1`` is equal to the hash value of ``p2``. The path is always
  565. :ref:`normalized <Normalization>` before the hash is computed.
  566. See :ref:`Creating a Path Variable` for details on the output variable.