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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
/****************************************************************************
**
** Implementation of ProjectGenerator class.
**
** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
**
** This file is part of qmake.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.TQPL
** included in the packaging of this file. Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/
#include "projectgenerator.h"
#include "option.h"
#include <tqdir.h>
#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqregexp.h>
TQString project_builtin_regx() //calculate the builtin regular expression..
{
TQString ret;
TQStringList builtin_exts(".c");
builtin_exts << Option::ui_ext << Option::yacc_ext << Option::lex_ext << ".ts";
builtin_exts += Option::h_ext + Option::cpp_ext;
for(TQStringList::Iterator ext_it = builtin_exts.begin();
ext_it != builtin_exts.end(); ++ext_it) {
if(!ret.isEmpty())
ret += "; ";
ret += TQString("*") + (*ext_it);
}
return ret;
}
ProjectGenerator::ProjectGenerator(TQMakeProject *p) : MakefileGenerator(p), init_flag(FALSE)
{
}
void
ProjectGenerator::init()
{
if(init_flag)
return;
int file_count = 0;
init_flag = TRUE;
TQMap<TQString, TQStringList> &v = project->variables();
TQString templ = Option::user_template.isEmpty() ? TQString("app") : Option::user_template;
if(!Option::user_template_prefix.isEmpty())
templ.prepend(Option::user_template_prefix);
v["TEMPLATE_ASSIGN"] += templ;
//figure out target
if(Option::output.name() == "-" || Option::output.name().isEmpty())
v["TARGET"] = TQStringList("unknown");
//the scary stuff
if(project->first("TEMPLATE_ASSIGN") != "subdirs") {
TQString builtin_regex = project_builtin_regx();
TQStringList dirs = Option::projfile::project_dirs;
if(Option::projfile::do_pwd) {
if(!v["INCLUDEPATH"].contains("."))
v["INCLUDEPATH"] += ".";
dirs.prepend(TQDir::currentDirPath());
}
for(TQStringList::Iterator pd = dirs.begin(); pd != dirs.end(); pd++) {
TQString dir, regex;
bool add_depend = FALSE;
if(TQFile::exists((*pd))) {
TQFileInfo fi((*pd));
if(fi.isDir()) {
dir = (*pd);
add_depend = TRUE;
if(dir.right(1) != Option::dir_sep)
dir += Option::dir_sep;
if(Option::projfile::do_recursive) {
TQDir d(dir);
d.setFilter(TQDir::Dirs);
for(int i = 0; i < (int)d.count(); i++) {
if(d[i] != "." && d[i] != "..")
dirs.append(dir + d[i] + TQDir::separator() + builtin_regex);
}
}
regex = builtin_regex;
} else {
TQString file = (*pd);
int s = file.findRev(Option::dir_sep);
if(s != -1)
dir = file.left(s+1);
if(addFile(file)) {
add_depend = TRUE;
file_count++;
}
}
} else { //regexp
regex = (*pd);
}
if(!regex.isEmpty()) {
int s = regex.findRev(Option::dir_sep);
if(s != -1) {
dir = regex.left(s+1);
regex = regex.right(regex.length() - (s+1));
}
if(Option::projfile::do_recursive) {
TQDir d(dir);
d.setFilter(TQDir::Dirs);
for(int i = 0; i < (int)d.count(); i++) {
if(d[i] != "." && d[i] != "..")
dirs.append(dir + d[i] + TQDir::separator() + regex);
}
}
TQDir d(dir, regex);
for(int i = 0; i < (int)d.count(); i++) {
TQString file = dir + d[i];
if (addFile(file)) {
add_depend = TRUE;
file_count++;
}
}
}
if(add_depend && !dir.isEmpty() && !v["DEPENDPATH"].contains(dir)) {
TQFileInfo fi(dir);
if(fi.absFilePath() != TQDir::currentDirPath())
v["DEPENDPATH"] += fileFixify(dir);
}
}
}
if(!file_count) { //shall we try a subdir?
TQStringList dirs = Option::projfile::project_dirs;
if(Option::projfile::do_pwd)
dirs.prepend(".");
const TQString out_file = fileFixify(Option::output.name());
for(TQStringList::Iterator pd = dirs.begin(); pd != dirs.end(); pd++) {
if(TQFile::exists((*pd))) {
TQString newdir = (*pd);
TQFileInfo fi(newdir);
if(fi.isDir()) {
newdir = fileFixify(newdir);
TQStringList &subdirs = v["SUBDIRS"];
if(TQFile::exists(fi.filePath() + TQDir::separator() + fi.fileName() + ".pro") &&
!subdirs.contains(newdir)) {
subdirs.append(newdir);
} else {
TQDir d(newdir, "*.pro");
d.setFilter(TQDir::Files);
for(int i = 0; i < (int)d.count(); i++) {
TQString nd = newdir;
if(nd == ".")
nd = "";
else if(!nd.isEmpty() && !nd.endsWith(TQString(TQChar(TQDir::separator()))))
nd += TQDir::separator();
nd += d[i];
fileFixify(nd);
if(d[i] != "." && d[i] != ".." && !subdirs.contains(nd) && !out_file.endsWith(nd))
subdirs.append(nd);
}
}
if(Option::projfile::do_recursive) {
TQDir d(newdir);
d.setFilter(TQDir::Dirs);
for(int i = 0; i < (int)d.count(); i++) {
TQString nd = fileFixify(newdir + TQDir::separator() + d[i]);
if(d[i] != "." && d[i] != ".." && !dirs.contains(nd))
dirs.append(nd);
}
}
}
} else { //regexp
TQString regx = (*pd), dir;
int s = regx.findRev(Option::dir_sep);
if(s != -1) {
dir = regx.left(s+1);
regx = regx.right(regx.length() - (s+1));
}
TQDir d(dir, regx);
d.setFilter(TQDir::Dirs);
TQStringList &subdirs = v["SUBDIRS"];
for(int i = 0; i < (int)d.count(); i++) {
TQString newdir(dir + d[i]);
TQFileInfo fi(newdir);
if(fi.fileName() != "." && fi.fileName() != "..") {
newdir = fileFixify(newdir);
if(TQFile::exists(fi.filePath() + TQDir::separator() + fi.fileName() + ".pro") &&
!subdirs.contains(newdir)) {
subdirs.append(newdir);
} else {
TQDir d(newdir, "*.pro");
d.setFilter(TQDir::Files);
for(int i = 0; i < (int)d.count(); i++) {
TQString nd = newdir + TQDir::separator() + d[i];
fileFixify(nd);
if(d[i] != "." && d[i] != ".." && !subdirs.contains(nd)) {
if(newdir + d[i] != Option::output_dir + Option::output.name())
subdirs.append(nd);
}
}
}
if(Option::projfile::do_recursive && !dirs.contains(newdir))
dirs.append(newdir);
}
}
}
}
v["TEMPLATE_ASSIGN"] = "subdirs";
return;
}
TQPtrList<MakefileDependDir> deplist;
deplist.setAutoDelete(TRUE);
{
TQStringList &d = v["DEPENDPATH"];
for(TQStringList::Iterator it = d.begin(); it != d.end(); ++it) {
TQString r = (*it), l = Option::fixPathToLocalOS((*it));
deplist.append(new MakefileDependDir(r, l));
}
}
TQStringList &h = v["HEADERS"];
bool no_qt_files = TRUE;
TQString srcs[] = { "SOURCES", "YACCSOURCES", "LEXSOURCES", "INTERFACES", TQString::null };
for(int i = 0; !srcs[i].isNull(); i++) {
TQStringList &l = v[srcs[i]];
for(TQStringList::Iterator val_it = l.begin(); val_it != l.end(); ++val_it) {
if(generateDependencies(deplist, (*val_it), TRUE)) {
TQStringList &tmp = findDependencies((*val_it));
if(!tmp.isEmpty()) {
for(TQStringList::Iterator dep_it = tmp.begin(); dep_it != tmp.end(); ++dep_it) {
TQString file_dir = (*dep_it).section(Option::dir_sep, 0, -2),
file_no_path = (*dep_it).section(Option::dir_sep, -1);
if(!file_dir.isEmpty()) {
for(MakefileDependDir *mdd = deplist.first(); mdd; mdd = deplist.next()) {
if(mdd->local_dir == file_dir && !v["INCLUDEPATH"].contains(mdd->real_dir))
v["INCLUDEPATH"] += mdd->real_dir;
}
}
if(no_qt_files && file_no_path.find(TQRegExp("^q[a-z_0-9].h$")) != -1)
no_qt_files = FALSE;
TQString h_ext;
for(TQStringList::Iterator hit = Option::h_ext.begin();
hit != Option::h_ext.end(); ++hit) {
if((*dep_it).endsWith((*hit))) {
h_ext = (*hit);
break;
}
}
if(!h_ext.isEmpty()) {
if((*dep_it).left(1).lower() == "q") {
TQString qhdr = (*dep_it).lower();
if(file_no_path == "tqthread.h")
addConfig("thread");
}
for(TQStringList::Iterator cppit = Option::cpp_ext.begin();
cppit != Option::cpp_ext.end(); ++cppit) {
TQString src((*dep_it).left((*dep_it).length() - h_ext.length()) +
(*cppit));
if(TQFile::exists(src)) {
bool exists = FALSE;
TQStringList &srcl = v["SOURCES"];
for(TQStringList::Iterator src_it = srcl.begin();
src_it != srcl.end(); ++src_it) {
if((*src_it).lower() == src.lower()) {
exists = TRUE;
break;
}
}
if(!exists)
srcl.append(src);
}
}
} else if((*dep_it).endsWith(Option::lex_ext) &&
file_no_path.startsWith(Option::lex_mod)) {
addConfig("lex_included");
}
if(!h.contains((*dep_it))) {
if(generateMocList((*dep_it)) && !findMocDestination((*dep_it)).isEmpty())
h += (*dep_it);
}
}
}
}
}
}
if(h.isEmpty())
addConfig("moc", FALSE);
//if we find a file that matches an forms it needn't be included in the project
TQStringList &u = v["INTERFACES"];
TQString no_ui[] = { "SOURCES", "HEADERS", TQString::null };
{
for(int i = 0; !no_ui[i].isNull(); i++) {
TQStringList &l = v[no_ui[i]];
for(TQStringList::Iterator val_it = l.begin(); val_it != l.end(); ) {
bool found = FALSE;
for(TQStringList::Iterator ui_it = u.begin(); ui_it != u.end(); ++ui_it) {
TQString s1 = (*val_it).right((*val_it).length() - ((*val_it).findRev(Option::dir_sep) + 1));
if(s1.findRev('.') != -1)
s1 = s1.left(s1.findRev('.')) + Option::ui_ext;
TQString u1 = (*ui_it).right((*ui_it).length() - ((*ui_it).findRev(Option::dir_sep) + 1));
if(s1 == u1) {
found = TRUE;
break;
}
}
if(!found && (*val_it).endsWith(Option::cpp_moc_ext))
found = TRUE;
if(found)
val_it = l.remove(val_it);
else
++val_it;
}
}
}
}
bool
ProjectGenerator::writeMakefile(TQTextStream &t)
{
t << "######################################################################" << endl;
t << "# Automatically generated by qmake (" << qmake_version() << ") " << endl;
t << "######################################################################" << endl << endl;
TQStringList::Iterator it;
for(it = Option::before_user_vars.begin(); it != Option::before_user_vars.end(); ++it)
t << (*it) << endl;
t << getWritableVar("TEMPLATE_ASSIGN", FALSE);
if(project->first("TEMPLATE_ASSIGN") == "subdirs") {
t << endl << "# Directories" << "\n"
<< getWritableVar("SUBDIRS");
} else {
t << getWritableVar("TARGET")
<< getWritableVar("CONFIG", FALSE)
<< getWritableVar("CONFIG_REMOVE", FALSE)
<< getWritableVar("DEPENDPATH")
<< getWritableVar("INCLUDEPATH") << endl;
t << "# Input" << "\n";
t << getWritableVar("HEADERS")
<< getWritableVar("INTERFACES")
<< getWritableVar("LEXSOURCES")
<< getWritableVar("YACCSOURCES")
<< getWritableVar("SOURCES")
<< getWritableVar("TRANSLATIONS");
}
for(it = Option::after_user_vars.begin(); it != Option::after_user_vars.end(); ++it)
t << (*it) << endl;
return TRUE;
}
bool
ProjectGenerator::addConfig(const TQString &cfg, bool add)
{
TQString where = "CONFIG";
if(!add)
where = "CONFIG_REMOVE";
if(!project->variables()[where].contains(cfg)) {
project->variables()[where] += cfg;
return TRUE;
}
return FALSE;
}
bool
ProjectGenerator::addFile(TQString file)
{
file = fileFixify(file, TQDir::currentDirPath());
TQString dir;
int s = file.findRev(Option::dir_sep);
if(s != -1)
dir = file.left(s+1);
if(file.mid(dir.length(), Option::h_moc_mod.length()) == Option::h_moc_mod)
return FALSE;
TQString where;
for(TQStringList::Iterator cppit = Option::cpp_ext.begin(); cppit != Option::cpp_ext.end(); ++cppit) {
if(file.endsWith((*cppit))) {
if(TQFile::exists(file.left(file.length() - (*cppit).length()) + Option::ui_ext))
return FALSE;
else
where = "SOURCES";
break;
}
}
if(where.isEmpty()) {
for(TQStringList::Iterator hit = Option::h_ext.begin(); hit != Option::h_ext.end(); ++hit) {
if(file.endsWith((*hit))) {
where = "HEADERS";
break;
}
}
}
if(where.isEmpty()) {
if(file.endsWith(Option::ui_ext))
where = "INTERFACES";
else if(file.endsWith(".c"))
where = "SOURCES";
else if(file.endsWith(Option::lex_ext))
where = "LEXSOURCES";
else if(file.endsWith(Option::yacc_ext))
where = "YACCSOURCES";
else if(file.endsWith(".ts"))
where = "TRANSLATIONS";
}
TQString newfile = fileFixify(file);
if(!where.isEmpty() && !project->variables()[where].contains(file)) {
project->variables()[where] += newfile;
return TRUE;
}
return FALSE;
}
TQString
ProjectGenerator::getWritableVar(const TQString &v, bool fixPath)
{
TQStringList &vals = project->variables()[v];
if(vals.isEmpty())
return "";
TQString ret;
if(v.endsWith("_REMOVE"))
ret = v.left(v.length() - 7) + " -= ";
else if(v.endsWith("_ASSIGN"))
ret = v.left(v.length() - 7) + " = ";
else
ret = v + " += ";
TQString join = vals.join(" ");
if(ret.length() + join.length() > 80) {
TQString spaces;
for(unsigned int i = 0; i < ret.length(); i++)
spaces += " ";
join = vals.join(" \\\n" + spaces);
}
#if 0
// ### Commented out for now so that project generation works.
// Sam: it had to do with trailing \'s (ie considered continuation lines)
if(fixPath)
join = join.replace("\\", "/");
#else
Q_UNUSED(fixPath);
#endif
return ret + join + "\n";
}
bool
ProjectGenerator::openOutput(TQFile &file) const
{
TQString outdir;
if(!file.name().isEmpty()) {
TQFileInfo fi(file);
if(fi.isDir())
outdir = fi.dirPath() + TQDir::separator();
}
if(!outdir.isEmpty() || file.name().isEmpty()) {
TQString dir = TQDir::currentDirPath();
int s = dir.findRev('/');
if(s != -1)
dir = dir.right(dir.length() - (s + 1));
file.setName(outdir + dir + ".pro");
}
return MakefileGenerator::openOutput(file);
}
|