CMakePresets.json: Split cmakeGeneratorConfig field

Make this field separate for both architecture and toolset. Allow
architecture and toolset to be either strings or objects with value
and strategy fields.

Fixes: #21317
This commit is contained in:
Kyle Edwards
2020-10-16 16:19:11 -04:00
parent 609122007d
commit 64afabdbcb
33 changed files with 242 additions and 96 deletions

View File

@@ -130,34 +130,34 @@ Format
the ``architecture`` field instead.
``architecture``
An optional string representing the platform name to use for generators
that support platforms.
``toolset``
An optional string representing the toolset name to use for generators
that support toolsets.
Optional fields representing the platform and toolset, respectively, for
generators that support them. Each may be either a string or an object
with the following fields:
``cmakeGeneratorConfig``
``value``
An optional string telling CMake how to handle the ``architecture`` and
``toolset`` fields. Valid values are:
An optional string representing the value.
``"default"``
``strategy``
Set the platform and toolset using the ``architecture`` and ``toolset``
fields respectively. On non-Visual Studio generators, this will result
in an error if ``architecture`` or ``toolset`` are set.
An optional string telling CMake how to handle the ``architecture`` or
``toolset`` field. Valid values are:
``"ignore"``
``"set"``
Do not set the platform or toolset at all, even on Visual Studio
generators. This is useful if, for example, a preset uses the Ninja
generator, and an IDE knows how to set up the Visual C++ environment
from the ``architecture`` and ``toolset`` fields. In that case, CMake
will ignore ``architecture`` and ``toolset``, but the IDE can use them
to set up the environment before invoking CMake.
Set the respective value. This will result in an error for generators
that do not support the respective field.
``"external"``
Do not set the value, even if the generator supports it. This is
useful if, for example, a preset uses the Ninja generator, and an IDE
knows how to set up the Visual C++ environment from the
``architecture`` and ``toolset`` fields. In that case, CMake will
ignore the field, but the IDE can use them to set up the environment
before invoking CMake.
``binaryDir``

View File

