aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authordec05eba <0xdec05eba@gmail.com>2018-09-20 06:37:59 +0200
committerdec05eba <0xdec05eba@gmail.com>2018-09-20 06:40:28 +0200
commit194a5708f3a42c6254cf99d8e03b5c8c60224feb (patch)
tree650a6427b438f5ebdbc31746e5d8e5b5debef7ff /include
parentce415d8b76e355c34dc42b68c4da5a69b2c392b8 (diff)
Initial commit, works to create ninja file
Diffstat (limited to 'include')
-rw-r--r--include/ninja/Ninja.hpp100
1 files changed, 100 insertions, 0 deletions
diff --git a/include/ninja/Ninja.hpp b/include/ninja/Ninja.hpp
new file mode 100644
index 0000000..cd9be0a
--- /dev/null
+++ b/include/ninja/Ninja.hpp
@@ -0,0 +1,100 @@
+#pragma once
+
+#include <string>
+#include <unordered_map>
+#include <stdexcept>
+#include <vector>
+
+namespace ninja
+{
+ enum class NinjaError
+ {
+ NONE,
+ VARIABLE_ALREADY_DEFINED,
+ RULE_ALREADY_DEFINED,
+ RULE_AREADY_BUILT
+ };
+
+ class NinjaException : public std::runtime_error
+ {
+ public:
+ NinjaException(const NinjaError _errorCode, const std::string &errMsg = "") :
+ std::runtime_error(errMsg),
+ errorCode(_errorCode)
+ {
+
+ }
+
+ const NinjaError errorCode;
+ };
+
+ struct NinjaVariable
+ {
+ const std::string name;
+ };
+
+ class NinjaBuildFile;
+
+ class NinjaArg
+ {
+ public:
+ enum class Type
+ {
+ NONE,
+ VALUE,
+ VARIABLE
+ };
+
+ NinjaArg() : type(Type::NONE) {}
+ NinjaArg(const std::string &value) : type(Type::VALUE), arg(value) {}
+ NinjaArg(const NinjaVariable &var) : type(Type::VARIABLE), arg(var.name) {}
+
+ Type type;
+ std::string arg;
+ };
+
+ struct NinjaArgValue
+ {
+ const NinjaVariable arg;
+ const std::string value;
+ };
+
+ // Functions throw NinjaException on failure
+ class NinjaRule
+ {
+ public:
+ NinjaRule(NinjaBuildFile *buildFile, const std::string &name, const std::string &command);
+ void build(const std::string &in, const std::string &out, const std::vector<ninja::NinjaArgValue> &additionalArgs);
+
+ const std::string name;
+ const std::string command;
+ std::string depFile;
+ private:
+ NinjaBuildFile *buildFile;
+ };
+
+ struct NinjaBuild
+ {
+ const NinjaRule *rule;
+ const std::string in;
+ const std::string out;
+ const std::vector<ninja::NinjaArgValue> additionalArgs;
+ };
+
+ // Functions throw NinjaException on failure
+ class NinjaBuildFile
+ {
+ public:
+ ~NinjaBuildFile();
+
+ void defineGlobalVariable(const std::string &name, const std::string &value);
+ NinjaRule* createRule(const std::string &name, const std::vector<NinjaArg> &commandArgs);
+ void build(const NinjaRule *rule, const std::string &in, const std::string &out, const std::vector<ninja::NinjaArgValue> &additionalArgs);
+
+ std::string generate() const;
+ private:
+ std::unordered_map<std::string, std::string> globalVariables;
+ std::unordered_map<std::string, NinjaRule*> rules;
+ std::vector<NinjaBuild> builds;
+ };
+} \ No newline at end of file