-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathuvconvertor.cpp
337 lines (292 loc) · 10.1 KB
/
uvconvertor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include "uvconvertor.hpp"
#include "tinyxml2/tinyxml2.h"
#include "nlohmann/json.hpp"
#include <iostream>
#include <fstream>
#include "utils.hpp"
#include <string>
#include <regex>
#include <algorithm>
namespace fs=std::filesystem;
using namespace tinyxml2;
using namespace nlohmann;
/***************************************************************
* @brief parse uvprojx file to list
* @param
* @note
* @Sample usage:
**************************************************************/
uVConvertor::uVConvertor(std::string uvProjx, std::string target)
{
/*Debug*/
//std::cout<<"[Debug]:Input file:"<<uvProjx<<std::endl;
replace(uvProjx.begin(),uvProjx.end(),'\\','/');
//std::cout<<"[Debug]:Input file:"<<uvProjx<<std::endl;
fs::path p(uvProjx);
ifPath=p.parent_path().string();
//加载文件
XMLDocument doc;
if(doc.LoadFile( uvProjx.c_str() )!=0)
{
//cout<<doc.ErrorID()<<std::endl;
return;
}
//2.找到根节点
tinyxml2::XMLElement* xmlroot = doc.RootElement();
if (xmlroot == NULL) {
return;
}
//cout<<"root:"<<xmlroot->Name()<<std::endl;
tinyxml2::XMLElement* target_elm;
if (target != "") {
while (true) {
target_elm = xmlroot->FirstChildElement("Targets")->FirstChildElement("Target");
if (!target_elm) {
break;
}
if (target_elm->FirstChildElement("TargetName")->GetText() == target) {
break;
}
xmlroot->FirstChildElement("Targets")->DeleteChild(target_elm);
};
if (!target_elm) {
std::cout<<"error: target <"<<target<<"> not found!"<<std::endl;
return;
}
} else {
target_elm = xmlroot->FirstChildElement("Targets")->FirstChildElement("Target");
}
tinyxml2::XMLElement* outputDirectory = target_elm->FirstChildElement("TargetOption") \
->FirstChildElement("TargetCommonOption")->FirstChildElement("OutputDirectory");
tinyxml2::XMLElement* outputName = target_elm->FirstChildElement("TargetOption") \
->FirstChildElement("TargetCommonOption")->FirstChildElement("OutputName");
// 获取编译生成的build.log.htm文件位置
std::string buildLogHtmPath;
buildLogHtmPath.append(ifPath + "/").append(outputDirectory->GetText()).append(outputName->GetText()).append(".build_log.htm");
/*Debug*/
//std::cout<<"[Debug] log file:"<<buildLogHtmPath<<std::endl;
replace(buildLogHtmPath.begin(),buildLogHtmPath.end(),'\\','/');
//std::cout<<"[Debug] log file:"<<buildLogHtmPath<<std::endl;
// 获取编译生成的dep文件位置
std::string depFilePath;
depFilePath.append(ifPath + "/").append(outputDirectory->GetText())
.append(p.filename().replace_extension().string()).append("_")
.append(target_elm->FirstChildElement("TargetName")->GetText()).append(".dep");
/*Debug*/
//std::cout<<"[Debug] Dep file:"<<depFilePath<<std::endl;
replace(depFilePath.begin(),depFilePath.end(),'\\','/');
//std::cout<<"[Debug] Dep file:"<<depFilePath<<std::endl;
// 提示编译获取编译日志文件
if (!fs::exists(depFilePath))
{
std::cout << "[Failed] File:\"" << fs::absolute(depFilePath).string() << "\" not exist!" << std::endl;
std::cout << "\n\nPlease compile first!!!\n" << std::endl;
}
else
{
// 解析编译日志文件,提取编译依赖
std::ifstream depFile(depFilePath, std::ifstream::in);
std::vector<std::string> buildDepStringList;
//std::cout << "Load : " << fs::absolute(depFilePath).string() << " Successful!" << std::endl;
while (depFile.good())
{
std::string lineStr;
getline(depFile, lineStr);
if (lineStr.find("F") == 0)
{
StringReplace(lineStr, "F \\(", ""); // 去除开头的F (
StringReplace(lineStr, "\r", " "); // 去除行尾的回车,巨坑,dep文件 F开头的行中,明明换行了,但是只有回车
StringReplace(lineStr, " -", " -"); // 为每个参数选项之间的空格增加一个空格,防止路径中含有空格导致参数分割错误。
StringReplace(lineStr, "-I \"{0,1}", "-I"); // 去除-I后面的空格和"字符
StringReplace(lineStr, "\\(0x[[:xdigit:]]+\\)", ""); // 去除不需要的地址
StringReplace(lineStr, "\\\\", "/"); // 所有的\\替换为/
lineStr.erase(lineStr.end() - 1);
buildDepStringList.push_back(lineStr);
}
}
depFile.close();
// 获取编译器所在路径
std::ifstream buildLogHtmFile(buildLogHtmPath, std::ifstream::in);
std::regex toolchainReg("Toolchain Path:.*");
std::string toolchainPath;
while (buildLogHtmFile.good())
{
std::string lineStr;
getline(buildLogHtmFile, lineStr);
if (std::regex_match(lineStr, toolchainReg))
{
StringReplace(lineStr, "(Bin|bin)", "Include");
StringReplace(lineStr, "Toolchain Path: ", "-I");
StringReplace(lineStr, "\\\\", "/");
toolchainPath = lineStr;
break;
}
}
buildLogHtmFile.close();
// 提取所有参数
for (const auto& buildDepString : buildDepStringList)
{
std::list<std::string> fileAndArguments, arguments;
StringSplit(buildDepString, "\\)\\(", fileAndArguments);
m_fileList.push_back(fileAndArguments.front());
fileAndArguments.pop_front();
if (!fileAndArguments.empty())
{
StringSplit(fileAndArguments.back(), " {2,}", arguments); // 按照两个空格分割参数
for (auto& argumentStr : arguments)
{
if (argumentStr.find("-I") == 0 && argumentStr.rfind("\"") == (argumentStr.size() - 1)) // 为-I开头的行去除行尾"字符
{
argumentStr.erase(argumentStr.end() - 1);
}
}
arguments.push_back(toolchainPath);
}
m_argumentsList.push_back(arguments);
}
}
}
uVConvertor::~uVConvertor()
{
}
/***************************************************************
* @brief print out list
* @param
* @note
* @Sample usage:
**************************************************************/
void uVConvertor::printItems()
{
//
std::cout<<"-------------------------------"<<std::endl;
//使用迭代器输出list容器中的元素
for (std::list<std::list<std::string>>::iterator args = m_argumentsList.begin(); args != m_argumentsList.end(); ++args) {
for (std::list<std::string>::iterator arg = args->begin(); arg != args->end(); ++arg)
{
std::cout << *arg << std::endl;
}
std::cout << "==============================" << std::endl;
}
std::cout<<"-------------------------------"<<std::endl;
for (std::list<std::string>::iterator it = m_fileList.begin(); it != m_fileList.end(); ++it) {
std::cout << *it << std::endl;
}
std::cout<<"-------------------------------"<<std::endl;
}
//"D:\PROJECT\uVConvertor\audioRec\USER\FATFS.uvprojx"
/***************************************************************
* @brief export list to json file
* @param
* @note
* @Sample usage:
**************************************************************/
void uVConvertor::toCompileJson(std::string outPath,std::string extOptions)
{
//extern options
std::list<std::string> extop;
StringSplit(extOptions, ",", extop);
//export to json
json j,j1;
//files
for (std::list<std::string>::iterator it = m_fileList.begin(); it != m_fileList.end(); ++it) {
j1.clear();
j1["file"]=*it;
j1["directory"]=ifPath;
std::list<std::list<std::string>>::iterator args = m_argumentsList.begin();
//arguments
for (std::list<std::string>::iterator arg = args->begin(); arg != args->end(); ++arg)
{
std::string arg_str = *arg;
//clangd can't handle -imacros argument with space
std::size_t found = arg_str.find("--imacros ");
if (found != std::string::npos)
{
j1["arguments"].push_back("--imacros");
arg_str.replace(found, 10, "");
j1["arguments"].push_back(arg_str);
continue;
}
found = arg_str.find("-imacros ");
if (found != std::string::npos)
{
j1["arguments"].push_back("-imacros");
arg_str.replace(found, 9, "");
j1["arguments"].push_back(arg_str);
continue;
}
found = arg_str.find("-preinclude=");
if (found != std::string::npos)
{
j1["arguments"].push_back("-imacros");
arg_str.replace(found, 12, "");
j1["arguments"].push_back(arg_str);
continue;
}
// Remove quotes from -D arguments
if (arg_str.find("-D") == 0) {
std::size_t quote_start = arg_str.find('"');
std::size_t quote_end = arg_str.rfind('"');
if (quote_start != std::string::npos && quote_end != std::string::npos && quote_end > quote_start) {
arg_str.erase(quote_start, 1); // Remove starting quote
arg_str.erase(quote_end - 1, 1); // Remove ending quote
}
j1["arguments"].push_back(arg_str);
continue;
}
found = arg_str.find("--diag_suppress=");
if (found != std::string::npos)
{
continue;
}
found = arg_str.find("-o ");
if (found != std::string::npos)
{
continue;
}
found = arg_str.find("--depend ");
if (found != std::string::npos)
{
continue;
}
found = arg_str.find("--apcs=");
if (found != std::string::npos)
{
continue;
}
found = arg_str.find("--split_sections");
if (found != std::string::npos)
{
continue;
}
found = arg_str.find("--cpu ");
if (found != std::string::npos)
{
continue;
}
j1["arguments"].push_back(*arg);
}
//extern options
if(extop.size()!=0)
{
for (std::list<std::string>::iterator e = extop.begin(); e != extop.end(); ++e)
{
std::string arg_str = *e;
if (arg_str.empty()) {
continue;
}
j1["arguments"].push_back(*e);
}
}
m_argumentsList.pop_front();
if (j1.contains("arguments"))
j.push_back(j1);
}
// write prettified JSON to another file
outPath.append("/compile_commands.json");
/*debug*/
//std::cout<<"[Debug] output file:"<<outPath<<std::endl;
replace(outPath.begin(),outPath.end(),'\\','/');
//std::cout<<"[Debug] output file:"<<outPath<<std::endl;
std::ofstream o(outPath);
o << std::setw(4) << j << std::endl;
}