@@ -85,19 +85,57 @@
"description": "An optional string representing the generator to use for the preset. If generator is not specified, it must be inherited from the inherits preset (unless this preset is hidden). Note that for Visual Studio generators, unlike in the command line -G argument, you cannot include the platform name in the generator name. Use the architecture field instead."
},
"architecture": {
"type": "string",
"description": "An optional string representing the platform name to use for Visual Studio generators."
"anyOf": [
{
"type": "string",
"description": "An optional string representing the platform for generators that support it."
},
{
"type": "object",
"description": "An optional object representing the platform for generators that support it.",
"properties": {
"value": {
"type": "string",
"description": "An optional string representing the value."
},
"strategy": {
"type": "string",
"description": "An optional string telling CMake how to handle the field. Valid values are: \"set\" Set the respective value. This will result in an error for generators that do not support the respective field. \"external\" Do not set the value, even if the generator supports it. This is useful if, for example, a preset uses the Ninja generator, and an IDE knows how to set up the Visual C++ environment from the architecture and toolset fields. In that case, CMake will ignore the field, but the IDE can use them to set up the environment before invoking CMake.",
"enum": [
"set",
"external"
]
}
},
"additionalProperties": false
}
]
},
"toolset": {
"type": "string",
"description": "An optional string representing the toolset name to use for Visual Studio generators."
},
"cmakeGeneratorConfig": {
"type": "string",
"description": "An optional string telling CMake how to handle the architecture and toolset fields. Valid values are: \"default\": Set the platform and toolset using the architecture and toolset fields respectively. On non-Visual Studio generators, this will result in an error if architecture or toolset are set. \"ignore\": Do not set the platform or toolset at all, even on Visual Studio generators. This is useful if, for example, a preset uses the Ninja generator, and an IDE knows how to set up the Visual C++ environment from the architecture and toolset fields. In that case, CMake will ignore architecture and toolset, but the IDE can use them to set up the environment before invoking CMake.",
"enum": [
"default",
"ignore"
"anyOf": [
{
"type": "string",
"description": "An optional string representing the toolset for generators that support it."
},
{
"type": "object",
"description": "An optional object representing the toolset for generators that support it.",
"properties": {
"value": {
"type": "string",
"description": "An optional string representing the value."
},
"strategy": {
"type": "string",
"description": "An optional string telling CMake how to handle the field. Valid values are: \"set\" Set the respective value. This will result in an error for generators that do not support the respective field. \"external\" Do not set the value, even if the generator supports it. This is useful if, for example, a preset uses the Ninja generator, and an IDE knows how to set up the Visual C++ environment from the architecture and toolset fields. In that case, CMake will ignore the field, but the IDE can use them to set up the environment before invoking CMake.",
"enum": [
"set",
"external"
]
}
},
"additionalProperties": false
}
]
},
"binaryDir": {

View File

@@ -859,8 +859,10 @@ bool CMakeSetupDialog::setupFirstConfigure()
if (presetData.isValid()) {
auto preset = presetData.value<QCMakePreset>();
dialog.setCurrentGenerator(preset.generator);
if (preset.setGenConfig) {
if (preset.setArchitecture) {
dialog.setPlatform(preset.architecture);
}
if (preset.setToolset) {
dialog.setToolset(preset.toolset);
}
dialog.setCompilerOption(CompilerOption::DefaultNative);

View File

@@ -551,9 +551,11 @@ void QCMake::loadPresets()
preset.generator = std::move(QString::fromLocal8Bit(p.Generator.data()));
preset.architecture =
std::move(QString::fromLocal8Bit(p.Architecture.data()));
preset.setArchitecture = !p.ArchitectureStrategy ||
p.ArchitectureStrategy == cmCMakePresetsFile::ArchToolsetStrategy::Set;
preset.toolset = std::move(QString::fromLocal8Bit(p.Toolset.data()));
preset.setGenConfig = !p.GeneratorConfig ||
p.GeneratorConfig == cmCMakePresetsFile::CMakeGeneratorConfig::Default;
preset.setToolset = !p.ToolsetStrategy ||
p.ToolsetStrategy == cmCMakePresetsFile::ArchToolsetStrategy::Set;
preset.enabled = it.Expanded &&
std::find_if(this->AvailableGenerators.begin(),
this->AvailableGenerators.end(),

View File

@@ -6,8 +6,9 @@ bool operator==(const QCMakePreset& lhs, const QCMakePreset& rhs)
{
return lhs.name == rhs.name && lhs.displayName == rhs.displayName &&
lhs.description == rhs.description && lhs.generator == rhs.generator &&
lhs.architecture == rhs.architecture && lhs.toolset == rhs.toolset &&
lhs.setGenConfig == rhs.setGenConfig && lhs.enabled == rhs.enabled;
lhs.architecture == rhs.architecture &&
lhs.setArchitecture == rhs.setArchitecture && lhs.toolset == rhs.toolset &&
lhs.setToolset == rhs.setToolset && lhs.enabled == rhs.enabled;
}
bool operator!=(const QCMakePreset& lhs, const QCMakePreset& rhs)
@@ -27,11 +28,13 @@ bool operator<(const QCMakePreset& lhs, const QCMakePreset& rhs)
(lhs.generator == rhs.generator &&
(lhs.architecture < rhs.architecture ||
(lhs.architecture == rhs.architecture &&
(lhs.toolset < rhs.toolset ||
(lhs.toolset == rhs.toolset &&
(lhs.setGenConfig < rhs.setGenConfig ||
(lhs.setGenConfig == rhs.setGenConfig &&
(lhs.enabled < rhs.enabled))))))))))))));
(lhs.setArchitecture < rhs.setArchitecture ||
(lhs.setArchitecture == rhs.setArchitecture &&
(lhs.toolset < rhs.toolset ||
(lhs.toolset == rhs.toolset &&
(lhs.setToolset < rhs.setToolset ||
(lhs.setToolset == rhs.setToolset &&
(lhs.enabled < rhs.enabled))))))))))))))));
}
bool operator<=(const QCMakePreset& lhs, const QCMakePreset& rhs)

View File

@@ -15,8 +15,9 @@ public:
QString description;
QString generator;
QString architecture;
bool setArchitecture;
QString toolset;
bool setGenConfig;
bool setToolset;
bool enabled;
};

View File

