aboutsummaryrefslogtreecommitdiff
path: root/backend/ninja/Ninja.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2017-12-28 22:55:50 +0100
committerdec05eba <dec05eba@protonmail.com>2017-12-28 22:56:34 +0100
commit179c5baaa2b24be61f65daad0e4d415914af4c71 (patch)
treeaff81e8c9b158bfe44789305012aa06ec951b482 /backend/ninja/Ninja.cpp
parent9ffe3db21297192b0f26929e74327487905ad444 (diff)
Add support for tests in a package where type is executable
Diffstat (limited to 'backend/ninja/Ninja.cpp')
-rw-r--r--backend/ninja/Ninja.cpp59
1 files changed, 50 insertions, 9 deletions
diff --git a/backend/ninja/Ninja.cpp b/backend/ninja/Ninja.cpp
index 43ded44..dda4aa4 100644
--- a/backend/ninja/Ninja.cpp
+++ b/backend/ninja/Ninja.cpp
@@ -36,11 +36,24 @@ namespace backend
++i;
}
- return move(result);
+ return result;
+ }
+
+ Ninja::LibraryType getNinjaLibraryType(PackageType packageType)
+ {
+ switch(packageType)
+ {
+ case PackageType::EXECUTABLE:
+ return Ninja::LibraryType::EXECUTABLE;
+ case PackageType::STATIC:
+ return Ninja::LibraryType::STATIC;
+ case PackageType::DYNAMIC:
+ case PackageType::LIBRARY:
+ return Ninja::LibraryType::DYNAMIC;
+ }
}
- Ninja::Ninja(LibraryType _libraryType) :
- libraryType(_libraryType)
+ Ninja::Ninja()
{
}
@@ -209,6 +222,8 @@ namespace backend
Result<bool> createBuildDirResult = createDirectoryRecursive(savePath);
if(createBuildDirResult.isErr())
return createBuildDirResult;
+
+ LibraryType libraryType = getNinjaLibraryType(config.getPackageType());
string ninjaBuildFilePath = savePath;
ninjaBuildFilePath += "/build.ninja";
@@ -229,6 +244,19 @@ namespace backend
result += "'";
}
result += "\n\n";
+
+ string defines;
+ for(const auto &definePair : config.getDefines())
+ {
+ defines += " '-D";
+ defines += definePair.first;
+ defines += "=";
+ defines += definePair.second;
+ defines += "'";
+ }
+
+ if(!defines.empty())
+ printf("Custom define: %s\n", defines.c_str());
string buildJob;
switch(libraryType)
@@ -295,7 +323,10 @@ namespace backend
result += ": cpp_COMPILER ../../";
result += sourceFile;
result += "\n";
- result += " ARGS = $globalIncDir '-I" + config.getPackageName() + "@exe' '-I..' '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Wall' '-Winvalid-pch' '-Wnon-virtual-dtor' " + optimizationFlags + " '-g'\n\n";
+ result += " ARGS = $globalIncDir '-I" + config.getPackageName() + "@exe' '-I..' ";
+ if(!defines.empty())
+ result += defines;
+ result += " '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Wall' '-Winvalid-pch' '-Wnon-virtual-dtor' " + optimizationFlags + " '-g'\n\n";
objectNames.emplace_back(objectName);
}
@@ -395,7 +426,7 @@ namespace backend
if(!buildResult)
return buildResult;
- Result<bool> buildTestResult = buildTests(projectGeneratedBinary);
+ Result<bool> buildTestResult = buildTests(projectGeneratedBinary, config, savePath);
if(!buildTestResult)
return buildTestResult;
@@ -417,14 +448,24 @@ namespace backend
return false;
}
- Result<bool> Ninja::buildTests(const std::string &projectGeneratedBinary)
+ Result<bool> Ninja::buildTests(const std::string &projectGeneratedBinary, const SibsConfig &config, const char *savePath)
{
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");
+ // Tests need parent project as dependency. Executables can't be included as dependency so we build it as dynamic library.
+ // `build` also builds tests
+ if(getNinjaLibraryType(config.getPackageType()) == LibraryType::EXECUTABLE)
+ {
+ SibsConfig parentProjConfigLib = config;
+ parentProjConfigLib.setPackageType(PackageType::DYNAMIC);
+ // HACK: We can build a package that is defined as executable and contains main function by redefining `main`
+ // as something else.
+ // TODO: Do not allow defining `main` in project.conf or as program argument to sibs (when sibs supports defines).
+ // It's ok if `define` fails. It could fail if `main` has already been replaced by other tests somehow.
+ parentProjConfigLib.define("main", "sibs_lib_ignore_main");
+ return build(parentProjConfigLib, savePath, nullptr, nullptr);
+ }
for(const string &testSourceDir : testSourceDirs)
{