| #include "cmInstrumentationQuery.h" | |
| #include <algorithm> | |
| #include <ctime> | |
| #include <functional> | |
| #include <iterator> | |
| #include <set> | |
| #include <vector> | |
| #include <cmext/string_view> | |
| #include <cm3p/json/value.h> | |
| #include "cmJSONHelpers.h" | |
| #include "cmStringAlgorithms.h" | |
| std::vector<std::string> const cmInstrumentationQuery::OptionString{ | |
| "staticSystemInformation", | |
| "dynamicSystemInformation", | |
| "captureOutput", | |
| "processMetrics", | |
| "compileTrace", | |
| "cdashSubmit", | |
| "cdashVerbose", | |
| "trace" | |
| }; | |
| std::vector<std::string> const cmInstrumentationQuery::HookString{ | |
| "postGenerate", "preBuild", "postBuild", "preCMakeBuild", | |
| "postCMakeBuild", "postCTest", "postCMakeInstall", "postCMakeWorkflow", | |
| "prepareForCDash", "manual" | |
| }; | |
| namespace ErrorMessages { | |
| JsonErrors::ErrorGenerator ErrorGeneratorBuilder( | |
| std::string const& errorMessage) | |
| { | |
| return [errorMessage](Json::Value const* value, cmJSONState* state) -> void { | |
| state->AddErrorAtValue(errorMessage, value); | |
| }; | |
| } | |
| static JsonErrors::ErrorGenerator const InvalidArray = | |
| ErrorGeneratorBuilder("Invalid Array"); | |
| JsonErrors::ErrorGenerator InvalidRootQueryObject( | |
| JsonErrors::ObjectError errorType, Json::Value::Members const& extraFields) | |
| { | |
| return JsonErrors::INVALID_NAMED_OBJECT( | |
| [](Json::Value const*, cmJSONState*) -> std::string { | |
| return "root object"; | |
| })(errorType, extraFields); | |
| } | |
| } | |
| using JSONHelperBuilder = cmJSONHelperBuilder; | |
| using Version = cmInstrumentationQuery::Version; | |
| template <typename E> | |
| static cmJSONHelper<E> EnumHelper(std::vector<std::string> const toString, | |
| std::string const& type) | |
| { | |
| return [toString, type](E& out, Json::Value const* value, | |
| cmJSONState* state) -> bool { | |
| for (size_t i = 0; i < toString.size(); ++i) { | |
| if (value->asString() == toString[i]) { | |
| out = static_cast<E>(i); | |
| return true; | |
| } | |
| } | |
| state->AddErrorAtValue( | |
| cmStrCat("Not a valid "_s, type, ": \""_s, value->asString(), '"'), | |
| value); | |
| return false; | |
| }; | |
| } | |
| static cmJSONHelper<cmInstrumentationQuery::Option> const OptionHelper = | |
| EnumHelper<cmInstrumentationQuery::Option>( | |
| cmInstrumentationQuery::OptionString, "option"); | |
| static cmJSONHelper<std::vector<cmInstrumentationQuery::Option>> const | |
| OptionListHelper = JSONHelperBuilder::Vector<cmInstrumentationQuery::Option>( | |
| ErrorMessages::InvalidArray, OptionHelper); | |
| static cmJSONHelper<cmInstrumentationQuery::Hook> const HookHelper = | |
| EnumHelper<cmInstrumentationQuery::Hook>(cmInstrumentationQuery::HookString, | |
| "hook"); | |
| static cmJSONHelper<std::vector<cmInstrumentationQuery::Hook>> const | |
| HookListHelper = JSONHelperBuilder::Vector<cmInstrumentationQuery::Hook>( | |
| ErrorMessages::InvalidArray, HookHelper); | |
| static cmJSONHelper<std::string> const CallbackHelper = | |
| JSONHelperBuilder::String(); | |
| static cmJSONHelper<std::vector<std::string>> const CallbackListHelper = | |
| JSONHelperBuilder::Vector<std::string>(ErrorMessages::InvalidArray, | |
| CallbackHelper); | |
| JsonErrors::ErrorGenerator InvalidVersionObject( | |
| JsonErrors::ObjectError errorType, Json::Value::Members const& extraFields) | |
| { | |
| return JsonErrors::INVALID_NAMED_OBJECT( | |
| [](Json::Value const*, cmJSONState*) -> std::string { | |
| return "version object"; | |
| })(errorType, extraFields); | |
| } | |
| static JSONHelperBuilder::Object<Version> const VersionObjectHelper = | |
| JSONHelperBuilder::Object<Version>(InvalidVersionObject, false) | |
| .Bind("major"_s, &Version::Major, JSONHelperBuilder::Int(), true) | |
| .Bind("minor"_s, &Version::Minor, JSONHelperBuilder::Int(), false); | |
| bool VersionHelper(Version& out, Json::Value const* value, cmJSONState* state) | |
| { | |
| out.Minor = 0; | |
| if (value->isInt()) { | |
| out.Major = value->asInt(); | |
| } else if (value->isObject()) { | |
| if (!VersionObjectHelper(out, value, state)) { | |
| return false; | |
| } | |
| } else { | |
| state->AddErrorAtValue("Version must be an integer or object", value); | |
| return false; | |
| } | |
| return true; | |
| } | |
| using QueryRoot = cmInstrumentationQuery::QueryJSONRoot; | |
| static JSONHelperBuilder::Object<QueryRoot> const QueryRootHelper = | |
| JSONHelperBuilder::Object<QueryRoot>(ErrorMessages::InvalidRootQueryObject, | |
| false) | |
| .Bind("version"_s, &QueryRoot::DataVersion, VersionHelper, true) | |
| .Bind("options"_s, &QueryRoot::Options, OptionListHelper, false) | |
| .Bind("hooks"_s, &QueryRoot::Hooks, HookListHelper, false) | |
| .Bind("callbacks"_s, &QueryRoot::Callbacks, CallbackListHelper, false); | |
| static JSONHelperBuilder::Object<QueryRoot> const QueryRootVersionOnlyHelper = | |
| JSONHelperBuilder::Object<QueryRoot>(ErrorMessages::InvalidRootQueryObject, | |
| true) | |
| .Bind("version"_s, &QueryRoot::DataVersion, VersionHelper, true); | |
| bool cmInstrumentationQuery::ReadJSON(std::string const& filename, | |
| std::string& errorMessage, | |
| std::set<Option>& options, | |
| std::set<Hook>& hooks, | |
| std::vector<Callback>& callbacks) | |
| { | |
| Json::Value root; | |
| this->ParseState = cmJSONState(filename, &root); | |
| if (!this->ParseState.errors.empty()) { | |
| errorMessage = this->ParseState.GetErrorMessage(true); | |
| return false; | |
| } | |
| if (!QueryRootVersionOnlyHelper(this->QueryRoot, &root, &this->ParseState)) { | |
| errorMessage = this->ParseState.GetErrorMessage(true); | |
| return false; | |
| } | |
| if (!this->ValidDataVersion(this->QueryRoot.DataVersion)) { | |
| // Ignore invalid data versions | |
| return true; | |
| } | |
| if (!QueryRootHelper(this->QueryRoot, &root, &this->ParseState)) { | |
| errorMessage = this->ParseState.GetErrorMessage(true); | |
| return false; | |
| } | |
| std::move(this->QueryRoot.Options.begin(), this->QueryRoot.Options.end(), | |
| std::inserter(options, options.end())); | |
| std::move(this->QueryRoot.Hooks.begin(), this->QueryRoot.Hooks.end(), | |
| std::inserter(hooks, hooks.end())); | |
| for (std::string const& callback : this->QueryRoot.Callbacks) { | |
| callbacks.push_back({ callback, this->QueryRoot.DataVersion }); | |
| } | |
| return true; | |
| } | |
| bool cmInstrumentationQuery::ValidDataVersion(Version version) | |
| { | |
| Version const latest = LatestDataVersion(); | |
| return version.Major == latest.Major && version.Minor <= latest.Minor; | |
| } | |
| Version cmInstrumentationQuery::LatestDataVersion() | |
| { | |
| Version latest; | |
| latest.Major = 1; | |
| latest.Minor = 3; | |
| return latest; | |
| } |