@@ -30,7 +30,7 @@ using ReadFileResult = cmCMakePresetsFile::ReadFileResult;
using CacheVariable = cmCMakePresetsFile::CacheVariable;
using UnexpandedPreset = cmCMakePresetsFile::UnexpandedPreset;
using ExpandedPreset = cmCMakePresetsFile::ExpandedPreset;
using CMakeGeneratorConfig = cmCMakePresetsFile::CMakeGeneratorConfig;
using ArchToolsetStrategy = cmCMakePresetsFile::ArchToolsetStrategy;
constexpr int MIN_VERSION = 1;
constexpr int MAX_VERSION = 1;
@@ -212,8 +212,8 @@ auto const PresetDebugHelper =
.Bind("find"_s, &UnexpandedPreset::DebugFind, PresetOptionalBoolHelper,
false);
ReadFileResult CMakeGeneratorConfigHelper(
cm::optional<CMakeGeneratorConfig>& out, const Json::Value* value)
ReadFileResult ArchToolsetStrategyHelper(
cm::optional<ArchToolsetStrategy>& out, const Json::Value* value)
{
if (!value) {
out = cm::nullopt;
@@ -224,19 +224,56 @@ ReadFileResult CMakeGeneratorConfigHelper(
return ReadFileResult::INVALID_PRESET;
}
if (value->asString() == "default") {
out = CMakeGeneratorConfig::Default;
if (value->asString() == "set") {
out = ArchToolsetStrategy::Set;
return ReadFileResult::READ_OK;
}
if (value->asString() == "ignore") {
out = CMakeGeneratorConfig::Ignore;
if (value->asString() == "external") {
out = ArchToolsetStrategy::External;
return ReadFileResult::READ_OK;
}
return ReadFileResult::INVALID_PRESET;
}
std::function<ReadFileResult(UnexpandedPreset&, const Json::Value*)>
ArchToolsetHelper(
std::string UnexpandedPreset::*valueField,
cm::optional<ArchToolsetStrategy> UnexpandedPreset::*strategyField)
{
auto const objectHelper =
cmJSONObjectHelper<UnexpandedPreset, ReadFileResult>(
ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET, false)
.Bind("value", valueField, PresetStringHelper, false)
.Bind("strategy", strategyField, ArchToolsetStrategyHelper, false);
return [valueField, strategyField, objectHelper](
UnexpandedPreset& out, const Json::Value* value) -> ReadFileResult {
if (!value) {
(out.*valueField).clear();
out.*strategyField = cm::nullopt;
return ReadFileResult::READ_OK;
}
if (value->isString()) {
out.*valueField = value->asString();
out.*strategyField = cm::nullopt;
return ReadFileResult::READ_OK;
}
if (value->isObject()) {
return objectHelper(out, value);
}
return ReadFileResult::INVALID_PRESET;
};
}
auto const ArchitectureHelper = ArchToolsetHelper(
&UnexpandedPreset::Architecture, &UnexpandedPreset::ArchitectureStrategy);
auto const ToolsetHelper = ArchToolsetHelper(
&UnexpandedPreset::Toolset, &UnexpandedPreset::ToolsetStrategy);
auto const PresetHelper =
cmJSONObjectHelper<UnexpandedPreset, ReadFileResult>(
ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET, false)
@@ -252,11 +289,8 @@ auto const PresetHelper =
false)
.Bind("generator"_s, &UnexpandedPreset::Generator, PresetStringHelper,
false)
.Bind("architecture"_s, &UnexpandedPreset::Architecture,
PresetStringHelper, false)
.Bind("toolset"_s, &UnexpandedPreset::Toolset, PresetStringHelper, false)
.Bind("cmakeGeneratorConfig"_s, &UnexpandedPreset::GeneratorConfig,
CMakeGeneratorConfigHelper, false)
.Bind("architecture"_s, ArchitectureHelper, false)
.Bind("toolset"_s, ToolsetHelper, false)
.Bind("binaryDir"_s, &UnexpandedPreset::BinaryDir, PresetStringHelper,
false)
.Bind<std::string>("cmakeExecutable"_s, nullptr, PresetStringHelper, false)
@@ -353,8 +387,12 @@ ReadFileResult VisitPreset(
InheritString(preset.Generator, parent->second.Unexpanded.Generator);
InheritString(preset.Architecture, parent->second.Unexpanded.Architecture);
InheritString(preset.Toolset, parent->second.Unexpanded.Toolset);
if (!preset.GeneratorConfig) {
preset.GeneratorConfig = parent->second.Unexpanded.GeneratorConfig;
if (!preset.ArchitectureStrategy) {
preset.ArchitectureStrategy =
parent->second.Unexpanded.ArchitectureStrategy;
}
if (!preset.ToolsetStrategy) {
preset.ToolsetStrategy = parent->second.Unexpanded.ToolsetStrategy;
}
InheritString(preset.BinaryDir, parent->second.Unexpanded.BinaryDir);
InheritOptionalBool(preset.WarnDev, parent->second.Unexpanded.WarnDev);

View File

@@ -12,10 +12,10 @@
class cmCMakePresetsFile
{
public:
enum class CMakeGeneratorConfig
enum class ArchToolsetStrategy
{
Default,
Ignore,
Set,
External,
};
class CacheVariable
@@ -50,8 +50,9 @@ public:
std::string Description;
std::string Generator;
std::string Architecture;
cm::optional<ArchToolsetStrategy> ArchitectureStrategy;
std::string Toolset;
cm::optional<CMakeGeneratorConfig> GeneratorConfig;
cm::optional<ArchToolsetStrategy> ToolsetStrategy;
std::string BinaryDir;
std::map<std::string, cm::optional<CacheVariable>> CacheVariables;

View File

@@ -1069,12 +1069,16 @@ void cmake::SetArgs(const std::vector<std::string>& args)
this->UnprocessedPresetVariables = expandedPreset->CacheVariables;
this->UnprocessedPresetEnvironment = expandedPreset->Environment;
if (!expandedPreset->GeneratorConfig ||
expandedPreset->GeneratorConfig ==
cmCMakePresetsFile::CMakeGeneratorConfig::Default) {
if (!expandedPreset->ArchitectureStrategy ||
expandedPreset->ArchitectureStrategy ==
cmCMakePresetsFile::ArchToolsetStrategy::Set) {
if (!this->GeneratorPlatformSet) {
this->SetGeneratorPlatform(expandedPreset->Architecture);
}
}
if (!expandedPreset->ToolsetStrategy ||
expandedPreset->ToolsetStrategy ==
cmCMakePresetsFile::ArchToolsetStrategy::Set) {
if (!this->GeneratorToolsetSet) {
this->SetGeneratorToolset(expandedPreset->Toolset);
}

View File

@@ -24,8 +24,9 @@ void QCMakePresetComboBoxTest::changePresets()
/*description=*/"",
/*generator=*/"Ninja",
/*architecture=*/"",
/*setArchitecture=*/true,
/*toolset=*/"",
/*setGenConfig=*/true,
/*setToolset=*/true,
/*enabled=*/true,
},
});
@@ -48,8 +49,9 @@ void QCMakePresetComboBoxTest::changePresets()
/*description=*/"",
/*generator=*/"Ninja Multi-Config",
/*architecture=*/"",
/*setArchitecture=*/true,
/*toolset=*/"",
/*setGenConfig=*/true,
/*setToolset=*/true,
/*enabled=*/true,
},
});

View File

@@ -32,8 +32,9 @@ void QCMakePresetItemModelTest::initTestCase_data()
/*description=*/"",
/*generator=*/"",
/*architecture=*/"",
/*setArchitecture=*/true,
/*toolset=*/"",
/*setGenConfig=*/true,
/*setToolset=*/true,
/*enabled=*/true,
},
QCMakePreset{
@@ -42,8 +43,9 @@ void QCMakePresetItemModelTest::initTestCase_data()
/*description=*/"",
/*generator=*/"",
/*architecture=*/"",
/*setArchitecture=*/true,
/*toolset=*/"",
/*setGenConfig=*/true,
/*setToolset=*/true,
/*enabled=*/true,
},
QCMakePreset{
@@ -52,8 +54,9 @@ void QCMakePresetItemModelTest::initTestCase_data()
/*description=*/"Long Description",
/*generator=*/"",
/*architecture=*/"",
/*setArchitecture=*/true,
/*toolset=*/"",
/*setGenConfig=*/true,
/*setToolset=*/true,
/*enabled=*/true,
},
QCMakePreset{
@@ -62,8 +65,9 @@ void QCMakePresetItemModelTest::initTestCase_data()
/*description=*/"",
/*generator=*/"",
/*architecture=*/"",
/*setArchitecture=*/true,
/*toolset=*/"",
/*setGenConfig=*/true,
/*setToolset=*/true,
/*enabled=*/false,
},
};

