shashashamti2008
shashashamti2008

Reputation: 2327

Getting the name of the cmake build directory excluding the full path

I am using CMake 3.23 and the build directory is C:\Dev\MyProject\LibA\cmake-build-debug-vs. ${CMAKE_CURRENT_BINARY_DIR} provide the full path to the build directory. Is there any way to only get cmake-build-debug-vs?

Does CMake have a dedicated variable for only the name of the build directory? I tried several built-in variables and they all return the full path.

Upvotes: 0

Views: 615

Answers (2)

fabian
fabian

Reputation: 82461

The get_filename_component command can be used to extract parts of file names

get_filename_component(DIR_NAME_NOPATH ${CMAKE_CURRENT_BINARY_DIR} NAME)
message("Binary dir name: '${DIR_NAME_NOPATH}'")

Upvotes: 1

Johnny_xy
Johnny_xy

Reputation: 406

If you want only the name of the latest path component (filename or directory; depending on what is the last component) then you could use CMake's relative new path features.

This will extract cmake-build-debug-vs and store it into the variable MY_BUILD_PATH_NAME.

cmake_path (GET CMAKE_CURRENT_BINARY_DIR PARENT_PATH MY_BUILD_PATH_NAME)
message (STATUS "MY_BUILD_PATH_NAME = \"${MY_BUILD_PATH_NAME}\"")

If you what to compute a relative path from your path to another path then you have to use the file(RELATIVE_PATH) API as already mentioned in the comments.

file (RELATIVE_PATH MY_BUILD_PATH_NAME "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/../")
message (STATUS "MY_BUILD_PATH_NAME = \"${MY_BUILD_PATH_NAME}\"")

This will compute ../ and store it into the variable MY_BUILD_PATH_NAME.

Upvotes: 1

Related Questions