find_*: unexpected behavior with quoted variable references
CMake: 3.25.3 Platform: Windows and Linux
Consider the following script (run with cmake -P
):
cmake_minimum_required(VERSION 3.25)
file(MAKE_DIRECTORY "/home/dir1")
file(MAKE_DIRECTORY "/home/dir2")
file(TOUCH "/home/dir1/myfile.txt")
set(MYHINTS "/home/dir1" "/home/dir2")
# FOUND
find_file(RESULT1 NAMES myfile.txt HINTS ${MYHINTS} NO_DEFAULT_PATH)
find_file(RESULT2 NAMES myfile.txt PATHS ${MYHINTS} NO_DEFAULT_PATH)
find_path(RESULT3 NAMES myfile.txt HINTS ${MYHINTS} NO_DEFAULT_PATH)
find_path(RESULT4 NAMES myfile.txt PATHS ${MYHINTS} NO_DEFAULT_PATH)
# NOTFOUND
find_file(RESULT5 NAMES myfile.txt HINTS "${MYHINTS}" NO_DEFAULT_PATH)
find_file(RESULT6 NAMES myfile.txt PATHS "${MYHINTS}" NO_DEFAULT_PATH)
find_path(RESULT7 NAMES myfile.txt HINTS "${MYHINTS}" NO_DEFAULT_PATH)
find_path(RESULT8 NAMES myfile.txt PATHS "${MYHINTS}" NO_DEFAULT_PATH)
foreach(i RANGE 1 8)
message(STATUS "RESULT${i} = ${RESULT${i}}")
endforeach()
File /home/dir1/myfile.txt
exists, but when find_file()
/find_path()
are given as HINTS or PATHS a quoted variable reference holding a list ("${MYHINTS}"
), the file is not found, whereas without the quoting (${MYHINTS}
) the file is found.
I am not sure how many other commands internally behave this way when expanding variable references holding lists, but I have the feeling that such behavior might not be consistent across CMake - e.g., string(REPLACE ";" "\n" MYLIST "${MYLIST}")
works (I don't have an example to repro right now).
Is there some recommended way of quoting variable references holding lists (e.g. in command arguments)? I can't recall finding any in the documentation. Also, are the above find_
commands (and maybe other find_
ones) somehow bugged?