Commit 64afabdb authored by Kyle Edwards's avatar Kyle Edwards
Browse files

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
parent 60912200
Pipeline #195956 failed with stages
in 78 minutes and 4 seconds
......@@ -130,34 +130,34 @@ Format
the ``architecture`` field instead.
``architecture``
``toolset``
An optional string representing the platform name to use for generators
that support platforms.
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:
``toolset``
``value``
An optional string representing the toolset name to use for generators
that support toolsets.
An optional string representing the value.
``cmakeGeneratorConfig``
``strategy``
An optional string telling CMake how to handle the ``architecture`` and
``toolset`` fields. Valid values are:
An optional string telling CMake how to handle the ``architecture`` or
``toolset`` field. Valid values are:
``"default"``
``"set"``
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.
Set the respective value. This will result in an error for generators
that do not support the respective field.
``"ignore"``
``"external"``
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.
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``
......
......@@ -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": {
......
......@@ -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);
......
......@@ -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(),
......
......@@ -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)
......
......@@ -15,8 +15,9 @@ public:
QString description;
QString generator;
QString architecture;
bool setArchitecture;
QString toolset;
bool setGenConfig;
bool setToolset;
bool enabled;
};
......
......@@ -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);
......
......@@ -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;
......
......@@ -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);
}
......
......@@ -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,
},
});
......
......@@ -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,
},
};
......
......@@ -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;
}
......
......@@ -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"
}
]
}
^CMake Error: Could not read presets from [^
]*/Tests/RunCMake/CMakePresets/InvalidCMakeGeneratorConfig: Invalid preset$
]*/Tests/RunCMake/CMakePresets/InvalidArchitectureStrategy: Invalid preset$
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment