mirror of
https://github.com/Kitware/CMake.git
synced 2025-10-17 07:11:52 +08:00
Refactor: Pass CMakePresets.json version to ExpandMacros() functions
This commit is contained in:
@@ -28,9 +28,9 @@
|
|||||||
return _result; \
|
return _result; \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define CHECK_EXPAND(out, field, expanders) \
|
#define CHECK_EXPAND(out, field, expanders, version) \
|
||||||
{ \
|
{ \
|
||||||
switch (ExpandMacros(field, expanders)) { \
|
switch (ExpandMacros(field, expanders, version)) { \
|
||||||
case ExpandMacroResult::Error: \
|
case ExpandMacroResult::Error: \
|
||||||
return false; \
|
return false; \
|
||||||
case ExpandMacroResult::Ignore: \
|
case ExpandMacroResult::Ignore: \
|
||||||
@@ -849,16 +849,19 @@ enum class ExpandMacroResult
|
|||||||
};
|
};
|
||||||
|
|
||||||
using MacroExpander = std::function<ExpandMacroResult(
|
using MacroExpander = std::function<ExpandMacroResult(
|
||||||
const std::string&, const std::string&, std::string&)>;
|
const std::string&, const std::string&, std::string&, int version)>;
|
||||||
|
|
||||||
ExpandMacroResult VisitEnv(std::string& value, CycleStatus& status,
|
ExpandMacroResult VisitEnv(std::string& value, CycleStatus& status,
|
||||||
const std::vector<MacroExpander>& macroExpanders);
|
const std::vector<MacroExpander>& macroExpanders,
|
||||||
|
int version);
|
||||||
ExpandMacroResult ExpandMacros(
|
ExpandMacroResult ExpandMacros(
|
||||||
std::string& out, const std::vector<MacroExpander>& macroExpanders);
|
std::string& out, const std::vector<MacroExpander>& macroExpanders,
|
||||||
ExpandMacroResult ExpandMacro(
|
int version);
|
||||||
std::string& out, const std::string& macroNamespace,
|
ExpandMacroResult ExpandMacro(std::string& out,
|
||||||
|
const std::string& macroNamespace,
|
||||||
const std::string& macroName,
|
const std::string& macroName,
|
||||||
const std::vector<MacroExpander>& macroExpanders);
|
const std::vector<MacroExpander>& macroExpanders,
|
||||||
|
int version);
|
||||||
|
|
||||||
bool ExpandMacros(const cmCMakePresetsFile& file,
|
bool ExpandMacros(const cmCMakePresetsFile& file,
|
||||||
const ConfigurePreset& preset,
|
const ConfigurePreset& preset,
|
||||||
@@ -866,7 +869,7 @@ bool ExpandMacros(const cmCMakePresetsFile& file,
|
|||||||
const std::vector<MacroExpander>& macroExpanders)
|
const std::vector<MacroExpander>& macroExpanders)
|
||||||
{
|
{
|
||||||
std::string binaryDir = preset.BinaryDir;
|
std::string binaryDir = preset.BinaryDir;
|
||||||
CHECK_EXPAND(out, binaryDir, macroExpanders)
|
CHECK_EXPAND(out, binaryDir, macroExpanders, file.GetVersion(preset))
|
||||||
|
|
||||||
if (!cmSystemTools::FileIsFullPath(binaryDir)) {
|
if (!cmSystemTools::FileIsFullPath(binaryDir)) {
|
||||||
binaryDir = cmStrCat(file.SourceDir, '/', binaryDir);
|
binaryDir = cmStrCat(file.SourceDir, '/', binaryDir);
|
||||||
@@ -876,7 +879,7 @@ bool ExpandMacros(const cmCMakePresetsFile& file,
|
|||||||
|
|
||||||
if (!preset.InstallDir.empty()) {
|
if (!preset.InstallDir.empty()) {
|
||||||
std::string installDir = preset.InstallDir;
|
std::string installDir = preset.InstallDir;
|
||||||
CHECK_EXPAND(out, installDir, macroExpanders)
|
CHECK_EXPAND(out, installDir, macroExpanders, file.GetVersion(preset))
|
||||||
|
|
||||||
if (!cmSystemTools::FileIsFullPath(installDir)) {
|
if (!cmSystemTools::FileIsFullPath(installDir)) {
|
||||||
installDir = cmStrCat(file.SourceDir, '/', installDir);
|
installDir = cmStrCat(file.SourceDir, '/', installDir);
|
||||||
@@ -887,67 +890,76 @@ bool ExpandMacros(const cmCMakePresetsFile& file,
|
|||||||
|
|
||||||
for (auto& variable : out->CacheVariables) {
|
for (auto& variable : out->CacheVariables) {
|
||||||
if (variable.second) {
|
if (variable.second) {
|
||||||
CHECK_EXPAND(out, variable.second->Value, macroExpanders)
|
CHECK_EXPAND(out, variable.second->Value, macroExpanders,
|
||||||
|
file.GetVersion(preset))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ExpandMacros(const cmCMakePresetsFile&, const BuildPreset&,
|
bool ExpandMacros(const cmCMakePresetsFile& file, const BuildPreset& preset,
|
||||||
cm::optional<BuildPreset>& out,
|
cm::optional<BuildPreset>& out,
|
||||||
const std::vector<MacroExpander>& macroExpanders)
|
const std::vector<MacroExpander>& macroExpanders)
|
||||||
{
|
{
|
||||||
for (auto& target : out->Targets) {
|
for (auto& target : out->Targets) {
|
||||||
CHECK_EXPAND(out, target, macroExpanders)
|
CHECK_EXPAND(out, target, macroExpanders, file.GetVersion(preset))
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto& nativeToolOption : out->NativeToolOptions) {
|
for (auto& nativeToolOption : out->NativeToolOptions) {
|
||||||
CHECK_EXPAND(out, nativeToolOption, macroExpanders)
|
CHECK_EXPAND(out, nativeToolOption, macroExpanders,
|
||||||
|
file.GetVersion(preset))
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ExpandMacros(const cmCMakePresetsFile&, const TestPreset&,
|
bool ExpandMacros(const cmCMakePresetsFile& file, const TestPreset& preset,
|
||||||
cm::optional<TestPreset>& out,
|
cm::optional<TestPreset>& out,
|
||||||
const std::vector<MacroExpander>& macroExpanders)
|
const std::vector<MacroExpander>& macroExpanders)
|
||||||
{
|
{
|
||||||
for (auto& overwrite : out->OverwriteConfigurationFile) {
|
for (auto& overwrite : out->OverwriteConfigurationFile) {
|
||||||
CHECK_EXPAND(out, overwrite, macroExpanders);
|
CHECK_EXPAND(out, overwrite, macroExpanders, file.GetVersion(preset));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (out->Output) {
|
if (out->Output) {
|
||||||
CHECK_EXPAND(out, out->Output->OutputLogFile, macroExpanders)
|
CHECK_EXPAND(out, out->Output->OutputLogFile, macroExpanders,
|
||||||
|
file.GetVersion(preset))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (out->Filter) {
|
if (out->Filter) {
|
||||||
if (out->Filter->Include) {
|
if (out->Filter->Include) {
|
||||||
CHECK_EXPAND(out, out->Filter->Include->Name, macroExpanders)
|
CHECK_EXPAND(out, out->Filter->Include->Name, macroExpanders,
|
||||||
CHECK_EXPAND(out, out->Filter->Include->Label, macroExpanders)
|
file.GetVersion(preset))
|
||||||
|
CHECK_EXPAND(out, out->Filter->Include->Label, macroExpanders,
|
||||||
|
file.GetVersion(preset))
|
||||||
|
|
||||||
if (out->Filter->Include->Index) {
|
if (out->Filter->Include->Index) {
|
||||||
CHECK_EXPAND(out, out->Filter->Include->Index->IndexFile,
|
CHECK_EXPAND(out, out->Filter->Include->Index->IndexFile,
|
||||||
macroExpanders);
|
macroExpanders, file.GetVersion(preset));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (out->Filter->Exclude) {
|
if (out->Filter->Exclude) {
|
||||||
CHECK_EXPAND(out, out->Filter->Exclude->Name, macroExpanders)
|
CHECK_EXPAND(out, out->Filter->Exclude->Name, macroExpanders,
|
||||||
CHECK_EXPAND(out, out->Filter->Exclude->Label, macroExpanders)
|
file.GetVersion(preset))
|
||||||
|
CHECK_EXPAND(out, out->Filter->Exclude->Label, macroExpanders,
|
||||||
|
file.GetVersion(preset))
|
||||||
|
|
||||||
if (out->Filter->Exclude->Fixtures) {
|
if (out->Filter->Exclude->Fixtures) {
|
||||||
CHECK_EXPAND(out, out->Filter->Exclude->Fixtures->Any, macroExpanders)
|
CHECK_EXPAND(out, out->Filter->Exclude->Fixtures->Any, macroExpanders,
|
||||||
|
file.GetVersion(preset))
|
||||||
CHECK_EXPAND(out, out->Filter->Exclude->Fixtures->Setup,
|
CHECK_EXPAND(out, out->Filter->Exclude->Fixtures->Setup,
|
||||||
macroExpanders)
|
macroExpanders, file.GetVersion(preset))
|
||||||
CHECK_EXPAND(out, out->Filter->Exclude->Fixtures->Cleanup,
|
CHECK_EXPAND(out, out->Filter->Exclude->Fixtures->Cleanup,
|
||||||
macroExpanders)
|
macroExpanders, file.GetVersion(preset))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (out->Execution) {
|
if (out->Execution) {
|
||||||
CHECK_EXPAND(out, out->Execution->ResourceSpecFile, macroExpanders)
|
CHECK_EXPAND(out, out->Execution->ResourceSpecFile, macroExpanders,
|
||||||
|
file.GetVersion(preset))
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -968,8 +980,8 @@ bool ExpandMacros(const cmCMakePresetsFile& file, const T& preset,
|
|||||||
|
|
||||||
MacroExpander defaultMacroExpander =
|
MacroExpander defaultMacroExpander =
|
||||||
[&file, &preset](const std::string& macroNamespace,
|
[&file, &preset](const std::string& macroNamespace,
|
||||||
const std::string& macroName,
|
const std::string& macroName, std::string& macroOut,
|
||||||
std::string& macroOut) -> ExpandMacroResult {
|
int /*version*/) -> ExpandMacroResult {
|
||||||
if (macroNamespace.empty()) {
|
if (macroNamespace.empty()) {
|
||||||
if (macroName == "sourceDir") {
|
if (macroName == "sourceDir") {
|
||||||
macroOut += file.SourceDir;
|
macroOut += file.SourceDir;
|
||||||
@@ -1006,11 +1018,12 @@ bool ExpandMacros(const cmCMakePresetsFile& file, const T& preset,
|
|||||||
MacroExpander environmentMacroExpander =
|
MacroExpander environmentMacroExpander =
|
||||||
[¯oExpanders, &out, &envCycles](
|
[¯oExpanders, &out, &envCycles](
|
||||||
const std::string& macroNamespace, const std::string& macroName,
|
const std::string& macroNamespace, const std::string& macroName,
|
||||||
std::string& result) -> ExpandMacroResult {
|
std::string& result, int version) -> ExpandMacroResult {
|
||||||
if (macroNamespace == "env" && !macroName.empty() && out) {
|
if (macroNamespace == "env" && !macroName.empty() && out) {
|
||||||
auto v = out->Environment.find(macroName);
|
auto v = out->Environment.find(macroName);
|
||||||
if (v != out->Environment.end() && v->second) {
|
if (v != out->Environment.end() && v->second) {
|
||||||
auto e = VisitEnv(*v->second, envCycles[macroName], macroExpanders);
|
auto e =
|
||||||
|
VisitEnv(*v->second, envCycles[macroName], macroExpanders, version);
|
||||||
if (e != ExpandMacroResult::Ok) {
|
if (e != ExpandMacroResult::Ok) {
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
@@ -1038,7 +1051,8 @@ bool ExpandMacros(const cmCMakePresetsFile& file, const T& preset,
|
|||||||
|
|
||||||
for (auto& v : out->Environment) {
|
for (auto& v : out->Environment) {
|
||||||
if (v.second) {
|
if (v.second) {
|
||||||
switch (VisitEnv(*v.second, envCycles[v.first], macroExpanders)) {
|
switch (VisitEnv(*v.second, envCycles[v.first], macroExpanders,
|
||||||
|
file.GetVersion(preset))) {
|
||||||
case ExpandMacroResult::Error:
|
case ExpandMacroResult::Error:
|
||||||
return false;
|
return false;
|
||||||
case ExpandMacroResult::Ignore:
|
case ExpandMacroResult::Ignore:
|
||||||
@@ -1054,7 +1068,8 @@ bool ExpandMacros(const cmCMakePresetsFile& file, const T& preset,
|
|||||||
}
|
}
|
||||||
|
|
||||||
ExpandMacroResult VisitEnv(std::string& value, CycleStatus& status,
|
ExpandMacroResult VisitEnv(std::string& value, CycleStatus& status,
|
||||||
const std::vector<MacroExpander>& macroExpanders)
|
const std::vector<MacroExpander>& macroExpanders,
|
||||||
|
int version)
|
||||||
{
|
{
|
||||||
if (status == CycleStatus::Verified) {
|
if (status == CycleStatus::Verified) {
|
||||||
return ExpandMacroResult::Ok;
|
return ExpandMacroResult::Ok;
|
||||||
@@ -1064,7 +1079,7 @@ ExpandMacroResult VisitEnv(std::string& value, CycleStatus& status,
|
|||||||
}
|
}
|
||||||
|
|
||||||
status = CycleStatus::InProgress;
|
status = CycleStatus::InProgress;
|
||||||
auto e = ExpandMacros(value, macroExpanders);
|
auto e = ExpandMacros(value, macroExpanders, version);
|
||||||
if (e != ExpandMacroResult::Ok) {
|
if (e != ExpandMacroResult::Ok) {
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
@@ -1073,7 +1088,8 @@ ExpandMacroResult VisitEnv(std::string& value, CycleStatus& status,
|
|||||||
}
|
}
|
||||||
|
|
||||||
ExpandMacroResult ExpandMacros(
|
ExpandMacroResult ExpandMacros(
|
||||||
std::string& out, const std::vector<MacroExpander>& macroExpanders)
|
std::string& out, const std::vector<MacroExpander>& macroExpanders,
|
||||||
|
int version)
|
||||||
{
|
{
|
||||||
std::string result;
|
std::string result;
|
||||||
std::string macroNamespace;
|
std::string macroNamespace;
|
||||||
@@ -1120,8 +1136,8 @@ ExpandMacroResult ExpandMacros(
|
|||||||
|
|
||||||
case State::MacroName:
|
case State::MacroName:
|
||||||
if (c == '}') {
|
if (c == '}') {
|
||||||
auto e =
|
auto e = ExpandMacro(result, macroNamespace, macroName,
|
||||||
ExpandMacro(result, macroNamespace, macroName, macroExpanders);
|
macroExpanders, version);
|
||||||
if (e != ExpandMacroResult::Ok) {
|
if (e != ExpandMacroResult::Ok) {
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
@@ -1153,10 +1169,11 @@ ExpandMacroResult ExpandMacros(
|
|||||||
ExpandMacroResult ExpandMacro(std::string& out,
|
ExpandMacroResult ExpandMacro(std::string& out,
|
||||||
const std::string& macroNamespace,
|
const std::string& macroNamespace,
|
||||||
const std::string& macroName,
|
const std::string& macroName,
|
||||||
const std::vector<MacroExpander>& macroExpanders)
|
const std::vector<MacroExpander>& macroExpanders,
|
||||||
|
int version)
|
||||||
{
|
{
|
||||||
for (auto const& macroExpander : macroExpanders) {
|
for (auto const& macroExpander : macroExpanders) {
|
||||||
auto result = macroExpander(macroNamespace, macroName, out);
|
auto result = macroExpander(macroNamespace, macroName, out, version);
|
||||||
if (result != ExpandMacroResult::Ignore) {
|
if (result != ExpandMacroResult::Ignore) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -1549,6 +1566,11 @@ cmCMakePresetsFile::ReadFileResult cmCMakePresetsFile::ReadJSONFile(
|
|||||||
if (v < MIN_VERSION || v > MAX_VERSION) {
|
if (v < MIN_VERSION || v > MAX_VERSION) {
|
||||||
return ReadFileResult::UNRECOGNIZED_VERSION;
|
return ReadFileResult::UNRECOGNIZED_VERSION;
|
||||||
}
|
}
|
||||||
|
if (user) {
|
||||||
|
this->UserVersion = v;
|
||||||
|
} else {
|
||||||
|
this->Version = v;
|
||||||
|
}
|
||||||
|
|
||||||
// Support for build and test presets added in version 2.
|
// Support for build and test presets added in version 2.
|
||||||
if (v < 2 &&
|
if (v < 2 &&
|
||||||
|
@@ -294,6 +294,13 @@ public:
|
|||||||
std::vector<std::string> TestPresetOrder;
|
std::vector<std::string> TestPresetOrder;
|
||||||
|
|
||||||
std::string SourceDir;
|
std::string SourceDir;
|
||||||
|
int Version;
|
||||||
|
int UserVersion;
|
||||||
|
|
||||||
|
int GetVersion(const Preset& preset) const
|
||||||
|
{
|
||||||
|
return preset.User ? this->UserVersion : this->Version;
|
||||||
|
}
|
||||||
|
|
||||||
static std::string GetFilename(const std::string& sourceDir);
|
static std::string GetFilename(const std::string& sourceDir);
|
||||||
static std::string GetUserFilename(const std::string& sourceDir);
|
static std::string GetUserFilename(const std::string& sourceDir);
|
||||||
|
Reference in New Issue
Block a user