cmake_path.rst 24 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> :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>)
  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-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/d")
  344. cmake_path(IS_PREFIX path "/a/b" result) # result = true
  345. cmake_path(IS_PREFIX path "/x/y/z" result) # result = false
  346. set(path "/a/b")
  347. cmake_path(IS_PREFIX path "/a/c/../b" NORMALIZE result) # result = true
  348. .. _COMPARE:
  349. ::
  350. cmake_path(COMPARE <input1> EQUAL <input2> <out-var>)
  351. cmake_path(COMPARE <input1> NOT_EQUAL <input2> <out-var>)
  352. Compares the lexical representations of two paths provided as string literals.
  353. No normalization is performed on either path. Equality is determined
  354. according to the following pseudo-code logic:
  355. ::
  356. if(NOT <input1>.root_name() STREQUAL <input2>.root_name())
  357. return FALSE
  358. if(<input1>.has_root_directory() XOR <input2>.has_root_directory())
  359. return FALSE
  360. Return FALSE if a relative portion of <input1> is not lexicographically
  361. equal to the relative portion of <input2>. This comparison is performed path
  362. component-wise. If all of the components compare equal, then return TRUE.
  363. .. note::
  364. Unlike most other ``cmake_path()`` subcommands, the ``COMPARE`` subcommand
  365. takes literal strings as input, not the names of variables.
  366. Modification
  367. ^^^^^^^^^^^^
  368. .. _cmake_path-SET:
  369. ::
  370. cmake_path(SET <path-var> [NORMALIZE] <input>)
  371. Assign the ``<input>`` path to ``<path-var>``. If ``<input>`` is a native
  372. path, it is converted into a cmake-style path with forward-slashes
  373. (``/``). On Windows, the long filename marker is taken into account.
  374. When the ``NORMALIZE`` option is specified, the path is :ref:`normalized
  375. <Normalization>` before the conversion.
  376. For example:
  377. .. code-block:: cmake
  378. set(native_path "c:\\a\\b/..\\c")
  379. cmake_path(SET path "${native_path}")
  380. message("CMake path is \"${path}\"")
  381. cmake_path(SET path NORMALIZE "${native_path}")
  382. message("Normalized CMake path is \"${path}\"")
  383. Output::
  384. CMake path is "c:/a/b/../c"
  385. Normalized CMake path is "c:/a/c"
  386. .. _APPEND:
  387. ::
  388. cmake_path(APPEND <path-var> [<input>...] [OUTPUT_VARIABLE <out-var>])
  389. Append 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. .. _APPEND_STRING:
  409. ::
  410. cmake_path(APPEND_STRING <path-var> [<input>...] [OUTPUT_VARIABLE <out-var>])
  411. Append all the ``<input>`` arguments to the ``<path-var>`` without adding any
  412. ``directory-separator``.
  413. .. _REMOVE_FILENAME:
  414. ::
  415. cmake_path(REMOVE_FILENAME <path-var> [OUTPUT_VARIABLE <out-var>])
  416. Removes the :ref:`filename <FILENAME_DEF>` component (as returned by
  417. :ref:`GET ... FILENAME <GET_FILENAME>`) from ``<path-var>``. After removal,
  418. any trailing ``directory-separator`` is left alone, if present.
  419. If ``OUTPUT_VARIABLE`` is not given, then after this function returns,
  420. `HAS_FILENAME`_ returns false for ``<path-var>``.
  421. For example:
  422. .. code-block:: cmake
  423. set(path "/a/b")
  424. cmake_path(REMOVE_FILENAME path)
  425. message("First path is \"${path}\"")
  426. # filename is now already empty, the following removes nothing
  427. cmake_path(REMOVE_FILENAME path)
  428. message("Second path is \"${result}\"")
  429. Output::
  430. First path is "/a/"
  431. Second path is "/a/"
  432. .. _REPLACE_FILENAME:
  433. ::
  434. cmake_path(REPLACE_FILENAME <path-var> <input> [OUTPUT_VARIABLE <out-var>])
  435. Replaces the :ref:`filename <FILENAME_DEF>` component from ``<path-var>``
  436. with ``<input>``. If ``<path-var>`` has no filename component (i.e.
  437. `HAS_FILENAME`_ returns false), the path is unchanged. The operation is
  438. equivalent to the following:
  439. .. code-block:: cmake
  440. cmake_path(HAS_FILENAME path has_filename)
  441. if(has_filename)
  442. cmake_path(REMOVE_FILENAME path)
  443. cmake_path(APPEND path input);
  444. endif()
  445. .. _REMOVE_EXTENSION:
  446. ::
  447. cmake_path(REMOVE_EXTENSION <path-var> [LAST_ONLY]
  448. [OUTPUT_VARIABLE <out-var>])
  449. Removes the :ref:`extension <EXTENSION_DEF>`, if any, from ``<path-var>``.
  450. .. _REPLACE_EXTENSION:
  451. ::
  452. cmake_path(REPLACE_EXTENSION <path-var> [LAST_ONLY] <input>
  453. [OUTPUT_VARIABLE <out-var>])
  454. Replaces the :ref:`extension <EXTENSION_DEF>` with ``<input>``. Its effect
  455. is equivalent to the following:
  456. .. code-block:: cmake
  457. cmake_path(REMOVE_EXTENSION path)
  458. if(NOT "input" MATCHES "^\\.")
  459. cmake_path(APPEND_STRING path ".")
  460. endif()
  461. cmake_path(APPEND_STRING path "input")
  462. Generation
  463. ^^^^^^^^^^
  464. .. _NORMAL_PATH:
  465. ::
  466. cmake_path(NORMAL_PATH <path-var> [OUTPUT_VARIABLE <out-var>])
  467. Normalize ``<path-var>`` according the steps described in :ref:`Normalization`.
  468. .. _cmake_path-RELATIVE_PATH:
  469. .. _RELATIVE_PATH:
  470. ::
  471. cmake_path(RELATIVE_PATH <path-var> [BASE_DIRECTORY <input>]
  472. [OUTPUT_VARIABLE <out-var>])
  473. Modifies ``<path-var>`` to make it relative to the ``BASE_DIRECTORY`` argument.
  474. If ``BASE_DIRECTORY`` is not specified, the default base directory will be
  475. :variable:`CMAKE_CURRENT_SOURCE_DIR`.
  476. For reference, the algorithm used to compute the relative path is the same
  477. as that used by C++
  478. `std::filesystem::path::lexically_relative
  479. <https://en.cppreference.com/w/cpp/filesystem/path/lexically_normal>`_.
  480. .. _ABSOLUTE_PATH:
  481. ::
  482. cmake_path(ABSOLUTE_PATH <path-var> [BASE_DIRECTORY <input>] [NORMALIZE]
  483. [OUTPUT_VARIABLE <out-var>])
  484. If ``<path-var>`` is a relative path (`IS_RELATIVE`_ is true), it is evaluated
  485. relative to the given base directory specified by ``BASE_DIRECTORY`` option.
  486. If ``BASE_DIRECTORY`` is not specified, the default base directory will be
  487. :variable:`CMAKE_CURRENT_SOURCE_DIR`.
  488. When the ``NORMALIZE`` option is specified, the path is :ref:`normalized
  489. <Normalization>` after the path computation.
  490. Because ``cmake_path()`` does not access the filesystem, symbolic links are
  491. not resolved. To compute a real path with symbolic links resolved, use the
  492. :command:`file(REAL_PATH)` command instead.
  493. Native Conversion
  494. ^^^^^^^^^^^^^^^^^
  495. For commands in this section, *native* refers to the host platform, not the
  496. target platform when cross-compiling.
  497. .. _cmake_path-NATIVE_PATH:
  498. .. _NATIVE_PATH:
  499. ::
  500. cmake_path(NATIVE_PATH <path-var> [NORMALIZE] <out-var>)
  501. Converts a cmake-style ``<path-var>`` into a native path with
  502. platform-specific slashes (``\`` on Windows hosts and ``/`` elsewhere).
  503. When the ``NORMALIZE`` option is specified, the path is :ref:`normalized
  504. <Normalization>` before the conversion.
  505. .. _CONVERT:
  506. .. _cmake_path-TO_CMAKE_PATH_LIST:
  507. .. _TO_CMAKE_PATH_LIST:
  508. ::
  509. cmake_path(CONVERT <input> TO_CMAKE_PATH_LIST <out-var> [NORMALIZE])
  510. Converts a native ``<input>`` path into a cmake-style path with forward
  511. slashes (``/``). On Windows hosts, the long filename marker is taken into
  512. account. The input can be a single path or a system search path like
  513. ``$ENV{PATH}``. A search path will be converted to a cmake-style list
  514. separated by ``;`` characters (on non-Windows platforms, this essentially
  515. means ``:`` separators are replaced with ``;``). The result of the
  516. conversion is stored in the ``<out-var>`` variable.
  517. When the ``NORMALIZE`` option is specified, the path is :ref:`normalized
  518. <Normalization>` before the conversion.
  519. .. note::
  520. Unlike most other ``cmake_path()`` subcommands, the ``CONVERT`` subcommand
  521. takes a literal string as input, not the name of a variable.
  522. .. _cmake_path-TO_NATIVE_PATH_LIST:
  523. .. _TO_NATIVE_PATH_LIST:
  524. ::
  525. cmake_path(CONVERT <input> TO_NATIVE_PATH_LIST <out-var> [NORMALIZE])
  526. Converts a cmake-style ``<input>`` path into a native path with
  527. platform-specific slashes (``\`` on Windows hosts and ``/`` elsewhere).
  528. The input can be a single path or a cmake-style list. A list will be
  529. converted into a native search path (``;``-separated on Windows,
  530. ``:``-separated on other platforms). The result of the conversion is
  531. stored in the ``<out-var>`` variable.
  532. When the ``NORMALIZE`` option is specified, the path is :ref:`normalized
  533. <Normalization>` before the conversion.
  534. .. note::
  535. Unlike most other ``cmake_path()`` subcommands, the ``CONVERT`` subcommand
  536. takes a literal string as input, not the name of a variable.
  537. For example:
  538. .. code-block:: cmake
  539. set(paths "/a/b/c" "/x/y/z")
  540. cmake_path(CONVERT "${paths}" TO_NATIVE_PATH_LIST native_paths)
  541. message("Native path list is \"${native_paths}\"")
  542. Output on Windows::
  543. Native path list is "\a\b\c;\x\y\z"
  544. Output on all other platforms::
  545. Native path list is "/a/b/c:/x/y/z"
  546. Hashing
  547. ^^^^^^^
  548. .. _HASH:
  549. ::
  550. cmake_path(HASH <path-var> <out-var>)
  551. Compute a hash value of ``<path-var>`` such that for two paths ``p1`` and
  552. ``p2`` that compare equal (:ref:`COMPARE ... EQUAL <COMPARE>`), the hash
  553. value of ``p1`` is equal to the hash value of ``p2``. The path is always
  554. :ref:`normalized <Normalization>` before the hash is computed.