Skip to content

Commit 58c888d

Browse files
author
Vladislav Kalugin
committed
not end
1 parent 465c934 commit 58c888d

File tree

2 files changed

+50
-35
lines changed

2 files changed

+50
-35
lines changed

server/src/building/BuildDatabase.cpp

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ static std::string tryConvertOptionToPath(const std::string &possibleFilePath,
3434
BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath,
3535
fs::path _serverBuildDir,
3636
utbot::ProjectContext _projectContext,
37-
const std::string &target) : serverBuildDir(std::move(_serverBuildDir)),
38-
projectContext(std::move(_projectContext)),
39-
buildCommandsJsonPath(std::move(_buildCommandsJsonPath)) {
40-
linkCommandsJsonPath = fs::canonical(buildCommandsJsonPath / "link_commands.json");
41-
compileCommandsJsonPath = fs::canonical(buildCommandsJsonPath / "compile_commands.json");
37+
std::string _target) :
38+
serverBuildDir(std::move(_serverBuildDir)),
39+
projectContext(std::move(_projectContext)),
40+
buildCommandsJsonPath(std::move(_buildCommandsJsonPath)),
41+
linkCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "link_commands.json")),
42+
compileCommandsJsonPath(fs::canonical(buildCommandsJsonPath / "compile_commands.json")) {
4243
if (!fs::exists(linkCommandsJsonPath) || !fs::exists(compileCommandsJsonPath)) {
4344
throw CompilationDatabaseException("Couldn't open link_commands.json or compile_commands.json files");
4445
}
@@ -51,7 +52,9 @@ BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath,
5152
filterInstalledFiles();
5253
addLocalSharedLibraries();
5354
fillTargetInfoParents();
54-
updateTarget(target);
55+
updateTarget(_target);,
56+
target(std::move(_target))
57+
createClangCompileCommandsJson(_target);
5558
}
5659

5760
std::shared_ptr<BuildDatabase> BuildDatabase::create(const utbot::ProjectContext &projectContext,
@@ -201,41 +204,47 @@ void BuildDatabase::initInfo(const nlohmann::json &linkCommandsJson) {
201204
}
202205
}
203206

