cmake_path.rst 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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-var>`` placeholder expect a variable name. An error
  47. will 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. ``<out-var>``
  50. placeholder expect a variable name.
  51. .. note::
  52. ``cmake_path`` command does not support list of paths. The ``<path-var>``
  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 ``<out-var>`` 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-var> :ref:`ROOT_NAME <GET_ROOT_NAME>` <out-var>)
  75. cmake_path(`GET`_ <path-var> :ref:`ROOT_DIRECTORY <GET_ROOT_DIRECTORY>` <out-var>)
  76. cmake_path(`GET`_ <path-var> :ref:`ROOT_PATH <GET_ROOT_PATH>` <out-var>)
  77. cmake_path(`GET`_ <path-var> :ref:`FILENAME <GET_FILENAME>` <out-var>)
  78. cmake_path(`GET`_ <path-var> :ref:`EXTENSION <GET_EXTENSION>` [LAST_ONLY] <out-var>)
  79. cmake_path(`GET`_ <path-var> :ref:`STEM <GET_STEM>` [LAST_ONLY] <out-var>)
  80. cmake_path(`GET`_ <path-var> :ref:`RELATIVE_PATH <GET_RELATIVE_PATH>` <out-var>)
  81. cmake_path(`GET`_ <path-var> :ref:`PARENT_PATH <GET_PARENT_PATH>` <out-var>)
  82. `Modification`_
  83. cmake_path(`APPEND`_ <path-var> [<input>...] [OUTPUT_VARIABLE <out-var>])
  84. cmake_path(`CONCAT`_ <path-var> [<input>...] [OUTPUT_VARIABLE <out-var>])
  85. cmake_path(`REMOVE_FILENAME`_ <path-var> [OUTPUT_VARIABLE <out-var>])
  86. cmake_path(`REPLACE_FILENAME`_ <path-var> <input> [OUTPUT_VARIABLE <out-var>])
  87. cmake_path(`REMOVE_EXTENSION`_ <path-var> [LAST_ONLY]
  88. [OUTPUT_VARIABLE <out-var>])
  89. cmake_path(`REPLACE_EXTENSION`_ <path-var> [LAST_ONLY] <input>
  90. [OUTPUT_VARIABLE <out-var>])
  91. `Generation`_
  92. cmake_path(`NORMAL_PATH`_ <path-var> [OUTPUT_VARIABLE <out-var>])
  93. cmake_path(`RELATIVE_PATH`_ <path-var> [BASE_DIRECTORY <input>]
  94. [OUTPUT_VARIABLE <out-var>])
  95. cmake_path(`PROXIMATE_PATH`_ <path-var> [BASE_DIRECTORY <input>]
  96. [OUTPUT_VARIABLE <out-var>])
  97. cmake_path(`ABSOLUTE_PATH`_ <path-var> [BASE_DIRECTORY <input>] [NORMALIZE]
  98. [OUTPUT_VARIABLE <out-var>])
  99. `Conversion`_
  100. cmake_path(`CMAKE_PATH`_ <path-var> [NORMALIZE] <input>)
  101. cmake_path(`NATIVE_PATH`_ <path-var> [NORMALIZE] <out-var>)
  102. cmake_path(`CONVERT`_ <input> `TO_CMAKE_PATH_LIST`_ <out-var>)
  103. cmake_path(`CONVERT`_ <input> `TO_NATIVE_PATH_LIST`_ <out-var>)
  104. `Comparison`_
  105. cmake_path(`COMPARE`_ <path-var> <OP> <input> <out-var>)
  106. `Query`_
  107. cmake_path(`HAS_ROOT_NAME`_ <path-var> <out-var>)
  108. cmake_path(`HAS_ROOT_DIRECTORY`_ <path-var> <out-var>)
  109. cmake_path(`HAS_ROOT_PATH`_ <path-var> <out-var>)
  110. cmake_path(`HAS_FILENAME`_ <path-var> <out-var>)
  111. cmake_path(`HAS_EXTENSION`_ <path-var> <out-var>)
  112. cmake_path(`HAS_STEM`_ <path-var> <out-var>)
  113. cmake_path(`HAS_RELATIVE_PATH`_ <path-var> <out-var>)
  114. cmake_path(`HAS_PARENT_PATH`_ <path-var> <out-var>)
  115. cmake_path(`IS_ABSOLUTE`_ <path-var> <out-var>)
  116. cmake_path(`IS_RELATIVE`_ <path-var> <out-var>)
  117. cmake_path(`IS_PREFIX`_ <path-var> <input> [NORMALIZE] <out-var>)
  118. `Hashing`_
  119. cmake_path(`HASH`_ <path-var> [NORMALIZE] <out-var>)
  120. Decomposition
  121. ^^^^^^^^^^^^^
  122. .. _GET:
  123. .. _GET_ROOT_NAME:
  124. .. code-block:: cmake
  125. cmake_path(GET <path-var> ROOT_NAME <out-var>)
  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-var> ROOT_DIRECTORY <out-var>)
  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-var> ROOT_PATH <out-var>)
  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-var> FILENAME <out-var>)
  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-var> EXTENSION [LAST_ONLY] <out-var>)
  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-var> STEM [LAST_ONLY] <out-var>)
  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-var> RELATIVE_PATH <out-var>)
  227. Returns path relative to ``root-path``, that is, a pathname composed of
  228. every component of ``<path-var>`` after ``root-path``. If ``<path-var>`` is
  229. an empty 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-var> PARENT_PATH <out-var>)
  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-var>``. Otherwise, the result is ``<path-var>`` 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-var> [<input>...] [OUTPUT_VARIABLE <out-var>])
  263. Append all the ``<input>`` arguments to the ``<path-var>`` using ``/`` as
  264. ``directory-separator``.
  265. For each ``<input>`` argument, the following algorithm (pseudo-code) applies:
  266. .. code-block:: cmake
  267. # <path> is the contents of <path-var>
  268. IF (<input>.is_absolute() OR
  269. (<input>.has_root_name() AND
  270. NOT <input>.root_name() STREQUAL <path>.root_name()))
  271. replaces <path> with <input>
  272. RETURN()
  273. ENDIF()
  274. IF (<input>.has_root_directory())
  275. remove any root-directory and the entire relative path from <path>
  276. ELSEIF (<path>.has_filename() OR
  277. (NOT <path-var>.has_root_directory() OR <path>.is_absolute()))
  278. appends directory-separator to <path>
  279. ENDIF()
  280. appends <input> omitting any root-name to <path>
  281. .. _CONCAT:
  282. .. code-block:: cmake
  283. cmake_path(CONCAT <path-var> [<input>...] [OUTPUT_VARIABLE <out-var>])
  284. Concatenates all the ``<input>`` arguments to the ``<path-var>`` without
  285. ``directory-separator``.
  286. .. _REMOVE_FILENAME:
  287. .. code-block:: cmake
  288. cmake_path(REMOVE_FILENAME <path-var> [OUTPUT_VARIABLE <out-var>])
  289. Removes the :ref:`filename <FILENAME_DEF>` component (as returned by
  290. :ref:`GET ... FILENAME <GET_FILENAME>`) from ``<path-var>``.
  291. After this function returns, if change is done in-place, `HAS_FILENAME`_
  292. returns false for ``<path-var>``.
  293. For Example:
  294. .. code-block:: cmake
  295. set (path "/a/b")
  296. cmake_path (REMOVE_FILENAME path)
  297. message ("First path is \"${path}\"")
  298. cmake_path (REMOVE_FILENAME path)
  299. message ("Second path is \"${result}\"")
  300. Will display::
  301. First path is "/a/"
  302. Second path is "/a/"
  303. .. _REPLACE_FILENAME:
  304. .. code-block:: cmake
  305. cmake_path(REPLACE_FILENAME <path-var> <input> [OUTPUT_VARIABLE <out-var>])
  306. Replaces the :ref:`filename <FILENAME_DEF>` component from ``<path-var>`` with
  307. ``<input>``.
  308. If ``<path-var>`` has no filename component (`HAS_FILENAME`_ returns false),
  309. the path is unchanged.
  310. Equivalent to the following:
  311. .. code-block:: cmake
  312. cmake_path(HAS_FILENAME path has_filename)
  313. if (has_filename)
  314. cmake_path(REMOVE_FILENAME path)
  315. cmake_path(APPEND path "replacement");
  316. endif()
  317. .. _REMOVE_EXTENSION:
  318. .. code-block:: cmake
  319. cmake_path(REMOVE_EXTENSION <path-var> [LAST_ONLY]
  320. [OUTPUT_VARIABLE <out-var>])
  321. Removes the :ref:`extension <EXTENSION_DEF>`, if any, from ``<path-var>``.
  322. .. _REPLACE_EXTENSION:
  323. .. code-block:: cmake
  324. cmake_path(REPLACE_EXTENSION <path-var> [LAST_ONLY] <input>
  325. [OUTPUT_VARIABLE <out-var>])
  326. Replaces the :ref:`extension <EXTENSION_DEF>` with ``<input>``.
  327. 1. If ``<path-var>`` has an :ref:`extension <EXTENSION_DEF>`
  328. (`HAS_EXTENSION`_ is true), it is removed.
  329. 2. A ``dot`` character is appended to ``<path-var>``, if ``<input>`` is not
  330. empty or does not begin with a ``dot`` character.
  331. 3. ``<input>`` is appended as if `CONCAT`_ was used.
  332. Equivalent to the following:
  333. .. code-block:: cmake
  334. cmake_path(REMOVE_EXTENSION path)
  335. if (NOT "input" MATCHES "^\\.")
  336. cmake_path(CONCAT path ".")
  337. endif()
  338. cmake_path(CONCAT path "input");
  339. Generation
  340. ^^^^^^^^^^
  341. .. _NORMAL_PATH:
  342. .. code-block:: cmake
  343. cmake_path(NORMAL_PATH <path-var> [OUTPUT_VARIABLE <out-var>])
  344. Normalize ``<path-var>``.
  345. A path can be normalized by following this algorithm:
  346. 1. If the path is empty, stop (normal form of an empty path is an empty
  347. path).
  348. 2. Replace each ``directory-separator`` (which may consist of multiple
  349. separators) with a single ``/``.
  350. 3. Replace each ``directory-separator`` character in the ``root-name`` with
  351. ``/``.
  352. 4. Remove each ``dot`` and any immediately following ``directory-separator``.
  353. 5. Remove each non-dot-dot filename immediately followed by a
  354. ``directory-separator`` and a ``dot-dot``, along with any immediately
  355. following ``directory-separator``.
  356. 6. If there is ``root-directory``, remove all ``dot-dots`` and any
  357. ``directory-separators`` immediately following them.
  358. 7. If the last filename is ``dot-dot``, remove any trailing
  359. ``directory-separator``.
  360. 8. If the path is empty, add a ``dot`` (normal form of ``./`` is ``.``).
  361. .. _cmake_path-RELATIVE_PATH:
  362. .. _RELATIVE_PATH:
  363. .. code-block:: cmake
  364. cmake_path(RELATIVE_PATH <path-var> [BASE_DIRECTORY <input>]
  365. [OUTPUT_VARIABLE <out-var>])
  366. Returns ``<path-var>`` made relative to ``BASE_DIRECTORY`` argument. If
  367. ``BASE_DIRECTORY`` is not specified, the default base directory will be
  368. :variable:`CMAKE_CURRENT_SOURCE_DIR`.
  369. For reference, the algorithm used to compute the relative path is described
  370. `here <https://en.cppreference.com/w/cpp/filesystem/path/lexically_normal>`_.
  371. .. _PROXIMATE_PATH:
  372. .. code-block:: cmake
  373. cmake_path(PROXIMATE_PATH <path-var> [BASE_DIRECTORY <input>]
  374. [OUTPUT_VARIABLE <out-var>])
  375. If the value of `RELATIVE_PATH`_ is not an empty path, return
  376. it. Otherwise return ``<path-var>``.
  377. If ``BASE_DIRECTORY`` is not specified, the default base directory will be
  378. :variable:`CMAKE_CURRENT_SOURCE_DIR`.
  379. .. _ABSOLUTE_PATH:
  380. .. code-block:: cmake
  381. cmake_path(ABSOLUTE_PATH <path-var> [BASE_DIRECTORY <input>] [NORMALIZE]
  382. [OUTPUT_VARIABLE <out-var>])
  383. If ``<path-var>`` is a relative path (`IS_RELATIVE`_ is true), it is evaluated
  384. relative to the given base directory specified by ``BASE_DIRECTORY`` option.
  385. If ``BASE_DIRECTORY`` is not specifired, the default base directory will be
  386. :variable:`CMAKE_CURRENT_SOURCE_DIR`.
  387. When ``NORMALIZE`` option is specified, the path is :ref:`normalized
  388. <NORMAL_PATH>` after the path computation.
  389. Because ``cmake_path`` does not access to the filesystem, symbolic links are
  390. not resolved. To compute a real path, use :command:`file(REAL_PATH)`
  391. command.
  392. Conversion
  393. ^^^^^^^^^^
  394. .. _cmake_path-CMAKE_PATH:
  395. .. _CMAKE_PATH:
  396. .. code-block:: cmake
  397. cmake_path(CMAKE_PATH <path-var> [NORMALIZE] <input>)
  398. Converts a native ``<input>`` path into cmake-style path with forward-slashes
  399. (``/``). On Windows, the long filename marker is taken into account.
  400. When ``NORMALIZE`` option is specified, the path is :ref:`normalized
  401. <NORMAL_PATH>` before the conversion.
  402. For Example:
  403. .. code-block:: cmake
  404. set (native_path "c:\\a\\b/..\\c")
  405. cmake_path (CMAKE_PATH path "${native_path}")
  406. message ("CMake path is \"${path}\"")
  407. cmake_path (CMAKE_PATH path NORMALIZE "${native_path}")
  408. message ("Normalized CMake path is \"${path}\"")
  409. Will display::
  410. CMake path is "c:/a/b/../c"
  411. Normalized CMake path is "c:/a/c"
  412. .. _cmake_path-NATIVE_PATH:
  413. .. _NATIVE_PATH:
  414. .. code-block:: cmake
  415. cmake_path(NATIVE_PATH <path-var> [NORMALIZE] <out-var>)
  416. Converts a cmake-style ``<path-var>`` into a native
  417. path with platform-specific slashes (``\`` on Windows and ``/`` elsewhere).
  418. When ``NORMALIZE`` option is specified, the path is :ref:`normalized
  419. <NORMAL_PATH>` before the conversion.
  420. .. _CONVERT:
  421. .. _cmake_path-TO_CMAKE_PATH_LIST:
  422. .. _TO_CMAKE_PATH_LIST:
  423. .. code-block:: cmake
  424. cmake_path(CONVERT <input> TO_CMAKE_PATH_LIST <out-var> [NORMALIZE])
  425. Converts a native ``<input>`` path into cmake-style path with forward-slashes
  426. (``/``). On Windows, the long filename marker is taken into account. The input can
  427. be a single path or a system search path like ``$ENV{PATH}``. A search path
  428. will be converted to a cmake-style list separated by ``;`` characters. The
  429. result of the conversion is stored in the ``<out-var>`` variable.
  430. When ``NORMALIZE`` option is specified, the path is :ref:`normalized
  431. <NORMAL_PATH>` before the conversion.
  432. .. _cmake_path-TO_NATIVE_PATH_LIST:
  433. .. _TO_NATIVE_PATH_LIST:
  434. .. code-block:: cmake
  435. cmake_path(CONVERT <input> TO_NATIVE_PATH_LIST <out-var> [NORMALIZE])
  436. Converts a cmake-style ``<input>`` path into a native path with
  437. platform-specific slashes (``\`` on Windows and ``/`` elsewhere). The input can
  438. be a single path or a cmake-style list. A list will be converted into a native
  439. search path. The result of the conversion is stored in the ``<out-var>``
  440. variable.
  441. When ``NORMALIZE`` option is specified, the path is :ref:`normalized
  442. <NORMAL_PATH>` before the conversion.
  443. For Example:
  444. .. code-block:: cmake
  445. set (paths "/a/b/c" "/x/y/z")
  446. cmake_path (CONVERT "${paths}" TO_NATIVE_PATH_LIST native_paths)
  447. message ("Native path list is \"${native_paths}\"")
  448. Will display, on Windows::
  449. Native path list is "\a\b\c;\x\y\z"
  450. And on the all other systems::
  451. Native path list is "/a/b/c:/x/y/z"
  452. Comparison
  453. ^^^^^^^^^^
  454. .. _COMPARE:
  455. .. code-block:: cmake
  456. cmake_path(COMPARE <path-var> EQUAL <input> <out-var>)
  457. cmake_path(COMPARE <path-var> NOT_EQUAL <input> <out-var>)
  458. Compares the lexical representations of the path and another path.
  459. For testing equality, the following algorithm (pseudo-code) apply:
  460. .. code-block:: cmake
  461. # <path> is the contents of <path-var>
  462. IF (NOT <path>.root_name() STREQUAL <input>.root_name())
  463. returns FALSE
  464. ELSEIF (<path>.has_root_directory() XOR <input>.has_root_directory())
  465. returns FALSE
  466. ENDIF()
  467. returns TRUE or FALSE if the relative portion of <path> is
  468. lexicographically equal or not to the relative portion of <input>.
  469. Comparison is performed path component-wise
  470. Query
  471. ^^^^^
  472. .. _HAS_ROOT_NAME:
  473. .. code-block:: cmake
  474. cmake_path(HAS_ROOT_NAME <path-var> <out-var>)
  475. Checks if ``<path-var>`` has ``root-name``.
  476. .. _HAS_ROOT_DIRECTORY:
  477. .. code-block:: cmake
  478. cmake_path(HAS_ROOT_DIRECTORY <path-var> <out-var>)
  479. Checks if ``<path-var>`` has ``root-directory``.
  480. .. _HAS_ROOT_PATH:
  481. .. code-block:: cmake
  482. cmake_path(HAS_ROOT_PATH <path-var> <out-var>)
  483. Checks if ``<path-var>`` has root path.
  484. Effectively, checks if ``<path-var>`` has ``root-name`` and ``root-directory``.
  485. .. _HAS_FILENAME:
  486. .. code-block:: cmake
  487. cmake_path(HAS_FILENAME <path-var> <out-var>)
  488. Checks if ``<path-var>`` has a :ref:`filename <FILENAME_DEF>`.
  489. .. _HAS_EXTENSION:
  490. .. code-block:: cmake
  491. cmake_path(HAS_EXTENSION <path-var> <out-var>)
  492. Checks if ``<path-var>`` has an :ref:`extension <EXTENSION_DEF>`. If the first
  493. character in the filename is a period, it is not treated as an extension (for
  494. example ".profile").
  495. .. _HAS_STEM:
  496. .. code-block:: cmake
  497. cmake_path(HAS_STEM <path-var> <out-var>)
  498. Checks if ``<path-var>`` has stem (:ref:`GET ... STEM <GET_STEM>` returns a non
  499. empty path).
  500. .. _HAS_RELATIVE_PATH:
  501. .. code-block:: cmake
  502. cmake_path(HAS_RELATIVE_PATH <path-var> <out-var>)
  503. Checks if ``<path-var>`` has relative path (`GET_RELATIVE_PATH`_ returns a
  504. non-empty path).
  505. .. _HAS_PARENT_PATH:
  506. .. code-block:: cmake
  507. cmake_path(HAS_PARENT_PATH <path-var> <out-var>)
  508. Checks if ``<path-var>`` has parent path. The result is true except if the path
  509. is only composed of a :ref:`filename <FILENAME_DEF>`.
  510. .. _IS_ABSOLUTE:
  511. .. code-block:: cmake
  512. cmake_path(IS_ABSOLUTE <path-var> <out-var>)
  513. Checks if ``<path-var>`` is absolute.
  514. An absolute path is a path that unambiguously identifies the location of a file
  515. without reference to an additional starting location.
  516. .. _IS_RELATIVE:
  517. .. code-block:: cmake
  518. cmake_path(IS_RELATIVE <path-var> <out-var>)
  519. Checks if path is relative (i.e. not :ref:`absolute <IS_ABSOLUTE>`).
  520. .. _IS_PREFIX:
  521. .. code-block:: cmake
  522. cmake_path(IS_PREFIX <path-var> <input> [NORMALIZE] <out-var>)
  523. Checks if ``<path-var>`` is the prefix of ``<input>``.
  524. When ``NORMALIZE`` option is specified, the paths are :ref:`normalized
  525. <NORMAL_PATH>` before the check.
  526. Hashing
  527. ^^^^^^^
  528. .. _HASH:
  529. .. code-block:: cmake
  530. cmake_path(HASH <path-var> [NORMALIZE] <out-var>)
  531. Compute hash value of ``<path-var>`` such that if for two paths (``p1`` and
  532. ``p2``) are equal (:ref:`COMPARE ... EQUAL <COMPARE>`) then hash value of p1 is
  533. equal to hash value of p2.
  534. When ``NORMALIZE`` option is specified, the paths are :ref:`normalized
  535. <NORMAL_PATH>` before the check.