diff --git a/ExternalProjectDependency.cmake b/ExternalProjectDependency.cmake index 467c6ba..8dd902a 100644 --- a/ExternalProjectDependency.cmake +++ b/ExternalProjectDependency.cmake @@ -1017,13 +1017,16 @@ endfunction() # # ExternalProject_SetIfNotDefined( [OBFUSCATE] [QUIET]) # -# The default value is set with: -# (1) if set, the value environment variable . -# (2) if set, the value of local variable variable . -# (3) if none of the above, the value passed as a parameter. +# If *NOT* already defined, the variable is set with: +# (1) the value of the environment variable , if defined. +# (2) the value of the local variable variable , if defined. +# (3) if none of the above is defined, the passed as a parameter. # -# Setting the optional parameter 'OBFUSCATE' will display 'OBFUSCATED' instead of the real value. -# Setting the optional parameter 'QUIET' will not display any message. +# Passing the optional parameter 'OBFUSCATE' will display 'OBFUSCATED' instead of the real value. +# Passing the optional parameter 'QUIET' will not display any message. +# +# For convenience, the value of the cache variable named will +# be displayed if it was set and if QUIET has not been passed. macro(ExternalProject_SetIfNotDefined var defaultvalue) set(_obfuscate FALSE) set(_quiet FALSE) @@ -1055,6 +1058,14 @@ macro(ExternalProject_SetIfNotDefined var defaultvalue) endif() set(${var} "${defaultvalue}") endif() + get_property(_is_set CACHE ${var} PROPERTY VALUE SET) + if(_is_set AND NOT _quiet) + set(_value "${${var}}") + if(_obfuscate) + set(_value "OBFUSCATED") + endif() + message(STATUS "Cache variable '${var}' set to '${_value}'") + endif() endmacro() #.rst: diff --git a/Tests/ArtichokeTestUtility.cmake b/Tests/ArtichokeTestUtility.cmake index e8889e1..1e04528 100644 --- a/Tests/ArtichokeTestUtility.cmake +++ b/Tests/ArtichokeTestUtility.cmake @@ -19,6 +19,14 @@ function(check_variable var_name expected_value) endif() endfunction() +function(check_env_variable var_name expected_value) + if(NOT "x$ENV{${var_name}}" STREQUAL "x${expected_value}") + message(FATAL_ERROR "CMake variable [$ENV{${var_name}}] is incorrectly set !\n" + "current:$ENV{${var_name}}\n" + "expected:${expected_value}") + endif() +endfunction() + function(configure_external_projects_for_test name) set(depends "${expected_${name}_DEPENDS}") set(indent "${ARGV1}") diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 922b5b7..67ea765 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -85,6 +85,7 @@ set(tests_source_dir ${CMAKE_SOURCE_DIR}) add_project_test(AddDependencies SuperBuild) add_project_test(AlwaysConfigure SuperBuild) add_project_test(ExternalProjectFileLookup SuperBuild) +add_project_test(ExternalProject_SetIfNotDefined-Cache SuperBuild) add_project_test(MarkAsSuperBuild SuperBuild) add_project_test(MarkAsSuperBuild-EP_ARGS_VAR SuperBuild) add_project_test(MarkAsSuperBuild-WithNotFoundVariable SuperBuild) diff --git a/Tests/ExternalProject_SetIfNotDefined-Cache-Test/CMakeLists.txt b/Tests/ExternalProject_SetIfNotDefined-Cache-Test/CMakeLists.txt new file mode 100644 index 0000000..41e39e0 --- /dev/null +++ b/Tests/ExternalProject_SetIfNotDefined-Cache-Test/CMakeLists.txt @@ -0,0 +1,41 @@ +cmake_minimum_required(VERSION 2.8.7) + +if(POLICY CMP0054) + cmake_policy(SET CMP0054 OLD) +endif() + +project(ExternalProject_SetIfNotDefined-Cache-Test NONE) + +set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../" ${CMAKE_MODULE_PATH}) +include(ExternalProjectDependency) + +include(${CMAKE_CURRENT_SOURCE_DIR}/../ArtichokeTestUtility.cmake) + +# prefix +set(p "ExternalProject_SetIfNotDefined-Cache-Test") + +# Check that cache variable is not overridden +set(${p}_BLUE "cache-blue" CACHE STRING "") +check_variable(${p}_BLUE "cache-blue") +ExternalProject_SetIfNotDefined(${p}_BLUE "default-blue") +check_variable(${p}_BLUE "cache-blue") + +# Check that env variable is not overriden +set(ENV{${p}_RED} "env-red") +check_env_variable(${p}_RED "env-red") +check_variable_not_defined(${p}_RED) +ExternalProject_SetIfNotDefined(${p}_RED "default-red") +check_variable(${p}_RED "env-red") + +# Check that local variable is not overriden +set(${p}_GREEN "local-green") +check_variable(${p}_GREEN "local-green") +ExternalProject_SetIfNotDefined(${p}_GREEN "default-green") +check_variable(${p}_GREEN "local-green") + +# Check that local variable still has precedence over cache variable +set(${p}_CYAN "cache-cyan" CACHE STRING "") +check_variable(${p}_CYAN "cache-cyan") +set(${p}_CYAN "local-cyan") +ExternalProject_SetIfNotDefined(${p}_CYAN "default-cyan") +check_variable(${p}_CYAN "local-cyan")