cmake_path.rst 25 KB

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