cmake_path.rst 23 KB

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