@@ -34,11 +34,12 @@ static std::string tryConvertOptionToPath(const std::string &possibleFilePath,
34
34
BuildDatabase::BuildDatabase (fs::path _buildCommandsJsonPath,
35
35
fs::path _serverBuildDir,
36
36
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" )) {
42
43
if (!fs::exists (linkCommandsJsonPath) || !fs::exists (compileCommandsJsonPath)) {
43
44
throw CompilationDatabaseException (" Couldn't open link_commands.json or compile_commands.json files" );
44
45
}
@@ -51,7 +52,9 @@ BuildDatabase::BuildDatabase(fs::path _buildCommandsJsonPath,
51
52
filterInstalledFiles ();
52
53
addLocalSharedLibraries ();
53
54
fillTargetInfoParents ();
54
- updateTarget (target);
55
+ updateTarget (_target);,
56
+ target (std::move (_target))
57
+ createClangCompileCommandsJson (_target);
55
58
}
56
59
57
60
std::shared_ptr<BuildDatabase> BuildDatabase::create (const utbot::ProjectContext &projectContext,
@@ -201,41 +204,47 @@ void BuildDatabase::initInfo(const nlohmann::json &linkCommandsJson) {
201
204
}
202
205
}
203
206
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);
215
211
216
212
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
+ }
219
230
}
220
231
221
232
fs::path clangCompileCommandsJsonPath = CompilationUtils::getClangCompileCommandsJsonPath (buildCommandsJsonPath);
222
233
JsonUtils::writeJsonToFile (clangCompileCommandsJsonPath, compileCommandsSingleFilesJson);
223
234
}
224
235
225
- void BuildDatabase::updateTarget (std::string target ) {
236
+ void BuildDatabase::updateTarget (const std::string &_target ) {
226
237
if (target == GrpcUtils::UTBOT_AUTO_TARGET_PATH) {
227
238
return ;
228
239
}
229
240
230
- auto new_target = GenerationUtils::findTarget (getAllTargets (), target );
241
+ auto new_target = GenerationUtils::findTarget (getAllTargets (), _target );
231
242
if (new_target.has_value ()) {
232
243
target = new_target.value ();
233
244
} else {
234
245
throw CompilationDatabaseException (" Can't find target: " + target);
235
246
}
236
247
237
- createClangCompileCommandsJson (target, buildCommandsJsonPath);
238
-
239
248
for (auto &[sourceFile, objectInfos]: sourceFileInfos) {
240
249
std::sort (objectInfos.begin (), objectInfos.end (), [&](const std::shared_ptr<ObjectFileInfo> &left,
241
250
const std::shared_ptr<ObjectFileInfo> &right) {
@@ -627,6 +636,7 @@ CollectionUtils::FileSet BuildDatabase::getStubFiles(
627
636
}
628
637
return {};
629
638
}
639
+
630
640
void BuildDatabase::assignStubFilesToLinkUnit (
631
641
std::shared_ptr<const BuildDatabase::TargetInfo> linkUnitInfo,
632
642
CollectionUtils::FileSet stubs) {
@@ -726,10 +736,14 @@ fs::path BuildDatabase::newDirForFile(const fs::path &file) const {
726
736
return Paths::createNewDirForFile (file, base, this ->serverBuildDir );
727
737
}
728
738
729
- CollectionUtils::FileSet BuildDatabase::getSourceFilesForTarget (const std::string &_target) {
739
+ CollectionUtils::FileSet BuildDatabase::getSourceFilesForTarget (const fs::path &_target) {
730
740
return CollectionUtils::transformTo<CollectionUtils::FileSet>(
731
- getArchiveObjectFiles (_target),
741
+ getArchiveObjectFiles (_target. value () ),
732
742
[this ](fs::path const &objectPath) {
733
743
return getClientCompilationUnitInfo (objectPath)->getSourcePath ();
734
744
});
735
745
}
746
+
747
+ bool BuildDatabase::isAutoTarget () const {
748
+ return !target.has_value ();
749
+ }
0 commit comments