Issue
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.
Solution
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
.
Answered By - Johnny_xy Answer Checked By - Cary Denson (WPSolving Admin)