Browse Source

CMakePresets.json: Add ${hostSystemName} macro

Kyle Edwards 4 years ago
parent
commit
0d497e159b

+ 6 - 0
Help/manual/cmake-presets.7.rst

@@ -830,6 +830,12 @@ Recognized macros include:
   test presets, this will evaluate to the generator specified by
   ``configurePreset``.
 
+``${hostSystemName}``
+
+  The name of the host operating system. Contains the same value as
+  :variable:`CMAKE_HOST_SYSTEM_NAME`. This is allowed in preset files
+  specifying version ``3`` or above.
+
 ``${dollar}``
 
   A literal dollar sign (``$``).

+ 5 - 0
Help/release/dev/cmake-presets-host-system-name.rst

@@ -0,0 +1,5 @@
+cmake-presets-host-system-name
+------------------------------
+
+* :manual:`cmake-presets(7)` gained support for a new ``${hostSystemName}``
+  macro.

+ 9 - 1
Source/cmCMakePresetsFile.cxx

@@ -9,6 +9,7 @@
 #include <iterator>
 #include <utility>
 
+#include <cm/string_view>
 #include <cmext/string_view>
 
 #include <cm3p/json/reader.h>
@@ -981,7 +982,7 @@ bool ExpandMacros(const cmCMakePresetsFile& file, const T& preset,
   MacroExpander defaultMacroExpander =
     [&file, &preset](const std::string& macroNamespace,
                      const std::string& macroName, std::string& macroOut,
-                     int /*version*/) -> ExpandMacroResult {
+                     int version) -> ExpandMacroResult {
     if (macroNamespace.empty()) {
       if (macroName == "sourceDir") {
         macroOut += file.SourceDir;
@@ -1010,6 +1011,13 @@ bool ExpandMacros(const cmCMakePresetsFile& file, const T& preset,
         macroOut += '$';
         return ExpandMacroResult::Ok;
       }
+      if (macroName == "hostSystemName") {
+        if (version < 3) {
+          return ExpandMacroResult::Error;
+        }
+        macroOut += cmSystemTools::GetSystemName();
+        return ExpandMacroResult::Ok;
+      }
     }
 
     return ExpandMacroResult::Ignore;

+ 3 - 0
Tests/RunCMake/CMakePresets/HostSystemName.cmake

@@ -0,0 +1,3 @@
+include(${CMAKE_CURRENT_LIST_DIR}/TestVariable.cmake)
+
+test_variable(TEST_HOST_SYSTEM_NAME "" "${CMAKE_HOST_SYSTEM_NAME}")

+ 13 - 0
Tests/RunCMake/CMakePresets/HostSystemName.json.in

@@ -0,0 +1,13 @@
+{
+  "version": 3,
+  "configurePresets": [
+    {
+      "name": "HostSystemName",
+      "generator": "@RunCMake_GENERATOR@",
+      "binaryDir": "${sourceDir}/build",
+      "cacheVariables": {
+        "TEST_HOST_SYSTEM_NAME": "${hostSystemName}"
+      }
+    }
+  ]
+}

+ 1 - 0
Tests/RunCMake/CMakePresets/HostSystemNameFuture-result.txt

@@ -0,0 +1 @@
+1

+ 2 - 0
Tests/RunCMake/CMakePresets/HostSystemNameFuture-stderr.txt

@@ -0,0 +1,2 @@
+^CMake Error: Could not read presets from [^
+]*/Tests/RunCMake/CMakePresets/HostSystemNameFuture: Invalid macro expansion$

+ 13 - 0
Tests/RunCMake/CMakePresets/HostSystemNameFuture.json.in

@@ -0,0 +1,13 @@
+{
+  "version": 2,
+  "configurePresets": [
+    {
+      "name": "HostSystemNameFuture",
+      "generator": "@RunCMake_GENERATOR@",
+      "binaryDir": "${sourceDir}/build",
+      "cacheVariables": {
+        "TEST_HOST_SYSTEM_NAME": "${hostSystemName}"
+      }
+    }
+  ]
+}

+ 6 - 0
Tests/RunCMake/CMakePresets/RunCMakeTest.cmake

@@ -261,6 +261,12 @@ set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/Debug.json.in")
 run_cmake_presets(NoDebug)
 run_cmake_presets(Debug)
 
+# Test ${hostSystemName} macro
+set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/HostSystemName.json.in")
+run_cmake_presets(HostSystemName)
+set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/HostSystemNameFuture.json.in")
+run_cmake_presets(HostSystemNameFuture)
+
 # Test the example from the documentation
 file(READ "${RunCMake_SOURCE_DIR}/../../../Help/manual/presets/example.json" _example)
 string(REPLACE "\"generator\": \"Ninja\"" "\"generator\": \"@RunCMake_GENERATOR@\"" _example "${_example}")