Skip to content

Commit b51157d

Browse files
committed
Generate separate output bitcode file when non-root target is chosen. Previously it could be reused between different builds which is wrong.
1 parent 217fac0 commit b51157d

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed

server/src/building/Linker.cpp

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -579,13 +579,15 @@ static const std::vector<std::string> LD_GOLD_OPTIONS = {
579579
"-relocatable"
580580
};
581581

582-
static std::vector<std::string> getLinkActionsForRootLibrary(
583-
fs::path const &workingDir, std::vector<fs::path> const &dependencies, fs::path const &output) {
582+
static std::vector<std::string>
583+
getLinkActionsForRootLibrary(fs::path const &workingDir,
584+
std::vector<fs::path> const &dependencies,
585+
fs::path const &rootOutput) {
584586
std::vector<std::string> commandLine = LD_GOLD_OPTIONS;
585587
commandLine.emplace_back("--whole-archive");
586588
CollectionUtils::extend(
587589
commandLine,
588-
std::vector<std::string>{ StringUtils::joinWith(dependencies, " "), "-o", output });
590+
std::vector<std::string>{ StringUtils::joinWith(dependencies, " "), "-o", rootOutput });
589591
utbot::LinkCommand linkAction{ commandLine, workingDir };
590592
return { linkAction.toStringWithChangingDirectory() };
591593
};
@@ -699,31 +701,34 @@ Linker::getLinkActionsForExecutable(fs::path const &workingDir,
699701
return commands;
700702
}
701703

702-
void Linker::declareRootLibraryTarget(printer::DefaultMakefilePrinter &bitcodeLinkMakefilePrinter,
703-
const fs::path &output,
704-
const vector<fs::path> &bitcodeDependencies,
705-
const fs::path &prefixPath,
706-
const utbot::RunCommand &removeAction,
707-
vector<utbot::LinkCommand> archiveActions) {
708-
fs::path temporaryOutput = Paths::addSuffix(output, "_tmp");
709-
utbot::RunCommand removeTemporaryAction =
710-
utbot::RunCommand::forceRemoveFile(temporaryOutput, testGen.serverBuildDir);
711-
std::vector<std::string> actions{ removeTemporaryAction.toStringWithChangingDirectory() };
704+
fs::path
705+
Linker::declareRootLibraryTarget(printer::DefaultMakefilePrinter &bitcodeLinkMakefilePrinter,
706+
const fs::path &output,
707+
const vector<fs::path> &bitcodeDependencies,
708+
const fs::path &prefixPath,
709+
vector<utbot::LinkCommand> archiveActions) {
710+
fs::path rootOutput = Paths::addSuffix(output, "_root");
711+
utbot::RunCommand removeAction =
712+
utbot::RunCommand::forceRemoveFile(output, testGen.serverBuildDir);
713+
std::vector<std::string> actions{ removeAction.toStringWithChangingDirectory() };
712714
for (auto &archiveAction : archiveActions) {
713-
archiveAction.setOutput(temporaryOutput);
715+
archiveAction.setOutput(output);
714716
}
715717
CollectionUtils::extend(
716718
actions, CollectionUtils::transform(
717719
archiveActions, std::bind(&utbot::LinkCommand::toStringWithChangingDirectory,
718720
std::placeholders::_1)));
719-
bitcodeLinkMakefilePrinter.declareTarget(temporaryOutput, bitcodeDependencies, actions);
721+
bitcodeLinkMakefilePrinter.declareTarget(output, bitcodeDependencies, actions);
720722

721723
auto linkActions =
722-
getLinkActionsForRootLibrary(prefixPath, { temporaryOutput, STUB_BITCODE_FILES }, output);
723-
linkActions.insert(linkActions.begin(), removeAction.toStringWithChangingDirectory());
724-
bitcodeLinkMakefilePrinter.declareTarget(output, { temporaryOutput, STUB_BITCODE_FILES },
724+
getLinkActionsForRootLibrary(prefixPath, { output, STUB_BITCODE_FILES }, rootOutput);
725+
utbot::RunCommand removeRootAction =
726+
utbot::RunCommand::forceRemoveFile(rootOutput, testGen.serverBuildDir);
727+
linkActions.insert(linkActions.begin(), removeRootAction.toStringWithChangingDirectory());
728+
bitcodeLinkMakefilePrinter.declareTarget(rootOutput, { output, STUB_BITCODE_FILES },
725729
linkActions);
726-
bitcodeLinkMakefilePrinter.declareTarget("all", { output }, {});
730+
bitcodeLinkMakefilePrinter.declareTarget("all", { rootOutput }, {});
731+
return rootOutput;
727732
}
728733

729734

@@ -769,14 +774,15 @@ Linker::addLinkTargetRecursively(const fs::path &fileToBuild,
769774
auto output = testGen.buildDatabase->getBitcodeFile(fileToBuild);
770775
output = LinkerUtils::applySuffix(output, unitType, suffixForParentOfStubs);
771776
if (Paths::isLibraryFile(fileToBuild)) {
772-
utbot::RunCommand removeAction =
773-
utbot::RunCommand::forceRemoveFile(output, testGen.serverBuildDir);
774777
auto archiveActions = getArchiveCommands(prefixPath, dependencies, *linkUnit, output);
775778
if (!hasParent) {
776-
declareRootLibraryTarget(bitcodeLinkMakefilePrinter, output, bitcodeDependencies,
777-
prefixPath, removeAction, archiveActions);
778-
779+
fs::path rootBitcode =
780+
declareRootLibraryTarget(bitcodeLinkMakefilePrinter, output,
781+
bitcodeDependencies, prefixPath, archiveActions);
782+
return { rootBitcode, BuildResult::Type::NONE };
779783
} else {
784+
utbot::RunCommand removeAction =
785+
utbot::RunCommand::forceRemoveFile(output, testGen.serverBuildDir);
780786
std::vector<std::string> actions = { removeAction.toStringWithChangingDirectory() };
781787
CollectionUtils::extend(
782788
actions,

server/src/building/Linker.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,11 @@ class Linker {
9090
const fs::path &stubsMakefile) const;
9191
Result<utbot::Void> linkWithStubsIfNeeded(const fs::path &linkMakefile, const fs::path &targetBitcode) const;
9292

93-
void declareRootLibraryTarget(printer::DefaultMakefilePrinter &bitcodeLinkMakefilePrinter,
94-
const fs::path &output,
95-
const vector<fs::path> &bitcodeDependencies,
96-
const fs::path &prefixPath,
97-
const utbot::RunCommand &removeAction,
98-
vector<utbot::LinkCommand> archiveActions);
93+
fs::path declareRootLibraryTarget(printer::DefaultMakefilePrinter &bitcodeLinkMakefilePrinter,
94+
const fs::path &output,
95+
const vector<fs::path> &bitcodeDependencies,
96+
const fs::path &prefixPath,
97+
vector<utbot::LinkCommand> archiveActions);
9998

10099
string getLinkArgument(const string &argument,
101100
const fs::path &workingDir,

0 commit comments

Comments
 (0)