#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; } static std::string escape(const std::string &str) { std::string result; result.reserve(str.size()); for(char c : str) { if(c == '"') result += "\\\""; else result += c; } return '"' + result + '"'; } static std::string combine_escape(const std::vector &strs) { std::string result; for(const std::string &str : strs) { if(!result.empty()) result += ' '; result += escape(str); } return result; } 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; } for(NinjaBuild *build : builds) { delete build; } } NinjaBuild* NinjaRule::build(const std::string &in, const std::string &out, const std::vector &additionalArgs, const std::vector &dependsOnBuilds) { return buildFile->build(this, in, out, additionalArgs, dependsOnBuilds); } NinjaBuild* NinjaRule::build(const std::vector &in, const std::string &out, const std::vector &additionalArgs, const std::vector &dependsOnBuilds) { return buildFile->build(this, in, out, additionalArgs, dependsOnBuilds); } 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; } NinjaBuild* NinjaBuildFile::build(const NinjaRule *rule, const std::string &in, const std::string &out, const std::vector &additionalArgs, const std::vector &dependsOnBuilds) { NinjaBuild *build = new NinjaBuild { rule, { in }, escape(out), additionalArgs, dependsOnBuilds }; builds.push_back(build); return build; } NinjaBuild* NinjaBuildFile::build(const NinjaRule *rule, const std::vector &in, const std::string &out, const std::vector &additionalArgs, const std::vector &dependsOnBuilds) { NinjaBuild *build = new NinjaBuild { rule, in, escape(out), additionalArgs, dependsOnBuilds }; builds.push_back(build); return build; } 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; } if(!rule.second->deps.empty()) { result += "\n deps = "; result += rule.second->deps; } result += "\n\n"; } for(const NinjaBuild *build : builds) { result += "build "; result += build->out; result += ": "; result += build->rule->name; result += ' '; result += combine_escape(build->in); if(!build->dependsOnBuilds.empty()) { result += " ||"; for(const NinjaBuild *dependsOnBuild : build->dependsOnBuilds) { result += ' ' + dependsOnBuild->out; } } for(const NinjaArgValue &additionalArg : build->additionalArgs) { result += "\n "; result += additionalArg.arg.name; result += " = "; result += additionalArg.value; } result += "\n\n"; } return result; } }