cmake_path.rst 25 KB

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