| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
- file Copyright.txt or https://cmake.org/licensing for details. */
- #include <functional>
- #include <map>
- #include <string>
- #include <utility>
- #include <vector>
- #include <cm/memory>
- #include <cm/optional>
- #include <cmext/string_view>
- #include <cm3p/json/reader.h>
- #include <cm3p/json/value.h>
- #include "cmsys/FStream.hxx"
- #include "cmCMakePresetsGraph.h"
- #include "cmCMakePresetsGraphInternal.h"
- #include "cmJSONHelpers.h"
- #include "cmVersion.h"
- namespace {
- using ReadFileResult = cmCMakePresetsGraph::ReadFileResult;
- using CacheVariable = cmCMakePresetsGraph::CacheVariable;
- using ConfigurePreset = cmCMakePresetsGraph::ConfigurePreset;
- using BuildPreset = cmCMakePresetsGraph::BuildPreset;
- using TestPreset = cmCMakePresetsGraph::TestPreset;
- using ArchToolsetStrategy = cmCMakePresetsGraph::ArchToolsetStrategy;
- constexpr int MIN_VERSION = 1;
- constexpr int MAX_VERSION = 3;
- struct CMakeVersion
- {
- unsigned int Major = 0;
- unsigned int Minor = 0;
- unsigned int Patch = 0;
- };
- struct RootPresets
- {
- CMakeVersion CMakeMinimumRequired;
- std::vector<cmCMakePresetsGraph::ConfigurePreset> ConfigurePresets;
- std::vector<cmCMakePresetsGraph::BuildPreset> BuildPresets;
- std::vector<cmCMakePresetsGraph::TestPreset> TestPresets;
- };
- std::unique_ptr<cmCMakePresetsGraphInternal::NotCondition> InvertCondition(
- std::unique_ptr<cmCMakePresetsGraph::Condition> condition)
- {
- auto retval = cm::make_unique<cmCMakePresetsGraphInternal::NotCondition>();
- retval->SubCondition = std::move(condition);
- return retval;
- }
- auto const ConditionStringHelper = cmJSONStringHelper<ReadFileResult>(
- ReadFileResult::READ_OK, ReadFileResult::INVALID_CONDITION);
- auto const ConditionBoolHelper = cmJSONBoolHelper<ReadFileResult>(
- ReadFileResult::READ_OK, ReadFileResult::INVALID_CONDITION);
- auto const ConditionStringListHelper =
- cmJSONVectorHelper<std::string, ReadFileResult>(
- ReadFileResult::READ_OK, ReadFileResult::INVALID_CONDITION,
- ConditionStringHelper);
- auto const ConstConditionHelper =
- cmJSONObjectHelper<cmCMakePresetsGraphInternal::ConstCondition,
- ReadFileResult>(ReadFileResult::READ_OK,
- ReadFileResult::INVALID_CONDITION, false)
- .Bind<std::string>("type"_s, nullptr, ConditionStringHelper, true)
- .Bind("value"_s, &cmCMakePresetsGraphInternal::ConstCondition::Value,
- ConditionBoolHelper, true);
- auto const EqualsConditionHelper =
- cmJSONObjectHelper<cmCMakePresetsGraphInternal::EqualsCondition,
- ReadFileResult>(ReadFileResult::READ_OK,
- ReadFileResult::INVALID_CONDITION, false)
- .Bind<std::string>("type"_s, nullptr, ConditionStringHelper, true)
- .Bind("lhs"_s, &cmCMakePresetsGraphInternal::EqualsCondition::Lhs,
- ConditionStringHelper, true)
- .Bind("rhs"_s, &cmCMakePresetsGraphInternal::EqualsCondition::Rhs,
- ConditionStringHelper, true);
- auto const InListConditionHelper =
- cmJSONObjectHelper<cmCMakePresetsGraphInternal::InListCondition,
- ReadFileResult>(ReadFileResult::READ_OK,
- ReadFileResult::INVALID_CONDITION, false)
- .Bind<std::string>("type"_s, nullptr, ConditionStringHelper, true)
- .Bind("string"_s, &cmCMakePresetsGraphInternal::InListCondition::String,
- ConditionStringHelper, true)
- .Bind("list"_s, &cmCMakePresetsGraphInternal::InListCondition::List,
- ConditionStringListHelper, true);
- auto const MatchesConditionHelper =
- cmJSONObjectHelper<cmCMakePresetsGraphInternal::MatchesCondition,
- ReadFileResult>(ReadFileResult::READ_OK,
- ReadFileResult::INVALID_CONDITION, false)
- .Bind<std::string>("type"_s, nullptr, ConditionStringHelper, true)
- .Bind("string"_s, &cmCMakePresetsGraphInternal::MatchesCondition::String,
- ConditionStringHelper, true)
- .Bind("regex"_s, &cmCMakePresetsGraphInternal::MatchesCondition::Regex,
- ConditionStringHelper, true);
- ReadFileResult SubConditionHelper(
- std::unique_ptr<cmCMakePresetsGraph::Condition>& out,
- const Json::Value* value);
- auto const ListConditionVectorHelper =
- cmJSONVectorHelper<std::unique_ptr<cmCMakePresetsGraph::Condition>,
- ReadFileResult>(ReadFileResult::READ_OK,
- ReadFileResult::INVALID_CONDITION,
- SubConditionHelper);
- auto const AnyAllOfConditionHelper =
- cmJSONObjectHelper<cmCMakePresetsGraphInternal::AnyAllOfCondition,
- ReadFileResult>(ReadFileResult::READ_OK,
- ReadFileResult::INVALID_CONDITION, false)
- .Bind<std::string>("type"_s, nullptr, ConditionStringHelper, true)
- .Bind("conditions"_s,
- &cmCMakePresetsGraphInternal::AnyAllOfCondition::Conditions,
- ListConditionVectorHelper);
- auto const NotConditionHelper =
- cmJSONObjectHelper<cmCMakePresetsGraphInternal::NotCondition,
- ReadFileResult>(ReadFileResult::READ_OK,
- ReadFileResult::INVALID_CONDITION, false)
- .Bind<std::string>("type"_s, nullptr, ConditionStringHelper, true)
- .Bind("condition"_s,
- &cmCMakePresetsGraphInternal::NotCondition::SubCondition,
- SubConditionHelper);
- ReadFileResult ConditionHelper(
- std::unique_ptr<cmCMakePresetsGraph::Condition>& out,
- const Json::Value* value)
- {
- if (!value) {
- out.reset();
- return ReadFileResult::READ_OK;
- }
- if (value->isBool()) {
- auto c = cm::make_unique<cmCMakePresetsGraphInternal::ConstCondition>();
- c->Value = value->asBool();
- out = std::move(c);
- return ReadFileResult::READ_OK;
- }
- if (value->isNull()) {
- out = cm::make_unique<cmCMakePresetsGraphInternal::NullCondition>();
- return ReadFileResult::READ_OK;
- }
- if (value->isObject()) {
- if (!value->isMember("type")) {
- return ReadFileResult::INVALID_CONDITION;
- }
- if (!(*value)["type"].isString()) {
- return ReadFileResult::INVALID_CONDITION;
- }
- auto type = (*value)["type"].asString();
- if (type == "const") {
- auto c = cm::make_unique<cmCMakePresetsGraphInternal::ConstCondition>();
- CHECK_OK(ConstConditionHelper(*c, value));
- out = std::move(c);
- return ReadFileResult::READ_OK;
- }
- if (type == "equals" || type == "notEquals") {
- auto c = cm::make_unique<cmCMakePresetsGraphInternal::EqualsCondition>();
- CHECK_OK(EqualsConditionHelper(*c, value));
- out = std::move(c);
- if (type == "notEquals") {
- out = InvertCondition(std::move(out));
- }
- return ReadFileResult::READ_OK;
- }
- if (type == "inList" || type == "notInList") {
- auto c = cm::make_unique<cmCMakePresetsGraphInternal::InListCondition>();
- CHECK_OK(InListConditionHelper(*c, value));
- out = std::move(c);
- if (type == "notInList") {
- out = InvertCondition(std::move(out));
- }
- return ReadFileResult::READ_OK;
- }
- if (type == "matches" || type == "notMatches") {
- auto c =
- cm::make_unique<cmCMakePresetsGraphInternal::MatchesCondition>();
- CHECK_OK(MatchesConditionHelper(*c, value));
- out = std::move(c);
- if (type == "notMatches") {
- out = InvertCondition(std::move(out));
- }
- return ReadFileResult::READ_OK;
- }
- if (type == "anyOf" || type == "allOf") {
- auto c =
- cm::make_unique<cmCMakePresetsGraphInternal::AnyAllOfCondition>();
- c->StopValue = (type == "anyOf");
- CHECK_OK(AnyAllOfConditionHelper(*c, value));
- out = std::move(c);
- return ReadFileResult::READ_OK;
- }
- if (type == "not") {
- auto c = cm::make_unique<cmCMakePresetsGraphInternal::NotCondition>();
- CHECK_OK(NotConditionHelper(*c, value));
- out = std::move(c);
- return ReadFileResult::READ_OK;
- }
- }
- return ReadFileResult::INVALID_CONDITION;
- }
- ReadFileResult SubConditionHelper(
- std::unique_ptr<cmCMakePresetsGraph::Condition>& out,
- const Json::Value* value)
- {
- std::unique_ptr<cmCMakePresetsGraph::Condition> ptr;
- auto result = ConditionHelper(ptr, value);
- if (ptr && ptr->IsNull()) {
- return ReadFileResult::INVALID_CONDITION;
- }
- out = std::move(ptr);
- return result;
- }
- ReadFileResult EnvironmentHelper(cm::optional<std::string>& out,
- const Json::Value* value)
- {
- if (!value || value->isNull()) {
- out = cm::nullopt;
- return ReadFileResult::READ_OK;
- }
- if (value->isString()) {
- out = value->asString();
- return ReadFileResult::READ_OK;
- }
- return ReadFileResult::INVALID_PRESET;
- }
- auto const VersionIntHelper = cmJSONIntHelper<ReadFileResult>(
- ReadFileResult::READ_OK, ReadFileResult::INVALID_VERSION);
- auto const VersionHelper = cmJSONRequiredHelper<int, ReadFileResult>(
- ReadFileResult::NO_VERSION, VersionIntHelper);
- auto const RootVersionHelper =
- cmJSONObjectHelper<int, ReadFileResult>(ReadFileResult::READ_OK,
- ReadFileResult::INVALID_ROOT)
- .Bind("version"_s, VersionHelper, false);
- auto const CMakeVersionUIntHelper = cmJSONUIntHelper<ReadFileResult>(
- ReadFileResult::READ_OK, ReadFileResult::INVALID_VERSION);
- auto const CMakeVersionHelper =
- cmJSONObjectHelper<CMakeVersion, ReadFileResult>(
- ReadFileResult::READ_OK, ReadFileResult::INVALID_CMAKE_VERSION, false)
- .Bind("major"_s, &CMakeVersion::Major, CMakeVersionUIntHelper, false)
- .Bind("minor"_s, &CMakeVersion::Minor, CMakeVersionUIntHelper, false)
- .Bind("patch"_s, &CMakeVersion::Patch, CMakeVersionUIntHelper, false);
- auto const RootPresetsHelper =
- cmJSONObjectHelper<RootPresets, ReadFileResult>(
- ReadFileResult::READ_OK, ReadFileResult::INVALID_ROOT, false)
- .Bind<int>("version"_s, nullptr, VersionHelper)
- .Bind("configurePresets"_s, &RootPresets::ConfigurePresets,
- cmCMakePresetsGraphInternal::ConfigurePresetsHelper, false)
- .Bind("buildPresets"_s, &RootPresets::BuildPresets,
- cmCMakePresetsGraphInternal::BuildPresetsHelper, false)
- .Bind("testPresets"_s, &RootPresets::TestPresets,
- cmCMakePresetsGraphInternal::TestPresetsHelper, false)
- .Bind("cmakeMinimumRequired"_s, &RootPresets::CMakeMinimumRequired,
- CMakeVersionHelper, false)
- .Bind<std::nullptr_t>(
- "vendor"_s, nullptr,
- cmCMakePresetsGraphInternal::VendorHelper(ReadFileResult::INVALID_ROOT),
- false);
- }
- namespace cmCMakePresetsGraphInternal {
- cmCMakePresetsGraph::ReadFileResult PresetStringHelper(
- std::string& out, const Json::Value* value)
- {
- static auto const helper = cmJSONStringHelper<ReadFileResult>(
- ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET);
- return helper(out, value);
- }
- cmCMakePresetsGraph::ReadFileResult PresetVectorStringHelper(
- std::vector<std::string>& out, const Json::Value* value)
- {
- static auto const helper = cmJSONVectorHelper<std::string, ReadFileResult>(
- ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET,
- cmCMakePresetsGraphInternal::PresetStringHelper);
- return helper(out, value);
- }
- cmCMakePresetsGraph::ReadFileResult PresetBoolHelper(bool& out,
- const Json::Value* value)
- {
- static auto const helper = cmJSONBoolHelper<ReadFileResult>(
- ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET);
- return helper(out, value);
- }
- cmCMakePresetsGraph::ReadFileResult PresetOptionalBoolHelper(
- cm::optional<bool>& out, const Json::Value* value)
- {
- static auto const helper = cmJSONOptionalHelper<bool, ReadFileResult>(
- ReadFileResult::READ_OK, PresetBoolHelper);
- return helper(out, value);
- }
- cmCMakePresetsGraph::ReadFileResult PresetIntHelper(int& out,
- const Json::Value* value)
- {
- static auto const helper = cmJSONIntHelper<ReadFileResult>(
- ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET);
- return helper(out, value);
- }
- cmCMakePresetsGraph::ReadFileResult PresetOptionalIntHelper(
- cm::optional<int>& out, const Json::Value* value)
- {
- static auto const helper = cmJSONOptionalHelper<int, ReadFileResult>(
- ReadFileResult::READ_OK, PresetIntHelper);
- return helper(out, value);
- }
- cmCMakePresetsGraph::ReadFileResult PresetVectorIntHelper(
- std::vector<int>& out, const Json::Value* value)
- {
- static auto const helper = cmJSONVectorHelper<int, ReadFileResult>(
- ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET, PresetIntHelper);
- return helper(out, value);
- }
- cmJSONHelper<std::nullptr_t, ReadFileResult> VendorHelper(ReadFileResult error)
- {
- return [error](std::nullptr_t& /*out*/,
- const Json::Value* value) -> ReadFileResult {
- if (!value) {
- return ReadFileResult::READ_OK;
- }
- if (!value->isObject()) {
- return error;
- }
- return ReadFileResult::READ_OK;
- };
- }
- ReadFileResult PresetConditionHelper(
- std::shared_ptr<cmCMakePresetsGraph::Condition>& out,
- const Json::Value* value)
- {
- std::unique_ptr<cmCMakePresetsGraph::Condition> ptr;
- auto result = ConditionHelper(ptr, value);
- out = std::move(ptr);
- return result;
- }
- ReadFileResult PresetVectorOneOrMoreStringHelper(std::vector<std::string>& out,
- const Json::Value* value)
- {
- out.clear();
- if (!value) {
- return ReadFileResult::READ_OK;
- }
- if (value->isString()) {
- out.push_back(value->asString());
- return ReadFileResult::READ_OK;
- }
- return PresetVectorStringHelper(out, value);
- }
- cmCMakePresetsGraph::ReadFileResult EnvironmentMapHelper(
- std::map<std::string, cm::optional<std::string>>& out,
- const Json::Value* value)
- {
- static auto const helper =
- cmJSONMapHelper<cm::optional<std::string>, ReadFileResult>(
- ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET,
- EnvironmentHelper);
- return helper(out, value);
- }
- }
- cmCMakePresetsGraph::ReadFileResult cmCMakePresetsGraph::ReadJSONFile(
- const std::string& filename, bool user)
- {
- cmsys::ifstream fin(filename.c_str());
- if (!fin) {
- return ReadFileResult::FILE_NOT_FOUND;
- }
- // If there's a BOM, toss it.
- cmsys::FStream::ReadBOM(fin);
- Json::Value root;
- Json::CharReaderBuilder builder;
- Json::CharReaderBuilder::strictMode(&builder.settings_);
- if (!Json::parseFromStream(builder, fin, &root, nullptr)) {
- return ReadFileResult::JSON_PARSE_ERROR;
- }
- int v = 0;
- auto result = RootVersionHelper(v, &root);
- if (result != ReadFileResult::READ_OK) {
- return result;
- }
- if (v < MIN_VERSION || v > MAX_VERSION) {
- return ReadFileResult::UNRECOGNIZED_VERSION;
- }
- if (user) {
- this->UserVersion = v;
- } else {
- this->Version = v;
- }
- // Support for build and test presets added in version 2.
- if (v < 2 &&
- (root.isMember("buildPresets") || root.isMember("testPresets"))) {
- return ReadFileResult::BUILD_TEST_PRESETS_UNSUPPORTED;
- }
- RootPresets presets;
- if ((result = RootPresetsHelper(presets, &root)) !=
- ReadFileResult::READ_OK) {
- return result;
- }
- unsigned int currentMajor = cmVersion::GetMajorVersion();
- unsigned int currentMinor = cmVersion::GetMinorVersion();
- unsigned int currentPatch = cmVersion::GetPatchVersion();
- auto const& required = presets.CMakeMinimumRequired;
- if (required.Major > currentMajor ||
- (required.Major == currentMajor &&
- (required.Minor > currentMinor ||
- (required.Minor == currentMinor &&
- (required.Patch > currentPatch))))) {
- return ReadFileResult::UNRECOGNIZED_CMAKE_VERSION;
- }
- for (auto& preset : presets.ConfigurePresets) {
- preset.User = user;
- if (preset.Name.empty()) {
- return ReadFileResult::INVALID_PRESET;
- }
- PresetPair<ConfigurePreset> presetPair;
- presetPair.Unexpanded = preset;
- presetPair.Expanded = cm::nullopt;
- if (!this->ConfigurePresets
- .emplace(std::make_pair(preset.Name, presetPair))
- .second) {
- return ReadFileResult::DUPLICATE_PRESETS;
- }
- // Support for installDir presets added in version 3.
- if (v < 3 && !preset.InstallDir.empty()) {
- return ReadFileResult::INSTALL_PREFIX_UNSUPPORTED;
- }
- // Support for conditions added in version 3.
- if (v < 3 && preset.ConditionEvaluator) {
- return ReadFileResult::CONDITION_UNSUPPORTED;
- }
- // Support for toolchainFile presets added in version 3.
- if (v < 3 && !preset.ToolchainFile.empty()) {
- return ReadFileResult::TOOLCHAIN_FILE_UNSUPPORTED;
- }
- this->ConfigurePresetOrder.push_back(preset.Name);
- }
- for (auto& preset : presets.BuildPresets) {
- preset.User = user;
- if (preset.Name.empty()) {
- return ReadFileResult::INVALID_PRESET;
- }
- PresetPair<BuildPreset> presetPair;
- presetPair.Unexpanded = preset;
- presetPair.Expanded = cm::nullopt;
- if (!this->BuildPresets.emplace(preset.Name, presetPair).second) {
- return ReadFileResult::DUPLICATE_PRESETS;
- }
- // Support for conditions added in version 3.
- if (v < 3 && preset.ConditionEvaluator) {
- return ReadFileResult::CONDITION_UNSUPPORTED;
- }
- this->BuildPresetOrder.push_back(preset.Name);
- }
- for (auto& preset : presets.TestPresets) {
- preset.User = user;
- if (preset.Name.empty()) {
- return ReadFileResult::INVALID_PRESET;
- }
- PresetPair<TestPreset> presetPair;
- presetPair.Unexpanded = preset;
- presetPair.Expanded = cm::nullopt;
- if (!this->TestPresets.emplace(preset.Name, presetPair).second) {
- return ReadFileResult::DUPLICATE_PRESETS;
- }
- // Support for conditions added in version 3.
- if (v < 3 && preset.ConditionEvaluator) {
- return ReadFileResult::CONDITION_UNSUPPORTED;
- }
- this->TestPresetOrder.push_back(preset.Name);
- }
- return ReadFileResult::READ_OK;
- }
|