1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
---|---|
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "toolchaindata.h" |
6 | #include "common/util/custompaths.h" |
7 | #include "common/toolchain/toolchain.h" |
8 | |
9 | #include <QJsonObject> |
10 | #include <QJsonDocument> |
11 | #include <QByteArray> |
12 | #include <QFile> |
13 | #include <QJsonArray> |
14 | #include <QDebug> |
15 | #include <QDir> |
16 | |
17 | ToolChainData::ToolChainData() |
18 | { |
19 | } |
20 | |
21 | bool ToolChainData::readToolChain(QString &filePath) |
22 | { |
23 | // Read all contents from toolchain file. |
24 | QFile file(filePath); |
25 | file.open(QIODevice::ReadOnly); |
26 | QByteArray data = file.readAll(); |
27 | file.close(); |
28 | |
29 | // Parse. |
30 | QJsonParseError parseError; |
31 | QJsonDocument doc = QJsonDocument::fromJson(data, &parseError); |
32 | if (parseError.error != QJsonParseError::NoError) { |
33 | qInfo() << "Toolchain file read failed!"; |
34 | return false; |
35 | } |
36 | |
37 | auto parseSubObj = [this](QJsonObject &obj, const QString &subobjName) { |
38 | if (obj.contains(subobjName)) { |
39 | QJsonValue cCompilersArray = obj.value(subobjName); |
40 | QJsonArray array = cCompilersArray.toArray(); |
41 | |
42 | QStringList parameters; |
43 | Params params; |
44 | for (int i = 0; i < array.size(); ++i) { |
45 | QJsonValue sub = array.at(i); |
46 | QJsonObject subObj = sub.toObject(); |
47 | |
48 | QJsonValue nameVal = subObj.value(kNameItem); |
49 | QString name = nameVal.toString(); |
50 | |
51 | QJsonValue pathVal = subObj.value(kPathItem); |
52 | QString path = pathVal.toString(); |
53 | |
54 | ToolChainParam param; |
55 | param.name = name; |
56 | param.path = path; |
57 | |
58 | params.push_back(param); |
59 | } |
60 | toolChains.insert(subobjName, params); |
61 | } |
62 | }; |
63 | |
64 | QJsonArray array = doc.array(); |
65 | for (auto node : array) { |
66 | auto obj = node.toObject(); |
67 | for (auto it = obj.begin(); it != obj.end(); it++) { |
68 | parseSubObj(obj, it.key()); |
69 | } |
70 | } |
71 | |
72 | return true; |
73 | } |
74 | |
75 | const ToolChainData::ToolChains &ToolChainData::getToolChanins() const |
76 | { |
77 | return toolChains; |
78 | } |
79 | |
80 | bool ToolChainData::readToolChainData(QString &retMsg) |
81 | { |
82 | bool ret = false; |
83 | QString toolChainFilePath = CustomPaths::user(CustomPaths::Configures) |
84 | + QDir::separator() + toolchains::K_TOOLCHAINFILE; |
85 | if (QFile(toolChainFilePath).exists()) { |
86 | ret = readToolChain(toolChainFilePath); |
87 | retMsg = ret ? "read tool chain successful!": "read tool chain failed!"; |
88 | } |
89 | |
90 | return ret; |
91 | } |
92 |