Mike E
Mike E

Reputation: 874

CMake: Get the complete representation of a path minus relative elements

I want to take a variable that has been set to a combination of path elements (potentially both absolute and relative) and get the absolute path from it. Something like what boost::filesystem::system_complete() does in C++. For example, I have something like:

set(EXTERNAL_LIB_DIR "${CMAKE_SOURCE_DIR}/../external" CACHE PATH "Location of externals")

which works but in the UI it's a bit ugly, as it might end up looking like C:/dev/repo/tool/../external. I'm wondering if there's a CMake built-in command to turn that into C:/dev/repo/external before I go and script a macro to do it. find_path kind of does this, but it requires that the path already exist and something worth searching for be there. I want it to work whether the path exists or not (I might use it for an overridden CMAKE_INSTALL_PREFIX default, for example).

Upvotes: 5

Views: 4494

Answers (2)

Kevin
Kevin

Reputation: 18243

As of CMake 3.20, you can use the cmake_path command to normalize the path, which supersedes the get_filename_component command.

cmake_path(SET MY_NEW_PATH NORMALIZE ${EXTERNAL_LIB_DIR})

This also converts any backslashes (\) into forward-slashes cleanly.

Upvotes: 1

tibur
tibur

Reputation: 11636

You can use:

get_filename_component(NEW_VAR ${EXTERNAL_LIB_DIR} REALPATH)

Upvotes: 14

Related Questions