5

I'm configuring some project with CMake. I need to get the path separator (e.g. / or \) into a CMake variable to do something with in, never mind what exactly. How can I do that?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 1
    Are the CMake commands `file(TO_CMAKE_PATH "${dir}" dir)` and `file(TO_NATIVE_PATH "${dir}" dir)` of any help? – vre Jan 26 '18 at 10:48
  • @vre: Possibly, if you can explain them a little more... – einpoklum Jan 26 '18 at 11:04
  • From the docs: The TO_CMAKE_PATH mode converts a native into a cmake-style path with forward-slashes (/). The input can be a single path or a system search path like $ENV{PATH}. A search path will be converted to a cmake-style list separated by ; characters. The TO_NATIVE_PATH mode converts a cmake-style into a native path with platform-specific slashes (\ on Windows and / elsewhere). Always use double quotes around the to be sure it is treated as a single argument to this command. – vre Jan 26 '18 at 11:09
  • @vre: But why would the empty path (`"${dir}"` is `""` initially, after all) result in something interesting after conversion? – einpoklum Jan 26 '18 at 20:13
  • I just tested with the following CMakeLists.txt `cmake_minimum_required (VERSION 2.8.12) project(a_test) file(TO_CMAKE_PATH "\\" path) message(STATUS "\\ converted to CMake path ${path}") file(TO_NATIVE_PATH "/" path) message(STATUS "/ converted to native path ${path}") file(TO_CMAKE_PATH "" path) message(STATUS "empty path converted to CMake path ${path}") file(TO_NATIVE_PATH "" path) message(STATUS "empty path converted to native path ${path}")` on windows and everything seems to work as expected. – vre Jan 26 '18 at 21:07

1 Answers1

4

There is no CMake variable for the system's path separator that can directly be used.

But as @vre commented using the file(TO_NATIVE_PATH) command you could do:

file(TO_NATIVE_PATH "/" _separator)
message("The systems's path separator is '${_separator}'")

If you have complete paths the $<SHELL_PATH:...> generator expression (CMake version >= 3.4) is very useful:

Content of ... converted to shell path style. For example, slashes are converted to backslashes in Windows shells and drive letters are converted to posix paths in MSYS shells. The ... must be an absolute path.

Florian
  • 39,996
  • 9
  • 133
  • 149
  • Umm, are you sure that `TO_NATIVE_PATH` of `/` will result in a path separator on all platforms? Specifically, on Windows, there isn't even a single root for the filesystem. – einpoklum Jan 26 '18 at 20:12
  • 2
    @einpoklum I wasn't sure so I've given it try on Windows and Ununtu. Both work fine. And if I look at the CMake source code in question like [`SystemTools::ConvertToWindowsOutputPath()`](https://gitlab.kitware.com/cmake/cmake/blob/master/Source/kwsys/SystemTools.cxx#L2000), it does mainly convert slashes (unlike the [`$` generator expression](https://github.com/Kitware/CMake/blob/master/Source/cmGeneratorExpressionNode.cxx#L1780)). – Florian Jan 26 '18 at 21:02