cmake_path’s PARENT_PATH acts "strangely" when input path has trailing slash

42 views Asked by At

cmake_path() can resolve the parent directory of an inputted directory by using the PARENT_PATH command. However, it yielded surprizing 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?
In case that it is indeed meant to be this way, what is the reasoning that makes returning the input path without the trailing slash appear natural and/or reasonable?

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 .

# 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)
0

There are 0 answers