Files
CMake/Tests/RunCMake/find_program/RelAndAbsPath.cmake
Sam Yates c76c1ea208 find_program: Consider CWD only for paths with separator
find_program() incorrectly prepended search path components
to absolute file paths, and incorrectly searched the current
working directory for files that contained no directory
separators.

* Replace calls cmFindProgramHelper::CheckDirectory(std::string())
  with call of new method cmFindProgramHelper::CheckCompoundNames()
  that checks for the presence of a directory separator in the
  file name.
* Use cmSystemTools::CollapseCombinedPath rather than string
  concatenation to properly combine absolute file names with
  search path components.
* Add unit tests to verify corrections.

Fixes: #18044
2018-06-14 14:28:03 -04:00

64 lines
1.5 KiB
CMake

# testNoSuchFile should only be found if the file absolute path is
# incorrectly prepended with the search path.
function(strip_windows_path_prefix p outvar)
if(CMAKE_HOST_SYSTEM_NAME MATCHES "Windows")
string(REGEX REPLACE "^.:" "" p "${p}")
endif()
set(${outvar} "${p}" PARENT_SCOPE)
endfunction()
strip_windows_path_prefix("${CMAKE_CURRENT_SOURCE_DIR}" srcdir)
file(MAKE_DIRECTORY "tmp${srcdir}")
configure_file(testCWD "tmp${srcdir}/testNoSuchFile" COPYONLY)
find_program(PROG_ABS
NAMES "${srcdir}/testNoSuchFile"
PATHS "${CMAKE_CURRENT_BINARY_DIR}/tmp"
NO_DEFAULT_PATH
)
message(STATUS "PROG_ABS='${PROG_ABS}'")
find_program(PROG_ABS_NPD
NAMES "${srcdir}/testNoSuchFile"
PATHS "${CMAKE_CURRENT_BINARY_DIR}/tmp"
NAMES_PER_DIR
NO_DEFAULT_PATH
)
message(STATUS "PROG_ABS_NPD='${PROG_ABS_NPD}'")
# ./testCWD should not be found without '.' being in the path list.
configure_file(testCWD testCWD COPYONLY)
find_program(PROG_CWD
NAMES testCWD
NO_DEFAULT_PATH
)
message(STATUS "PROG_CWD='${PROG_CWD}'")
find_program(PROG_CWD_NPD
NAMES testCWD
NAMES_PER_DIR
NO_DEFAULT_PATH
)
message(STATUS "PROG_CWD_NPD='${PROG_CWD_NPD}'")
# Confirm that adding '.' to path does locate ./testCWD.
find_program(PROG_CWD_DOT
NAMES testCWD
PATHS .
NO_DEFAULT_PATH
)
message(STATUS "PROG_CWD_DOT='${PROG_CWD_DOT}'")
find_program(PROG_CWD_DOT_NPD
NAMES testCWD
PATHS .
NAMES_PER_DIR
NO_DEFAULT_PATH
)
message(STATUS "PROG_CWD_DOT_NPD='${PROG_CWD_DOT_NPD}'")