cmake_minimum_required(VERSION 2.8) #helper function(print_variable_and_cache varname prefix) set(msg "${prefix}\${${varname}} = '${${varname}}' - ") get_property(varPropIsSet VARIABLE PROPERTY "${varname}" SET) if (varPropIsSet) get_property(propValue VARIABLE PROPERTY "${varname}") list(APPEND msg "Variable is '${propValue}'") else() list(APPEND msg "Variable is not set") endif() list(APPEND msg " - ") get_property(cachePropIsSet CACHE "${varname}" PROPERTY VALUE SET) if (cachePropIsSet) get_property(propValue CACHE "${varname}" PROPERTY VALUE) get_property(propAdv CACHE "${varname}" PROPERTY ADVANCED) get_property(propType CACHE "${varname}" PROPERTY TYPE) list(APPEND msg "Cache entry is '${propValue}' (Advanced: ${propAdv}, Type: ${propType})") else() list(APPEND msg "Cache entry is not set") endif() message(${msg}) endfunction() #for convenience: undo previous run unset(my_path1 CACHE) unset(my_path2 CACHE) unset(my_path3 CACHE) unset(my_path4 CACHE) unset(my_path5 CACHE) unset(my_path6 CACHE) unset(my_path7 CACHE) unset(my_path8 CACHE) unset(my_path9 CACHE) message("cmake version ${CMAKE_VERSION}") message("#T1: Stripped down test case") #somewhere in main-CMakeLists.txt set(my_path1 ${CMAKE_CURRENT_LIST_FILE}) #first invocation of a findScript find_path(my_path1 NAMES CMakeLists.txt HINTS ${CMAKE_CURRENT_LIST_DIR}) mark_as_advanced(my_path1) #second invocation of a findScript print_variable_and_cache(my_path1 "Pre FP: ") find_path(my_path1 NAMES CMakeLists.txt HINTS ${CMAKE_CURRENT_LIST_DIR}) print_variable_and_cache(my_path1 "Post FP: ") mark_as_advanced(my_path1) #in main again message("my_path1 is ''${my_path1} <- This value is empty here") message("FAILED: my_path1 is empty! It should be the previous value or the found path, but not empty!") message("") message("#T2: even shorter example") set(my_path2 "anyvalue") mark_as_advanced(my_path2) print_variable_and_cache(my_path2 "Pre FP: ") find_path(my_path2 NAMES CMakeLists.txt HINTS ${CMAKE_CURRENT_LIST_DIR}) print_variable_and_cache(my_path2 "Post FP: ") message("FAILED: my_path2 is empty!") message("") message("#T3: default usage") print_variable_and_cache(my_path3 "Pre FP: ") find_path(my_path3 NAMES CMakeLists.txt HINTS ${CMAKE_CURRENT_LIST_DIR}) print_variable_and_cache(my_path3 "Post FP: ") message("WORKS") message("") message("#T4: using a pre-set variable") set(my_path4 "anyvalue") print_variable_and_cache(my_path4 "Pre FP: ") find_path(my_path4 NAMES CMakeLists.txt HINTS ${CMAKE_CURRENT_LIST_DIR}) print_variable_and_cache(my_path4 "Post FP: ") message("WORKS. But cache variable is not written") message("") message("#T5: hiding an empty cache var") set(my_path5 "" CACHE STRING "") set(my_path5 "anyvalue") print_variable_and_cache(my_path5 "Pre FP: ") find_path(my_path5 NAMES CMakeLists.txt HINTS ${CMAKE_CURRENT_LIST_DIR}) print_variable_and_cache(my_path5 "Post FP: ") message("WORKS. But cache var is not written. <- I except at least this behaviour for the my_path[12] tests") message("") message("#T6: hiding a cache value ") set(my_path6 "anycachedvalue" CACHE STRING "") set(my_path6 "anyvalue") print_variable_and_cache(my_path6 "Pre FP: ") find_path(my_path6 NAMES CMakeLists.txt HINTS ${CMAKE_CURRENT_LIST_DIR}) print_variable_and_cache(my_path6 "Post FP: ") message("WORKS. But cache var is not written.") message("") message("#T7: with advanced vars") mark_as_advanced(my_path7) print_variable_and_cache(my_path7 "Pre FP: ") find_path(my_path7 NAMES CMakeLists.txt HINTS ${CMAKE_CURRENT_LIST_DIR}) print_variable_and_cache(my_path7 "Post FP: ") message("WORKS. (This is strange. I would except that this one is failing too.)") message("") message("#T8: with false var and set cache") set(my_path8 "anycachedvalue" CACHE STRING "") set(my_path8 "my_path8-NOTFOUND") print_variable_and_cache(my_path8 "Pre FP: ") find_path(my_path8 NAMES CMakeLists.txt HINTS ${CMAKE_CURRENT_LIST_DIR}) print_variable_and_cache(my_path8 "Post FP: ") message("WORKS. This writes to the cache-variable. Any other test with a defined variable is not") message("") message("#T9: with emtpy var and set cache") set(my_path9 "anycachedvalue" CACHE STRING "") set(my_path9 "") print_variable_and_cache(my_path9 "Pre FP: ") find_path(my_path9 NAMES CMakeLists.txt HINTS ${CMAKE_CURRENT_LIST_DIR}) print_variable_and_cache(my_path9 "Post FP: ") message("FAILED(?). Shouldn't be '' the same as '*-NOTFOUND' (see T8)") message("") #[[ Output: cmake version 3.2.1 #T1: Stripped down test case Pre FP: ${my_path1} = 'D:/src/CMake_findpath_advanced/CMakeLists.txt' - Variable is 'D:/src/CMake_findpath_advanced/CMakeLists.txt' - Cache entry is '' (Advanced: 1, Type: UNINITIALIZED) Post FP: ${my_path1} = '' - Variable is '' - Cache entry is '' (Advanced: 1, Type: PATH) my_path1 is '' <- This value is empty here FAILED: my_path1 is empty! It should be the previous value or the found path, but not empty! #T2: even shorter example Pre FP: ${my_path2} = 'anyvalue' - Variable is 'anyvalue' - Cache entry is '' (Advanced: 1, Type: UNINITIALIZED) Post FP: ${my_path2} = '' - Variable is '' - Cache entry is '' (Advanced: 1, Type: PATH) FAILED: my_path2 is empty! #T3: default usage Pre FP: ${my_path3} = '' - Variable is not set - Cache entry is not set Post FP: ${my_path3} = 'D:/src/CMake_findpath_advanced' - Variable is 'D:/src/CMake_findpath_advanced' - Cache entry is 'D:/src/CMake_findpath_advanced' (Advanced: , Type: PATH) WORKS #T4: using a pre-set variable Pre FP: ${my_path4} = 'anyvalue' - Variable is 'anyvalue' - Cache entry is not set Post FP: ${my_path4} = 'anyvalue' - Variable is 'anyvalue' - Cache entry is not set WORKS. But cache variable is not written #T5: hiding an empty cache var Pre FP: ${my_path5} = 'anyvalue' - Variable is 'anyvalue' - Cache entry is '' (Advanced: , Type: STRING) Post FP: ${my_path5} = 'anyvalue' - Variable is 'anyvalue' - Cache entry is '' (Advanced: , Type: STRING) WORKS. But cache var is not written. <- I except at least this behaviour for the my_path[12] tests #T6: hiding a cache value Pre FP: ${my_path6} = 'anyvalue' - Variable is 'anyvalue' - Cache entry is 'anycachedvalue' (Advanced: , Type: STRING) Post FP: ${my_path6} = 'anyvalue' - Variable is 'anyvalue' - Cache entry is 'anycachedvalue' (Advanced: , Type: STRING) WORKS. But cache var is not written. #T7: with advanced vars Pre FP: ${my_path7} = '' - Variable is not set - Cache entry is '' (Advanced: 1, Type: UNINITIALIZED) Post FP: ${my_path7} = 'D:/src/CMake_findpath_advanced' - Variable is 'D:/src/CMake_findpath_advanced' - Cache entry is 'D:/src/CMake_findpath_advanced' (Advanced: 1, Type: PATH) WORKS. (This is strange. I would except that this one is failing too.) #T8: with false var and set cache Pre FP: ${my_path8} = 'my_path8-NOTFOUND' - Variable is 'my_path8-NOTFOUND' - Cache entry is 'anycachedvalue' (Advanced: , Type: STRING) Post FP: ${my_path8} = 'D:/src/CMake_findpath_advanced' - Variable is 'D:/src/CMake_findpath_advanced' - Cache entry is 'D:/src/CMake_findpath_advanced' (Advanced: , Type: PATH) WORKS. This writes to the cache-variable. Any other test with a defined variable is not #T9: with emtpy var and set cache Pre FP: ${my_path9} = '' - Variable is '' - Cache entry is 'anycachedvalue' (Advanced: , Type: STRING) Post FP: ${my_path9} = '' - Variable is '' - Cache entry is 'anycachedvalue' (Advanced: , Type: STRING) FAILED(?). Shouldn't be '' the same as '*-NOTFOUND' (see T8) Configuring done #]]