Add support for target condition

toml-checker archive_eaf03eb7
Duncan Ogilvie 3 years ago
parent 8ea5b77ba5
commit eaf03eb785

@ -84,6 +84,7 @@ struct Target {
ConditionVector precompile_headers; ConditionVector precompile_headers;
ConditionVector private_precompile_headers; ConditionVector private_precompile_headers;
std::string condition;
std::string alias; std::string alias;
Condition<tsl::ordered_map<std::string, std::string>> properties; Condition<tsl::ordered_map<std::string, std::string>> properties;

@ -214,6 +214,10 @@ CMake::CMake(const std::string &path, bool build) {
sources.insert(sources.end(), headers.begin(), headers.end()); sources.insert(sources.end(), headers.begin(), headers.end());
} }
if (t.contains("condition")) {
target.condition = toml::find(t, "condition").as_string();
}
if (t.contains("alias")) { if (t.contains("alias")) {
target.alias = toml::find(t, "alias").as_string(); target.alias = toml::find(t, "alias").as_string();
} }

@ -158,8 +158,10 @@ struct Command {
bool first_arg = true; bool first_arg = true;
bool had_newline = false; bool had_newline = false;
bool generated = false; bool generated = false;
std::string post_comment;
Command(std::stringstream &ss, int depth, const std::string &command) : ss(ss), depth(depth), command(command) {} Command(std::stringstream &ss, int depth, std::string command, std::string post_comment)
: ss(ss), depth(depth), command(std::move(command)), post_comment(std::move(post_comment)) {}
~Command() { ~Command() {
if (!generated) { if (!generated) {
@ -303,7 +305,11 @@ struct Command {
(void)std::initializer_list<bool>{print_arg(values)...}; (void)std::initializer_list<bool>{print_arg(values)...};
if (had_newline) if (had_newline)
ss << '\n' << indent(depth); ss << '\n' << indent(depth);
ss << ")\n"; ss << ")";
if (!post_comment.empty()) {
ss << " # " << post_comment;
}
ss << "\n";
return CommandEndl(ss); return CommandEndl(ss);
} }
}; };
@ -326,18 +332,18 @@ struct Generator {
std::stringstream ss; std::stringstream ss;
int indent = 0; int indent = 0;
Command cmd(const std::string &command) { Command cmd(const std::string &command, const std::string &post_comment = "") {
if (command.empty()) if (command.empty())
throw std::invalid_argument("command cannot be empty"); throw std::invalid_argument("command cannot be empty");
if (command == "if") { if (command == "if") {
indent++; indent++;
return Command(ss, indent - 1, command); return Command(ss, indent - 1, command, post_comment);
} else if (command == "else" || command == "elseif") { } else if (command == "else" || command == "elseif") {
return Command(ss, indent - 1, command); return Command(ss, indent - 1, command, post_comment);
} else if (command == "endif") { } else if (command == "endif") {
indent--; indent--;
} }
return Command(ss, indent, command); return Command(ss, indent, command, post_comment);
} }
CommandEndl comment(const std::string &comment) { CommandEndl comment(const std::string &comment) {
@ -390,7 +396,7 @@ struct Generator {
// TODO: somehow print line number information here? // TODO: somehow print line number information here?
throw std::runtime_error("Unknown condition '" + condition + "'"); throw std::runtime_error("Unknown condition '" + condition + "'");
} }
cmd("if")(RawArg(cmake.conditions[condition])); cmd("if", condition)(RawArg(cmake.conditions[condition]));
} }
if (!itr.second.empty()) { if (!itr.second.empty()) {
@ -441,7 +447,7 @@ int generate_cmake(const char *path, bool root) {
// Helper lambdas for more convenient CMake generation // Helper lambdas for more convenient CMake generation
auto &ss = gen.ss; auto &ss = gen.ss;
auto cmd = [&gen](const std::string &comment) { return gen.cmd(comment); }; auto cmd = [&gen](const std::string &command) { return gen.cmd(command); };
auto comment = [&gen](const std::string &comment) { return gen.comment(comment); }; auto comment = [&gen](const std::string &comment) { return gen.comment(comment); };
auto endl = [&gen]() { gen.endl(); }; auto endl = [&gen]() { gen.endl(); };
auto inject_includes = [&gen](const std::vector<std::string> &includes) { gen.inject_includes(includes); }; auto inject_includes = [&gen](const std::vector<std::string> &includes) { gen.inject_includes(includes); };
@ -657,6 +663,15 @@ int generate_cmake(const char *path, bool root) {
if (!cmake.targets.empty()) { if (!cmake.targets.empty()) {
for (const auto &target : cmake.targets) { for (const auto &target : cmake.targets) {
comment("Target " + target.name); comment("Target " + target.name);
if (!target.condition.empty()) {
const auto &condition = target.condition;
if (cmake.conditions.count(condition) == 0) {
throw std::runtime_error("Unknown condition '" + condition + "' for [target." + target.name + "]");
}
gen.cmd("if", condition)(RawArg(cmake.conditions[condition]));
}
cmd("set")("CMKR_TARGET", target.name); cmd("set")("CMKR_TARGET", target.name);
gen.handle_condition(target.include_before, gen.handle_condition(target.include_before,
@ -791,6 +806,11 @@ int generate_cmake(const char *path, bool root) {
cmd("unset")("CMKR_TARGET"); cmd("unset")("CMKR_TARGET");
cmd("unset")("CMKR_SOURCES"); cmd("unset")("CMKR_SOURCES");
if (!target.condition.empty()) {
cmd("endif")();
}
endl(); endl();
} }
} }

Loading…
Cancel
Save