1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmInstrumentationCommand.h"
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <functional>
#include <set>
#include <cmext/string_view>
#include "cmArgumentParser.h"
#include "cmArgumentParserTypes.h"
#include "cmExecutionStatus.h"
#include "cmExperimental.h"
#include "cmInstrumentation.h"
#include "cmInstrumentationQuery.h"
#include "cmMakefile.h"
#include "cmStringAlgorithms.h"
#include "cmake.h"
namespace {
bool isCharDigit(char ch)
{
return std::isdigit(static_cast<unsigned char>(ch));
}
bool validateVersion(std::string const& key, std::string const& versionString,
int& version, cmExecutionStatus& status)
{
if (!std::all_of(versionString.begin(), versionString.end(), isCharDigit)) {
status.SetError(cmStrCat("given a non-integer ", key, '.'));
return false;
}
version = std::atoi(versionString.c_str());
if (version != 1) {
status.SetError(cmStrCat(
"QUERY subcommand given an unsupported ", key, " \"", versionString,
"\" (the only currently supported version is 1)."));
return false;
}
return true;
}
template <typename E>
std::function<bool(std::string const&, E&)> EnumParser(
std::vector<std::string> const toString)
{
return [toString](std::string const& value, E& out) -> bool {
for (size_t i = 0; i < toString.size(); ++i) {
if (value == toString[i]) {
out = (E)i;
return true;
}
}
return false;
};
}
}
bool cmInstrumentationCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
// if (status->GetMakefile().GetPropertyKeys) {
if (!cmExperimental::HasSupportEnabled(
status.GetMakefile(), cmExperimental::Feature::Instrumentation)) {
status.SetError(
"requires the experimental Instrumentation flag to be enabled");
return false;
}
if (args.empty()) {
status.SetError("must be called with arguments.");
return false;
}
struct Arguments : public ArgumentParser::ParseResult
{
ArgumentParser::NonEmpty<std::string> ApiVersion;
ArgumentParser::NonEmpty<std::string> DataVersion;
ArgumentParser::NonEmpty<std::vector<std::string>> Queries;
ArgumentParser::NonEmpty<std::vector<std::string>> Hooks;
ArgumentParser::NonEmpty<std::vector<std::vector<std::string>>> Callbacks;
};
static auto const parser = cmArgumentParser<Arguments>{}
.Bind("API_VERSION"_s, &Arguments::ApiVersion)
.Bind("DATA_VERSION"_s, &Arguments::DataVersion)
.Bind("QUERIES"_s, &Arguments::Queries)
.Bind("HOOKS"_s, &Arguments::Hooks)
.Bind("CALLBACK"_s, &Arguments::Callbacks);
std::vector<std::string> unparsedArguments;
Arguments const arguments = parser.Parse(args, &unparsedArguments);
if (arguments.MaybeReportError(status.GetMakefile())) {
return true;
}
if (!unparsedArguments.empty()) {
status.SetError("given unknown argument \"" + unparsedArguments.front() +
"\".");
return false;
}
int apiVersion;
int dataVersion;
if (!validateVersion("API_VERSION", arguments.ApiVersion, apiVersion,
status) ||
!validateVersion("DATA_VERSION", arguments.DataVersion, dataVersion,
status)) {
return false;
}
std::set<cmInstrumentationQuery::Query> queries;
auto queryParser = EnumParser<cmInstrumentationQuery::Query>(
cmInstrumentationQuery::QueryString);
for (auto const& arg : arguments.Queries) {
cmInstrumentationQuery::Query query;
if (!queryParser(arg, query)) {
status.SetError(
cmStrCat("given invalid argument to QUERIES \"", arg, '"'));
return false;
}
queries.insert(query);
}
std::set<cmInstrumentationQuery::Hook> hooks;
auto hookParser = EnumParser<cmInstrumentationQuery::Hook>(
cmInstrumentationQuery::HookString);
for (auto const& arg : arguments.Hooks) {
cmInstrumentationQuery::Hook hook;
if (!hookParser(arg, hook)) {
status.SetError(
cmStrCat("given invalid argument to HOOKS \"", arg, '"'));
return false;
}
hooks.insert(hook);
}
status.GetMakefile()
.GetCMakeInstance()
->GetInstrumentation()
->WriteJSONQuery(queries, hooks, arguments.Callbacks);
return true;
}
|