cmake_path.rst 25 KB

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