From 179c5baaa2b24be61f65daad0e4d415914af4c71 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 28 Dec 2017 22:55:50 +0100 Subject: Add support for tests in a package where type is executable --- backend/ninja/Ninja.cpp | 59 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 9 deletions(-) (limited to 'backend/ninja/Ninja.cpp') 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 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 buildTestResult = buildTests(projectGeneratedBinary); + Result buildTestResult = buildTests(projectGeneratedBinary, config, savePath); if(!buildTestResult) return buildTestResult; @@ -417,14 +448,24 @@ namespace backend return false; } - Result Ninja::buildTests(const std::string &projectGeneratedBinary) + Result Ninja::buildTests(const std::string &projectGeneratedBinary, const SibsConfig &config, const char *savePath) { if(testSourceDirs.empty()) return Result::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::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) { -- cgit v1.2.3