From 94caff5f66cacdd21e5a93cd3de9150a22eeaa3a Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 16 Dec 2017 04:21:33 +0100 Subject: Add support for sub project (unit tests) --- backend/ninja/Ninja.cpp | 177 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 145 insertions(+), 32 deletions(-) (limited to 'backend/ninja/Ninja.cpp') 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& 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 validatePkgConfigPackageVersionExists(const Dependency &dependency) { Result dependencyValidationResult = PkgConfig::validatePackageExists(dependency.name); @@ -156,7 +196,7 @@ namespace backend return Result::Ok(true); } - Result Ninja::createBuildFile(const std::string &packageName, const vector &dependencies, const char *savePath, LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, LinkerFlagCallbackFunc dynamicLinkerFlagCallback) + Result 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 linkerFlags = getLinkerFlags(config.getDependencies(), staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallback); + if(linkerFlags.isErr()) + return Result::Err(linkerFlags.getErrMsg()); + + string projectGeneratedBinary = allLinkerFlags; + projectGeneratedBinary += " '"; + projectGeneratedBinary += savePath; + projectGeneratedBinary += "/"; switch(libraryType) { case LibraryType::EXECUTABLE: { - Result linkerFlags = getLinkerFlags(dependencies, staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallback); - if(linkerFlags.isErr()) - return Result::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 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 linkerFlags = getLinkerFlags(dependencies, staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallback); - if(linkerFlags.isErr()) - return Result::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::Err("Unexpected error"); } + projectGeneratedBinary += "'"; Result 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 buildTestResult = buildTests(projectGeneratedBinary); + if(!buildTestResult) + return buildTestResult; + + return Result::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 Ninja::buildTests(const std::string &projectGeneratedBinary) + { + 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"); + + 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 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 buildFileResult = ninja.createBuildFile(sibsTestConfig, debugBuildPath.c_str()); + if (!buildFileResult) + return buildFileResult; + + Result buildResult = ninja.build(debugBuildPath.c_str()); + if (!buildResult) + return buildResult; + } + } + return Result::Ok(true); } @@ -332,14 +448,11 @@ namespace backend string command = "ninja -C '"; command += buildFilePath; command += "'"; - Result execResult = exec(command.c_str()); + Result execResult = exec(command.c_str(), true); if(execResult.isOk()) { if(execResult.unwrap().exitCode == 0) - { - printf("%s", execResult.unwrap().execStdout.c_str()); return Result::Ok(true); - } else return Result::Err(execResult.unwrap().execStdout); } -- cgit v1.2.3