204-
void BuildDatabase::createClangCompileCommandsJson(const fs::path &target, const fs::path &buildCommandsJsonPath) {
205-
CollectionUtils::MapFileTo<std::pair<nlohmann::json, std::shared_ptr<ObjectFileInfo>>> fileCompileCommands;
206-
for (const auto &[compileCommand, objectInfo]: compileCommands_temp) {
207-
const fs::path &sourcePath = objectInfo->getSourcePath();
208-
if ((target == GrpcUtils::UTBOT_AUTO_TARGET_PATH ||
209-
targetInfos[target]->files.contains(objectInfo->getOutputFile())) &&
210-
(!CollectionUtils::containsKey(fileCompileCommands, sourcePath) ||
211-
conflictPriorityMore(objectInfo, fileCompileCommands[sourcePath].second))) {
212-
fileCompileCommands[sourcePath] = {compileCommand, objectInfo};
213-
}
214-
}
207+
void BuildDatabase::createClangCompileCommandsJson(const fs::path &_target) {
208+
LOG_IF_S(ERROR, !isAutoTarget() && _target != target) << "Try change non-auto target";
209+
210+
CollectionUtils::FileSet targetFileSet = getArchiveObjectFiles(_target);
215211

216212
nlohmann::json compileCommandsSingleFilesJson;
217-
for (const auto &compileCommand: fileCompileCommands) {
218-
compileCommandsSingleFilesJson.push_back(compileCommand.second.first);
213+
if (target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) {
214+
for (const auto &[compileCommand, objectInfo]: compileCommands_temp) {
215+
compileCommandsSingleFilesJson.push_back(compileCommand);
216+
}
217+
} else {
218+
CollectionUtils::MapFileTo<std::pair<nlohmann::json, std::shared_ptr<ObjectFileInfo>>> fileCompileCommands;
219+
for (const auto &[compileCommand, objectInfo]: compileCommands_temp) {
220+
const fs::path &sourcePath = objectInfo->getSourcePath();
221+
if (CollectionUtils::contains(targetFileSet, objectInfo->getOutputFile()) &&
222+
(!CollectionUtils::containsKey(fileCompileCommands, sourcePath) ||
223+
conflictPriorityMore(objectInfo, fileCompileCommands[sourcePath].second))) {
224+
fileCompileCommands[sourcePath] = {compileCommand, objectInfo};
225+
}
226+
}
227+
for (const auto &compileCommand: fileCompileCommands) {
228+
compileCommandsSingleFilesJson.push_back(compileCommand.second.first);
229+
}
219230
}
220231

221232
fs::path clangCompileCommandsJsonPath = CompilationUtils::getClangCompileCommandsJsonPath(buildCommandsJsonPath);
222233
JsonUtils::writeJsonToFile(clangCompileCommandsJsonPath, compileCommandsSingleFilesJson);
223234
}
224235

225-
void BuildDatabase::updateTarget(std::string target) {
236+
void BuildDatabase::updateTarget(const std::string &_target) {
226237
if (target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) {
227238
return;
228239
}
229240

230-
auto new_target = GenerationUtils::findTarget(getAllTargets(), target);
241+
auto new_target = GenerationUtils::findTarget(getAllTargets(), _target);
231242
if (new_target.has_value()) {
232243
target = new_target.value();
233244
} else {
234245
throw CompilationDatabaseException("Can't find target: " + target);
235246
}
236247

237-
createClangCompileCommandsJson(target, buildCommandsJsonPath);
238-
239248
for (auto &[sourceFile, objectInfos]: sourceFileInfos) {
240249
std::sort(objectInfos.begin(), objectInfos.end(), [&](const std::shared_ptr<ObjectFileInfo> &left,
241250
const std::shared_ptr<ObjectFileInfo> &right) {
@@ -627,6 +636,7 @@ CollectionUtils::FileSet BuildDatabase::getStubFiles(
627636
}
628637
return {};
629638
}
639+
630640
void BuildDatabase::assignStubFilesToLinkUnit(
631641
std::shared_ptr<const BuildDatabase::TargetInfo> linkUnitInfo,
632642
CollectionUtils::FileSet stubs) {
@@ -726,10 +736,14 @@ fs::path BuildDatabase::newDirForFile(const fs::path &file) const {
726736
return Paths::createNewDirForFile(file, base, this->serverBuildDir);
727737
}
728738

729-
CollectionUtils::FileSet BuildDatabase::getSourceFilesForTarget(const std::string &_target) {
739+
CollectionUtils::FileSet BuildDatabase::getSourceFilesForTarget(const fs::path &_target) {
730740
return CollectionUtils::transformTo<CollectionUtils::FileSet>(
731-
getArchiveObjectFiles(_target),
741+
getArchiveObjectFiles(_target.value()),
732742
[this](fs::path const &objectPath) {
733743
return getClientCompilationUnitInfo(objectPath)->getSourcePath();
734744
});
735745
}
746+
747+
bool BuildDatabase::isAutoTarget() const {
748+
return !target.has_value();
749+
}

server/src/building/BuildDatabase.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ class BuildDatabase {
9696
BuildDatabase(fs::path _buildCommandsJsonPath,
9797
fs::path _serverBuildDir,
9898
utbot::ProjectContext _projectContext,
99-
const std::string &target);
99+
std::string _target);
100100

101101
static std::shared_ptr<BuildDatabase> create(const utbot::ProjectContext &projectContext,
102102
const std::string &target);
103103

104-
void updateTarget(std::string target);
104+
void updateTarget(const std::string &target);
105105

106106
const fs::path &getCompileCommandsJson();
107107
const fs::path &getLinkCommandsJson();
@@ -198,12 +198,12 @@ class BuildDatabase {
198198

199199
std::shared_ptr<TargetInfo> getPriorityTarget() const;
200200
private:
201-
fs::path serverBuildDir;
202-
utbot::ProjectContext projectContext;
203-
fs::path buildCommandsJsonPath;
204-
fs::path linkCommandsJsonPath;
205-
fs::path compileCommandsJsonPath;
206-
std::optional<std::string> target;
201+
const fs::path serverBuildDir;
202+
const utbot::ProjectContext projectContext;
203+
const fs::path buildCommandsJsonPath;
204+
const fs::path linkCommandsJsonPath;
205+
const fs::path compileCommandsJsonPath;
206+
const fs::path target;
207207
CollectionUtils::MapFileTo<std::vector<std::shared_ptr<ObjectFileInfo>>> sourceFileInfos;
208208
CollectionUtils::MapFileTo<std::shared_ptr<ObjectFileInfo>> objectFileInfos;
209209
CollectionUtils::MapFileTo<std::shared_ptr<TargetInfo>> targetInfos;
@@ -224,18 +224,19 @@ class BuildDatabase {
224224
static fs::path getCorrespondingBitcodeFile(const fs::path &filepath);
225225
void initObjects(const nlohmann::json &compileCommandsJson);
226226
void initInfo(const nlohmann::json &linkCommandsJson);
227-
void createClangCompileCommandsJson(const fs::path &target, const fs::path &buildCommandsJsonPath);
227+
void createClangCompileCommandsJson(const fs::path _target);
228228
void mergeLibraryOptions(std::vector<std::string> &jsonArguments) const;
229229
fs::path newDirForFile(fs::path const& file) const;
230230
fs::path createExplicitObjectFileCompilationCommand(const std::shared_ptr<ObjectFileInfo> &objectInfo);
231-
CollectionUtils::FileSet getSourceFilesForTarget(const std::string &_target);
231+
CollectionUtils::FileSet getSourceFilesForTarget(const fs::path _target);
232232

233233
using sharedLibrariesMap = std::unordered_map<std::string, CollectionUtils::MapFileTo<fs::path>>;
234234

235235
void addLibrariesForCommand(utbot::BaseCommand &command,
236236
BaseFileInfo &info,
237237
sharedLibrariesMap &sharedLibraryFiles,
238238
bool objectFiles = false);
239+
bool isAutoTarget() const;
239240
};
240241

241242
#endif //UNITTESTBOT_BUILDDATABASE_H

0 commit comments

Comments
 (0)