From d9090882cae78695765204a3e1b60c6a9bf27977 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 9 Dec 2017 02:43:02 +0100 Subject: Added ninja backend, very simple project works --- backend/ninja/Ninja.cpp | 95 +++++++++++++++++++++++++++++++++++++++++++++++++ backend/ninja/Ninja.hpp | 21 +++++++++++ 2 files changed, 116 insertions(+) create mode 100644 backend/ninja/Ninja.cpp create mode 100644 backend/ninja/Ninja.hpp (limited to 'backend') 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 +#include "Ninja.hpp" +#include "../../include/FileUtil.hpp" + +using namespace std; + +namespace backend +{ + string join(const vector &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 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 +#include + +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 sourceFiles; + }; +} + +#endif //BACKEND_NINJA_HPP -- cgit v1.2.3