cmake_path.rst 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. cmake_path
  2. ----------
  3. .. versionadded:: 3.19
  4. Filesystem path manipulation command.
  5. This command is dedicated to the manipulation of objects of type path which
  6. represent paths on a filesystem. Only syntactic aspects of paths are handled:
  7. the pathname may represent a non-existing path or even one that is not allowed
  8. to exist on the current file system or OS.
  9. For operations involving the filesystem, have a look at the :command:`file`
  10. command.
  11. The path name has the following syntax:
  12. 1. ``root-name`` (optional): identifies the root on a filesystem with multiple
  13. roots (such as ``"C:"`` or ``"//myserver"``).
  14. 2. ``root-directory`` (optional): a directory separator that, if present, marks
  15. this path as absolute. If it is missing (and the first element other than
  16. the ``root-name`` is a ``item-name``), then the path is relative.
  17. Zero or more of the following:
  18. 3. ``item-name``: sequence of characters that aren't directory separators. This
  19. name may identify a file, a hard link, a symbolic link, or a directory. Two
  20. special ``item-names`` are recognized:
  21. * ``dot``: the item name consisting of a single dot character ``.`` is a
  22. directory name that refers to the current directory.
  23. * ``dot-dot``: the item name consisting of two dot characters ``..`` is a
  24. directory name that refers to the parent directory.
  25. 4. ``directory-separator``: the forward slash character ``/``. If this
  26. character is repeated, it is treated as a single directory separator:
  27. ``/usr///////lib`` is the same as ``/usr/lib``.
  28. .. _FILENAME_DEF:
  29. A path has a filename if it does not ends with a ``directory-separator``. The
  30. filename is the last ``item-name`` of the path.
  31. .. _EXTENSION_DEF:
  32. A :ref:`filename <FILENAME_DEF>` can have an extension. By default, the
  33. extension is defined as the sub-string beginning at the leftmost period
  34. (including the period) and until the end of the pathname. When the option
  35. ``LAST_ONLY`` is specified, the extension is the sub-string beginning at the
  36. rightmost period.
  37. The following exceptions apply:
  38. * If the first character in the :ref:`filename <FILENAME_DEF>` is a period,
  39. that period is ignored (a filename like ``".profile"`` is not treated as an
  40. extension).
  41. * If the pathname is either ``.`` or ``..``.
  42. .. note::
  43. ``cmake_path`` command handles paths in the format of the build system, not
  44. the target system. So this is not generally applicable to the target system
  45. in cross-compiling environment.
  46. For all commands, ``<path>`` placeholder expect a variable name. An error will
  47. be raised if the variable does not exist, except for `APPEND`_ and
  48. `CMAKE_PATH`_ sub-commands. ``<input>`` placeholder expect a string literal.
  49. ``[<input>...]`` placeholder expect zero or more arguments. ``<output>``
  50. placeholder expect a variable name.
  51. .. note::
  52. ``cmake_path`` command does not support list of paths. The ``<path>``
  53. placeholder must store only one path name.
  54. To initialize a path variable, three possibilities can be used:
  55. 1. :command:`set` command.
  56. 2. :ref:`cmake_path(APPEND) <APPEND>` command. Can be used to build a path from
  57. already available path fragments.
  58. 3. :ref:`cmake_path(CMAKE_PATH) <CMAKE_PATH>` command. Mainly used to build a
  59. path variable from a native path.
  60. .. code-block:: cmake
  61. # To build the path "${CMAKE_CURRENT_SOURCE_DIR}/data"
  62. set (path1 "${CMAKE_CURRENT_SOURCE_DIR}/data")
  63. cmake_path(APPEND path2 "${CMAKE_CURRENT_SOURCE_DIR}" "data")
  64. cmake_path(CMAKE_PATH path3 "${CMAKE_CURRENT_SOURCE_DIR}/data")
  65. `Modification`_ and `Generation`_ sub-commands store the result in-place or in
  66. the variable specified by ``OUTPUT_VARIABLE`` option. All other sub-commands,
  67. except `CMAKE_PATH`_, store the result in the required ``<output>`` variable.
  68. Sub-commands supporting ``NORMALIZE`` option will :ref:`normalize <NORMAL_PATH>`
  69. the path.
  70. Synopsis
  71. ^^^^^^^^
  72. .. parsed-literal::
  73. `Decomposition`_
  74. cmake_path(`GET`_ <path> :ref:`ROOT_NAME <GET_ROOT_NAME>` <output>)
  75. cmake_path(`GET`_ <path> :ref:`ROOT_DIRECTORY <GET_ROOT_DIRECTORY>` <output>)
  76. cmake_path(`GET`_ <path> :ref:`ROOT_PATH <GET_ROOT_PATH>` <output>)
  77. cmake_path(`GET`_ <path> :ref:`FILENAME <GET_FILENAME>` <output>)
  78. cmake_path(`GET`_ <path> :ref:`EXTENSION <GET_EXTENSION>` [LAST_ONLY] <output>)
  79. cmake_path(`GET`_ <path> :ref:`STEM <GET_STEM>` [LAST_ONLY] <output>)
  80. cmake_path(`GET`_ <path> :ref:`RELATIVE_PATH <GET_RELATIVE_PATH>` <output>)
  81. cmake_path(`GET`_ <path> :ref:`PARENT_PATH <GET_PARENT_PATH>` <output>)
  82. `Modification`_
  83. cmake_path(`APPEND`_ <path> [<input>...] [OUTPUT_VARIABLE <output>])
  84. cmake_path(`CONCAT`_ <path> [<input>...] [OUTPUT_VARIABLE <output>])
  85. cmake_path(`REMOVE_FILENAME`_ <path> [OUTPUT_VARIABLE <output>])
  86. cmake_path(`REPLACE_FILENAME`_ <path> <input> [OUTPUT_VARIABLE <output>])
  87. cmake_path(`REMOVE_EXTENSION`_ <path> [LAST_ONLY]
  88. [OUTPUT_VARIABLE <output>])
  89. cmake_path(`REPLACE_EXTENSION`_ <path> [LAST_ONLY] <input>
  90. [OUTPUT_VARIABLE <output>])
  91. `Generation`_
  92. cmake_path(`NORMAL_PATH`_ <path> [OUTPUT_VARIABLE <output>])
  93. cmake_path(`RELATIVE_PATH`_ <path> [BASE_DIRECTORY <path>]
  94. [OUTPUT_VARIABLE <output>])
  95. cmake_path(`PROXIMATE_PATH`_ <path> [BASE_DIRECTORY <path>]
  96. [OUTPUT_VARIABLE <output>])
  97. cmake_path(`ABSOLUTE_PATH`_ <path> [BASE_DIRECTORY <path>] [NORMALIZE]
  98. [OUTPUT_VARIABLE <output>])
  99. `Conversion`_
  100. cmake_path(`CMAKE_PATH`_ <path> [NORMALIZE] <input>)
  101. cmake_path(`NATIVE_PATH`_ <path> [NORMALIZE] <output>)
  102. cmake_path(`CONVERT`_ <input> `TO_CMAKE_PATH_LIST`_ <output>)
  103. cmake_path(`CONVERT`_ <input> `TO_NATIVE_PATH_LIST`_ <output>)
  104. `Comparison`_
  105. cmake_path(`COMPARE`_ <path> <OP> <input> <output>)
  106. `Query`_
  107. cmake_path(`HAS_ROOT_NAME`_ <path> <output>)
  108. cmake_path(`HAS_ROOT_DIRECTORY`_ <path> <output>)
  109. cmake_path(`HAS_ROOT_PATH`_ <path> <output>)
  110. cmake_path(`HAS_FILENAME`_ <path> <output>)
  111. cmake_path(`HAS_EXTENSION`_ <path> <output>)
  112. cmake_path(`HAS_STEM`_ <path> <output>)
  113. cmake_path(`HAS_RELATIVE_PATH`_ <path> <output>)
  114. cmake_path(`HAS_PARENT_PATH`_ <path> <output>)
  115. cmake_path(`IS_ABSOLUTE`_ <path> <output>)
  116. cmake_path(`IS_RELATIVE`_ <path> <output>)
  117. cmake_path(`IS_PREFIX`_ <path> <input> [NORMALIZE] <output>)
  118. `Hashing`_
  119. cmake_path(`HASH`_ <path> [NORMALIZE] <output>)
  120. Decomposition
  121. ^^^^^^^^^^^^^
  122. .. _GET:
  123. .. _GET_ROOT_NAME:
  124. .. code-block:: cmake
  125. cmake_path(GET <path> ROOT_NAME <output>)
  126. Returns the root name of the path. If the path does not include a root name,
  127. returns an empty path.
  128. .. note::
  129. Only ``Windows`` system has the concept of ``root-name``, so on all other
  130. systems, it is always an empty path.
  131. For example:
  132. .. code-block:: cmake
  133. set (path "c:/a")
  134. cmake_path (GET path ROOT_NAME output)
  135. message ("Root name is \"${output}\"")
  136. Will display::
  137. Root name is "c:"
  138. .. _GET_ROOT_DIRECTORY:
  139. .. code-block:: cmake
  140. cmake_path(GET <path> ROOT_DIRECTORY <output>)
  141. Returns the root directory of the path. If the path does not include a root
  142. directory, returns an empty path.
  143. For example:
  144. .. code-block:: cmake
  145. set (path "c:/a")
  146. cmake_path (GET path ROOT_DIRECTORY output)
  147. message ("Root directory is \"${output}\"")
  148. Will display::
  149. Root directory is "/"
  150. .. _GET_ROOT_PATH:
  151. .. code-block:: cmake
  152. cmake_path(GET <path> ROOT_PATH <output>)
  153. Returns the root path of the path. If the path does not include a root path,
  154. returns an empty path.
  155. Effectively, returns the following: ``root-name root-directory``.
  156. For example:
  157. .. code-block:: cmake
  158. set (path "c:/a")
  159. cmake_path (GET path ROOT_PATH output)
  160. message ("Root path is \"${output}\"")
  161. Will display::
  162. Root path is "c:/"
  163. .. _GET_FILENAME:
  164. .. code-block:: cmake
  165. cmake_path(GET <path> FILENAME <output>)
  166. Returns the :ref:`filename <FILENAME_DEF>` component of the path. If the path
  167. ends with a ``directory-separator``, there is no filename, so returns an empty
  168. path.
  169. For example:
  170. .. code-block:: cmake
  171. set (path "/a")
  172. cmake_path (GET path FILENAME output)
  173. message ("First filename is \"${output}\"")
  174. set (path "/a/")
  175. cmake_path (GET path FILENAME output)
  176. message ("Second filename is \"${output}\"")
  177. Will display::
  178. First filename is "a"
  179. Second filename is ""
  180. .. _GET_EXTENSION:
  181. .. code-block:: cmake
  182. cmake_path(GET <path> EXTENSION [LAST_ONLY] <output>)
  183. Returns the :ref:`extension <EXTENSION_DEF>` of the filename component.
  184. If the :ref:`filename <FILENAME_DEF>` component of the path contains a period
  185. (``.``), and is not one of the special filesystem elements ``dot`` or
  186. ``dot-dot``, then the :ref:`extension <EXTENSION_DEF>` is returned.
  187. For example:
  188. .. code-block:: cmake
  189. set (path "name.ext1.ext2")
  190. cmake_path (GET path EXTENSION result)
  191. message ("Full extension is \"${result}\"")
  192. cmake_path (GET path EXTENSION LAST_ONLY result)
  193. message ("Last extension is \"${result}\"")
  194. Will display::
  195. Full extension is ".ext1.ext2"
  196. Last extension is ".ext2"
  197. The following exceptions apply:
  198. * If the first character in the filename is a period, that period is ignored
  199. (a filename like ``".profile"`` is not treated as an extension).
  200. * If the pathname is either ``.`` or ``..``, or if
  201. :ref:`filename <FILENAME_DEF>` component does not contain the ``.``
  202. character, then an empty path is returned.
  203. .. _GET_STEM:
  204. .. code-block:: cmake
  205. cmake_path(GET <path> STEM [LAST_ONLY] <output>)
  206. Returns the :ref:`filename <FILENAME_DEF>` component of the path stripped of
  207. its :ref:`extension <EXTENSION_DEF>`.
  208. For Example:
  209. .. code-block:: cmake
  210. set (path "name.ext1.ext2")
  211. cmake_path (GET path STEM result)
  212. message ("Filename without the extension is \"${result}\"")
  213. cmake_path (GET path STEM LAST_ONLY result)
  214. message ("Filename whiteout the last extension is \"${result}\"")
  215. Will display::
  216. Filename without the extension is "name"
  217. Filename without the last extension is "name.ext1"
  218. The following exceptions apply:
  219. * If the first character in the filename is a period, that period is ignored
  220. (a filename like ``".profile"`` is not treated as an extension).
  221. * If the filename is one of the special filesystem components ``dot`` or
  222. ``dot-dot``, or if it has no periods, the function returns the entire
  223. :ref:`filename <FILENAME_DEF>` component.
  224. .. _GET_RELATIVE_PATH:
  225. .. code-block:: cmake
  226. cmake_path(GET <path> RELATIVE_PATH <output>)
  227. Returns path relative to ``root-path``, that is, a pathname composed of
  228. every component of ``<path>`` after ``root-path``. If ``<path>`` is an empty
  229. path, returns an empty path.
  230. For Example:
  231. .. code-block:: cmake
  232. set (path "/a/b")
  233. cmake_path (GET path RELATIVE_PATH result)
  234. message ("Relative path is \"${result}\"")
  235. set (path "/")
  236. cmake_path (GET path RELATIVE_PATH result)
  237. message ("Relative path is \"${result}\"")
  238. Will display::
  239. Relative path is "a/b"
  240. Relative path is ""
  241. .. _GET_PARENT_PATH:
  242. .. code-block:: cmake
  243. cmake_path(GET <path> PARENT_PATH <output>)
  244. Returns the path to the parent directory.
  245. If `HAS_RELATIVE_PATH`_ sub-command returns false, the result is a copy of
  246. ``<path>``. Otherwise, the result is ``<path>`` with one fewer element.
  247. For Example:
  248. .. code-block:: cmake
  249. set (path "c:/a/b")
  250. cmake_path (GET path PARENT_PATH result)
  251. message ("Parent path is \"${result}\"")
  252. set (path "c:/")
  253. cmake_path (GET path PARENT_PATH result)
  254. message ("Parent path is \"${result}\"")
  255. Will display::
  256. Parent path is "c:/a"
  257. Relative path is "c:/"
  258. Modification
  259. ^^^^^^^^^^^^
  260. .. _APPEND:
  261. .. code-block:: cmake
  262. cmake_path(APPEND <path> [<input>...] [OUTPUT_VARIABLE <output>])
  263. Append all the ``<input>`` arguments to the ``<path>`` using ``/`` as
  264. ``directory-separator``.
  265. For each ``<input>`` argument, the following algorithm (pseudo-code) applies:
  266. .. code-block:: cmake
  267. IF (<input>.is_absolute() OR
  268. (<input>.has_root_name() AND
  269. NOT <input>.root_name() STREQUAL <path>.root_name()))
  270. replaces <path> with <input>
  271. RETURN()
  272. ENDIF()
  273. IF (<input>.has_root_directory())
  274. remove any root-directory and the entire relative path from <path>
  275. ELSEIF (<path>.has_filename() OR
  276. (NOT <path>.has_root_directory() OR <path>.is_absolute()))
  277. appends directory-separator to <path>
  278. ENDIF()
  279. appends <input> omitting any root-name to <path>
  280. .. _CONCAT:
  281. .. code-block:: cmake
  282. cmake_path(CONCAT <path> [<input>...] [OUTPUT_VARIABLE <output>])
  283. Concatenates all the ``<input>`` arguments to the ``<path>`` without
  284. ``directory-separator``.
  285. .. _REMOVE_FILENAME:
  286. .. code-block:: cmake
  287. cmake_path(REMOVE_FILENAME <path> [OUTPUT_VARIABLE <output>])
  288. Removes the :ref:`filename <FILENAME_DEF>` component (as returned by
  289. :ref:`GET ... FILENAME <GET_FILENAME>`) from ``<path>``.
  290. After this function returns, if change is done in-place, `HAS_FILENAME`_
  291. returns false for ``<path>``.
  292. For Example:
  293. .. code-block:: cmake
  294. set (path "/a/b")
  295. cmake_path (REMOVE_FILENAME path)
  296. message ("First path is \"${path}\"")
  297. cmake_path (REMOVE_FILENAME path)
  298. message ("Second path is \"${result}\"")
  299. Will display::
  300. First path is "/a/"
  301. Second path is "/a/"
  302. .. _REPLACE_FILENAME:
  303. .. code-block:: cmake
  304. cmake_path(REPLACE_FILENAME <path> <input> [OUTPUT_VARIABLE <output>])
  305. Replaces the :ref:`filename <FILENAME_DEF>` component from ``<path>`` with
  306. ``<input>``.
  307. If ``<path>`` has no filename component (`HAS_FILENAME`_ returns false), the
  308. path is unchanged.
  309. Equivalent to the following:
  310. .. code-block:: cmake
  311. cmake_path(HAS_FILENAME path has_filename)
  312. if (has_filename)
  313. cmake_path(REMOVE_FILENAME path)
  314. cmake_path(APPEND path "replacement");
  315. endif()
  316. .. _REMOVE_EXTENSION:
  317. .. code-block:: cmake
  318. cmake_path(REMOVE_EXTENSION <path> [LAST_ONLY] [OUTPUT_VARIABLE <output>])
  319. Removes the :ref:`extension <EXTENSION_DEF>`, if any, from ``<path>``.
  320. .. _REPLACE_EXTENSION:
  321. .. code-block:: cmake
  322. cmake_path(REPLACE_EXTENSION <path> [LAST_ONLY] <input>
  323. [OUTPUT_VARIABLE <output>])
  324. Replaces the :ref:`extension <EXTENSION_DEF>` with ``<input>``.
  325. 1. If ``<path>`` has an :ref:`extension <EXTENSION_DEF>`
  326. (`HAS_EXTENSION`_ is true), it is removed.
  327. 2. A ``dot`` character is appended to ``<path>``, if ``<input>`` is not empty
  328. or does not begin with a ``dot`` character.
  329. 3. ``<input>`` is appended as if `CONCAT`_ was used.
  330. Equivalent to the following:
  331. .. code-block:: cmake
  332. cmake_path(REMOVE_EXTENSION path)
  333. if (NOT "input" MATCHES "^\\.")
  334. cmake_path(CONCAT path ".")
  335. endif()
  336. cmake_path(CONCAT path "input");
  337. Generation
  338. ^^^^^^^^^^
  339. .. _NORMAL_PATH:
  340. .. code-block:: cmake
  341. cmake_path(NORMAL_PATH <path> [OUTPUT_VARIABLE <output>])
  342. Normalize ``<path>``.
  343. A path can be normalized by following this algorithm:
  344. 1. If the path is empty, stop (normal form of an empty path is an empty
  345. path).
  346. 2. Replace each ``directory-separator`` (which may consist of multiple
  347. separators) with a single ``/``.
  348. 3. Replace each ``directory-separator`` character in the ``root-name`` with
  349. ``/``.
  350. 4. Remove each ``dot`` and any immediately following ``directory-separator``.
  351. 5. Remove each non-dot-dot filename immediately followed by a
  352. ``directory-separator`` and a ``dot-dot``, along with any immediately
  353. following ``directory-separator``.
  354. 6. If there is ``root-directory``, remove all ``dot-dots`` and any
  355. ``directory-separators`` immediately following them.
  356. 7. If the last filename is ``dot-dot``, remove any trailing
  357. ``directory-separator``.
  358. 8. If the path is empty, add a ``dot`` (normal form of ``./`` is ``.``).
  359. .. _cmake_path-RELATIVE_PATH:
  360. .. _RELATIVE_PATH:
  361. .. code-block:: cmake
  362. cmake_path(RELATIVE_PATH <path> [BASE_DIRECTORY <path>]
  363. [OUTPUT_VARIABLE <output>])
  364. Returns ``<path>`` made relative to ``BASE_DIRECTORY`` argument. If
  365. ``BASE_DIRECTORY`` is not specified, the default base directory will be
  366. :variable:`CMAKE_CURRENT_SOURCE_DIR`.
  367. For reference, the algorithm used to compute the relative path is described
  368. `here <https://en.cppreference.com/w/cpp/filesystem/path/lexically_normal>`_.
  369. .. _PROXIMATE_PATH:
  370. .. code-block:: cmake
  371. cmake_path(PROXIMATE_PATH <path> [BASE_DIRECTORY <path>]
  372. [OUTPUT_VARIABLE <output>])
  373. If the value of `RELATIVE_PATH`_ is not an empty path, return
  374. it. Otherwise return ``<path>``.
  375. If ``BASE_DIRECTORY`` is not specified, the default base directory will be
  376. :variable:`CMAKE_CURRENT_SOURCE_DIR`.
  377. .. _ABSOLUTE_PATH:
  378. .. code-block:: cmake
  379. cmake_path(ABSOLUTE_PATH <path> [BASE_DIRECTORY <path>] [NORMALIZE]
  380. [OUTPUT_VARIABLE <output>])
  381. If ``<path>`` is a relative path (`IS_RELATIVE`_ is true), it is evaluated
  382. relative to the given base directory specified by ``BASE_DIRECTORY`` option.
  383. If ``BASE_DIRECTORY`` is not specifired, the default base directory will be
  384. :variable:`CMAKE_CURRENT_SOURCE_DIR`.
  385. When ``NORMALIZE`` option is specified, the path is :ref:`normalized
  386. <NORMAL_PATH>` after the path computation.
  387. Because ``cmake_path`` does not access to the filesystem, symbolic links are
  388. not resolved. To compute a real path, use :command:`file(REAL_PATH)`
  389. command.
  390. Conversion
  391. ^^^^^^^^^^
  392. .. _cmake_path-CMAKE_PATH:
  393. .. _CMAKE_PATH:
  394. .. code-block:: cmake
  395. cmake_path(CMAKE_PATH <path> [NORMALIZE] <input>)
  396. Converts a native ``<input>`` path into cmake-style path with forward-slashes
  397. (``/``). On Windows, the long filename marker is taken into account.
  398. When ``NORMALIZE`` option is specified, the path is :ref:`normalized
  399. <NORMAL_PATH>` before the conversion.
  400. For Example:
  401. .. code-block:: cmake
  402. set (native_path "c:\\a\\b/..\\c")
  403. cmake_path (CMAKE_PATH path "${native_path}")
  404. message ("CMake path is \"${path}\"")
  405. cmake_path (CMAKE_PATH path NORMALIZE "${native_path}")
  406. message ("Normalized CMake path is \"${path}\"")
  407. Will display::
  408. CMake path is "c:/a/b/../c"
  409. Normalized CMake path is "c:/a/c"
  410. .. _cmake_path-NATIVE_PATH:
  411. .. _NATIVE_PATH:
  412. .. code-block:: cmake
  413. cmake_path(NATIVE_PATH <path> [NORMALIZE] <output>)
  414. Converts a cmake-style ``<path>`` into a native
  415. path with platform-specific slashes (``\`` on Windows and ``/`` elsewhere).
  416. When ``NORMALIZE`` option is specified, the path is :ref:`normalized
  417. <NORMAL_PATH>` before the conversion.
  418. .. _CONVERT:
  419. .. _cmake_path-TO_CMAKE_PATH_LIST:
  420. .. _TO_CMAKE_PATH_LIST:
  421. .. code-block:: cmake
  422. cmake_path(CONVERT <input> TO_CMAKE_PATH_LIST <output> [NORMALIZE])
  423. Converts a native ``<input>`` path into cmake-style path with forward-slashes
  424. (``/``). On Windows, the long filename marker is taken into account. The input can
  425. be a single path or a system search path like ``$ENV{PATH}``. A search path
  426. will be converted to a cmake-style list separated by ``;`` characters. The
  427. result of the conversion is stored in the ``<output>`` variable.
  428. When ``NORMALIZE`` option is specified, the path is :ref:`normalized
  429. <NORMAL_PATH>` before the conversion.
  430. .. _cmake_path-TO_NATIVE_PATH_LIST:
  431. .. _TO_NATIVE_PATH_LIST:
  432. .. code-block:: cmake
  433. cmake_path(CONVERT <input> TO_NATIVE_PATH_LIST <output> [NORMALIZE])
  434. Converts a cmake-style ``<input>`` path into a native path with
  435. platform-specific slashes (``\`` on Windows and ``/`` elsewhere). The input can
  436. be a single path or a cmake-style list. A list will be converted into a native
  437. search path. The result of the conversion is stored in the ``<output>``
  438. variable.
  439. When ``NORMALIZE`` option is specified, the path is :ref:`normalized
  440. <NORMAL_PATH>` before the conversion.
  441. For Example:
  442. .. code-block:: cmake
  443. set (paths "/a/b/c" "/x/y/z")
  444. cmake_path (CONVERT "${paths}" TO_NATIVE_PATH_LIST native_paths)
  445. message ("Native path list is \"${native_paths}\"")
  446. Will display, on Windows::
  447. Native path list is "\a\b\c;\x\y\z"
  448. And on the all other systems::
  449. Native path list is "/a/b/c:/x/y/z"
  450. Comparison
  451. ^^^^^^^^^^
  452. .. _COMPARE:
  453. .. code-block:: cmake
  454. cmake_path(COMPARE <path> EQUAL <input> <output>)
  455. cmake_path(COMPARE <path> NOT_EQUAL <input> <output>)
  456. Compares the lexical representations of the path and another path.
  457. For testing equality, the following algorithm (pseudo-code) apply:
  458. .. code-block:: cmake
  459. IF (NOT <path>.root_name() STREQUAL <input>.root_name())
  460. returns FALSE
  461. ELSEIF (<path>.has_root_directory() XOR <input>.has_root_directory())
  462. returns FALSE
  463. ENDIF()
  464. returns TRUE or FALSE if the relative portion of <path> is
  465. lexicographically equal or not to the relative portion of <input>.
  466. Comparison is performed path component-wise
  467. Query
  468. ^^^^^
  469. .. _HAS_ROOT_NAME:
  470. .. code-block:: cmake
  471. cmake_path(HAS_ROOT_NAME <path> <output>)
  472. Checks if ``<path>`` has ``root-name``.
  473. .. _HAS_ROOT_DIRECTORY:
  474. .. code-block:: cmake
  475. cmake_path(HAS_ROOT_DIRECTORY <path> <output>)
  476. Checks if ``<path>`` has ``root-directory``.
  477. .. _HAS_ROOT_PATH:
  478. .. code-block:: cmake
  479. cmake_path(HAS_ROOT_PATH <path> <output>)
  480. Checks if ``<path>`` has root path.
  481. Effectively, checks if ``<path>`` has ``root-name`` and ``root-directory``.
  482. .. _HAS_FILENAME:
  483. .. code-block:: cmake
  484. cmake_path(HAS_FILENAME <path> <output>)
  485. Checks if ``<path>`` has a :ref:`filename <FILENAME_DEF>`.
  486. .. _HAS_EXTENSION:
  487. .. code-block:: cmake
  488. cmake_path(HAS_EXTENSION <path> <output>)
  489. Checks if ``<path>`` has an :ref:`extension <EXTENSION_DEF>`. If the first
  490. character in the filename is a period, it is not treated as an extension (for
  491. example ".profile").
  492. .. _HAS_STEM:
  493. .. code-block:: cmake
  494. cmake_path(HAS_STEM <path> <output>)
  495. Checks if ``<path>`` has stem (:ref:`GET ... STEM <GET_STEM>` returns a non
  496. empty path).
  497. .. _HAS_RELATIVE_PATH:
  498. .. code-block:: cmake
  499. cmake_path(HAS_RELATIVE_PATH <path> <output>)
  500. Checks if ``<path>`` has relative path (`GET_RELATIVE_PATH`_ returns a
  501. non-empty path).
  502. .. _HAS_PARENT_PATH:
  503. .. code-block:: cmake
  504. cmake_path(HAS_PARENT_PATH <path> <output>)
  505. Checks if ``<path>`` has parent path. The result is true except if the path is
  506. only composed of a :ref:`filename <FILENAME_DEF>`.
  507. .. _IS_ABSOLUTE:
  508. .. code-block:: cmake
  509. cmake_path(IS_ABSOLUTE <path> <output>)
  510. Checks if ``<path>`` is absolute.
  511. An absolute path is a path that unambiguously identifies the location of a file
  512. without reference to an additional starting location.
  513. .. _IS_RELATIVE:
  514. .. code-block:: cmake
  515. cmake_path(IS_RELATIVE <path> <output>)
  516. Checks if path is relative (i.e. not :ref:`absolute <IS_ABSOLUTE>`).
  517. .. _IS_PREFIX:
  518. .. code-block:: cmake
  519. cmake_path(IS_PREFIX <path> <input> [NORMALIZE] <output>)
  520. Checks if ``<path>`` is the prefix of ``<input>``.
  521. When ``NORMALIZE`` option is specified, the paths are :ref:`normalized
  522. <NORMAL_PATH>` before the check.
  523. Hashing
  524. ^^^^^^^
  525. .. _HASH:
  526. .. code-block:: cmake
  527. cmake_path(HASH <path> [NORMALIZE] <output>)
  528. Compute hash value of ``<path>`` such that if for two paths (``p1`` and ``p2``)
  529. are equal (:ref:`COMPARE ... EQUAL <COMPARE>`) then hash value of p1 is equal
  530. to hash value of p2.
  531. When ``NORMALIZE`` option is specified, the paths are :ref:`normalized
  532. <NORMAL_PATH>` before the check.