#include "../include/ninja/Ninja.hpp" #include namespace ninja { static std::string joinArgs(const std::vector &args) { std::string result; for(const NinjaArg &arg : args) { if(!result.empty()) result += ' '; NinjaArg::Type argType = arg.type; if(!arg.arg.empty() && arg.arg[0] == '$') argType = NinjaArg::Type::RAW; switch(argType) { case NinjaArg::Type::VALUE: { result += '\'' + arg.arg + '\''; break; } case NinjaArg::Type::VARIABLE: { result += '$' + arg.arg; break; } case NinjaArg::Type::RAW: { result += arg.arg; break; } } } return result; } static bool isAlpha(char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } static bool isNum(char c) { return c >= '0' && c <= '9'; } static bool isAlnum(char c) { return isAlpha(c) || isNum(c); } static bool isValidName(const std::string &name) { if(name.empty()) return false; if(name.size() > 100) return false; if(!isAlpha(name[0]) && name[0] != '_') return false; for(int i = 1; i < name.size(); ++i) { if(!isAlnum(name[i]) && name[i] != '_') return false; } return true; } NinjaVariable::NinjaVariable(const std::string &_name) : name(_name) { if(!isValidName(name)) throw NinjaException(NinjaError::INVALID_NAME, "Invalid variable name, has to match [a-zA-Z_]{1}[a-zA-Z0-9_]{0,98}"); } NinjaRule::NinjaRule(NinjaBuildFile *_buildFile, const std::string &_name, const std::string &_command) : buildFile(_buildFile), name(_name), command(_command) { assert(buildFile); } NinjaBuildFile::~NinjaBuildFile() { for(auto &rule : rules) { delete rule.second; } } void NinjaRule::build(const std::string &in, const std::string &out, const std::vector &additionalArgs) { buildFile->build(this, in, out, additionalArgs); } void NinjaBuildFile::defineGlobalVariable(const std::string &name, const std::string &value) { if(!isValidName(name)) throw NinjaException(NinjaError::INVALID_NAME, "Invalid global variable name, has to match [a-zA-Z_]{1}[a-zA-Z0-9_]{0,98}"); auto it = globalVariables.find(name); if(it != globalVariables.end()) throw NinjaException(NinjaError::VARIABLE_ALREADY_DEFINED, "Global variable already defined: " + name); globalVariables[name] = value; } NinjaRule* NinjaBuildFile::createRule(const std::string &name, const std::vector &commandArgs) { if(!isValidName(name)) throw NinjaException(NinjaError::INVALID_NAME, "Invalid rule name, has to match [a-zA-Z_]{1}[a-zA-Z0-9_]{0,98}"); auto it = rules.find(name); if(it != rules.end()) throw NinjaException(NinjaError::RULE_ALREADY_DEFINED, "Rule already defined: " + name); NinjaRule *rule = new NinjaRule(this, name, joinArgs(commandArgs)); rules[name] = rule; return rule; } void NinjaBuildFile::build(const NinjaRule *rule, const std::string &in, const std::string &out, const std::vector &additionalArgs) { builds.push_back({ rule, in, out, additionalArgs }); } std::string NinjaBuildFile::generate() const { std::string result; for(const auto &globalVar : globalVariables) { result += globalVar.first; result += " = "; result += globalVar.second; result += '\n'; } if(!globalVariables.empty()) result += '\n'; for(const auto &rule : rules) { result += "rule "; result += rule.first; result += "\n command = "; result += rule.second->command; if(!rule.second->depFile.empty()) { result += "\n depfile = "; result += rule.second->depFile; } result += "\n\n"; } // TODO: enclose all names with quotes to escape whitespace? for(const NinjaBuild &build : builds) { result += "build "; result += build.out; result += ": "; result += build.rule->name; result += ' '; result += build.in; for(const NinjaArgValue &additionalArg : build.additionalArgs) { result += "\n "; result += additionalArg.arg.name; result += " = "; result += additionalArg.value; } result += "\n\n"; } return result; } }