-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathconverter.cpp
More file actions
452 lines (416 loc) · 15.2 KB
/
converter.cpp
File metadata and controls
452 lines (416 loc) · 15.2 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "converter.h"
#include <QDir>
#include <QFile>
#include <QRegExp>
#include <QDebug>
void Converter::run()
{
qreal max = Files.count() * 3, now=0;
foreach(QString file2do,Files)
{
emit progress(now/max,"parse "+file2do);now += 1;
if(parseProFile(file2do))
{
emit progress(now/max,"parse "+file2do);now += 1;
prepareCmakeSettings(file2do);
emit progress(now/max,"parse "+file2do);now += 1;
writeCMakeLists(file2do);
} else {emit progress(now/max,"nix damit: "+file2do);now += 2;}
}
}
void Converter::setBaseDir(QString basis)
{
// What are we going to do?
// We'll scan the basedir for the *.pro file, read it and add the content
// to our Files list. If it contains subdirs, we'll also add them here...
QStringList dirs(basis),liste;
QString string;
QDir base;
QRegExp templ("\\s*TEMPLATE\\s*[+]?=(.*)"),
subre("\\s*SUBDIRS\\s*[+]?=(.*)",
Qt::CaseSensitive,QRegExp::RegExp2);
int i;
base.setFilter(QDir::Files | QDir::Readable);
base.setNameFilters(QStringList()<<"*.pro");
while(dirs.count())
{
qDebug()<<"DIRS loop:"<<dirs;
base.setPath(dirs.first());
liste = base.entryList();
if(liste.count() > 0)
{
Files << QString("%1/%2").arg(dirs.first()).arg(liste.first());
qDebug()<<"found: "<<liste.first();
QFile dieses(Files.last());
dieses.open(QIODevice::ReadOnly);
string = dieses.readAll();
dieses.close();
string.remove("\\\n");
liste = string.split('\n');
i = liste.indexOf(templ);
if(i > -1)
{
string = templ.cap(1);
if(string.contains("subdirs",Qt::CaseInsensitive))
{
i = liste.indexOf(subre);
if(i > -1)
{
string = subre.cap(1).simplified();
if(string.contains('"'))
{
liste = string.split('"',QString::SkipEmptyParts);
for(i=0;i<liste.count();i++)
liste[i] = liste[i].trimmed();
liste.removeAll("");
} else {
liste = string.split(QRegExp("\\s+"),QString::SkipEmptyParts);
}
foreach(string, liste)
dirs.append(QString("%1/%2").arg(dirs.first()).arg(string));
}
}
}
}
dirs.takeFirst();
}
qDebug()<<"QMake files to do:"<<Files;
}
void Converter::initQMakeVars()
{
DESTDIR = "";
CONFIG = QStringList() << "qt" << "warn_on" << "release" << "incremental" << "link_prl";
QT = QStringList() << "core" << "gui";
DEFINES.clear();
SOURCES.clear();
HEADERS.clear();
FORMS.clear();
DISTFILES.clear();
INCLUDEPATH.clear();
LIBS.clear();
RESOURCES.clear();
SUBDIRS.clear();
TARGET.clear();
TEMPLATE.clear();
TRANSLATIONS.clear();
TOMOC.clear();
}
bool Converter::parseProFile(QString proFile)
{
QFile file(proFile);
QStringList lines;
QString work;
QRegExp liner("^\\s*(\\w+)\\s*([-+]?=)\\s*(.*)$",Qt::CaseInsensitive,QRegExp::RegExp2);
int op; QStringList *sl;
if(file.open(QIODevice::ReadOnly))
{
work = file.readAll();
file.close();
qDebug()<< "parsing QMake file:" << proFile.section('/',-1);
// make multilines simple
work.remove("\\\n");
// get the lines
lines = work.split('\n',QString::SkipEmptyParts);
initQMakeVars();
// now parse the lines ...
while(lines.count())
{
// capture all the contents...
lines.first().indexOf(liner);
// cap(1) is the variable, cap(2) is the operator
if(liner.captureCount() > 0)
{
if(liner.cap(1) == "TEMPLATE")
{
TEMPLATE = liner.cap(3);
}
else if(liner.cap(1) == "TARGET")
{
TARGET = liner.cap(3);
}
else if(liner.cap(1) == "DESTDIR")
{
DESTDIR = liner.cap(3);
}
else
{
sl = 0;
if(liner.cap(2) == "-=") {
op = -1;
} else if(liner.cap(2) == "+=") {
op = +1;
} else op = 0;
if(liner.cap(1) == "CONFIG")
sl = &CONFIG;
else if(liner.cap(1) == "QT")
sl = &QT;
else if(liner.cap(1) == "DEFINES")
sl = &DEFINES;
else if(liner.cap(1) == "SOURCES")
sl = &SOURCES;
else if(liner.cap(1) == "HEADERS")
sl = &HEADERS;
else if(liner.cap(1) == "FORMS")
sl = &FORMS;
else if(liner.cap(1) == "DISTFILES")
sl = &DISTFILES;
else if(liner.cap(1) == "INCLUDEPATH")
sl = &INCLUDEPATH;
else if(liner.cap(1) == "LIBS")
sl = &LIBS;
else if(liner.cap(1) == "RESOURCES")
sl = &RESOURCES;
else if(liner.cap(1) == "SUBDIRS")
sl = &SUBDIRS;
else if(liner.cap(1) == "TRANSLATIONS")
sl = &TRANSLATIONS;
if(sl) operateOnVar(sl,op,liner.cap(3));
}
}
else // nothing captured with our regex - parsing the line needed!
{
//TODO: write something complicated here ...
}
lines.takeFirst(); //just that the loop won't be infinite...
}
if(TEMPLATE.isEmpty()) TEMPLATE = "app";
qDebug()<<"CONFIG:"<<CONFIG;
qDebug()<<"QT:"<<QT;
qDebug()<<"DEFINES:"<<DEFINES;
qDebug()<<"DISTFILES:"<<DISTFILES;
qDebug()<<"LIBS:"<<LIBS;
qDebug()<<"SUBDIRS:"<<SUBDIRS;
qDebug()<<"INCLUDEPATH:"<<INCLUDEPATH;
qDebug()<<"TEMPLATE:"<<TEMPLATE<<", TARGET:"<<TARGET<<", DESTDIR:"<<DESTDIR;
} else return false;
return true;
}
void Converter::operateOnVar(QStringList*& var, int &op, QString val)
{
// First part: split value correctly
// - it can contain quoted strings
// - or it simply contains space-separated strings ...
// -> make them all quoted for that purpose, split them, and remove quotes
// afterwards ...
int i; QString v=val;
i = 0;
while(i<v.count())
{
while((i<v.count()) && (v[i].isSpace())) i++;
if(v[i] != '"')
{
v.insert(i++,'"');
while((i<v.count()) && (!v[i].isSpace())) i++;
v.insert(i++,"\"+++");
} else {
++i;
while((i<v.count()) && (v[i] != '"')) i++;
v.insert(++i,"+++");
}
i+=3;
}
//2nd part: split string ...
QStringList lst = v.split(QRegExp("\"[+]{3}\\s*\"?"),QString::SkipEmptyParts);
v = lst.takeFirst(); v.remove('"'); lst.insert(0,v);
v = lst.takeLast(); v.remove('"'); lst << v;
qDebug()<< lst;
//3rd part: operate on the variable...
switch(op)
{
case -1: //"-="
foreach(QString item, lst)
{
var->removeAll(item);
}
break;
case 0:
var->clear();
case 1:
*var << lst;
break;
}
}
void Converter::prepareCmakeSettings(QString proFile)
{
/* TODOs:
Here we will have to check some stuff. F.e. which files need "mocing"
and other things we might need to include in our CMakeLists.
The qmake-way for mocing was to detect the Q_OBJECT macro (as far as I
understood). Actually this macro shouldn't be found in sources, only
in headers. But we cannot make sure this wouldn't be the case. So we
will just grep all source and header files for the Q_OBJECT macro. If
it's in any of the lists, we add it to the "tomoc" list and remove it
from the other.
*/
int i = 0; QString dir;
dir = proFile.section('/',0,-2);
while(i < HEADERS.count())
{
if(findQOMacro(QString("%1/%2").arg(dir).arg(HEADERS[i])))
{
TOMOC << HEADERS.takeAt(i);
} else ++i;
}
i=0;
while(i < SOURCES.count())
{
if(findQOMacro(QString("%1/%2").arg(dir).arg(SOURCES[i])))
{
TOMOC << SOURCES.takeAt(i);
} else ++i;
}
}
bool Converter::findQOMacro(QString fn)
{
QRegExp qom("^.*Q_OBJECT\\s*$"); QFile file(fn); bool found(false);
if(file.open(QIODevice::ReadOnly))
{
while(!found && !file.atEnd())
{
if(qom.exactMatch(file.readLine())) found=true;
}
file.close();
return found;
} else return false;
}
void Converter::writeCMakeLists(QString proFile)
{
/* Let's start writing a CMakeLists.txt file ...
*/
QString links,help,fn = proFile.section('/',0,-2) + "/CMakeLists.txt";
QFile file(fn); QTextStream text(&file); int i;
#ifdef _DEBUG
if(file.exists()) file.remove();
#endif
if(file.open(QIODevice::WriteOnly))
{
if(!TARGET.isEmpty()) {
text << QString("project( %1 )\n").arg(TARGET);
} else TARGET = proFile.section('/',0,-2).section('/',-1);
text << "cmake_minimum_required( VERSION 3.1 )\n";
text << "set( CMAKE_CXX_STANDARD 14 )\n";
text << "set( CMAKE_INCLUDE_CURRENT_DIR ON )\n";
text << "set( CMAKE_AUTOMOC ON )\n";
// check the config-variable and take respective action
QRegExp buildtype(".*(debug|release|debug_and_release).*",Qt::CaseInsensitive);
i = CONFIG.lastIndexOf(buildtype);
if(!buildtype.cap(1).isEmpty())
{
// buildtype is set - so let's set it for CMake
text << "set( CMAKE_BUILD_TYPE ";
// workaround the "debug_and_release" option: just build release
// with debug info ...
if(buildtype.cap(1)=="debug_and_release")
text << "RelWithDebInfo";
else if(buildtype.cap(1)=="debug")
text << "Debug";
else text << "Release";
text << " )\n";
}
QRegExp warnings(".*(warn_on|warn_off).*",Qt::CaseInsensitive);
i = CONFIG.lastIndexOf(warnings);
if(!warnings.cap(1).isEmpty() || CONFIG.contains("largefile"))
{
text << "add_definitions ( ";
if(warnings.cap(1) == "warn_on")
text << "-Wall ";
else
text << "-w ";
if(CONFIG.contains("largefile"))
text << "-D_FILE_OFFSET_BITS=64 ";
text << ")\n";
}
if(CONFIG.contains("qt"))
{
text << "find_package ( Qt5Widgets REQUIRED )\n";
text << "find_package ( Qt5Core REQUIRED )\n";
text << "find_package ( Qt5Gui REQUIRED )\n";
links.replace("INCLUDE_DIR","LIBRARY");
}
if(!DESTDIR.isEmpty())
{
if(TEMPLATE.contains("app"))
text << QString("set ( EXECUTABLE_OUTPUT_PATH %1 )\n").arg(DESTDIR);
if(TEMPLATE.contains("lib"))
text << QString("set ( LIBRARY_OUTPUT_PATH %1 )\n").arg(DESTDIR);
}
// check subdirs/app/lib and add directories/files accordingly
if(TEMPLATE.contains("subdirs",Qt::CaseInsensitive))
foreach(QString sd, SUBDIRS)
{
text << QString("add_subdirectory( %1 )\n").arg(sd);
} else
if(TEMPLATE.contains(QRegExp("(app)|(lib)")))
{
//write the source/header/ui/moc-stuff
//prepare helper
help = QString("${%1_SRCS}").arg(TARGET);
// write headers
text << QString("set ( %1_HDRS\n\t%2\n\t)\n\n")
.arg(TARGET).arg(HEADERS.join("\n\t"));
// write sources
text << QString("set ( %1_SRCS\n\t%2\n\t)\n\n")
.arg(TARGET).arg(SOURCES.join("\n\t"));
// write forms
if(!FORMS.isEmpty())
{ text << QString("set ( %1_UIS\n\t%2\n\t)\n")
.arg(TARGET).arg(FORMS.join("\n\t"));
text << QString("QT5_WRAP_UI(UIS ${%1_UIS})\n\n").arg(TARGET);
help += " ${UIS}";
}
// write resources
if(!RESOURCES.isEmpty())
{ text << QString("set ( %1_RSCS\n\t%2\n\t)\n")
.arg(TARGET).arg(RESOURCES.join("\n\t"));
text << QString("QT5_ADD_RESOURCES(RSCS ${%1_RSCS})\n\n")
.arg(TARGET);
help += " ${RSCS}";
}
// write translations
if(!TRANSLATIONS.isEmpty())
{ text << QString("set ( %1_TRS\n\t%2\n\t)\n")
.arg(TARGET).arg(TRANSLATIONS.join("\n\t"));
text << QString("QT5_ADD_TRANSLATION(TRS ${%1_TRS})\n\n")
.arg(TARGET);
help += " ${TRS}";
}
// write 2Bmoc-ed
if(!TOMOC.isEmpty())
{ text << QString("set ( %1_MOCS\n\t%2\n\t)\n")
.arg(TARGET).arg(TOMOC.join("\n\t"));
text << QString("QT5_WRAP_CPP(MOCS ${%1_MOCS})\n\n").arg(TARGET);
help += " ${MOCS}";
}
if(TEMPLATE == "app")
{
text << QString("add_executable ( %1 ").arg(TARGET);
} else { //"lib" (or even a plugin
text << QString("add_library ( %1 ").arg(TARGET);
if(CONFIG.contains("plugin"))
text << "MODULE ";
else
text << "SHARED ";
}
text << QString("${%1_SRCS} ${UIS} ${RSCS} ${TRS} ${MOCS} )\n").arg(TARGET);
text << QString("target_link_libraries ( %1 %2 Qt5::Core Qt5::Gui Qt5::Widgets )\n").arg(TARGET)
.arg(links);
}
text.flush();
file.close();
}
}
#include "converter.moc"