Browse Source

Merge topic 'cmake-presets-boolean-cache'

920d180047 CMakePresets.json: Allow boolean for cache variable value

Acked-by: Kitware Robot <[email protected]>
Merge-request: !5398
Kyle Edwards 5 years ago
parent
commit
6abe14d226

+ 6 - 4
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

+ 14 - 2
Help/manual/presets/schema.json

@@ -118,6 +118,10 @@
                   "type": "null",
                   "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": [

+ 23 - 1
Source/cmCMakePresetsFile.cxx

@@ -77,15 +77,37 @@ auto const RootVersionHelper =
 auto const VariableStringHelper = cmJSONStringHelper<ReadFileResult>(
   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<CacheVariable, ReadFileResult>(
     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<CacheVariable>& 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=*/"",

+ 16 - 0
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"

+ 6 - 0
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:
 

+ 6 - 0
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" "$")