1
0

cmake_path.rst 25 KB

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