CMake expanduser tilde ~

To have the most reliable path operations in CMake, it’s typically best to resolve paths to the full expanded path. Note: there are a few CMake functions that desire relative paths, but those are clearly spelled out in the docs.

get_filename_components(... ABSOLUTE) expands the “~” to the user home directory. Without this, on Windows the else() statement is executed.

set(p "~")

# comment out this line on Windows to see the problem
get_filename_component(p ${p} ABSOLUTE)

  message(STATUS "${p} is a directory")
  message(FATAL_ERROR "${p} is not a directory")

Related: CMake file separator