@@ -56,40 +56,15 @@ BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath,
5656    target = GrpcUtils::UTBOT_AUTO_TARGET_PATH;
5757}
5858
59- BuildDatabase::BuildDatabase (BuildDatabase&  baseBuildDatabase,
59+ BuildDatabase::BuildDatabase (BuildDatabase & baseBuildDatabase,
6060                             const  std::string &_target) :
6161        serverBuildDir(baseBuildDatabase.serverBuildDir),
6262        projectContext(baseBuildDatabase.projectContext),
6363        buildCommandsJsonPath(baseBuildDatabase.buildCommandsJsonPath),
6464        linkCommandsJsonPath(baseBuildDatabase.linkCommandsJsonPath),
6565        compileCommandsJsonPath(baseBuildDatabase.compileCommandsJsonPath),
66-         isAutoTarget(false ) {
67- 
68- //     BuildDatabase baseBuildDatabase(buildCommandsJsonPath, serverBuildDir, projectContext, false);
69- 
70- 
71- //     fs::path target;
72-     // TODO target incorrect name now
73-     if  (Paths::isSourceFile (_target)) {
74-         fs::path root = baseBuildDatabase.getRootForSource (_target);
75-         target = root;
76-     } else  if  (_target == GrpcUtils::UTBOT_AUTO_TARGET_PATH || _target.empty ()) {
77-         fs::path root = baseBuildDatabase.getRootForFirstSource ();
78-         target = root;
79-         isAutoTarget = true ;
80-     } else  {
81-         auto  new_target = GenerationUtils::findTarget (baseBuildDatabase.getAllTargets (), _target);
82-         if  (new_target.has_value ()) {
83-             target = new_target.value ();
84-         } else  {
85-             throw  CompilationDatabaseException (" Can't find target: " 
86-         }
87-     }
88- 
89- 
90- //     CollectionUtils::MapFileTo<std::vector<std::shared_ptr<ObjectFileInfo>>> sourceFileInfos;
91- //     CollectionUtils::MapFileTo<std::shared_ptr<ObjectFileInfo>> objectFileInfos;
92- //     CollectionUtils::MapFileTo<std::vector<fs::path>> objectFileTargets;
66+         target(_target),
67+         isAutoTarget(_target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) {
9368    {
9469        auto  objectFilesList = baseBuildDatabase.getArchiveObjectFiles (target);
9570        for  (const  auto  &objectFilePath: objectFilesList) {
@@ -103,23 +78,32 @@ BuildDatabase::BuildDatabase(BuildDatabase& baseBuildDatabase,
10378                    baseBuildDatabase.objectFileTargets [objectFileInfo->getOutputFile ()];
10479        }
10580    }
106- //     CollectionUtils::MapFileTo<std::shared_ptr<TargetInfo>> targetInfos;
107- //     CollectionUtils::MapFileTo<CollectionUtils::FileSet> linkUnitToStubFiles;
81+ 
10882    {
10983        auto  targetFilesList = baseBuildDatabase.getArchiveTargetFiles (target);
11084        for  (const  auto  &objectFilePath: targetFilesList) {
11185            targetInfos[objectFilePath] = baseBuildDatabase.targetInfos [objectFilePath];
112-             linkUnitToStubFiles[objectFilePath] = baseBuildDatabase.linkUnitToStubFiles [objectFilePath];
11386        }
11487    }
11588
116- //     std::vector<std::pair<nlohmann::json, std::shared_ptr<ObjectFileInfo>>> compileCommands_temp;
11789    compileCommands_temp = baseBuildDatabase.compileCommands_temp ;
118- 
11990    createClangCompileCommandsJson ();
12091}
12192
122- std::shared_ptr<BuildDatabase> BuildDatabase::createBaseForTarget (const  std::string &_target) {
93+ std::shared_ptr<BuildDatabase> BuildDatabase::createBuildDatabaseForSourceOrTarget (const  std::string &_targetOrSourcePath) {
94+     fs::path _target;
95+     if  (Paths::isSourceFile (_targetOrSourcePath)) {
96+         _target = getRootForSource (_targetOrSourcePath);
97+     } else  if  (_targetOrSourcePath == GrpcUtils::UTBOT_AUTO_TARGET_PATH || _targetOrSourcePath.empty ()) {
98+         _target = getRootForFirstSource ();
99+     } else  {
100+         auto  new_target = GenerationUtils::findTarget (getAllTargets (), _targetOrSourcePath);
101+         if  (new_target.has_value ()) {
102+             _target = new_target.value ();
103+         } else  {
104+             throw  CompilationDatabaseException (" Can't find target: " 
105+         }
106+     }
123107    return  std::make_shared<BuildDatabase>(std::move (BuildDatabase (*this , _target)));
124108}
125109
@@ -220,8 +204,7 @@ void BuildDatabase::initObjects(const nlohmann::json &compileCommandsJson) {
220204        sourceFileInfos[sourcePath].emplace_back (objectInfo);
221205    }
222206    for  (auto  &[sourceFile, objectInfos]: sourceFileInfos) {
223-         // Need stable sort for save order of 32 and 64 bits files
224-         std::stable_sort (objectInfos.begin (), objectInfos.end (), conflictPriorityMore);
207+         std::sort (objectInfos.begin (), objectInfos.end (), conflictPriorityMore);
225208    }
226209}
227210
@@ -292,27 +275,6 @@ void BuildDatabase::createClangCompileCommandsJson() {
292275    compilationDatabase = CompilationUtils::getCompilationDatabase (clangCompileCommandsJsonPath);
293276}
294277
295- // void BuildDatabase::updateTarget(const fs::path &_target) {
296- //     if (_target.string() == GrpcUtils::UTBOT_AUTO_TARGET_PATH) {
297- //         return;
298- //     }
299- // 
300- //     for (auto &[sourceFile, objectInfos]: sourceFileInfos) {
301- //         std::sort(objectInfos.begin(), objectInfos.end(), [&](const std::shared_ptr<ObjectFileInfo> &left,
302- //                                                               const std::shared_ptr<ObjectFileInfo> &right) {
303- //             if (CollectionUtils::containsKey(targetInfos, target)) {
304- //                 if (CollectionUtils::containsKey(targetInfos[target]->files, left->getOutputFile())) {
305- //                     return true;
306- //                 }
307- //                 if (CollectionUtils::containsKey(targetInfos[target]->files, right->getOutputFile())) {
308- //                     return false;
309- //                 }
310- //             }
311- //             return false;
312- //         });
313- //     }
314- // }
315- 
316278void  BuildDatabase::mergeLibraryOptions (std::vector<std::string> &jsonArguments) const  {
317279    for  (auto  it = jsonArguments.begin (); it != jsonArguments.end (); it++) {
318280        if  (*it == DynamicLibraryUtils::libraryDirOption || *it == DynamicLibraryUtils::linkFlag) {
@@ -718,21 +680,6 @@ fs::path BuildDatabase::TargetInfo::getOutput() const {
718680    return  commands[0 ].getOutput ();
719681}
720682
721- // CollectionUtils::FileSet BuildDatabase::getStubFiles(
722- //     const std::shared_ptr<const BuildDatabase::TargetInfo> &linkUnitInfo) const {
723- //     auto iterator = linkUnitToStubFiles.find(linkUnitInfo->getOutput());
724- //     if (iterator != linkUnitToStubFiles.end()) {
725- //         return iterator->second;
726- //     }
727- //     return {};
728- // }
729- 
730- void  BuildDatabase::assignStubFilesToLinkUnit (
731-     std::shared_ptr<const  BuildDatabase::TargetInfo> linkUnitInfo,
732-     CollectionUtils::FileSet stubs) {
733-     linkUnitToStubFiles.emplace (linkUnitInfo->getOutput (), std::move (stubs));
734- }
735- 
736683std::vector<std::shared_ptr<BuildDatabase::TargetInfo>> BuildDatabase::getRootTargets () const  {
737684    return  CollectionUtils::filterOut (
738685            CollectionUtils::getValues (targetInfos),
0 commit comments