aboutsummaryrefslogtreecommitdiff
path: root/backend/ninja
diff options
context:
space:
mode:
Diffstat (limited to 'backend/ninja')
-rw-r--r--backend/ninja/Ninja.cpp177
-rw-r--r--backend/ninja/Ninja.hpp12
2 files changed, 155 insertions, 34 deletions
diff --git a/backend/ninja/Ninja.cpp b/backend/ninja/Ninja.cpp
index d8345a4..6a6eafd 100644
--- a/backend/ninja/Ninja.cpp
+++ b/backend/ninja/Ninja.cpp
@@ -47,8 +47,28 @@ namespace backend
void Ninja::addSourceFile(const char *filepath)
{
- if(filepath && !containsSourceFile(filepath))
- sourceFiles.emplace_back(filepath);
+ string filePathStr = filepath ? filepath : "";
+ if(filepath && !containsSourceFile(filePathStr))
+ {
+ sourceFiles.emplace_back(filePathStr);
+ printf("Adding source file: %s\n", filepath);
+ }
+ }
+
+ void Ninja::addTestSourceDir(const char *dir)
+ {
+ string dirStr = dir ? dir : "";
+ if(dir && !containsTestSourceDir(dirStr))
+ {
+ testSourceDirs.emplace_back(dirStr);
+ printf("Adding test source directory: %s\n", dir);
+ }
+ }
+
+ void Ninja::addDependency(const std::string &binaryFile)
+ {
+ if(!containsDependency(binaryFile))
+ binaryDependencies.emplace_back(binaryFile);
}
const std::vector<std::string>& Ninja::getSourceFiles() const
@@ -56,7 +76,7 @@ namespace backend
return sourceFiles;
}
- bool Ninja::containsSourceFile(const char *filepath) const
+ bool Ninja::containsSourceFile(const string &filepath) const
{
for(const string &sourceFile : sourceFiles)
{
@@ -66,6 +86,26 @@ namespace backend
return false;
}
+ bool Ninja::containsTestSourceDir(const string &dir) const
+ {
+ for(const string &testSourceDir : testSourceDirs)
+ {
+ if(testSourceDir == dir)
+ return true;
+ }
+ return false;
+ }
+
+ bool Ninja::containsDependency(const string &dependency) const
+ {
+ for(const string &binaryDependency : binaryDependencies)
+ {
+ if(binaryDependency == dependency)
+ return true;
+ }
+ return false;
+ }
+
Result<bool> validatePkgConfigPackageVersionExists(const Dependency &dependency)
{
Result<bool> dependencyValidationResult = PkgConfig::validatePackageExists(dependency.name);
@@ -156,7 +196,7 @@ namespace backend
return Result<bool>::Ok(true);
}
- Result<bool> Ninja::createBuildFile(const std::string &packageName, const vector<Dependency> &dependencies, const char *savePath, LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, LinkerFlagCallbackFunc dynamicLinkerFlagCallback)
+ Result<bool> Ninja::createBuildFile(const SibsConfig &config, const char *savePath, LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, LinkerFlagCallbackFunc dynamicLinkerFlagCallback)
{
// TODO: Do not quit here if no source files are provided. The source-less project could have dependencies
if(sourceFiles.empty())
@@ -199,7 +239,7 @@ namespace backend
result += "rule cpp_BUILD_STATIC\n";
result += " command = ar rcs lib";
- result += packageName;
+ result += config.getPackageName();
result += ".a";
result += " $in\n\n";
buildJob = "cpp_BUILD_STATIC";
@@ -225,25 +265,28 @@ namespace backend
objectNames.reserve(sourceFiles.size());
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";
+ string objectName = config.getPackageName() + "@exe/" + sourceFile + ".o";
result += "build ";
result += objectName;
result += ": cpp_COMPILER ../../";
result += sourceFile;
result += "\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";
+ result += " ARGS = $globalLibDir '-I" + config.getPackageName() + "@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);
}
string allLinkerFlags;
+
+ // TODO: Somehow check loading order, because it has to be correct to work.. Or does it for dynamic libraries?
+ // Anyways it's required for static libraries (especially on Windows)
+ for(const string &binaryDependency : binaryDependencies)
+ {
+ allLinkerFlags += " ";
+ allLinkerFlags += binaryDependency;
+ }
+
if(!staticLinkerFlagCallbackFunc || libraryType == LibraryType::DYNAMIC)
{
staticLinkerFlagCallbackFunc = [&allLinkerFlags](const string &linkerFlag)
@@ -260,16 +303,20 @@ namespace backend
allLinkerFlags += linkerFlag;
};
+ Result<bool> linkerFlags = getLinkerFlags(config.getDependencies(), staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallback);
+ if(linkerFlags.isErr())
+ return Result<bool>::Err(linkerFlags.getErrMsg());
+
+ string projectGeneratedBinary = allLinkerFlags;
+ projectGeneratedBinary += " '";
+ projectGeneratedBinary += savePath;
+ projectGeneratedBinary += "/";
switch(libraryType)
{
case LibraryType::EXECUTABLE:
{
- Result<bool> linkerFlags = getLinkerFlags(dependencies, staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallback);
- if(linkerFlags.isErr())
- return Result<bool>::Err(linkerFlags.getErrMsg());
-
result += "build ";
- result += packageName;
+ result += config.getPackageName();
result += ": " + buildJob + " ";
result += join(objectNames, " ");
result += "\n";
@@ -279,29 +326,23 @@ namespace backend
result += allLinkerFlags;
}
result += "\n\n";
+ projectGeneratedBinary += config.getPackageName();
break;
}
case LibraryType::STATIC:
{
- Result<bool> linkerFlags = getLinkerFlags(dependencies, staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallback);
- if(linkerFlags.isErr())
- return linkerFlags;
-
result += "build ";
- result += packageName;
+ result += config.getPackageName();
result += ": " + buildJob + " ";
result += join(objectNames, " ");
result += "\n\n";
+ projectGeneratedBinary += config.getPackageName() + ".a";
break;
}
case LibraryType::DYNAMIC:
{
- Result<bool> linkerFlags = getLinkerFlags(dependencies, staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallback);
- if(linkerFlags.isErr())
- return Result<bool>::Err(linkerFlags.getErrMsg());
-
result += "build lib";
- result += packageName;
+ result += config.getPackageName();
result += ".so: " + buildJob + " ";
result += join(objectNames, " ");
result += "\n";
@@ -312,18 +353,93 @@ namespace backend
//result += " '-Wl,--no-whole-archive'";
}
result += "\n\n";
+ projectGeneratedBinary += "lib" + config.getPackageName() + ".so";
break;
}
default:
assert(false);
return Result<bool>::Err("Unexpected error");
}
+ projectGeneratedBinary += "'";
Result<bool> fileOverwriteResult = sibs::fileOverwrite(ninjaBuildFilePath.c_str(), sibs::StringView(result.data(), result.size()));
if(fileOverwriteResult.isErr())
return fileOverwriteResult;
printf("Created ninja build file: %s\n", ninjaBuildFilePath.c_str());
+
+ Result<bool> buildTestResult = buildTests(projectGeneratedBinary);
+ if(!buildTestResult)
+ return buildTestResult;
+
+ return Result<bool>::Ok(true);
+ }
+
+ const char *sourceFileExtensions[] = { "c", "cc", "cpp", "cxx" };
+ bool isSourceFile(tinydir_file *file)
+ {
+ if(!file->is_reg)
+ return false;
+
+ for(const char *sourceFileExtension : sourceFileExtensions)
+ {
+ if(_tinydir_strcmp(sourceFileExtension, file->extension) == 0)
+ return true;
+ }
+
+ return false;
+ }
+
+ Result<bool> Ninja::buildTests(const std::string &projectGeneratedBinary)
+ {
+ if(testSourceDirs.empty())
+ return Result<bool>::Ok(true);
+
+ // TODO: Include executable as dependency??? or compile project as dynamic library even if it's not a library
+ if(libraryType == LibraryType::EXECUTABLE)
+ return Result<bool>::Err("Unit tests are currently only supported in projects that compile to static/dynamic library");
+
+ for(const string &testSourceDir : testSourceDirs)
+ {
+ string projectConfFilePath = testSourceDir;
+ projectConfFilePath += "/project.conf";
+
+ FileType projectConfFileType = getFileType(projectConfFilePath.c_str());
+ SibsTestConfig sibsTestConfig(testSourceDir);
+ if(projectConfFileType == FileType::REGULAR)
+ {
+ Result<bool> result = Config::readFromFile(projectConfFilePath.c_str(), sibsTestConfig);
+ if(!result)
+ return result;
+ }
+
+ backend::Ninja ninja;
+ ninja.addDependency(projectGeneratedBinary);
+ walkDirFilesRecursive(testSourceDir.c_str(), [&ninja, &sibsTestConfig](tinydir_file *file)
+ {
+ if (isSourceFile(file))
+ {
+ ninja.addSourceFile(file->path + sibsTestConfig.getProjectPath().size() + 1);
+ }
+ else
+ {
+ //printf("Ignoring non-source file: %s\n", file->path + projectPath.size());
+ }
+ });
+
+ if(!ninja.getSourceFiles().empty())
+ {
+ string debugBuildPath = testSourceDir + "/sibs-build/debug";
+ Result<bool> buildFileResult = ninja.createBuildFile(sibsTestConfig, debugBuildPath.c_str());
+ if (!buildFileResult)
+ return buildFileResult;
+
+ Result<bool> buildResult = ninja.build(debugBuildPath.c_str());
+ if (!buildResult)
+ return buildResult;
+ }
+ }
+
return Result<bool>::Ok(true);
}
@@ -332,14 +448,11 @@ namespace backend
string command = "ninja -C '";
command += buildFilePath;
command += "'";
- Result<ExecResult> execResult = exec(command.c_str());
+ Result<ExecResult> execResult = exec(command.c_str(), true);
if(execResult.isOk())
{
if(execResult.unwrap().exitCode == 0)
- {
- printf("%s", execResult.unwrap().execStdout.c_str());
return Result<bool>::Ok(true);
- }
else
return Result<bool>::Err(execResult.unwrap().execStdout);
}
diff --git a/backend/ninja/Ninja.hpp b/backend/ninja/Ninja.hpp
index a0239d2..7013f78 100644
--- a/backend/ninja/Ninja.hpp
+++ b/backend/ninja/Ninja.hpp
@@ -4,6 +4,7 @@
#include "../../include/Dependency.hpp"
#include "../../include/Result.hpp"
#include "../../include/Linker.hpp"
+#include "../../include/Conf.hpp"
#include <vector>
#include <string>
#include <functional>
@@ -24,14 +25,21 @@ namespace backend
Ninja(LibraryType libraryType = LibraryType::EXECUTABLE);
void addSourceFile(const char *filepath);
+ void addTestSourceDir(const char *dir);
+ void addDependency(const std::string &binaryFile);
const std::vector<std::string>& getSourceFiles() const;
- sibs::Result<bool> createBuildFile(const std::string &packageName, const std::vector<sibs::Dependency> &dependencies, const char *savePath, sibs::LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc = nullptr, sibs::LinkerFlagCallbackFunc dynamicLinkerFlagCallback = nullptr);
+ sibs::Result<bool> createBuildFile(const sibs::SibsConfig &config, const char *savePath, sibs::LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc = nullptr, sibs::LinkerFlagCallbackFunc dynamicLinkerFlagCallback = nullptr);
sibs::Result<bool> build(const char *buildFilePath);
private:
- bool containsSourceFile(const char *filepath) const;
+ sibs::Result<bool> buildTests(const std::string &projectGeneratedBinary);
+ bool containsSourceFile(const std::string &filepath) const;
+ bool containsTestSourceDir(const std::string &dir) const;
+ bool containsDependency(const std::string &dependency) const;
sibs::Result<bool> getLinkerFlags(const std::vector<sibs::Dependency> &dependencies, sibs::LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, sibs::LinkerFlagCallbackFunc dynamicLinkerFlagCallback) const;
private:
std::vector<std::string> sourceFiles;
+ std::vector<std::string> testSourceDirs;
+ std::vector<std::string> binaryDependencies;
LibraryType libraryType;
};
}