aboutsummaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2017-12-09 02:43:02 +0100
committerdec05eba <dec05eba@protonmail.com>2017-12-09 02:43:02 +0100
commitd9090882cae78695765204a3e1b60c6a9bf27977 (patch)
treee3e75fef97df7e02cd715af718ae7c3ffeaee00f /backend
parentfb2072deb3e50afdb062570a3a80ec1afb5bfb56 (diff)
Added ninja backend, very simple project works
Diffstat (limited to 'backend')
-rw-r--r--backend/ninja/Ninja.cpp95
-rw-r--r--backend/ninja/Ninja.hpp21
2 files changed, 116 insertions, 0 deletions
diff --git a/backend/ninja/Ninja.cpp b/backend/ninja/Ninja.cpp
new file mode 100644
index 0000000..7006d30
--- /dev/null
+++ b/backend/ninja/Ninja.cpp
@@ -0,0 +1,95 @@
+#include <cstring>
+#include "Ninja.hpp"
+#include "../../include/FileUtil.hpp"
+
+using namespace std;
+
+namespace backend
+{
+ string join(const vector<string> &list, const char *joinStr)
+ {
+ if(list.empty()) return "";
+ string result;
+ long stringSize = 0;
+ long joinStrLen = strlen(joinStr);
+ int i = 0;
+ for(const string &str : list)
+ {
+ stringSize += str.size();
+ if(!str.empty() && i > 0)
+ stringSize += joinStrLen;
+ ++i;
+ }
+
+ result.reserve(stringSize);
+
+ i = 0;
+ for(const string &str : list)
+ {
+ if(i > 0);
+ result += joinStr;
+ result += str;
+ ++i;
+ }
+
+ return move(result);
+ }
+
+ void Ninja::addSourceFile(const char *filepath)
+ {
+ if(filepath && !containsSourceFile(filepath))
+ sourceFiles.emplace_back(filepath);
+ }
+
+ bool Ninja::containsSourceFile(const char *filepath) const
+ {
+ for(const string &sourceFile : sourceFiles)
+ {
+ if(sourceFile == filepath)
+ return true;
+ }
+ return false;
+ }
+
+ void Ninja::build(const std::string &packageName, const char *savePath)
+ {
+ if(sourceFiles.empty()) return;
+ printf("Package name: %s\n", packageName.c_str());
+
+ string result;
+ result.reserve(16384);
+
+ result += "cflags = -Wall -Werror\n\n";
+
+ result += "rule cpp_COMPILER\n";
+ result += " command = ccache c++ $ARGS -c $in -o $out\n\n";
+
+ result += "rule cpp_LINKER\n";
+ result += " command = ccache c++ $ARGS -o $out $in $LINK_ARGS $aliasing\n\n";
+
+ vector<string> objectNames;
+ for(const string &sourceFile : sourceFiles)
+ {
+ //string sourceFileEncoded = sourceFile;
+ //replace(sourceFileEncoded, '/', '@');
+ string objectName = packageName + "@exe/" + sourceFile + ".o";
+ result += "build ";
+ result += objectName;
+ result += ": cpp_COMPILER ../../";
+ result += sourceFile;
+ result += "\n";
+ result += " ARGS = '-I" + packageName + "@exe' '-I.' '-I..' '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Wall' '-Winvalid-pch' '-Wnon-virtual-dtor' '-O0' '-g'\n\n";
+ objectNames.emplace_back(objectName);
+ }
+
+ result += "build ";
+ result += packageName;
+ result += ": cpp_LINKER ";
+ result += join(objectNames, " ");
+ result += "\n";
+ result += " LINK_ARGS = '-Wl,--no-undefined' '-Wl,--as-needed'\n\n";
+
+ sibs::fileOverwrite(savePath, sibs::StringView(result.data(), result.size()));
+ printf("Created ninja build file: %s\n", savePath);
+ }
+} \ No newline at end of file
diff --git a/backend/ninja/Ninja.hpp b/backend/ninja/Ninja.hpp
new file mode 100644
index 0000000..ad71c80
--- /dev/null
+++ b/backend/ninja/Ninja.hpp
@@ -0,0 +1,21 @@
+#ifndef BACKEND_NINJA_HPP
+#define BACKEND_NINJA_HPP
+
+#include <vector>
+#include <string>
+
+namespace backend
+{
+ class Ninja
+ {
+ public:
+ void addSourceFile(const char *filepath);
+ void build(const std::string &packageName, const char *savePath);
+ private:
+ bool containsSourceFile(const char *filepath) const;
+ private:
+ std::vector<std::string> sourceFiles;
+ };
+}
+
+#endif //BACKEND_NINJA_HPP