From fb1110e08140c31076b61c31ab60e84a0c39a1db Mon Sep 17 00:00:00 2001 From: Alexey Utkin Date: Wed, 7 Sep 2022 18:49:00 +0300 Subject: [PATCH] [BUG] Cannot run generated test for 32 bits projects #442 - propagates `-m32` to linker and for compilation GT files --- server/src/building/BuildDatabase.cpp | 4 +- server/src/building/BuildDatabase.h | 2 + server/src/building/Linker.cpp | 65 ++++++++++--------- server/src/printers/NativeMakefilePrinter.cpp | 26 ++++++-- server/src/printers/NativeMakefilePrinter.h | 1 + 5 files changed, 60 insertions(+), 38 deletions(-) diff --git a/server/src/building/BuildDatabase.cpp b/server/src/building/BuildDatabase.cpp index 487ba0ee6..f8b4e56cc 100644 --- a/server/src/building/BuildDatabase.cpp +++ b/server/src/building/BuildDatabase.cpp @@ -18,6 +18,8 @@ #include #include +const std::string BuildDatabase::BITS_32_FLAG = "-m32"; + BuildDatabase::BuildDatabase( fs::path serverBuildDir, fs::path buildCommandsJsonPath, @@ -426,7 +428,7 @@ void BuildDatabase::BaseFileInfo::addFile(fs::path file) { } bool BuildDatabase::ObjectFileInfo::is32bits() const { - return CollectionUtils::contains(command.getCommandLine(), "-m32"); + return CollectionUtils::contains(command.getCommandLine(), BITS_32_FLAG); } fs::path BuildDatabase::TargetInfo::getOutput() const { diff --git a/server/src/building/BuildDatabase.h b/server/src/building/BuildDatabase.h index 8c17f1d92..49902297f 100644 --- a/server/src/building/BuildDatabase.h +++ b/server/src/building/BuildDatabase.h @@ -18,6 +18,8 @@ class BuildDatabase { public: + static const std::string BITS_32_FLAG; + struct KleeFilesInfo { explicit KleeFilesInfo(fs::path kleeFile); diff --git a/server/src/building/Linker.cpp b/server/src/building/Linker.cpp index 56d42307b..17032ec51 100644 --- a/server/src/building/Linker.cpp +++ b/server/src/building/Linker.cpp @@ -254,9 +254,9 @@ std::vector Linker::getTestMethods() { continue; } isAnyOneLinked = true; + auto compilationUnitInfo = + testGen.getClientCompilationUnitInfo(fileName); for (const auto &[methodName, _] : tests.methods) { - auto compilationUnitInfo = - testGen.getClientCompilationUnitInfo(fileName); if (compilationUnitInfo->kleeFilesInfo->isCorrectMethod(methodName)) { testMethods.emplace_back(methodName, bitcodePath, @@ -266,37 +266,40 @@ std::vector Linker::getTestMethods() { } } } else { - [&] { - for (auto &[fileName, tests] : testGen.tests) { - if (CollectionUtils::contains(brokenLinkFiles, bitcodeFileName.at(fileName))) { - LOG_S(ERROR) << "Couldn't link bitcode file for current source file: " - << fileName; - continue; - } - isAnyOneLinked = true; - if (fileName != lineInfo->filePath) { - continue; - } - for (const auto &[methodName, method] : tests.methods) { - if (methodName == lineInfo->methodName || - (lineInfo->forClass && - method.classObj.has_value() && - method.classObj->type.typeName() == lineInfo->scopeName)) { - auto compilationUnitInfo = - testGen.getClientCompilationUnitInfo(fileName); - if (compilationUnitInfo->kleeFilesInfo->isCorrectMethod(methodName)) { - tests::TestMethod testMethod{ methodName, - bitcodeFileName.at(lineInfo->filePath), - fileName, - compilationUnitInfo->is32bits()}; - testMethods.emplace_back(testMethod); - } - if (!lineInfo->forClass) - return; + bool needBreak = false; + for (auto &[fileName, tests] : testGen.tests) { + if (CollectionUtils::contains(brokenLinkFiles, bitcodeFileName.at(fileName))) { + LOG_S(ERROR) << "Couldn't link bitcode file for current source file: " + << fileName; + continue; + } + isAnyOneLinked = true; + if (fileName != lineInfo->filePath) { + continue; + } + for (const auto &[methodName, method] : tests.methods) { + if (methodName == lineInfo->methodName || + (lineInfo->forClass && + method.classObj.has_value() && + method.classObj->type.typeName() == lineInfo->scopeName)) { + auto compilationUnitInfo = + testGen.getClientCompilationUnitInfo(fileName); + if (compilationUnitInfo->kleeFilesInfo->isCorrectMethod(methodName)) { + testMethods.emplace_back(methodName, + bitcodeFileName.at(lineInfo->filePath), + fileName, + compilationUnitInfo->is32bits()); + } + if (!lineInfo->forClass) { + needBreak = true; + break; } } } - }(); + if (needBreak) { + break; + } + } } if (!isAnyOneLinked) { throw CompilationDatabaseException("Couldn't link any files"); @@ -317,7 +320,7 @@ Linker::Linker(BaseTestGen &testGen, Result Linker::link(const CollectionUtils::MapFileTo &bitcodeFiles, const fs::path &target, - std::string const &suffixForParentOfStubs, + const std::string &suffixForParentOfStubs, const std::optional &testedFilePath, const CollectionUtils::FileSet &stubSources, bool errorOnMissingBitcode) { diff --git a/server/src/printers/NativeMakefilePrinter.cpp b/server/src/printers/NativeMakefilePrinter.cpp index 2a7bd5cb7..d6deb04a3 100644 --- a/server/src/printers/NativeMakefilePrinter.cpp +++ b/server/src/printers/NativeMakefilePrinter.cpp @@ -144,23 +144,36 @@ namespace printer { } void NativeMakefilePrinter::init() { - + bits32Flag = ""; + for (auto &[fileName, _] : testGen->tests) { + const auto &ptr = testGen->getClientCompilationUnitInfo(fileName); + if (ptr && ptr->is32bits()) { + bits32Flag = BuildDatabase::BITS_32_FLAG; + break; + } + } + declareAction(stringFormat("$(shell mkdir -p %s >/dev/null)", getRelativePath(buildDirectory))); declareAction(stringFormat("$(shell mkdir -p %s >/dev/null)", getRelativePath(dependencyDirectory))); declareTarget(FORCE, {}, {}); - comment("gtest"); + comment("{ gtest"); fs::path gtestBuildDirectory = getRelativePath(buildDirectory / "googletest"); fs::path defaultPath = "default.c"; - std::vector defaultGtestCompileCommandLine{ getRelativePathForLinker(primaryCxxCompiler), "-c", "-std=c++11", - FPIC_FLAG, defaultPath }; + std::vector defaultGtestCompileCommandLine{ + getRelativePathForLinker(primaryCxxCompiler), + bits32Flag, + "-c", + "-std=c++11", + FPIC_FLAG, + defaultPath }; utbot::CompileCommand defaultGtestCompileCommand{ defaultGtestCompileCommandLine, getRelativePath(buildDirectory), defaultPath }; gtestAllTargets(defaultGtestCompileCommand, gtestBuildDirectory); gtestMainTargets(defaultGtestCompileCommand, gtestBuildDirectory); - comment("/gtest"); + comment("} gtest"); } fs::path NativeMakefilePrinter::getTemporaryDependencyFile(fs::path const &file) { @@ -340,6 +353,7 @@ namespace printer { sharedOutput.value()) }; if (rootLinkUnitInfo->commands.front().isArchiveCommand()) { std::vector dynamicLinkCommandLine{ getRelativePathForLinker(cxxLinker), "$(LDFLAGS)", + bits32Flag, pthreadFlag, coverageLinkFlags, sanitizerLinkFlags, "-o", getRelativePath( @@ -367,7 +381,7 @@ namespace printer { } dynamicLinkCommand.setOptimizationLevel(OPTIMIZATION_FLAG); dynamicLinkCommand.addFlagsToBegin( - { pthreadFlag, coverageLinkFlags, sanitizerLinkFlags }); + { bits32Flag, pthreadFlag, coverageLinkFlags, sanitizerLinkFlags }); std::copy_if(rootLinkUnitInfo->files.begin(), rootLinkUnitInfo->files.end(), std::back_inserter(filesToLink), [](const fs::path& path) {return Paths::isLibraryFile(path);}); for(std::string& file : filesToLink) { diff --git a/server/src/printers/NativeMakefilePrinter.h b/server/src/printers/NativeMakefilePrinter.h index d6ce86631..fa40cc015 100644 --- a/server/src/printers/NativeMakefilePrinter.h +++ b/server/src/printers/NativeMakefilePrinter.h @@ -24,6 +24,7 @@ namespace printer { CompilationUtils::CompilerName primaryCxxCompilerName; fs::path cxxLinker; + std::string bits32Flag; std::string pthreadFlag; std::string coverageLinkFlags; std::string sanitizerLinkFlags;