View File

@@ -16,8 +16,9 @@ QCMakePreset makePreset()
/*description=*/"description",
/*generator=*/"generator",
/*architecture=*/"architecture",
/*setArchitecture=*/true,
/*toolset=*/"toolset",
/*setGenConfig=*/true,
/*setToolset=*/true,
/*enabled=*/true,
};
}
@@ -69,12 +70,14 @@ void QCMakePresetTest::equality_data()
QTest::newRow("architecture")
<< makePreset(&QCMakePreset::architecture, "other-architecture") << false
<< true << false;
QTest::newRow("setArchitecture")
<< makePreset(&QCMakePreset::setArchitecture, false) << false << false
<< true;
QTest::newRow("toolset") << makePreset(&QCMakePreset::toolset,
"other-toolset")
<< false << false << true;
QTest::newRow("setGenConfig")
<< makePreset(&QCMakePreset::setGenConfig, false) << false << false
<< true;
QTest::newRow("setToolset")
<< makePreset(&QCMakePreset::setToolset, false) << false << false << true;
QTest::newRow("enabled") << makePreset(&QCMakePreset::enabled, false)
<< false << false << true;
}

View File

@@ -451,28 +451,38 @@
"binaryDir": "${sourceDir}/build"
},
{
"name": "CMakeGeneratorConfigNone",
"name": "ArchToolsetStrategyNone",
"generator": "@RunCMake_GENERATOR@",
"architecture": "a",
"toolset": "a",
"binaryDir": "${sourceDir}/build"
},
{
"name": "CMakeGeneratorConfigBase",
"name": "ArchToolsetStrategyBase",
"generator": "@RunCMake_GENERATOR@",
"architecture": "a",
"toolset": "a",
"cmakeGeneratorConfig": "ignore",
"architecture": {
"value": "a",
"strategy": "external"
},
"toolset": {
"value": "a",
"strategy": "external"
},
"binaryDir": "${sourceDir}/build"
},
{
"name": "CMakeGeneratorConfigDefault",
"inherits": "CMakeGeneratorConfigBase",
"cmakeGeneratorConfig": "default"
"name": "ArchToolsetStrategyDefault",
"inherits": "ArchToolsetStrategyBase",
"architecture": {
"strategy": "set"
},
"toolset": {
"strategy": "set"
}
},
{
"name": "CMakeGeneratorConfigIgnore",
"inherits": "CMakeGeneratorConfigBase"
"name": "ArchToolsetStrategyIgnore",
"inherits": "ArchToolsetStrategyBase"
}
]
}

