1515#include < queue>
1616#include < set>
1717#include < unordered_map>
18+ #include < utils/GrpcUtils.h>
1819
1920static std::string tryConvertOptionToPath (const std::string &possibleFilePath,
2021 const fs::path &dirPath) {
@@ -32,7 +33,8 @@ static std::string tryConvertOptionToPath(const std::string &possibleFilePath,
3233
3334BuildDatabase::BuildDatabase (const fs::path& buildCommandsJsonPath,
3435 fs::path serverBuildDir,
35- utbot::ProjectContext projectContext)
36+ utbot::ProjectContext projectContext,
37+ const std::string &target)
3638 : serverBuildDir(std::move(serverBuildDir)), projectContext(std::move(projectContext)) {
3739 linkCommandsJsonPath = fs::canonical (buildCommandsJsonPath / " link_commands.json" );
3840 compileCommandsJsonPath = fs::canonical (buildCommandsJsonPath / " compile_commands.json" );
@@ -42,20 +44,23 @@ BuildDatabase::BuildDatabase(const fs::path& buildCommandsJsonPath,
4244 auto linkCommandsJson = JsonUtils::getJsonFromFile (linkCommandsJsonPath);
4345 auto compileCommandsJson = JsonUtils::getJsonFromFile (compileCommandsJsonPath);
4446
45- createClangCompileCommandsJson (buildCommandsJsonPath, compileCommandsJson);
47+ initObjects ( compileCommandsJson);
4648 initInfo (linkCommandsJson);
4749 filterInstalledFiles ();
4850 addLocalSharedLibraries ();
4951 fillTargetInfoParents ();
52+ createClangCompileCommandsJson (target, buildCommandsJsonPath);
53+ // filter
5054}
5155
52- std::shared_ptr<BuildDatabase> BuildDatabase::create (const utbot::ProjectContext &projectContext) {
56+ std::shared_ptr<BuildDatabase> BuildDatabase::create (const utbot::ProjectContext &projectContext,
57+ const std::string &target) {
5358 fs::path compileCommandsJsonPath =
54- CompilationUtils::substituteRemotePathToCompileCommandsJsonPath (
55- projectContext.projectPath , projectContext.buildDirRelativePath );
59+ CompilationUtils::substituteRemotePathToCompileCommandsJsonPath (
60+ projectContext.projectPath , projectContext.buildDirRelativePath );
5661 fs::path serverBuildDir = Paths::getTmpDir (projectContext.projectName );
5762 std::shared_ptr<BuildDatabase> buildDatabase =
58- std::make_shared<BuildDatabase>(compileCommandsJsonPath, serverBuildDir, projectContext);
63+ std::make_shared<BuildDatabase>(compileCommandsJsonPath, serverBuildDir, projectContext, target );
5964 return buildDatabase;
6065}
6166
@@ -78,10 +83,8 @@ fs::path BuildDatabase::createExplicitObjectFileCompilationCommand(const std::sh
7883 }
7984}
8085
81- void BuildDatabase::createClangCompileCommandsJson (const fs::path &buildCommandsJsonPath,
82- const nlohmann::json &compileCommandsJson) {
83- CollectionUtils::MapFileTo<std::pair<nlohmann::json, std::shared_ptr<ObjectFileInfo>>> fileCompileCommands;
84- for (auto const & compileCommand: compileCommandsJson) {
86+ void BuildDatabase::initObjects (const nlohmann::json &compileCommandsJson) {
87+ for (const nlohmann::json &compileCommand: compileCommandsJson) {
8588 auto objectInfo = std::make_shared<ObjectFileInfo>();
8689
8790 fs::path directory = compileCommand.at (" directory" ).get <std::string>();
@@ -103,11 +106,12 @@ void BuildDatabase::createClangCompileCommandsJson(const fs::path &buildCommands
103106 objectInfo->command .removeWerror ();
104107 fs::path outputFile = objectInfo->getOutputFile ();
105108 fs::path kleeFilePathTemplate =
106- Paths::createNewDirForFile (sourceFile, projectContext.buildDir , serverBuildDir);
109+ Paths::createNewDirForFile (sourceFile, projectContext.buildDir , serverBuildDir);
107110 fs::path kleeFile = Paths::addSuffix (kleeFilePathTemplate, " _klee" );
108111 objectInfo->kleeFilesInfo = std::make_shared<KleeFilesInfo>(kleeFile);
109112
110- if (CollectionUtils::containsKey (objectFileInfos, outputFile) || CollectionUtils::containsKey (targetInfos, outputFile)) {
113+ if (CollectionUtils::containsKey (objectFileInfos, outputFile) ||
114+ CollectionUtils::containsKey (targetInfos, outputFile)) {
111115 /*
112116 * If the condition above is true, that means that the output file
113117 * is built from multiple sources. Hence, it is not an object file,
@@ -129,9 +133,9 @@ void BuildDatabase::createClangCompileCommandsJson(const fs::path &buildCommands
129133 // create targetInfo
130134 targetInfo = targetInfos[outputFile] = std::make_shared<TargetInfo>();
131135 targetInfo->commands .emplace_back (
132- std::initializer_list<std::string>{ targetObjectInfo->command .getCompiler (),
133- " -o" , outputFile, tmpObjectFileName },
134- directory);
136+ std::initializer_list<std::string>{targetObjectInfo->command .getCompiler (),
137+ " -o" , outputFile, tmpObjectFileName},
138+ directory);
135139 targetInfo->addFile (tmpObjectFileName);
136140 }
137141 // redirect new compilation command to temporary file
@@ -143,22 +147,13 @@ void BuildDatabase::createClangCompileCommandsJson(const fs::path &buildCommands
143147 } else {
144148 objectFileInfos[outputFile] = objectInfo;
145149 }
150+ compileCommands_temp.emplace_back (compileCommand, objectInfo);
146151 const fs::path &sourcePath = objectInfo->getSourcePath ();
147- if (!CollectionUtils::containsKey (sourceFileInfos, sourcePath) ||
148- conflictPriorityMore (objectInfo, fileCompileCommands[sourcePath].second )) {
149- fileCompileCommands[sourcePath] = { compileCommand, objectInfo };
150- }
151152 sourceFileInfos[sourcePath].emplace_back (objectInfo);
152153 }
153154 for (auto &[sourceFile, objectInfos]: sourceFileInfos) {
154155 std::sort (objectInfos.begin (), objectInfos.end (), conflictPriorityMore);
155156 }
156- nlohmann::json compileCommandsSingleFilesJson;
157- for (const auto &compileCommand: fileCompileCommands) {
158- compileCommandsSingleFilesJson.push_back (compileCommand.second .first );
159- }
160- fs::path clangCompileCommandsJsonPath = CompilationUtils::getClangCompileCommandsJsonPath (buildCommandsJsonPath);
161- JsonUtils::writeJsonToFile (clangCompileCommandsJsonPath, compileCommandsSingleFilesJson);
162157}
163158
164159void BuildDatabase::initInfo (const nlohmann::json &linkCommandsJson) {
@@ -204,6 +199,27 @@ void BuildDatabase::initInfo(const nlohmann::json &linkCommandsJson) {
204199 }
205200}
206201
202+ void BuildDatabase::createClangCompileCommandsJson (const fs::path &target, const fs::path &buildCommandsJsonPath) {
203+ CollectionUtils::MapFileTo<std::pair<nlohmann::json, std::shared_ptr<ObjectFileInfo>>> fileCompileCommands;
204+ for (const auto &[compileCommand, objectInfo]: compileCommands_temp) {
205+ const fs::path &sourcePath = objectInfo->getSourcePath ();
206+ if ((targetInfos[target]->files .contains (objectInfo->getOutputFile ()) ||
207+ target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) &&
208+ (!CollectionUtils::containsKey (fileCompileCommands, sourcePath) ||
209+ conflictPriorityMore (objectInfo, fileCompileCommands[sourcePath].second ))) {
210+ fileCompileCommands[sourcePath] = {compileCommand, objectInfo};
211+ }
212+ }
213+
214+ nlohmann::json compileCommandsSingleFilesJson;
215+ for (const auto &compileCommand: fileCompileCommands) {
216+ compileCommandsSingleFilesJson.push_back (compileCommand.second .first );
217+ }
218+
219+ fs::path clangCompileCommandsJsonPath = CompilationUtils::getClangCompileCommandsJsonPath (buildCommandsJsonPath);
220+ JsonUtils::writeJsonToFile (clangCompileCommandsJsonPath, compileCommandsSingleFilesJson);
221+ }
222+
207223void BuildDatabase::mergeLibraryOptions (std::vector<std::string> &jsonArguments) const {
208224 for (auto it = jsonArguments.begin (); it != jsonArguments.end (); it++) {
209225 if (*it == DynamicLibraryUtils::libraryDirOption || *it == DynamicLibraryUtils::linkFlag) {
@@ -468,7 +484,8 @@ BuildDatabase::getClientCompilationUnitInfo(const fs::path &filepath) const {
468484 throw CompilationDatabaseException (" File not found in compilation_commands.json: " + filepath.string ());
469485 }
470486 return sourceFileInfos.at (filepath)[0 ];
471- } if (Paths::isObjectFile (filepath)) {
487+ }
488+ if (Paths::isObjectFile (filepath)) {
472489 if (!CollectionUtils::contains (objectFileInfos, filepath)) {
473490 throw CompilationDatabaseException (" File not found in compilation_commands.json: " + filepath.string ());
474491 }
@@ -477,8 +494,7 @@ BuildDatabase::getClientCompilationUnitInfo(const fs::path &filepath) const {
477494 throw CompilationDatabaseException (" File is not a compilation unit or an object file: " + filepath.string ());
478495}
479496
480- std::shared_ptr<const BuildDatabase::TargetInfo>
481- BuildDatabase::getClientLinkUnitInfo (const fs::path &filepath) const {
497+ std::shared_ptr<const BuildDatabase::TargetInfo> BuildDatabase::getClientLinkUnitInfo (const fs::path &filepath) const {
482498 if (Paths::isSourceFile (filepath)) {
483499 auto compilationInfo = getClientCompilationUnitInfo (filepath);
484500 return targetInfos.at (compilationInfo->linkUnit );
@@ -491,8 +507,8 @@ BuildDatabase::getClientLinkUnitInfo(const fs::path &filepath) const {
491507}
492508
493509bool BuildDatabase::conflictPriorityMore (
494- const std::shared_ptr<BuildDatabase::ObjectFileInfo> &left,
495- const std::shared_ptr<BuildDatabase::ObjectFileInfo> &right) {
510+ const std::shared_ptr<BuildDatabase::ObjectFileInfo> &left,
511+ const std::shared_ptr<BuildDatabase::ObjectFileInfo> &right) {
496512 if (StringUtils::contains (left->getOutputFile ().string (), " 64" )) {
497513 return true ;
498514 }
@@ -675,6 +691,7 @@ std::shared_ptr<BuildDatabase::TargetInfo> BuildDatabase::getPriorityTarget() co
675691 });
676692 return *it;
677693}
694+
678695fs::path BuildDatabase::newDirForFile (const fs::path &file) const {
679696 fs::path base = Paths::longestCommonPrefixPath (this ->projectContext .buildDir ,
680697 this ->projectContext .projectPath );
0 commit comments