aboutsummaryrefslogtreecommitdiff
path: root/backend/ninja/Ninja.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2017-12-16 04:21:33 +0100
committerdec05eba <dec05eba@protonmail.com>2017-12-16 04:21:44 +0100
commit94caff5f66cacdd21e5a93cd3de9150a22eeaa3a (patch)
treeb3b7689e5a25c908369bb5fad5a59e085253b76e /backend/ninja/Ninja.cpp
parent28d6b571139998915bce147abb58617884431192 (diff)
Add support for sub project (unit tests)
Diffstat (limited to 'backend/ninja/Ninja.cpp')
-rw-r--r--backend/ninja/Ninja.cpp177
1 files changed, 145 insertions, 32 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);
}