From 194a5708f3a42c6254cf99d8e03b5c8c60224feb Mon Sep 17 00:00:00 2001 From: dec05eba <0xdec05eba@gmail.com> Date: Thu, 20 Sep 2018 06:37:59 +0200 Subject: Initial commit, works to create ninja file --- src/Ninja.cpp | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 src/Ninja.cpp (limited to 'src/Ninja.cpp') diff --git a/src/Ninja.cpp b/src/Ninja.cpp new file mode 100644 index 0000000..7b3212e --- /dev/null +++ b/src/Ninja.cpp @@ -0,0 +1,121 @@ +#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 += ' '; + result += '\''; + if(arg.type == NinjaArg::Type::VARIABLE) + result += "$"; + result += arg.arg; + result += '\''; + } + return result; + } + + 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) + { + 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) + { + 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) + { + for(const NinjaBuild &build : builds) + { + if(build.rule == rule) + throw NinjaException(NinjaError::RULE_AREADY_BUILT, "Rule already built: " + rule->name); + } + 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; + } +} -- cgit v1.2.3