cmake_path’s PARENT_PATH acts "strangely" when input path has trailing slash
cmake_path()
can resolve the parent directory of an inputted directory by using the PARENT_PATH
command. However, it yielded surprising results (to me at least) in the case the inputted path ends with /
: the parent directory is not returned!! Instead, it returns the inputted path without the trailing /
.
My question: is that a bug or a feature?
Documentation of cmake_path() about PARENT_PATH. A blog post about the same issue and link to the author’s CMakeLists.txt.
I am using cmake 3.27.1 .
CMakeLists.txt
# cmake_path is available since CMake 3.20
cmake_minimum_required(VERSION 3.20)
project(DemoParentPath CXX)
set(path_in "/absolute/path/parent/child")
cmake_path(GET path_in PARENT_PATH path_out)
message(STATUS " in: \"${path_in}\", out: \"${path_out}\"")
set(path_in "/absolute/path/parent/child/")
cmake_path(GET path_in PARENT_PATH path_out)
message(STATUS " in: \"${path_in}\", out: \"${path_out}\"")
set(path_in "relative/path/parent/child")
cmake_path(GET path_in PARENT_PATH path_out)
message(STATUS " in: \"${path_in}\", out: \"${path_out}\"")
set(path_in "relative/path/parent/child/")
cmake_path(GET path_in PARENT_PATH path_out)
message(STATUS " in: \"${path_in}\", out: \"${path_out}\"")
Output:
Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.22621.
in: "/absolute/path/parent/child", out: "/absolute/path/parent"
in: "/absolute/path/parent/child/", out: "/absolute/path/parent/child"
in: "relative/path/parent/child", out: "relative/path/parent"
in: "relative/path/parent/child/", out: "relative/path/parent/child"
Configuring done (0.0s)
Edited by Gaël Lorieul