From 920d18004731c97a828a937e2074b6882ffeb20d Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Mon, 19 Oct 2020 14:01:40 -0400 Subject: CMakePresets.json: Allow boolean for cache variable value Fixes: #21327 --- Help/manual/cmake-presets.7.rst | 10 ++++++---- Help/manual/presets/schema.json | 16 ++++++++++++++-- Source/cmCMakePresetsFile.cxx | 24 +++++++++++++++++++++++- Tests/RunCMake/CMakePresets/CMakePresets.json.in | 16 ++++++++++++++++ Tests/RunCMake/CMakePresets/Good-stdout.txt | 6 ++++++ Tests/RunCMake/CMakePresets/Good.cmake | 6 ++++++ 6 files changed, 71 insertions(+), 7 deletions(-) diff --git a/Help/manual/cmake-presets.7.rst b/Help/manual/cmake-presets.7.rst index 93e1151..7040ad5 100644 --- a/Help/manual/cmake-presets.7.rst +++ b/Help/manual/cmake-presets.7.rst @@ -176,9 +176,10 @@ Format ``cacheVariables`` An optional map of cache variables. The key is the variable name (which - may not be an empty string), and the value is either ``null``, a string - representing the value of the variable (which supports macro expansion), - or an object with the following fields: + may not be an empty string), and the value is either ``null``, a boolean + (which is equivalent to a value of ``"TRUE"`` or ``"FALSE"`` and a type + of ``BOOL``), a string representing the value of the variable (which + supports macro expansion), or an object with the following fields: ``type`` @@ -186,7 +187,8 @@ Format ``value`` - A required string representing the value of the variable. This field + A required string or boolean representing the value of the variable. + A boolean is equivalent to ``"TRUE"`` or ``"FALSE"``. This field supports macro expansion. Cache variables are inherited through the ``inherits`` field, and the diff --git a/Help/manual/presets/schema.json b/Help/manual/presets/schema.json index 564c94c..ba4568f 100644 --- a/Help/manual/presets/schema.json +++ b/Help/manual/presets/schema.json @@ -119,6 +119,10 @@ "description": "Setting a variable to null causes it to not be set, even if a value was inherited from another preset." }, { + "type": "boolean", + "description": "A boolean representing the value of the variable. Equivalent to \"TRUE\" or \"FALSE\"." + }, + { "type": "string", "description": "A string representing the value of the variable (which supports macro expansion)." }, @@ -131,8 +135,16 @@ "description": "An optional string representing the type of the variable. It should be BOOL, FILEPATH, PATH, STRING, or INTERNAL." }, "value": { - "type": "string", - "description": "A required string representing the value of the variable. This field supports macro expansion." + "anyOf": [ + { + "type": "boolean", + "description": "A required boolean representing the value of the variable. Equivalent to \"TRUE\" or \"FALSE\"." + }, + { + "type": "string", + "description": "A required string representing the value of the variable. This field supports macro expansion." + } + ] } }, "required": [ diff --git a/Source/cmCMakePresetsFile.cxx b/Source/cmCMakePresetsFile.cxx index 90a0faa..456094f 100644 --- a/Source/cmCMakePresetsFile.cxx +++ b/Source/cmCMakePresetsFile.cxx @@ -77,15 +77,37 @@ auto const RootVersionHelper = auto const VariableStringHelper = cmJSONStringHelper( ReadFileResult::READ_OK, ReadFileResult::INVALID_VARIABLE); +ReadFileResult VariableValueHelper(std::string& out, const Json::Value* value) +{ + if (!value) { + out.clear(); + return ReadFileResult::READ_OK; + } + + if (value->isBool()) { + out = value->asBool() ? "TRUE" : "FALSE"; + return ReadFileResult::READ_OK; + } + + return VariableStringHelper(out, value); +} + auto const VariableObjectHelper = cmJSONObjectHelper( ReadFileResult::READ_OK, ReadFileResult::INVALID_VARIABLE, false) .Bind("type"_s, &CacheVariable::Type, VariableStringHelper, false) - .Bind("value"_s, &CacheVariable::Value, VariableStringHelper); + .Bind("value"_s, &CacheVariable::Value, VariableValueHelper); ReadFileResult VariableHelper(cm::optional& out, const Json::Value* value) { + if (value->isBool()) { + out = CacheVariable{ + /*Type=*/"BOOL", + /*Value=*/value->asBool() ? "TRUE" : "FALSE", + }; + return ReadFileResult::READ_OK; + } if (value->isString()) { out = CacheVariable{ /*Type=*/"", diff --git a/Tests/RunCMake/CMakePresets/CMakePresets.json.in b/Tests/RunCMake/CMakePresets/CMakePresets.json.in index 8bfc602..18b9c3a 100644 --- a/Tests/RunCMake/CMakePresets/CMakePresets.json.in +++ b/Tests/RunCMake/CMakePresets/CMakePresets.json.in @@ -46,6 +46,22 @@ "type": "BOOL", "value": "OFF" }, + "TEST_BOOL_TRUE": true, + "TEST_BOOL_FALSE": false, + "TEST_TYPED_BOOL_TRUE": { + "type": "STRING", + "value": true + }, + "TEST_TYPED_BOOL_FALSE": { + "type": "STRING", + "value": false + }, + "TEST_UNTYPED_BOOL_TRUE": { + "value": true + }, + "TEST_UNTYPED_BOOL_FALSE": { + "value": false + }, "TEST_PRESET_NAME": { "type": "STRING", "value": "x${presetName}x" diff --git a/Tests/RunCMake/CMakePresets/Good-stdout.txt b/Tests/RunCMake/CMakePresets/Good-stdout.txt index ce6189e..7adf1ca 100644 --- a/Tests/RunCMake/CMakePresets/Good-stdout.txt +++ b/Tests/RunCMake/CMakePresets/Good-stdout.txt @@ -1,5 +1,7 @@ Preset CMake variables: + TEST_BOOL_FALSE:BOOL="FALSE" + TEST_BOOL_TRUE:BOOL="TRUE" TEST_DOLLAR="\$" TEST_D_ENV_REF="xEnvironment variablex" TEST_D_ENV_REF_P="" @@ -32,6 +34,10 @@ Preset CMake variables: TEST_TRAILING_DOLLAR="a \$" TEST_TRAILING_UNKNOWN_NAMESPACE="\$unknown{namespace" TEST_TRUE:BOOL="TRUE" + TEST_TYPED_BOOL_FALSE:STRING="FALSE" + TEST_TYPED_BOOL_TRUE:STRING="TRUE" + TEST_UNTYPED_BOOL_FALSE="FALSE" + TEST_UNTYPED_BOOL_TRUE="TRUE" Preset environment variables: diff --git a/Tests/RunCMake/CMakePresets/Good.cmake b/Tests/RunCMake/CMakePresets/Good.cmake index d8e3e2d..55b85da 100644 --- a/Tests/RunCMake/CMakePresets/Good.cmake +++ b/Tests/RunCMake/CMakePresets/Good.cmake @@ -8,6 +8,12 @@ test_variable(TEST_SOURCE_PARENT_DIR "PATH" "${_parent_dir}") test_variable(TEST_SOURCE_LIST "FILEPATH" "${CMAKE_SOURCE_DIR}/CMakeLists.txt") test_variable(TEST_TRUE "BOOL" "TRUE") test_variable(TEST_OFF "BOOL" "OFF") +test_variable(TEST_BOOL_TRUE "BOOL" "TRUE") +test_variable(TEST_BOOL_FALSE "BOOL" "FALSE") +test_variable(TEST_TYPED_BOOL_TRUE "STRING" "TRUE") +test_variable(TEST_TYPED_BOOL_FALSE "STRING" "FALSE") +test_variable(TEST_UNTYPED_BOOL_TRUE "UNINITIALIZED" "TRUE") +test_variable(TEST_UNTYPED_BOOL_FALSE "UNINITIALIZED" "FALSE") test_variable(TEST_PRESET_NAME "STRING" "xGoodx") test_variable(TEST_GENERATOR "UNINITIALIZED" "x${CMAKE_GENERATOR}x") test_variable(TEST_DOLLAR "UNINITIALIZED" "$") -- cgit v0.12