source: trunk/qmake/project.cpp@ 7

Last change on this file since 7 was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 45.1 KB
Line 
1/****************************************************************************
2** $Id: project.cpp 2 2005-11-16 15:49:26Z dmik $
3**
4** Implementation of QMakeProject class.
5**
6** Copyright (C) 1992-2003 Trolltech AS. All rights reserved.
7**
8** This file is part of qmake.
9**
10** This file may be distributed under the terms of the Q Public License
11** as defined by Trolltech AS of Norway and appearing in the file
12** LICENSE.QPL included in the packaging of this file.
13**
14** This file may be distributed and/or modified under the terms of the
15** GNU General Public License version 2 as published by the Free Software
16** Foundation and appearing in the file LICENSE.GPL included in the
17** packaging of this file.
18**
19** Licensees holding valid Qt Enterprise Edition licenses may use this
20** file in accordance with the Qt Commercial License Agreement provided
21** with the Software.
22**
23** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
24** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25**
26** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
27** information about Qt Commercial License Agreements.
28** See http://www.trolltech.com/qpl/ for QPL licensing information.
29** See http://www.trolltech.com/gpl/ for GPL licensing information.
30**
31** Contact info@trolltech.com if any conditions of this licensing are
32** not clear to you.
33**
34**********************************************************************/
35
36#include "project.h"
37#include "property.h"
38#include "option.h"
39#include <qfile.h>
40#include <qdir.h>
41#include <qregexp.h>
42#include <qtextstream.h>
43#include <qvaluestack.h>
44#ifdef Q_OS_UNIX
45# include <unistd.h>
46#endif
47#include <stdio.h>
48#include <stdlib.h>
49
50#ifdef Q_OS_WIN32
51#define QT_POPEN _popen
52#else
53#define QT_POPEN popen
54#endif
55
56struct parser_info {
57 QString file;
58 int line_no;
59} parser;
60
61static void qmake_error_msg(const char *msg)
62{
63 fprintf(stderr, "%s:%d: %s\n", parser.file.latin1(), parser.line_no, msg);
64}
65
66QStringList qmake_mkspec_paths()
67{
68 QStringList ret;
69 const QString concat = QDir::separator() + QString("mkspecs");
70 if(const char *qmakepath = getenv("QMAKEPATH")) {
71#ifdef Q_OS_WIN
72 QStringList lst = QStringList::split(';', qmakepath);
73 for(QStringList::Iterator it = lst.begin(); it != lst.end(); ++it) {
74 QStringList lst2 = QStringList::split(':', (*it));
75 for(QStringList::Iterator it2 = lst2.begin(); it2 != lst2.end(); ++it2)
76 ret << ((*it2) + concat);
77 }
78#else
79 QStringList lst = QStringList::split(':', qmakepath);
80 for(QStringList::Iterator it = lst.begin(); it != lst.end(); ++it)
81 ret << ((*it) + concat);
82#endif
83 }
84 if(const char *qtdir = getenv("QTDIR"))
85 ret << (QString(qtdir) + concat);
86#ifdef QT_INSTALL_PREFIX
87 ret << (QT_INSTALL_PREFIX + concat);
88#endif
89#if defined(HAVE_QCONFIG_CPP)
90 ret << (qInstallPath() + concat);
91#endif
92#ifdef QT_INSTALL_DATA
93 ret << (QT_INSTALL_DATA + concat);
94#endif
95#if defined(HAVE_QCONFIG_CPP)
96 ret << (qInstallPathData() + concat);
97#endif
98
99 /* prefer $QTDIR if it is set */
100 if (getenv("QTDIR"))
101 ret << getenv("QTDIR");
102 ret << qInstallPathData();
103 return ret;
104}
105
106static QString varMap(const QString &x)
107{
108 QString ret(x);
109 if(ret.startsWith("TMAKE")) //tmake no more!
110 ret = "QMAKE" + ret.mid(5);
111 else if(ret == "INTERFACES")
112 ret = "FORMS";
113 else if(ret == "QMAKE_POST_BUILD")
114 ret = "QMAKE_POST_LINK";
115 else if(ret == "TARGETDEPS")
116 ret = "POST_TARGETDEPS";
117 else if(ret == "LIBPATH")
118 ret = "QMAKE_LIBDIR";
119 else if(ret == "QMAKE_EXT_MOC")
120 ret = "QMAKE_EXT_CPP_MOC";
121 else if(ret == "QMAKE_MOD_MOC")
122 ret = "QMAKE_H_MOD_MOC";
123 else if(ret == "QMAKE_LFLAGS_SHAPP")
124 ret = "QMAKE_LFLAGS_APP";
125 else if(ret == "PRECOMPH")
126 ret = "PRECOMPILED_HEADER";
127 else if(ret == "PRECOMPCPP")
128 ret = "PRECOMPILED_SOURCE";
129 else if(ret == "INCPATH")
130 ret = "INCLUDEPATH";
131 return ret;
132}
133
134static QStringList split_arg_list(const QString &params)
135{
136 QChar quote = 0;
137 QStringList args;
138 for(int x = 0, last = 0, parens = 0; x <= (int)params.length(); x++) {
139 if(x == (int)params.length()) {
140 QString mid = params.mid(last, x - last).stripWhiteSpace();
141 if(mid[(int)mid.length()-1] == quote)
142 mid.truncate(1);
143 args << mid;
144 } else if(params[x] == ')') {
145 parens--;
146 } else if(params[x] == '(') {
147 parens++;
148 } else if(params[x] == quote) {
149 quote = 0;
150 } else if(params[x] == '\'' || params[x] == '"') {
151 quote = params[x];
152 } else if(!parens && !quote && params[x] == ',') {
153 args << params.mid(last, x - last).stripWhiteSpace();
154 last = x+1;
155 }
156 }
157 return args;
158}
159
160static QStringList split_value_list(const QString &vals, bool do_semicolon=FALSE)
161{
162 QString build;
163 QStringList ret;
164 QValueStack<QChar> quote;
165 for(int x = 0; x < (int)vals.length(); x++) {
166 if(x != (int)vals.length()-1 && vals[x] == '\\' && (vals[x+1] == '\'' || vals[x+1] == '"'))
167 build += vals[x++]; //get that 'escape'
168 else if(!quote.isEmpty() && vals[x] == quote.top())
169 quote.pop();
170 else if(vals[x] == '\'' || vals[x] == '"')
171 quote.push(vals[x]);
172
173 if(quote.isEmpty() && ((do_semicolon && vals[x] == ';') || vals[x] == ' ')) {
174 ret << build;
175 build = "";
176 } else {
177 build += vals[x];
178 }
179 }
180 if(!build.isEmpty())
181 ret << build;
182 return ret;
183}
184
185QMakeProject::QMakeProject()
186{
187 prop = NULL;
188}
189
190QMakeProject::QMakeProject(QMakeProperty *p)
191{
192 prop = p;
193}
194
195void
196QMakeProject::reset()
197{
198 /* scope blocks start at true */
199 test_status = TestNone;
200 scope_flag = 0x01;
201 scope_block = 0;
202}
203
204bool
205QMakeProject::parse(const QString &t, QMap<QString, QStringList> &place)
206{
207 QString s = t.simplifyWhiteSpace();
208 int hash_mark = s.find("#");
209 if(hash_mark != -1) //good bye comments
210 s = s.left(hash_mark);
211 if(s.isEmpty()) /* blank_line */
212 return TRUE;
213
214 if(s.stripWhiteSpace().left(1) == "}") {
215 debug_msg(1, "Project Parser: %s:%d : Leaving block %d", parser.file.latin1(),
216 parser.line_no, scope_block);
217 test_status = ((scope_flag & (0x01 << scope_block)) ? TestFound : TestSeek);
218 scope_block--;
219 s = s.mid(1).stripWhiteSpace();
220 if(s.isEmpty())
221 return TRUE;
222 }
223 if(!(scope_flag & (0x01 << scope_block))) {
224 /* adjust scope for each block which appears on a single line */
225 for(int i = (s.contains('{')-s.contains('}')); i>0; i--)
226 scope_flag &= ~(0x01 << (++scope_block));
227 debug_msg(1, "Project Parser: %s:%d : Ignored due to block being false.",
228 parser.file.latin1(), parser.line_no);
229 return TRUE;
230 }
231
232 QString scope, var, op;
233 QStringList val;
234#define SKIP_WS(d) while(*d && (*d == ' ' || *d == '\t')) d++
235 const char *d = s.latin1();
236 SKIP_WS(d);
237 bool scope_failed = FALSE, else_line = FALSE, or_op=FALSE;
238 int parens = 0, scope_count=0;
239 while(*d) {
240 if(!parens) {
241 if(*d == '=')
242 break;
243 if(*d == '+' || *d == '-' || *d == '*' || *d == '~') {
244 if(*(d+1) == '=') {
245 break;
246 } else if(*(d+1) == ' ') {
247 const char *k = d + 1;
248 SKIP_WS(k);
249 if(*k == '=') {
250 QString msg;
251 qmake_error_msg(*d + "must be followed immediately by =");
252 return FALSE;
253 }
254 }
255 }
256 }
257
258 if ( *d == '(' )
259 ++parens;
260 else if ( *d == ')' )
261 --parens;
262
263 if(!parens && (*d == ':' || *d == '{' || *d == ')' || *d == '|')) {
264 scope_count++;
265 scope = var.stripWhiteSpace();
266 if ( *d == ')' )
267 scope += *d; /* need this */
268 var = "";
269
270 bool test = scope_failed;
271 if(scope.lower() == "else") {
272 if(scope_count != 1 || test_status == TestNone) {
273 qmake_error_msg("Unexpected " + scope + " ('" + s + "')");
274 return FALSE;
275 }
276 else_line = TRUE;
277 test = (test_status == TestSeek);
278 debug_msg(1, "Project Parser: %s:%d : Else%s %s.", parser.file.latin1(), parser.line_no,
279 scope == "else" ? "" : QString(" (" + scope + ")").latin1(),
280 test ? "considered" : "excluded");
281 } else {
282 QString comp_scope = scope;
283 bool invert_test = (comp_scope.left(1) == "!");
284 if(invert_test)
285 comp_scope = comp_scope.right(comp_scope.length()-1);
286 int lparen = comp_scope.find('(');
287 if(or_op || !scope_failed) {
288 if(lparen != -1) { /* if there is an lparen in the scope, it IS a function */
289 int rparen = comp_scope.findRev(')');
290 if(rparen == -1) {
291 QCString error;
292 error.sprintf("Function missing right paren: %s ('%s')",
293 comp_scope.latin1(), s.latin1());
294 qmake_error_msg(error);
295 return FALSE;
296 }
297 QString func = comp_scope.left(lparen);
298 test = doProjectTest(func, comp_scope.mid(lparen+1, rparen - lparen - 1), place);
299 if ( *d == ')' && !*(d+1) ) {
300 if(invert_test)
301 test = !test;
302 test_status = (test ? TestFound : TestSeek);
303 return TRUE; /* assume we are done */
304 }
305 } else {
306 test = isActiveConfig(comp_scope.stripWhiteSpace(), TRUE, &place);
307 }
308 if(invert_test)
309 test = !test;
310 }
311 }
312 if(!test && !scope_failed)
313 debug_msg(1, "Project Parser: %s:%d : Test (%s) failed.", parser.file.latin1(),
314 parser.line_no, scope.latin1());
315 if(test == or_op)
316 scope_failed = !test;
317 or_op = (*d == '|');
318 if(*d == '{') { /* scoping block */
319 if(!scope_failed)
320 scope_flag |= (0x01 << (++scope_block));
321 else
322 scope_flag &= ~(0x01 << (++scope_block));
323 debug_msg(1, "Project Parser: %s:%d : Entering block %d (%d).", parser.file.latin1(),
324 parser.line_no, scope_block, !scope_failed);
325 }
326 } else if(!parens && *d == '}') {
327 if(!scope_block) {
328 warn_msg(WarnParser, "Possible braces mismatch %s:%d", parser.file.latin1(), parser.line_no);
329 } else {
330 debug_msg(1, "Project Parser: %s:%d : Leaving block %d", parser.file.latin1(),
331 parser.line_no, scope_block);
332 --scope_block;
333 }
334 } else {
335 var += *d;
336 }
337 d++;
338 }
339 var = var.stripWhiteSpace();
340 if(!scope_count || (scope_count == 1 && else_line))
341 test_status = TestNone;
342 else if(!else_line || test_status != TestFound)
343 test_status = (scope_failed ? TestSeek : TestFound);
344 if(scope_failed)
345 return TRUE; /* oh well */
346 if(!*d) {
347 if(!var.stripWhiteSpace().isEmpty())
348 qmake_error_msg("Parse Error ('" + s + "')");
349 return var.isEmpty(); /* allow just a scope */
350 }
351
352 SKIP_WS(d);
353 for( ; *d && op.find('=') == -1; op += *(d++))
354 ;
355 op.replace(QRegExp("\\s"), "");
356
357 SKIP_WS(d);
358 QString vals(d); /* vals now contains the space separated list of values */
359 int rbraces = vals.contains('}'), lbraces = vals.contains('{');
360 if(scope_block && rbraces - lbraces == 1) {
361 debug_msg(1, "Project Parser: %s:%d : Leaving block %d", parser.file.latin1(),
362 parser.line_no, scope_block);
363 test_status = ((scope_flag & (0x01 << scope_block)) ? TestFound : TestSeek);
364 scope_block--;
365 vals.truncate(vals.length()-1);
366 } else if(rbraces != lbraces) {
367 warn_msg(WarnParser, "Possible braces mismatch {%s} %s:%d",
368 vals.latin1(), parser.file.latin1(), parser.line_no);
369 }
370 doVariableReplace(vals, place);
371
372#undef SKIP_WS
373
374 if(!var.isEmpty() && Option::mkfile::do_preprocess) {
375 static QString last_file("*none*");
376 if(parser.file != last_file) {
377 fprintf(stdout, "#file %s:%d\n", parser.file.latin1(), parser.line_no);
378 last_file = parser.file;
379 }
380 fprintf(stdout, "%s %s %s\n", var.latin1(), op.latin1(), vals.latin1());
381 }
382 var = varMap(var); //backwards compatability
383
384 /* vallist is the broken up list of values */
385 QStringList vallist = split_value_list(vals, (var == "DEPENDPATH" || var == "INCLUDEPATH"));
386 if(!vallist.grep("=").isEmpty())
387 warn_msg(WarnParser, "Detected possible line continuation: {%s} %s:%d",
388 var.latin1(), parser.file.latin1(), parser.line_no);
389
390 QStringList &varlist = place[var]; /* varlist is the list in the symbol table */
391 debug_msg(1, "Project Parser: %s:%d :%s: :%s: (%s)", parser.file.latin1(), parser.line_no,
392 var.latin1(), op.latin1(), vallist.isEmpty() ? "" : vallist.join(" :: ").latin1());
393
394 /* now do the operation */
395 if(op == "~=") {
396 if(vallist.count() != 1) {
397 qmake_error_msg("~= operator only accepts one right hand paramater ('" +
398 s + "')");
399 return FALSE;
400 }
401 QString val(vallist.first());
402 if(val.length() < 4 || val.at(0) != 's') {
403 qmake_error_msg("~= operator only can handle s/// function ('" +
404 s + "')");
405 return FALSE;
406 }
407 QChar sep = val.at(1);
408 QStringList func = QStringList::split(sep, val, TRUE);
409 if(func.count() < 3 || func.count() > 4) {
410 qmake_error_msg("~= operator only can handle s/// function ('" +
411 s + "')");
412 return FALSE;
413 }
414 bool global = FALSE, case_sense = TRUE;
415 if(func.count() == 4) {
416 global = func[3].find('g') != -1;
417 case_sense = func[3].find('i') == -1;
418 }
419 QRegExp regexp(func[1], case_sense);
420 for(QStringList::Iterator varit = varlist.begin();
421 varit != varlist.end(); ++varit) {
422 if((*varit).contains(regexp)) {
423 (*varit) = (*varit).replace(regexp, func[2]);
424 if(!global)
425 break;
426 }
427 }
428 } else {
429 if(op == "=") {
430 if(!varlist.isEmpty())
431 warn_msg(WarnParser, "Operator=(%s) clears variables previously set: %s:%d",
432 var.latin1(), parser.file.latin1(), parser.line_no);
433 varlist.clear();
434 }
435 for(QStringList::Iterator valit = vallist.begin();
436 valit != vallist.end(); ++valit) {
437 if((*valit).isEmpty())
438 continue;
439 if((op == "*=" && !(*varlist.find((*valit)))) ||
440 op == "=" || op == "+=")
441 varlist.append((*valit));
442 else if(op == "-=")
443 varlist.remove((*valit));
444 }
445 }
446 if(var == "REQUIRES") /* special case to get communicated to backends! */
447 doProjectCheckReqs(vallist, place);
448
449 return TRUE;
450}
451
452bool
453QMakeProject::read(const QString &file, QMap<QString, QStringList> &place)
454{
455 parser_info pi = parser;
456 reset();
457
458 QString filename = Option::fixPathToLocalOS(file);
459 doVariableReplace(filename, place);
460 bool ret = FALSE, using_stdin = FALSE;
461 QFile qfile;
462 if(!strcmp(filename, "-")) {
463 qfile.setName("");
464 ret = qfile.open(IO_ReadOnly, stdin);
465 using_stdin = TRUE;
466 } else {
467 qfile.setName(filename);
468 ret = qfile.open(IO_ReadOnly);
469 }
470 if ( ret ) {
471 QTextStream t( &qfile );
472 QString s, line;
473 parser.file = filename;
474 parser.line_no = 0;
475 while ( !t.eof() ) {
476 parser.line_no++;
477 line = t.readLine().stripWhiteSpace();
478 int prelen = line.length();
479
480 int hash_mark = line.find("#");
481 if(hash_mark != -1) //good bye comments
482 line = line.left(hash_mark);
483 if(!line.isEmpty() && line.right(1) == "\\") {
484 if (!line.startsWith("#")) {
485 line.truncate(line.length() - 1);
486 s += line + " ";
487 }
488 } else if(!line.isEmpty() || (line.isEmpty() && !prelen)) {
489 if(s.isEmpty() && line.isEmpty())
490 continue;
491 if(!line.isEmpty())
492 s += line;
493 if(!s.isEmpty()) {
494 if(!(ret = parse(s, place)))
495 break;
496 s = "";
497 }
498 }
499 }
500 if(!using_stdin)
501 qfile.close();
502 }
503 parser = pi;
504 if(scope_block != 0)
505 warn_msg(WarnParser, "%s: Unterminated conditional at end of file.",
506 file.latin1());
507 return ret;
508}
509
510bool
511QMakeProject::read(const QString &project, const QString &, uchar cmd)
512{
513 pfile = project;
514 return read(cmd);
515}
516
517bool
518QMakeProject::read(uchar cmd)
519{
520 if(cfile.isEmpty()) {
521 // hack to get the Option stuff in there
522 base_vars["QMAKE_EXT_CPP"] = Option::cpp_ext;
523 base_vars["QMAKE_EXT_H"] = Option::h_ext;
524 if(!Option::user_template_prefix.isEmpty())
525 base_vars["TEMPLATE_PREFIX"] = Option::user_template_prefix;
526
527 if(cmd & ReadCache && Option::mkfile::do_cache) { /* parse the cache */
528 if(Option::mkfile::cachefile.isEmpty()) { //find it as it has not been specified
529 QString dir = QDir::convertSeparators(Option::output_dir);
530 while(!QFile::exists((Option::mkfile::cachefile = dir +
531 QDir::separator() + ".qmake.cache"))) {
532 dir = dir.left(dir.findRev(QDir::separator()));
533 if(dir.isEmpty() || dir.find(QDir::separator()) == -1) {
534 Option::mkfile::cachefile = "";
535 break;
536 }
537 if(Option::mkfile::cachefile_depth == -1)
538 Option::mkfile::cachefile_depth = 1;
539 else
540 Option::mkfile::cachefile_depth++;
541 }
542 }
543 if(!Option::mkfile::cachefile.isEmpty()) {
544 read(Option::mkfile::cachefile, cache);
545 if(Option::mkfile::qmakespec.isEmpty() && !cache["QMAKESPEC"].isEmpty())
546 Option::mkfile::qmakespec = cache["QMAKESPEC"].first();
547 }
548 }
549 if(cmd & ReadConf) { /* parse mkspec */
550 QStringList mkspec_roots = qmake_mkspec_paths();
551 if(Option::mkfile::qmakespec.isEmpty()) {
552 for(QStringList::Iterator it = mkspec_roots.begin(); it != mkspec_roots.end(); ++it) {
553 QString mkspec = (*it) + QDir::separator() + "default";
554 if(QFile::exists(mkspec)) {
555 Option::mkfile::qmakespec = mkspec;
556 break;
557 }
558 }
559 if(Option::mkfile::qmakespec.isEmpty()) {
560 fprintf(stderr, "QMAKESPEC has not been set, so configuration cannot be deduced.\n");
561 return FALSE;
562 }
563 }
564
565 if(QDir::isRelativePath(Option::mkfile::qmakespec)) {
566 bool found_mkspec = FALSE;
567 for(QStringList::Iterator it = mkspec_roots.begin(); it != mkspec_roots.end(); ++it) {
568 QString mkspec = (*it) + QDir::separator() + Option::mkfile::qmakespec;
569 if(QFile::exists(mkspec)) {
570 found_mkspec = TRUE;
571 Option::mkfile::qmakespec = mkspec;
572 break;
573 }
574 }
575 if(!found_mkspec) {
576 fprintf(stderr, "Could not find mkspecs for your QMAKESPEC after trying:\n\t%s\n",
577 mkspec_roots.join("\n\t").latin1());
578 return FALSE;
579 }
580 }
581
582 /* parse qmake configuration */
583 while(Option::mkfile::qmakespec.endsWith(QString(QChar(QDir::separator()))))
584 Option::mkfile::qmakespec.truncate(Option::mkfile::qmakespec.length()-1);
585 QString spec = Option::mkfile::qmakespec + QDir::separator() + "qmake.conf";
586 if(!QFile::exists(spec) &&
587 QFile::exists(Option::mkfile::qmakespec + QDir::separator() + "tmake.conf"))
588 spec = Option::mkfile::qmakespec + QDir::separator() + "tmake.conf";
589 debug_msg(1, "QMAKESPEC conf: reading %s", spec.latin1());
590 if(!read(spec, base_vars)) {
591 fprintf(stderr, "Failure to read QMAKESPEC conf file %s.\n", spec.latin1());
592 return FALSE;
593 }
594 if(Option::mkfile::do_cache && !Option::mkfile::cachefile.isEmpty()) {
595 debug_msg(1, "QMAKECACHE file: reading %s", Option::mkfile::cachefile.latin1());
596 read(Option::mkfile::cachefile, base_vars);
597 }
598 }
599 if(cmd & ReadCmdLine) {
600 /* commandline */
601 cfile = pfile;
602 parser.line_no = 1; //really arg count now.. duh
603 parser.file = "(internal)";
604 reset();
605 for(QStringList::Iterator it = Option::before_user_vars.begin();
606 it != Option::before_user_vars.end(); ++it) {
607 if(!parse((*it), base_vars)) {
608 fprintf(stderr, "Argument failed to parse: %s\n", (*it).latin1());
609 return FALSE;
610 }
611 parser.line_no++;
612 }
613 }
614 }
615
616 vars = base_vars; /* start with the base */
617
618 if(cmd & ReadProFile) { /* parse project file */
619 debug_msg(1, "Project file: reading %s", pfile.latin1());
620 if(pfile != "-" && !QFile::exists(pfile) && !pfile.endsWith(".pro"))
621 pfile += ".pro";
622 if(!read(pfile, vars))
623 return FALSE;
624 }
625
626 if(cmd & ReadPostFiles) { /* parse post files */
627 const QStringList l = vars["QMAKE_POST_INCLUDE_FILES"];
628 for(QStringList::ConstIterator it = l.begin(); it != l.end(); ++it)
629 read((*it), vars);
630 }
631
632 if(cmd & ReadCmdLine) {
633 parser.line_no = 1; //really arg count now.. duh
634 parser.file = "(internal)";
635 reset();
636 for(QStringList::Iterator it = Option::after_user_vars.begin();
637 it != Option::after_user_vars.end(); ++it) {
638 if(!parse((*it), vars)) {
639 fprintf(stderr, "Argument failed to parse: %s\n", (*it).latin1());
640 return FALSE;
641 }
642 parser.line_no++;
643 }
644 }
645
646 /* now let the user override the template from an option.. */
647 if(!Option::user_template.isEmpty()) {
648 debug_msg(1, "Overriding TEMPLATE (%s) with: %s", vars["TEMPLATE"].first().latin1(),
649 Option::user_template.latin1());
650 vars["TEMPLATE"].clear();
651 vars["TEMPLATE"].append(Option::user_template);
652 }
653
654 QStringList &templ = vars["TEMPLATE"];
655 if(templ.isEmpty())
656 templ.append(QString("app"));
657 else if(vars["TEMPLATE"].first().endsWith(".t"))
658 templ = QStringList(templ.first().left(templ.first().length() - 2));
659 if ( !Option::user_template_prefix.isEmpty() )
660 templ.first().prepend(Option::user_template_prefix);
661
662 if(vars["TARGET"].isEmpty()) {
663 // ### why not simply use:
664 // QFileInfo fi(pfile);
665 // fi.baseName();
666 QString tmp = pfile;
667 if(tmp.findRev('/') != -1)
668 tmp = tmp.right( tmp.length() - tmp.findRev('/') - 1 );
669 if(tmp.findRev('.') != -1)
670 tmp = tmp.left(tmp.findRev('.'));
671 vars["TARGET"].append(tmp);
672 }
673
674 QString test_version = getenv("QTESTVERSION");
675 if (!test_version.isEmpty()) {
676 QString s = vars["TARGET"].first();
677 if (s == "qt" || s == "qt-mt" || s == "qte" || s == "qte-mt") {
678 QString &ver = vars["VERSION"].first();
679// fprintf(stderr,"Current QT version number: " + ver + "\n");
680 if (ver != "" && ver != test_version) {
681 ver = test_version;
682 fprintf(stderr,"Changed QT version number to " + test_version + "!\n");
683 }
684 }
685 }
686 return TRUE;
687}
688
689bool
690QMakeProject::isActiveConfig(const QString &x, bool regex, QMap<QString, QStringList> *place)
691{
692 if(x.isEmpty())
693 return TRUE;
694
695 if((Option::target_mode == Option::TARG_MACX_MODE || Option::target_mode == Option::TARG_QNX6_MODE ||
696 Option::target_mode == Option::TARG_UNIX_MODE) && x == "unix")
697 return TRUE;
698 else if(Option::target_mode == Option::TARG_MACX_MODE && x == "macx")
699 return TRUE;
700 else if(Option::target_mode == Option::TARG_QNX6_MODE && x == "qnx6")
701 return TRUE;
702 else if(Option::target_mode == Option::TARG_MAC9_MODE && x == "mac9")
703 return TRUE;
704 else if((Option::target_mode == Option::TARG_MAC9_MODE || Option::target_mode == Option::TARG_MACX_MODE) &&
705 x == "mac")
706 return TRUE;
707 else if(Option::target_mode == Option::TARG_WIN_MODE && x == "win32")
708 return TRUE;
709
710
711 QRegExp re(x, FALSE, TRUE);
712 QString spec = Option::mkfile::qmakespec.right(Option::mkfile::qmakespec.length() -
713 (Option::mkfile::qmakespec.findRev(QDir::separator())+1));
714 if((regex && re.exactMatch(spec)) || (!regex && spec == x))
715 return TRUE;
716#ifdef Q_OS_UNIX
717 else if(spec == "default") {
718 static char *buffer = NULL;
719 if(!buffer)
720 buffer = (char *)malloc(1024);
721 int l = readlink(Option::mkfile::qmakespec, buffer, 1024);
722 if(l != -1) {
723 buffer[l] = '\0';
724 QString r = buffer;
725 if(r.findRev('/') != -1)
726 r = r.mid(r.findRev('/') + 1);
727 if((regex && re.exactMatch(r)) || (!regex && r == x))
728 return TRUE;
729 }
730 }
731#endif
732
733
734 QStringList &configs = (place ? (*place)["CONFIG"] : vars["CONFIG"]);
735 for(QStringList::Iterator it = configs.begin(); it != configs.end(); ++it) {
736 if((regex && re.exactMatch((*it))) || (!regex && (*it) == x))
737 if(re.exactMatch((*it)))
738 return TRUE;
739 }
740 return FALSE;
741}
742
743bool
744QMakeProject::doProjectTest(const QString& func, const QString &params, QMap<QString, QStringList> &place)
745{
746 QStringList args = split_arg_list(params);
747 for(QStringList::Iterator arit = args.begin(); arit != args.end(); ++arit) {
748 QString tmp = (*arit).stripWhiteSpace();
749 if((tmp[0] == '\'' || tmp[0] == '"') && tmp.right(1) == tmp.left(1))
750 tmp = tmp.mid(1, tmp.length() - 2);
751 }
752 return doProjectTest(func.stripWhiteSpace(), args, place);
753}
754
755bool
756QMakeProject::doProjectTest(const QString& func, QStringList args, QMap<QString, QStringList> &place)
757{
758 for(QStringList::Iterator arit = args.begin(); arit != args.end(); ++arit) {
759 (*arit) = (*arit).stripWhiteSpace(); // blah, get rid of space
760 doVariableReplace((*arit), place);
761 }
762 debug_msg(1, "Running project test: %s( %s )", func.latin1(), args.join("::").latin1());
763
764 if(func == "requires") {
765 return doProjectCheckReqs(args, place);
766 } else if(func == "equals") {
767 if(args.count() != 2) {
768 fprintf(stderr, "%s:%d: equals(variable, value) requires two arguments.\n", parser.file.latin1(),
769 parser.line_no);
770 return FALSE;
771 }
772 QString value = args[1];
773 if((value.left(1) == "\"" || value.left(1) == "'") && value.right(1) == value.left(1))
774 value = value.mid(1, value.length()-2);
775 return vars[args[0]].join(" ") == value;
776 } else if(func == "exists") {
777 if(args.count() != 1) {
778 fprintf(stderr, "%s:%d: exists(file) requires one argument.\n", parser.file.latin1(),
779 parser.line_no);
780 return FALSE;
781 }
782 QString file = args.first();
783 file = Option::fixPathToLocalOS(file);
784 doVariableReplace(file, place);
785
786 if(QFile::exists(file))
787 return TRUE;
788 //regular expression I guess
789 QString dirstr = QDir::currentDirPath();
790 int slsh = file.findRev(Option::dir_sep);
791 if(slsh != -1) {
792 dirstr = file.left(slsh+1);
793 file = file.right(file.length() - slsh - 1);
794 }
795 QDir dir(dirstr, file);
796 return dir.count() != 0;
797 } else if(func == "system") {
798 if(args.count() != 1) {
799 fprintf(stderr, "%s:%d: system(exec) requires one argument.\n", parser.file.latin1(),
800 parser.line_no);
801 return FALSE;
802 }
803 return system(args.first().latin1()) == 0;
804 } else if(func == "contains") {
805 if(args.count() != 2) {
806 fprintf(stderr, "%s:%d: contains(var, val) requires two arguments.\n", parser.file.latin1(),
807 parser.line_no);
808 return FALSE;
809 }
810 QRegExp regx(args[1]);
811 QStringList &l = place[args[0]];
812 for(QStringList::ConstIterator it = l.begin(); it != l.end(); ++it) {
813 if(regx.exactMatch((*it)))
814 return TRUE;
815 }
816 return FALSE;
817 } else if(func == "infile") {
818 if(args.count() < 2 || args.count() > 3) {
819 fprintf(stderr, "%s:%d: infile(file, var, val) requires at least 2 arguments.\n",
820 parser.file.latin1(), parser.line_no);
821 return FALSE;
822 }
823 QMakeProject proj;
824 QString file = args[0];
825 doVariableReplace(file, place);
826 fixEnvVariables(file);
827 int di = file.findRev(Option::dir_sep);
828 QDir sunworkshop42workaround = QDir::current();
829 QString oldpwd = sunworkshop42workaround.currentDirPath();
830 if(di != -1) {
831 if(!QDir::setCurrent(file.left(file.findRev(Option::dir_sep)))) {
832 fprintf(stderr, "Cannot find directory: %s\n", file.left(di).latin1());
833 return FALSE;
834 }
835 file = file.right(file.length() - di - 1);
836 }
837 parser_info pi = parser;
838 bool ret = !proj.read(file, oldpwd);
839 parser = pi;
840 if(ret) {
841 fprintf(stderr, "Error processing project file: %s\n", file.latin1());
842 QDir::setCurrent(oldpwd);
843 return FALSE;
844 }
845 if(args.count() == 2) {
846 ret = !proj.isEmpty(args[1]);
847 } else {
848 QRegExp regx(args[2]);
849 QStringList &l = proj.values(args[1]);
850 for(QStringList::ConstIterator it = l.begin(); it != l.end(); ++it) {
851 if(regx.exactMatch((*it))) {
852 ret = TRUE;
853 break;
854 }
855 }
856 }
857 QDir::setCurrent(oldpwd);
858 return ret;
859 } else if(func == "count") {
860 if(args.count() != 2) {
861 fprintf(stderr, "%s:%d: count(var, count) requires two arguments.\n", parser.file.latin1(),
862 parser.line_no);
863 return FALSE;
864 }
865 return vars[args[0]].count() == args[1].toUInt();
866 } else if(func == "isEmpty") {
867 if(args.count() != 1) {
868 fprintf(stderr, "%s:%d: isEmpty(var) requires one argument.\n", parser.file.latin1(),
869 parser.line_no);
870 return FALSE;
871 }
872 return vars[args[0]].isEmpty();
873 } else if(func == "include" || func == "load") {
874 QString seek_var;
875 if(args.count() == 2 && func == "include") {
876 seek_var = args[1];
877 } else if(args.count() != 1) {
878 QString func_desc = "include(file)";
879 if(func == "load")
880 func_desc = "load(feature)";
881 fprintf(stderr, "%s:%d: %s requires one argument.\n", parser.file.latin1(),
882 parser.line_no, func_desc.latin1());
883 return FALSE;
884 }
885
886 QString file = args.first();
887 file = Option::fixPathToLocalOS(file);
888 file.replace("\"", "");
889 doVariableReplace(file, place);
890 if(func == "load") {
891 if(!file.endsWith(Option::prf_ext))
892 file += Option::prf_ext;
893 if(file.find(Option::dir_sep) == -1 || !QFile::exists(file)) {
894 bool found = FALSE;
895 const QString concat = QDir::separator() + QString("mkspecs") +
896 QDir::separator() + QString("features");
897 QStringList feature_roots;
898 if(const char *mkspec_path = getenv("QMAKEFEATURES")) {
899#ifdef Q_OS_WIN
900 QStringList lst = QStringList::split(';', mkspec_path);
901 for(QStringList::Iterator it = lst.begin(); it != lst.end(); ++it)
902 feature_roots += QStringList::split(':', (*it));
903#else
904 feature_roots += QStringList::split(':', mkspec_path);
905#endif
906 }
907 if(const char *qmakepath = getenv("QMAKEPATH")) {
908#ifdef Q_OS_WIN
909 QStringList lst = QStringList::split(';', qmakepath);
910 for(QStringList::Iterator it = lst.begin(); it != lst.end(); ++it) {
911 QStringList lst2 = QStringList::split(':', (*it));
912 for(QStringList::Iterator it2 = lst2.begin(); it2 != lst2.end(); ++it2)
913 feature_roots << ((*it2) + concat);
914 }
915#else
916 QStringList lst = QStringList::split(':', qmakepath);
917 for(QStringList::Iterator it = lst.begin(); it != lst.end(); ++it)
918 feature_roots << ((*it) + concat);
919#endif
920 }
921 feature_roots << Option::mkfile::qmakespec;
922 if(const char *qtdir = getenv("QTDIR"))
923 feature_roots << (qtdir + concat);
924#ifdef QT_INSTALL_PREFIX
925 feature_roots << (QT_INSTALL_PREFIX + concat);
926#endif
927#if defined(HAVE_QCONFIG_CPP)
928 feature_roots << (qInstallPath() + concat);
929#endif
930#ifdef QT_INSTALL_DATA
931 feature_roots << (QT_INSTALL_DATA + concat);
932#endif
933#if defined(HAVE_QCONFIG_CPP)
934 feature_roots << (qInstallPathData() + concat);
935#endif
936 for(QStringList::Iterator it = feature_roots.begin(); it != feature_roots.end(); ++it) {
937 QString prf = (*it) + QDir::separator() + file;
938 if(QFile::exists(prf)) {
939 found = TRUE;
940 file = prf;
941 break;
942 }
943 }
944 if(!found) {
945 printf("Project LOAD(): Feature %s cannot be found.\n", args.first().latin1());
946 exit(3);
947 }
948 }
949 }
950 if(QDir::isRelativePath(file)) {
951 QStringList include_roots;
952 include_roots << Option::output_dir;
953 QString pfilewd = QFileInfo(parser.file).dirPath();
954 if(pfilewd.isEmpty())
955 include_roots << pfilewd;
956 if(Option::output_dir != QDir::currentDirPath())
957 include_roots << QDir::currentDirPath();
958 for(QStringList::Iterator it = include_roots.begin(); it != include_roots.end(); ++it) {
959 if(QFile::exists((*it) + QDir::separator() + file)) {
960 file = (*it) + QDir::separator() + file;
961 break;
962 }
963 }
964 }
965
966 if(Option::mkfile::do_preprocess) //nice to see this first..
967 fprintf(stderr, "#switching file %s(%s) - %s:%d\n", func.latin1(), file.latin1(),
968 parser.file.latin1(), parser.line_no);
969 debug_msg(1, "Project Parser: %s'ing file %s.", func.latin1(), file.latin1());
970 QString orig_file = file;
971 int di = file.findRev(Option::dir_sep);
972 QDir sunworkshop42workaround = QDir::current();
973 QString oldpwd = sunworkshop42workaround.currentDirPath();
974 if(di != -1) {
975 if(!QDir::setCurrent(file.left(file.findRev(Option::dir_sep)))) {
976 fprintf(stderr, "Cannot find directory: %s\n", file.left(di).latin1());
977 return FALSE;
978 }
979 file = file.right(file.length() - di - 1);
980 }
981 parser_info pi = parser;
982 int sb = scope_block;
983 int sf = scope_flag;
984 TestStatus sc = test_status;
985 bool r = FALSE;
986 if(!seek_var.isNull()) {
987 QMap<QString, QStringList> tmp;
988 if((r = read(file.latin1(), tmp)))
989 place[seek_var] += tmp[seek_var];
990 } else {
991 r = read(file.latin1(), place);
992 }
993 if(r)
994 vars["QMAKE_INTERNAL_INCLUDED_FILES"].append(orig_file);
995 else
996 warn_msg(WarnParser, "%s:%d: Failure to include file %s.",
997 pi.file.latin1(), pi.line_no, orig_file.latin1());
998 parser = pi;
999 test_status = sc;
1000 scope_flag = sf;
1001 scope_block = sb;
1002 QDir::setCurrent(oldpwd);
1003 return r;
1004 } else if(func == "error" || func == "message") {
1005 if(args.count() != 1) {
1006 fprintf(stderr, "%s:%d: %s(message) requires one argument.\n", parser.file.latin1(),
1007 parser.line_no, func.latin1());
1008 return FALSE;
1009 }
1010 QString msg = args.first();
1011 if((msg.startsWith("\"") || msg.startsWith("'")) && msg.endsWith(msg.left(1)))
1012 msg = msg.mid(1, msg.length()-2);
1013 msg.replace(QString("${QMAKE_FILE}"), parser.file.latin1());
1014 msg.replace(QString("${QMAKE_LINE_NUMBER}"), QString::number(parser.line_no));
1015 msg.replace(QString("${QMAKE_DATE}"), QDateTime::currentDateTime().toString());
1016 doVariableReplace(msg, place);
1017 fixEnvVariables(msg);
1018 fprintf(stderr, "Project %s: %s\n", func.upper().latin1(), msg.latin1());
1019 if(func == "message")
1020 return TRUE;
1021 exit(2);
1022 } else {
1023 fprintf(stderr, "%s:%d: Unknown test function: %s\n", parser.file.latin1(), parser.line_no,
1024 func.latin1());
1025 }
1026 return FALSE;
1027}
1028
1029bool
1030QMakeProject::doProjectCheckReqs(const QStringList &deps, QMap<QString, QStringList> &place)
1031{
1032 bool ret = FALSE;
1033 for(QStringList::ConstIterator it = deps.begin(); it != deps.end(); ++it) {
1034 QString chk = (*it);
1035 if(chk.isEmpty())
1036 continue;
1037 bool invert_test = (chk.left(1) == "!");
1038 if(invert_test)
1039 chk = chk.right(chk.length() - 1);
1040
1041 bool test;
1042 int lparen = chk.find('(');
1043 if(lparen != -1) { /* if there is an lparen in the chk, it IS a function */
1044 int rparen = chk.findRev(')');
1045 if(rparen == -1) {
1046 QCString error;
1047 error.sprintf("Function (in REQUIRES) missing right paren: %s", chk.latin1());
1048 qmake_error_msg(error);
1049 } else {
1050 QString func = chk.left(lparen);
1051 test = doProjectTest(func, chk.mid(lparen+1, rparen - lparen - 1), place);
1052 }
1053 } else {
1054 test = isActiveConfig(chk, TRUE, &place);
1055 }
1056 if(invert_test) {
1057 chk.prepend("!");
1058 test = !test;
1059 }
1060 if(!test) {
1061 debug_msg(1, "Project Parser: %s:%d Failed test: REQUIRES = %s",
1062 parser.file.latin1(), parser.line_no, chk.latin1());
1063 place["QMAKE_FAILED_REQUIREMENTS"].append(chk);
1064 ret = FALSE;
1065 }
1066 }
1067 return ret;
1068}
1069
1070
1071QString
1072QMakeProject::doVariableReplace(QString &str, const QMap<QString, QStringList> &place)
1073{
1074 for(int var_begin, var_last=0; (var_begin = str.find("$$", var_last)) != -1; var_last = var_begin) {
1075 if(var_begin >= (int)str.length() + 2) {
1076 break;
1077 } else if(var_begin != 0 && str[var_begin-1] == '\\') {
1078 str.replace(var_begin-1, 1, "");
1079 var_begin += 1;
1080 continue;
1081 }
1082
1083 int var_incr = var_begin + 2;
1084 bool in_braces = FALSE, as_env = FALSE, as_prop = FALSE;
1085 if(str[var_incr] == '{') {
1086 in_braces = TRUE;
1087 var_incr++;
1088 while(var_incr < (int)str.length() &&
1089 (str[var_incr] == ' ' || str[var_incr] == '\t' || str[var_incr] == '\n'))
1090 var_incr++;
1091 }
1092 if(str[var_incr] == '(') {
1093 as_env = TRUE;
1094 var_incr++;
1095 } else if(str[var_incr] == '[') {
1096 as_prop = TRUE;
1097 var_incr++;
1098 }
1099 QString val, args;
1100 while(var_incr < (int)str.length() &&
1101 (str[var_incr].isLetter() || str[var_incr].isNumber() || str[var_incr] == '.' || str[var_incr] == '_'))
1102 val += str[var_incr++];
1103 if(as_env) {
1104 if(str[var_incr] != ')') {
1105 var_incr++;
1106 warn_msg(WarnParser, "%s:%d: Unterminated env-variable replacement '%s' (%s)",
1107 parser.file.latin1(), parser.line_no,
1108 str.mid(var_begin, QMAX(var_incr - var_begin,
1109 (int)str.length())).latin1(), str.latin1());
1110 var_begin += var_incr;
1111 continue;
1112 }
1113 var_incr++;
1114 } else if(as_prop) {
1115 if(str[var_incr] != ']') {
1116 var_incr++;
1117 warn_msg(WarnParser, "%s:%d: Unterminated prop-variable replacement '%s' (%s)",
1118 parser.file.latin1(), parser.line_no,
1119 str.mid(var_begin, QMAX(var_incr - var_begin, int(str.length()))).latin1(), str.latin1());
1120 var_begin += var_incr;
1121 continue;
1122 }
1123 var_incr++;
1124 } else if(str[var_incr] == '(') { //args
1125 for(int parens = 0; var_incr < (int)str.length(); var_incr++) {
1126 if(str[var_incr] == '(') {
1127 parens++;
1128 if(parens == 1)
1129 continue;
1130 } else if(str[var_incr] == ')') {
1131 parens--;
1132 if(!parens) {
1133 var_incr++;
1134 break;
1135 }
1136 }
1137 args += str[var_incr];
1138 }
1139 }
1140 if(var_incr > (int)str.length() || (in_braces && str[var_incr] != '}')) {
1141 var_incr++;
1142 warn_msg(WarnParser, "%s:%d: Unterminated variable replacement '%s' (%s)",
1143 parser.file.latin1(), parser.line_no,
1144 str.mid(var_begin, QMAX(var_incr - var_begin,
1145 (int)str.length())).latin1(), str.latin1());
1146 var_begin += var_incr;
1147 continue;
1148 } else if(in_braces) {
1149 var_incr++;
1150 }
1151
1152 QString replacement;
1153 if(as_env) {
1154 replacement = getenv(val);
1155 } else if(as_prop) {
1156 if(prop)
1157 replacement = prop->value(val);
1158 } else if(args.isEmpty()) {
1159 if(val.left(1) == ".")
1160 replacement = "";
1161 else if(val == "LITERAL_WHITESPACE")
1162 replacement = "\t";
1163 else if(val == "LITERAL_DOLLAR")
1164 replacement = "$";
1165 else if(val == "LITERAL_HASH")
1166 replacement = "#";
1167 else if(val == "PWD")
1168 replacement = QDir::currentDirPath();
1169 else if(val == "DIR_SEPARATOR")
1170 replacement = Option::dir_sep;
1171 else
1172 replacement = place[varMap(val)].join(" ");
1173 } else {
1174 QStringList arg_list = split_arg_list(doVariableReplace(args, place));
1175 debug_msg(1, "Running function: %s( %s )", val.latin1(), arg_list.join("::").latin1());
1176 if(val.lower() == "member") {
1177 if(arg_list.count() < 1 || arg_list.count() > 3) {
1178 fprintf(stderr, "%s:%d: member(var, start, end) requires three arguments.\n",
1179 parser.file.latin1(), parser.line_no);
1180 } else {
1181 const QStringList &var = place[varMap(arg_list.first())];
1182 int start = 0;
1183 if(arg_list.count() >= 2)
1184 start = arg_list[1].toInt();
1185 if(start < 0)
1186 start += var.count();
1187 int end = start;
1188 if(arg_list.count() == 3)
1189 end = arg_list[2].toInt();
1190 if(end < 0)
1191 end += var.count();
1192 if(end < start)
1193 end = start;
1194 for(int i = start; i <= end && (int)var.count() >= i; i++) {
1195 if(!replacement.isEmpty())
1196 replacement += " ";
1197 replacement += var[i];
1198 }
1199 }
1200 } else if(val.lower() == "fromfile") {
1201 if(arg_list.count() != 2) {
1202 fprintf(stderr, "%s:%d: fromfile(file, variable) requires two arguments.\n",
1203 parser.file.latin1(), parser.line_no);
1204 } else {
1205 QString file = arg_list[0];
1206 file = Option::fixPathToLocalOS(file);
1207 file.replace("\"", "");
1208
1209 if(QDir::isRelativePath(file)) {
1210 QStringList include_roots;
1211 include_roots << Option::output_dir;
1212 QString pfilewd = QFileInfo(parser.file).dirPath();
1213 if(pfilewd.isEmpty())
1214 include_roots << pfilewd;
1215 if(Option::output_dir != QDir::currentDirPath())
1216 include_roots << QDir::currentDirPath();
1217 for(QStringList::Iterator it = include_roots.begin(); it != include_roots.end(); ++it) {
1218 if(QFile::exists((*it) + QDir::separator() + file)) {
1219 file = (*it) + QDir::separator() + file;
1220 break;
1221 }
1222 }
1223 }
1224 QString orig_file = file;
1225 int di = file.findRev(Option::dir_sep);
1226 QDir sunworkshop42workaround = QDir::current();
1227 QString oldpwd = sunworkshop42workaround.currentDirPath();
1228 if(di != -1 && QDir::setCurrent(file.left(file.findRev(Option::dir_sep))))
1229 file = file.right(file.length() - di - 1);
1230 parser_info pi = parser;
1231 int sb = scope_block;
1232 int sf = scope_flag;
1233 TestStatus sc = test_status;
1234 QMap<QString, QStringList> tmp;
1235 bool r = read(file.latin1(), tmp);
1236 if(r) {
1237 replacement = tmp[arg_list[1]].join(" ");
1238 vars["QMAKE_INTERNAL_INCLUDED_FILES"].append(orig_file);
1239 } else {
1240 warn_msg(WarnParser, "%s:%d: Failure to include file %s.",
1241 pi.file.latin1(), pi.line_no, orig_file.latin1());
1242 }
1243 parser = pi;
1244 test_status = sc;
1245 scope_flag = sf;
1246 scope_block = sb;
1247 QDir::setCurrent(oldpwd);
1248 }
1249 } else if(val.lower() == "eval") {
1250 for(QStringList::ConstIterator arg_it = arg_list.begin();
1251 arg_it != arg_list.end(); ++arg_it) {
1252 if(!replacement.isEmpty())
1253 replacement += " ";
1254 replacement += place[(*arg_it)].join(" ");
1255 }
1256 } else if(val.lower() == "list") {
1257 static int x = 0;
1258 replacement.sprintf(".QMAKE_INTERNAL_TMP_VAR_%d", x++);
1259 QStringList &lst = (*((QMap<QString, QStringList>*)&place))[replacement];
1260 lst.clear();
1261 for(QStringList::ConstIterator arg_it = arg_list.begin();
1262 arg_it != arg_list.end(); ++arg_it)
1263 lst += split_value_list((*arg_it));
1264 } else if(val.lower() == "join") {
1265 if(arg_list.count() < 1 || arg_list.count() > 4) {
1266 fprintf(stderr, "%s:%d: join(var, glue, before, after) requires four"
1267 "arguments.\n", parser.file.latin1(), parser.line_no);
1268 } else {
1269 QString glue, before, after;
1270 if(arg_list.count() >= 2)
1271 glue = arg_list[1].replace("\"", "" );
1272 if(arg_list.count() >= 3)
1273 before = arg_list[2].replace("\"", "" );
1274 if(arg_list.count() == 4)
1275 after = arg_list[3].replace("\"", "" );
1276 const QStringList &var = place[varMap(arg_list.first())];
1277 if(!var.isEmpty())
1278 replacement = before + var.join(glue) + after;
1279 }
1280 } else if(val.lower() == "split") {
1281 if(arg_list.count() < 2 || arg_list.count() > 3) {
1282 fprintf(stderr, "%s:%d split(var, sep, join) requires three arguments\n",
1283 parser.file.latin1(), parser.line_no);
1284 } else {
1285 QString sep = arg_list[1], join = " ";
1286 if(arg_list.count() == 3)
1287 join = arg_list[2];
1288 QStringList var = place[varMap(arg_list.first())];
1289 for(QStringList::Iterator vit = var.begin(); vit != var.end(); ++vit) {
1290 QStringList lst = QStringList::split(sep, (*vit));
1291 for(QStringList::Iterator spltit = lst.begin(); spltit != lst.end(); ++spltit) {
1292 if(!replacement.isEmpty())
1293 replacement += join;
1294 replacement += (*spltit);
1295 }
1296 }
1297 }
1298 } else if(val.lower() == "find") {
1299 if(arg_list.count() != 2) {
1300 fprintf(stderr, "%s:%d find(var, str) requires two arguments\n",
1301 parser.file.latin1(), parser.line_no);
1302 } else {
1303 QRegExp regx(arg_list[1]);
1304 const QStringList &var = place[varMap(arg_list.first())];
1305 for(QStringList::ConstIterator vit = var.begin();
1306 vit != var.end(); ++vit) {
1307 if(regx.search(*vit) != -1) {
1308 if(!replacement.isEmpty())
1309 replacement += " ";
1310 replacement += (*vit);
1311 }
1312 }
1313 }
1314 } else if(val.lower() == "system") {
1315 if(arg_list.count() != 1) {
1316 fprintf(stderr, "%s:%d system(execut) requires one argument\n",
1317 parser.file.latin1(), parser.line_no);
1318 } else {
1319 char buff[256];
1320 FILE *proc = QT_POPEN(arg_list.join(" ").latin1(), "r");
1321 while(proc && !feof(proc)) {
1322 int read_in = fread(buff, 1, 255, proc);
1323 if(!read_in)
1324 break;
1325 for(int i = 0; i < read_in; i++) {
1326 if(buff[i] == '\n' || buff[i] == '\t')
1327 buff[i] = ' ';
1328 }
1329 buff[read_in] = '\0';
1330 replacement += buff;
1331 }
1332 }
1333 } else if(val.lower() == "files") {
1334 if(arg_list.count() != 1) {
1335 fprintf(stderr, "%s:%d files(pattern) requires one argument\n",
1336 parser.file.latin1(), parser.line_no);
1337 } else {
1338 QString dir, regex = arg_list[0];
1339 regex = Option::fixPathToLocalOS(regex);
1340 regex.replace("\"", "");
1341 if(regex.findRev(QDir::separator()) != -1) {
1342 dir = regex.left(regex.findRev(QDir::separator()) + 1);
1343 regex = regex.right(regex.length() - dir.length());
1344 }
1345 QDir d(dir, regex);
1346 for(int i = 0; i < (int)d.count(); i++) {
1347 if(!replacement.isEmpty())
1348 replacement += " ";
1349 replacement += dir + d[i];
1350 }
1351 }
1352 } else if(val.lower() == "prompt") {
1353 if(arg_list.count() != 1) {
1354 fprintf(stderr, "%s:%d prompt(question) requires one argument\n",
1355 parser.file.latin1(), parser.line_no);
1356 } else if(projectFile() == "-") {
1357 fprintf(stderr, "%s:%d prompt(question) cannot be used when '-o -' is used.\n",
1358 parser.file.latin1(), parser.line_no);
1359 } else {
1360 QString msg = arg_list.first();
1361 if((msg.startsWith("\"") || msg.startsWith("'")) && msg.endsWith(msg.left(1)))
1362 msg = msg.mid(1, msg.length()-2);
1363 msg.replace(QString("${QMAKE_FILE}"), parser.file.latin1());
1364 msg.replace(QString("${QMAKE_LINE_NUMBER}"), QString::number(parser.line_no));
1365 msg.replace(QString("${QMAKE_DATE}"), QDateTime::currentDateTime().toString());
1366 doVariableReplace(msg, place);
1367 fixEnvVariables(msg);
1368 if(!msg.endsWith("?"))
1369 msg += "?";
1370 fprintf(stderr, "Project %s: %s ", val.upper().latin1(), msg.latin1());
1371
1372 QFile qfile;
1373 if(qfile.open(IO_ReadOnly, stdin)) {
1374 QTextStream t(&qfile);
1375 replacement = t.readLine();
1376 }
1377 }
1378 } else {
1379 fprintf(stderr, "%s:%d: Unknown replace function: %s\n",
1380 parser.file.latin1(), parser.line_no, val.latin1());
1381 }
1382 }
1383 //actually do replacement now..
1384 int mlen = var_incr - var_begin;
1385 debug_msg(2, "Project Parser [var replace]: '%s' :: %s -> %s", str.latin1(),
1386 str.mid(var_begin, mlen).latin1(), replacement.latin1());
1387 str.replace(var_begin, mlen, replacement);
1388 var_begin += replacement.length();
1389 }
1390 return str;
1391}
Note: See TracBrowser for help on using the repository browser.