@@ -34,11 +34,12 @@ static std::string tryConvertOptionToPath(const std::string &possibleFilePath,
3434BuildDatabase::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
5760std::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 ) {
226- if (target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) {
236+ void BuildDatabase::updateTarget (const std::string &_target ) {
237+ 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+
630640void BuildDatabase::assignStubFilesToLinkUnit (
631641 std::shared_ptr<const BuildDatabase::TargetInfo> linkUnitInfo,
632642 CollectionUtils::FileSet stubs) {
@@ -726,10 +736,15 @@ 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) {
740+ LOG_IF_S (ERROR, !isAutoTarget () && target != _target.c_str ()) << " Try get sources for different target" ;
730741 return CollectionUtils::transformTo<CollectionUtils::FileSet>(
731742 getArchiveObjectFiles (_target),
732743 [this ](fs::path const &objectPath) {
733744 return getClientCompilationUnitInfo (objectPath)->getSourcePath ();
734745 });
735746}
747+
748+ bool BuildDatabase::isAutoTarget () const {
749+ return target == GrpcUtils::UTBOT_AUTO_TARGET_PATH;
750+ }
0 commit comments