| 32 | namespace { |
| 33 | |
| 34 | CMakeFilesCompilationData importCommands(const Path& commandsFile) |
| 35 | { |
| 36 | // NOTE: to get compile_commands.json, you need -DCMAKE_EXPORT_COMPILE_COMMANDS=ON |
| 37 | QFile f(commandsFile.toLocalFile()); |
| 38 | bool r = f.open(QFile::ReadOnly|QFile::Text); |
| 39 | if(!r) { |
| 40 | qCWarning(CMAKE) << "Couldn't open commands file" << commandsFile; |
| 41 | return {}; |
| 42 | } |
| 43 | |
| 44 | qCDebug(CMAKE) << "Found commands file" << commandsFile; |
| 45 | |
| 46 | CMakeFilesCompilationData data; |
| 47 | QJsonParseError error; |
| 48 | const QJsonDocument document = QJsonDocument::fromJson(f.readAll(), &error); |
| 49 | if (error.error) { |
| 50 | qCWarning(CMAKE) << "Failed to parse JSON in commands file:" << error.errorString() << commandsFile; |
| 51 | data.isValid = false; |
| 52 | return data; |
| 53 | } else if (!document.isArray()) { |
| 54 | qCWarning(CMAKE) << "JSON document in commands file is not an array: " << commandsFile; |
| 55 | data.isValid = false; |
| 56 | return data; |
| 57 | } |
| 58 | |
| 59 | MakeFileResolver resolver; |
| 60 | const QString KEY_COMMAND = QStringLiteral("command"); |
| 61 | const QString KEY_DIRECTORY = QStringLiteral("directory"); |
| 62 | const QString KEY_FILE = QStringLiteral("file"); |
| 63 | auto rt = ICore::self()->runtimeController()->currentRuntime(); |
| 64 | const auto values = document.array(); |
| 65 | for (const QJsonValue& value : values) { |
| 66 | if (!value.isObject()) { |
| 67 | qCWarning(CMAKE) << "JSON command file entry is not an object:" << value; |
| 68 | continue; |
| 69 | } |
| 70 | const QJsonObject entry = value.toObject(); |
| 71 | if (!entry.contains(KEY_FILE) || !entry.contains(KEY_COMMAND) || !entry.contains(KEY_DIRECTORY)) { |
| 72 | qCWarning(CMAKE) << "JSON command file entry does not contain required keys:" << entry; |
| 73 | continue; |
| 74 | } |
| 75 | |
| 76 | PathResolutionResult result = resolver.processOutput(entry[KEY_COMMAND].toString(), entry[KEY_DIRECTORY].toString()); |
| 77 | |
| 78 | auto convert = [rt](const Path &path) { return rt->pathInHost(path); }; |
| 79 | |
| 80 | CMakeFile ret; |
| 81 | ret.includes = kTransform<Path::List>(result.paths, convert); |
| 82 | ret.frameworkDirectories = kTransform<Path::List>(result.frameworkDirectories, convert); |
| 83 | ret.defines = result.defines; |
| 84 | const Path path(rt->pathInHost(Path(entry[KEY_FILE].toString()))); |
| 85 | qCDebug(CMAKE) << "entering..." << path << entry[KEY_FILE]; |
| 86 | data.files[path] = ret; |
| 87 | } |
| 88 | |
| 89 | data.isValid = true; |
| 90 | data.rebuildFileForFolderMapping(); |
| 91 | return data; |
no test coverage detected