aboutsummaryrefslogtreecommitdiff
path: root/backend/ninja
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2017-12-10 04:05:20 +0100
committerdec05eba <dec05eba@protonmail.com>2017-12-10 04:06:02 +0100
commit99570d3fa5abf548d4e8e5e479d82ee66898b602 (patch)
treed52ac6662d51baccde2983639de0ffda3acc9578 /backend/ninja
parent1d3e221a7a20bfd03517e3ae1e35e4a309a69b6a (diff)
Add support for recursive global lib dependencies
Diffstat (limited to 'backend/ninja')
-rw-r--r--backend/ninja/Ninja.cpp77
-rw-r--r--backend/ninja/Ninja.hpp6
2 files changed, 63 insertions, 20 deletions
diff --git a/backend/ninja/Ninja.cpp b/backend/ninja/Ninja.cpp
index 3df4e41..20e2374 100644
--- a/backend/ninja/Ninja.cpp
+++ b/backend/ninja/Ninja.cpp
@@ -76,7 +76,7 @@ namespace backend
// TODO: First check if pkg-config is installed. If it's not, only check dependencies that exists in the dependencies sub directory.
// If pkg-config is installed and dependency is not installed, check in dependencies sub directory.
- Result<string> Ninja::getLinkerFlags(const vector<Dependency> &dependencies) const
+ Result<string> Ninja::getLinkerFlags(const vector<Dependency> &dependencies, LinkerFlagCallbackFunc linkerFlagCallbackFunc) const
{
if(dependencies.empty()) return Result<string>::Ok("");
@@ -96,7 +96,7 @@ namespace backend
else
{
printf("%s, trying global lib\n", pkgConfigDependencyValidation.getErrMsg().c_str());
- Result<string> globalLibLinkerFlagsResult = GlobalLib::getDynamicLibsLinkerFlags(globalLibDir, dependency.name, dependency.version);
+ Result<string> globalLibLinkerFlagsResult = GlobalLib::getStaticLibsLinkerFlags(globalLibDir, dependency.name, dependency.version, linkerFlagCallbackFunc);
if(globalLibLinkerFlagsResult.isErr())
return globalLibLinkerFlagsResult;
@@ -110,12 +110,14 @@ namespace backend
if(pkgConfigLinkerFlagsResult.isErr())
return pkgConfigLinkerFlagsResult;
+ linkerFlagCallbackFunc(pkgConfigLinkerFlagsResult.unwrap());
+
string allLinkerFlags = pkgConfigLinkerFlagsResult.unwrap();
allLinkerFlags += globalLibLinkerFlags;
return Result<string>::Ok(allLinkerFlags);
}
- Result<bool> Ninja::createBuildFile(const std::string &packageName, const vector<Dependency> &dependencies, const char *savePath)
+ Result<bool> Ninja::createBuildFile(const std::string &packageName, const vector<Dependency> &dependencies, const char *savePath, LinkerFlagCallbackFunc linkerFlagCallbackFunc)
{
if(sourceFiles.empty())
return Result<bool>::Err("No source files provided");
@@ -126,7 +128,12 @@ namespace backend
string result;
result.reserve(16384);
- result += "cflags = -Wall -Werror\n\n";
+ string globalLibDir = getHomeDir();
+ globalLibDir += "/.sibs/lib";
+
+ result += "globalLibDir = '-I";
+ result += globalLibDir;
+ result += "'\n\n";
result += "rule cpp_COMPILER\n";
result += " command = ccache c++ $ARGS -c $in -o $out\n\n";
@@ -159,6 +166,12 @@ namespace backend
vector<string> objectNames;
for(const string &sourceFile : sourceFiles)
{
+ // TODO: Handle tests differently.
+ // Maybe test directory should have project file and sub directories with project files
+ // should be their own project?
+ if(_tinydir_strncmp(sourceFile.c_str(), "tests", 5) == 0)
+ continue;
+
//string sourceFileEncoded = sourceFile;
//replace(sourceFileEncoded, '/', '@');
string objectName = packageName + "@exe/" + sourceFile + ".o";
@@ -167,23 +180,51 @@ namespace backend
result += ": cpp_COMPILER ../../";
result += sourceFile;
result += "\n";
- // TODO: Create .deps directory if it doesn't exist. Should be a symlink to homedir/.sibs/lib
- result += " ARGS = '-I../../.deps' '-I" + packageName + "@exe' '-I.' '-I..' '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Wall' '-Winvalid-pch' '-Wnon-virtual-dtor' '-O0' '-g'\n\n";
+ result += " ARGS = $globalLibDir '-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<string> linkerFlags = getLinkerFlags(dependencies);
- if(linkerFlags.isErr())
- return Result<bool>::Err(linkerFlags.getErrMsg());
-
- result += "build ";
- result += packageName;
- result += ": " + linkerJob + " ";
- result += join(objectNames, " ");
- result += "\n";
- result += " LINK_ARGS = '-Wl,--no-undefined' '-Wl,--as-needed' ";
- result += linkerFlags.unwrap();
- result += "\n\n";
+ switch(libraryType)
+ {
+ case LibraryType::EXECUTABLE:
+ {
+ string allLinkerFlags;
+ Result<string> linkerFlags = getLinkerFlags(dependencies, [&allLinkerFlags](const string &linkerFlag)
+ {
+ allLinkerFlags += " ";
+ allLinkerFlags += linkerFlag;
+ });
+ if(linkerFlags.isErr())
+ return Result<bool>::Err(linkerFlags.getErrMsg());
+
+ result += "build ";
+ result += packageName;
+ result += ": " + linkerJob + " ";
+ result += join(objectNames, " ");
+ result += "\n";
+ result += " LINK_ARGS = '-Wl,--no-undefined' '-Wl,--as-needed' ";
+ result += allLinkerFlags;
+ result += "\n\n";
+ break;
+ }
+ case LibraryType::STATIC:
+ {
+ string allLinkerFlags;
+ Result<string> linkerFlags = getLinkerFlags(dependencies, linkerFlagCallbackFunc);
+ if(linkerFlags.isErr())
+ return Result<bool>::Err(linkerFlags.getErrMsg());
+
+ result += "build ";
+ result += packageName;
+ result += ": " + linkerJob + " ";
+ result += join(objectNames, " ");
+ result += "\n\n";
+ break;
+ }
+ default:
+ assert(false);
+ return Result<bool>::Err("NOT IMPLEMENTED YET!");
+ }
bool fileOverwritten = sibs::fileOverwrite(ninjaBuildFilePath.c_str(), sibs::StringView(result.data(), result.size()));
if(!fileOverwritten)
diff --git a/backend/ninja/Ninja.hpp b/backend/ninja/Ninja.hpp
index 9ca6ace..71b12a5 100644
--- a/backend/ninja/Ninja.hpp
+++ b/backend/ninja/Ninja.hpp
@@ -3,8 +3,10 @@
#include "../../include/Dependency.hpp"
#include "../../include/Result.hpp"
+#include "../../include/Linker.hpp"
#include <vector>
#include <string>
+#include <functional>
namespace backend
@@ -22,11 +24,11 @@ namespace backend
Ninja(LibraryType libraryType = LibraryType::EXECUTABLE);
void addSourceFile(const char *filepath);
- sibs::Result<bool> createBuildFile(const std::string &packageName, const std::vector<sibs::Dependency> &dependencies, const char *savePath);
+ sibs::Result<bool> createBuildFile(const std::string &packageName, const std::vector<sibs::Dependency> &dependencies, const char *savePath, sibs::LinkerFlagCallbackFunc linkerFlagCallbackFunc = nullptr);
sibs::Result<bool> build(const char *buildFilePath);
private:
bool containsSourceFile(const char *filepath) const;
- sibs::Result<std::string> getLinkerFlags(const std::vector<sibs::Dependency> &dependencies) const;
+ sibs::Result<std::string> getLinkerFlags(const std::vector<sibs::Dependency> &dependencies, sibs::LinkerFlagCallbackFunc linkerFlagCallbackFunc) const;
private:
std::vector<std::string> sourceFiles;
LibraryType libraryType;