1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
---|---|
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "targetsmanager.h" |
6 | #include "services/project/projectservice.h" |
7 | #include "cmake/project/mainframe/cbp/cbpparser.h" |
8 | |
9 | //static const char *kProjectFile = ".cproject"; |
10 | static const char *kProjectFile = ".cbp"; |
11 | |
12 | // target name build all. |
13 | static const char *kGlobalBuild = "all"; |
14 | |
15 | // target name rebuild all. |
16 | static const char *kGlobalRebuild = "rebuild_cache"; |
17 | |
18 | // target name rebuild all. |
19 | static const char *kGlobalClean = "clean"; |
20 | |
21 | using namespace dpfservice; |
22 | |
23 | TargetsManager::TargetsManager(QObject *parent) : QObject(parent) |
24 | , parser(new CMakeCbpParser()) |
25 | { |
26 | |
27 | } |
28 | |
29 | TargetsManager::~TargetsManager() |
30 | { |
31 | if (parser) { |
32 | delete parser; |
33 | parser = nullptr; |
34 | } |
35 | } |
36 | |
37 | QString TargetsManager::getCMakeConfigFile() |
38 | { |
39 | return kProjectFile; |
40 | } |
41 | |
42 | bool TargetsManager::isGloablTarget(Target &target) |
43 | { |
44 | return target.srcPath.isEmpty(); |
45 | } |
46 | |
47 | QString TargetsManager::cbpFilePath(const QString &buildFolder) |
48 | { |
49 | QString cbpFilePath; |
50 | QDir dir(buildFolder); |
51 | |
52 | QStringList filters; |
53 | filters << "*.cbp"; |
54 | |
55 | QFileInfoList fileInfoList = dir.entryInfoList(filters, QDir::Files); |
56 | if (fileInfoList.size() == 0) { |
57 | qInfo() << ".cbp file not found!"; |
58 | return {}; |
59 | } |
60 | return fileInfoList.first().filePath(); |
61 | } |
62 | |
63 | TargetsManager *TargetsManager::instance() |
64 | { |
65 | static TargetsManager instance; |
66 | return &instance; |
67 | } |
68 | |
69 | CMakeCbpParser *TargetsManager::cbpParser() const |
70 | { |
71 | return parser; |
72 | } |
73 | |
74 | void TargetsManager::readTargets(const QString &buildDirectory, const QString &workspaceDirectory) |
75 | { |
76 | if (buildDirectory.isEmpty()) { |
77 | qCritical() << "build directory not set!"; |
78 | return; |
79 | } |
80 | |
81 | exeTargets.clear(); |
82 | targets.clear(); |
83 | buildTargetNameList.clear(); |
84 | exeTargetNameList.clear(); |
85 | |
86 | QString cbp = cbpFilePath(buildDirectory); |
87 | if (parser) { |
88 | delete parser; |
89 | parser = new CMakeCbpParser(); |
90 | } |
91 | parser->parseCbpFile(cbp, workspaceDirectory); |
92 | |
93 | QStringList targetNameList; |
94 | QStringList tempExeTargetNameList; |
95 | |
96 | QList<CMakeBuildTarget> cbpTargets = parser->getBuildTargets(); |
97 | for (auto cbpTarget : cbpTargets) { |
98 | |
99 | QStringList commandItems = cbpTarget.makeCommand.split(" "); |
100 | QStringList argsFiltered; |
101 | for (auto &arg : commandItems) { |
102 | if (!arg.isEmpty()) { |
103 | argsFiltered << arg.replace("\"", ""); |
104 | } |
105 | } |
106 | |
107 | Target target; |
108 | target.name = cbpTarget.title; |
109 | target.buildCommand = argsFiltered.first(); |
110 | argsFiltered.pop_front(); |
111 | target.buildArguments = argsFiltered; |
112 | target.output = cbpTarget.output; |
113 | target.buildTarget = cbpTarget.title; |
114 | target.workingDir = cbpTarget.workingDirectory; |
115 | targets.push_back(target); |
116 | |
117 | if (cbpTarget.type == CBPTargetType::kExecutable) { |
118 | exeTargets.push_back(target); |
119 | tempExeTargetNameList.push_back(target.buildTarget); |
120 | |
121 | if (cbpTarget.title == cbpParser()->getProjectName()) { |
122 | exeTargetSelected = target; |
123 | } |
124 | } else if (cbpTarget.type == CBPTargetType::kUtility) { |
125 | if (target.name == kGlobalBuild) { |
126 | buildTargetSelected = target; |
127 | } else if (target.name == kGlobalClean) { |
128 | cleanTargetSelected = target; |
129 | } else if (target.name == kGlobalRebuild) { |
130 | rebuildTargetSelected = target; |
131 | } |
132 | } |
133 | targetNameList.append(target.buildTarget); |
134 | } |
135 | |
136 | buildTargetNameList = targetNameList.toSet().values(); |
137 | exeTargetNameList = tempExeTargetNameList.toSet().values(); |
138 | |
139 | if (exeTargetSelected.name.isEmpty() && exeTargets.size() > 0) { |
140 | exeTargetSelected = exeTargets.front(); |
141 | } |
142 | |
143 | emit initialized(); |
144 | } |
145 | |
146 | const QStringList TargetsManager::getTargetNamesList() const |
147 | { |
148 | return buildTargetNameList; |
149 | } |
150 | |
151 | const QStringList TargetsManager::getExeTargetNamesList() const |
152 | { |
153 | return exeTargetNameList; |
154 | } |
155 | |
156 | Target TargetsManager::getTargetByName(const QString &targetName) |
157 | { |
158 | Target result; |
159 | for (auto iter = targets.begin(); iter != targets.end(); ++iter) { |
160 | if (targetName == iter->name) { |
161 | result = *iter; |
162 | } |
163 | } |
164 | |
165 | return result; |
166 | } |
167 | |
168 | Target TargetsManager::getActivedTargetByTargetType(const TargetType type) |
169 | { |
170 | Target result; |
171 | switch (type) { |
172 | case kBuildTarget: |
173 | result = buildTargetSelected; |
174 | break; |
175 | case kRebuildTarget: |
176 | result = rebuildTargetSelected; |
177 | break; |
178 | case kCleanTarget: |
179 | result = cleanTargetSelected; |
180 | break; |
181 | case kActiveExecTarget: |
182 | result = exeTargetSelected; |
183 | break; |
184 | default: |
185 | break; |
186 | } |
187 | return result; |
188 | } |
189 | |
190 | const Targets TargetsManager::getAllTargets() const |
191 | { |
192 | return targets; |
193 | } |
194 | |
195 | void TargetsManager::updateActivedBuildTarget(const QString &targetName) |
196 | { |
197 | buildTargetSelected = getTargetByName(targetName); |
198 | } |
199 | |
200 | void TargetsManager::updateActivedCleanTarget(const QString &targetName) |
201 | { |
202 | cleanTargetSelected = getTargetByName(targetName); |
203 | } |
204 | |
205 | void TargetsManager::updateActiveExceTarget(const QString &targetName) |
206 | { |
207 | exeTargetSelected = getTargetByName(targetName); |
208 | } |
209 | |
210 |