aboutsummaryrefslogtreecommitdiff
path: root/backend
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
parent9ffe3db21297192b0f26929e74327487905ad444 (diff)
Add support for tests in a package where type is executable
Diffstat (limited to 'backend')
-rw-r--r--backend/ninja/Ninja.cpp59
-rw-r--r--backend/ninja/Ninja.hpp5
2 files changed, 52 insertions, 12 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)
{
diff --git a/backend/ninja/Ninja.hpp b/backend/ninja/Ninja.hpp
index fddcaf3..17649aa 100644
--- a/backend/ninja/Ninja.hpp
+++ b/backend/ninja/Ninja.hpp
@@ -22,7 +22,7 @@ namespace backend
DYNAMIC,
};
- Ninja(LibraryType libraryType = LibraryType::EXECUTABLE);
+ Ninja();
void addSourceFile(const char *filepath);
void addTestSourceDir(const char *dir);
@@ -30,7 +30,7 @@ namespace backend
const std::vector<std::string>& getSourceFiles() const;
sibs::Result<bool> build(const sibs::SibsConfig &config, const char *savePath, sibs::LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc = nullptr, sibs::LinkerFlagCallbackFunc dynamicLinkerFlagCallback = nullptr);
private:
- sibs::Result<bool> buildTests(const std::string &projectGeneratedBinary);
+ sibs::Result<bool> buildTests(const std::string &projectGeneratedBinary, const sibs::SibsConfig &config, const char *savePath);
bool containsSourceFile(const std::string &filepath) const;
bool containsTestSourceDir(const std::string &dir) const;
bool containsDependency(const std::string &dependency) const;
@@ -40,7 +40,6 @@ namespace backend
std::vector<std::string> sourceFiles;
std::vector<std::string> testSourceDirs;
std::vector<std::string> binaryDependencies;
- LibraryType libraryType;
};
}