1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-15 20:46:37 +08:00

CMakePresets.json: Add ${hostSystemName} macro

This commit is contained in:
Kyle Edwards
2021-03-10 16:46:20 -05:00
parent 79d03ab505
commit 0d497e159b
9 changed files with 58 additions and 1 deletions

View File

@@ -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 (``$``).

View File

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

View File

@@ -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;

View File

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

View File

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

View File

@@ -0,0 +1 @@
1

View File

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

View File

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

View File

@@ -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}")