View File

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

View File

@@ -0,0 +1,13 @@
{
"version": 1,
"configurePresets": [
{
"name": "InvalidArchitectureStrategy",
"generator": "@RunCMake_GENERATOR@",
"binaryDir": "${sourceDir}/build",
"architecture": {
"strategy": {}
}
}
]
}

View File

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

View File

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

View File

@@ -2,10 +2,12 @@
"version": 1,
"configurePresets": [
{
"name": "InvalidCMakeGeneratorConfig",
"name": "InvalidToolsetStrategy",
"generator": "@RunCMake_GENERATOR@",
"binaryDir": "${sourceDir}/build",
"cmakeGeneratorConfig": {}
"toolset": {
"strategy": {}
}
}
]
}

View File

@@ -117,8 +117,10 @@ run_cmake_presets(InvalidInheritance)
run_cmake_presets(ErrorNoWarningDev)
run_cmake_presets(ErrorNoWarningDeprecated)
set(CMakePresets_SCHEMA_EXPECTED_RESULT 1)
run_cmake_presets(InvalidCMakeGeneratorConfig)
run_cmake_presets(UnknownCMakeGeneratorConfig)
run_cmake_presets(InvalidArchitectureStrategy)
run_cmake_presets(UnknownArchitectureStrategy)
run_cmake_presets(InvalidToolsetStrategy)
run_cmake_presets(UnknownToolsetStrategy)
run_cmake_presets(EmptyCacheKey)
run_cmake_presets(EmptyEnvKey)
set(CMakePresets_SCHEMA_EXPECTED_RESULT 0)
@@ -196,9 +198,9 @@ if(RunCMake_GENERATOR MATCHES "^Visual Studio ")
run_cmake_presets(VisualStudioInheritanceMultiSecond)
endif()
else()
run_cmake_presets(CMakeGeneratorConfigNone)
run_cmake_presets(CMakeGeneratorConfigDefault)
run_cmake_presets(CMakeGeneratorConfigIgnore)
run_cmake_presets(ArchToolsetStrategyNone)
run_cmake_presets(ArchToolsetStrategyDefault)
run_cmake_presets(ArchToolsetStrategyIgnore)
endif()
# Test bad command line arguments

View File

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

View File

@@ -0,0 +1,13 @@
{
"version": 1,
"configurePresets": [
{
"name": "UnknownArchitectureStrategy",
"generator": "@RunCMake_GENERATOR@",
"binaryDir": "${sourceDir}/build",
"architecture": {
"strategy": "unknown"
}
}
]
}

View File

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

View File

@@ -0,0 +1 @@
1

View File

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

View File

@@ -2,10 +2,12 @@
"version": 1,
"configurePresets": [
{
"name": "UnknownCMakeGeneratorConfig",
"name": "UnknownToolsetStrategy",
"generator": "@RunCMake_GENERATOR@",
"binaryDir": "${sourceDir}/build",
"cmakeGeneratorConfig": "unknown"
"toolset": {
"strategy": "unknown"
}
}
]
}