1
/****************************************************************************
2
**
3
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
6
**
7
** This file is part of the qmake application of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** No Commercial Usage
11
** This file contains pre-release code and may not be distributed.
12
** You may use this file in accordance with the terms and conditions
13
** contained in the Technology Preview License Agreement accompanying
14
** this package.
15
**
16
** GNU Lesser General Public License Usage
17
** Alternatively, this file may be used under the terms of the GNU Lesser
18
** General Public License version 2.1 as published by the Free Software
19
** Foundation and appearing in the file LICENSE.LGPL included in the
20
** packaging of this file.  Please review the following information to
21
** ensure the GNU Lesser General Public License version 2.1 requirements
22
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23
**
24
** In addition, as a special exception, Nokia gives you certain additional
25
** rights.  These rights are described in the Nokia Qt LGPL Exception
26
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27
**
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
30
**
31
**
32
**
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
#include "project.h"
43
#include "property.h"
44
#include "option.h"
45
#include "cachekeys.h"
46
47
#include <qdatetime.h>
48
#include <qfile.h>
49
#include <qfileinfo.h>
50
#include <qdir.h>
51
#include <qregexp.h>
52
#include <qtextstream.h>
53
#include <qstack.h>
54
#include <qhash.h>
55
#include <qdebug.h>
56
#ifdef Q_OS_UNIX
57
#include <unistd.h>
58
#include <sys/utsname.h>
59
#elif defined(Q_OS_WIN32)
60
#include <windows.h>
61
#endif
62
#include <stdio.h>
63
#include <stdlib.h>
64
65
// Included from tools/shared
66
#include <symbian/epocroot.h>
67
68
#ifdef Q_OS_WIN32
69
#define QT_POPEN _popen
70
#define QT_PCLOSE _pclose
71
#else
72
#define QT_POPEN popen
73
#define QT_PCLOSE pclose
74
#endif
75
76
QT_BEGIN_NAMESPACE
77
78
//expand fucntions
79
enum ExpandFunc { E_MEMBER=1, E_FIRST, E_LAST, E_CAT, E_FROMFILE, E_EVAL, E_LIST,
80
                  E_SPRINTF, E_JOIN, E_SPLIT, E_BASENAME, E_DIRNAME, E_SECTION,
81
                  E_FIND, E_SYSTEM, E_UNIQUE, E_QUOTE, E_ESCAPE_EXPAND,
82
                  E_UPPER, E_LOWER, E_FILES, E_PROMPT, E_RE_ESCAPE, E_REPLACE,
83
                  E_SIZE, E_GENERATE_UID };
84
QMap<QString, ExpandFunc> qmake_expandFunctions()
85
{
86
    static QMap<QString, ExpandFunc> *qmake_expand_functions = 0;
87
    if(!qmake_expand_functions) {
88
        qmake_expand_functions = new QMap<QString, ExpandFunc>;
89
        qmakeAddCacheClear(qmakeDeleteCacheClear_QMapStringInt, (void**)&qmake_expand_functions);
90
        qmake_expand_functions->insert("member", E_MEMBER);
91
        qmake_expand_functions->insert("first", E_FIRST);
92
        qmake_expand_functions->insert("last", E_LAST);
93
        qmake_expand_functions->insert("cat", E_CAT);
94
        qmake_expand_functions->insert("fromfile", E_FROMFILE);
95
        qmake_expand_functions->insert("eval", E_EVAL);
96
        qmake_expand_functions->insert("list", E_LIST);
97
        qmake_expand_functions->insert("sprintf", E_SPRINTF);
98
        qmake_expand_functions->insert("join", E_JOIN);
99
        qmake_expand_functions->insert("split", E_SPLIT);
100
        qmake_expand_functions->insert("basename", E_BASENAME);
101
        qmake_expand_functions->insert("dirname", E_DIRNAME);
102
        qmake_expand_functions->insert("section", E_SECTION);
103
        qmake_expand_functions->insert("find", E_FIND);
104
        qmake_expand_functions->insert("system", E_SYSTEM);
105
        qmake_expand_functions->insert("unique", E_UNIQUE);
106
        qmake_expand_functions->insert("quote", E_QUOTE);
107
        qmake_expand_functions->insert("escape_expand", E_ESCAPE_EXPAND);
108
        qmake_expand_functions->insert("upper", E_UPPER);
109
        qmake_expand_functions->insert("lower", E_LOWER);
110
        qmake_expand_functions->insert("re_escape", E_RE_ESCAPE);
111
        qmake_expand_functions->insert("files", E_FILES);
112
        qmake_expand_functions->insert("prompt", E_PROMPT);
113
        qmake_expand_functions->insert("replace", E_REPLACE);
114
        qmake_expand_functions->insert("size", E_SIZE);
115
        qmake_expand_functions->insert("generate_uid", E_GENERATE_UID);
116
    }
117
    return *qmake_expand_functions;
118
}
119
//replace functions
120
enum TestFunc { T_REQUIRES=1, T_GREATERTHAN, T_LESSTHAN, T_EQUALS,
121
                T_EXISTS, T_EXPORT, T_CLEAR, T_UNSET, T_EVAL, T_CONFIG, T_SYSTEM,
122
                T_RETURN, T_BREAK, T_NEXT, T_DEFINED, T_CONTAINS, T_INFILE,
123
                T_COUNT, T_ISEMPTY, T_INCLUDE, T_LOAD, T_DEBUG, T_ERROR,
124
                T_MESSAGE, T_WARNING, T_IF };
125
QMap<QString, TestFunc> qmake_testFunctions()
126
{
127
    static QMap<QString, TestFunc> *qmake_test_functions = 0;
128
    if(!qmake_test_functions) {
129
        qmake_test_functions = new QMap<QString, TestFunc>;
130
        qmake_test_functions->insert("requires", T_REQUIRES);
131
        qmake_test_functions->insert("greaterThan", T_GREATERTHAN);
132
        qmake_test_functions->insert("lessThan", T_LESSTHAN);
133
        qmake_test_functions->insert("equals", T_EQUALS);
134
        qmake_test_functions->insert("isEqual", T_EQUALS);
135
        qmake_test_functions->insert("exists", T_EXISTS);
136
        qmake_test_functions->insert("export", T_EXPORT);
137
        qmake_test_functions->insert("clear", T_CLEAR);
138
        qmake_test_functions->insert("unset", T_UNSET);
139
        qmake_test_functions->insert("eval", T_EVAL);
140
        qmake_test_functions->insert("CONFIG", T_CONFIG);
141
        qmake_test_functions->insert("if", T_IF);
142
        qmake_test_functions->insert("isActiveConfig", T_CONFIG);
143
        qmake_test_functions->insert("system", T_SYSTEM);
144
        qmake_test_functions->insert("return", T_RETURN);
145
        qmake_test_functions->insert("break", T_BREAK);
146
        qmake_test_functions->insert("next", T_NEXT);
147
        qmake_test_functions->insert("defined", T_DEFINED);
148
        qmake_test_functions->insert("contains", T_CONTAINS);
149
        qmake_test_functions->insert("infile", T_INFILE);
150
        qmake_test_functions->insert("count", T_COUNT);
151
        qmake_test_functions->insert("isEmpty", T_ISEMPTY);
152
        qmake_test_functions->insert("include", T_INCLUDE);
153
        qmake_test_functions->insert("load", T_LOAD);
154
        qmake_test_functions->insert("debug", T_DEBUG);
155
        qmake_test_functions->insert("error", T_ERROR);
156
        qmake_test_functions->insert("message", T_MESSAGE);
157
        qmake_test_functions->insert("warning", T_WARNING);
158
    }
159
    return *qmake_test_functions;
160
}
161
162
struct parser_info {
163
    QString file;
164
    int line_no;
165
    bool from_file;
166
} parser;
167
168
static QString remove_quotes(const QString &arg)
169
{
170
    const ushort SINGLEQUOTE = '\'';
171
    const ushort DOUBLEQUOTE = '"';
172
173
    const QChar *arg_data = arg.data();
174
    const ushort first = arg_data->unicode();
175
    const int arg_len = arg.length();
176
    if(first == SINGLEQUOTE || first == DOUBLEQUOTE) {
177
        const ushort last = (arg_data+arg_len-1)->unicode();
178
        if(last == first)
179
            return arg.mid(1, arg_len-2);
180
    }
181
    return arg;
182
}
183
184
static QString varMap(const QString &x)
185
{
186
    QString ret(x);
187
    if(ret.startsWith("TMAKE")) //tmake no more!
188
        ret = "QMAKE" + ret.mid(5);
189
    else if(ret == "INTERFACES")
190
        ret = "FORMS";
191
    else if(ret == "QMAKE_POST_BUILD")
192
        ret = "QMAKE_POST_LINK";
193
    else if(ret == "TARGETDEPS")
194
        ret = "POST_TARGETDEPS";
195
    else if(ret == "LIBPATH")
196
        ret = "QMAKE_LIBDIR";
197
    else if(ret == "QMAKE_EXT_MOC")
198
        ret = "QMAKE_EXT_CPP_MOC";
199
    else if(ret == "QMAKE_MOD_MOC")
200
        ret = "QMAKE_H_MOD_MOC";
201
    else if(ret == "QMAKE_LFLAGS_SHAPP")
202
        ret = "QMAKE_LFLAGS_APP";
203
    else if(ret == "PRECOMPH")
204
        ret = "PRECOMPILED_HEADER";
205
    else if(ret == "PRECOMPCPP")
206
        ret = "PRECOMPILED_SOURCE";
207
    else if(ret == "INCPATH")
208
        ret = "INCLUDEPATH";
209
    else if(ret == "QMAKE_EXTRA_WIN_COMPILERS" || ret == "QMAKE_EXTRA_UNIX_COMPILERS")
210
        ret = "QMAKE_EXTRA_COMPILERS";
211
    else if(ret == "QMAKE_EXTRA_WIN_TARGETS" || ret == "QMAKE_EXTRA_UNIX_TARGETS")
212
        ret = "QMAKE_EXTRA_TARGETS";
213
    else if(ret == "QMAKE_EXTRA_UNIX_INCLUDES")
214
        ret = "QMAKE_EXTRA_INCLUDES";
215
    else if(ret == "QMAKE_EXTRA_UNIX_VARIABLES")
216
        ret = "QMAKE_EXTRA_VARIABLES";
217
    else if(ret == "QMAKE_RPATH")
218
        ret = "QMAKE_LFLAGS_RPATH";
219
    else if(ret == "QMAKE_FRAMEWORKDIR")
220
        ret = "QMAKE_FRAMEWORKPATH";
221
    else if(ret == "QMAKE_FRAMEWORKDIR_FLAGS")
222
        ret = "QMAKE_FRAMEWORKPATH_FLAGS";
223
    return ret;
224
}
225
226
static QStringList split_arg_list(QString params)
227
{
228
    int quote = 0;
229
    QStringList args;
230
231
    const ushort LPAREN = '(';
232
    const ushort RPAREN = ')';
233
    const ushort SINGLEQUOTE = '\'';
234
    const ushort DOUBLEQUOTE = '"';
235
    const ushort COMMA = ',';
236
    const ushort SPACE = ' ';
237
    //const ushort TAB = '\t';
238
239
    ushort unicode;
240
    const QChar *params_data = params.data();
241
    const int params_len = params.length();
242
    int last = 0;
243
    while(last < params_len && (params_data[last].unicode() == SPACE
244
                                /*|| params_data[last].unicode() == TAB*/))
245
        ++last;
246
    for(int x = last, parens = 0; x <= params_len; x++) {
247
        unicode = params_data[x].unicode();
248
        if(x == params_len) {
249
            while(x && params_data[x-1].unicode() == SPACE)
250
                --x;
251
            QString mid(params_data+last, x-last);
252
            if(quote) {
253
                if(mid[0] == quote && mid[(int)mid.length()-1] == quote)
254
                    mid = mid.mid(1, mid.length()-2);
255
                quote = 0;
256
            }
257
            args << mid;
258
            break;
259
        }
260
        if(unicode == LPAREN) {
261
            --parens;
262
        } else if(unicode == RPAREN) {
263
            ++parens;
264
        } else if(quote && unicode == quote) {
265
            quote = 0;
266
        } else if(!quote && (unicode == SINGLEQUOTE || unicode == DOUBLEQUOTE)) {
267
            quote = unicode;
268
        }
269
        if(!parens && !quote && unicode == COMMA) {
270
            QString mid = params.mid(last, x - last).trimmed();
271
            args << mid;
272
            last = x+1;
273
            while(last < params_len && (params_data[last].unicode() == SPACE
274
                                        /*|| params_data[last].unicode() == TAB*/))
275
                ++last;
276
        }
277
    }
278
    return args;
279
}
280
281
static QStringList split_value_list(const QString &vals, bool do_semicolon=false)
282
{
283
    QString build;
284
    QStringList ret;
285
    QStack<char> quote;
286
287
    const ushort LPAREN = '(';
288
    const ushort RPAREN = ')';
289
    const ushort SINGLEQUOTE = '\'';
290
    const ushort DOUBLEQUOTE = '"';
291
    const ushort BACKSLASH = '\\';
292
    const ushort SEMICOLON = ';';
293
294
    ushort unicode;
295
    const QChar *vals_data = vals.data();
296
    const int vals_len = vals.length();
297
    for(int x = 0, parens = 0; x < vals_len; x++) {
298
        unicode = vals_data[x].unicode();
299
        if(x != (int)vals_len-1 && unicode == BACKSLASH &&
300
            (vals_data[x+1].unicode() == SINGLEQUOTE || vals_data[x+1].unicode() == DOUBLEQUOTE)) {
301
            build += vals_data[x++]; //get that 'escape'
302
        } else if(!quote.isEmpty() && unicode == quote.top()) {
303
            quote.pop();
304
        } else if(unicode == SINGLEQUOTE || unicode == DOUBLEQUOTE) {
305
            quote.push(unicode);
306
        } else if(unicode == RPAREN) {
307
            --parens;
308
        } else if(unicode == LPAREN) {
309
            ++parens;
310
        }
311
312
        if(!parens && quote.isEmpty() && ((do_semicolon && unicode == SEMICOLON) ||
313
                                           vals_data[x] == Option::field_sep)) {
314
            ret << build;
315
            build.clear();
316
        } else {
317
            build += vals_data[x];
318
        }
319
    }
320
    if(!build.isEmpty())
321
        ret << build;
322
    return ret;
323
}
324
325
//just a parsable entity
326
struct ParsableBlock
327
{
328
    ParsableBlock() : ref_cnt(1) { }
329
    virtual ~ParsableBlock() { }
330
331
    struct Parse {
332
        QString text;
333
        parser_info pi;
334
        Parse(const QString &t) : text(t){ pi = parser; }
335
    };
336
    QList<Parse> parselist;
337
338
    inline int ref() { return ++ref_cnt; }
339
    inline int deref() { return --ref_cnt; }
340
341
protected:
342
    int ref_cnt;
343
    virtual bool continueBlock() = 0;
344
    bool eval(QMakeProject *p, QMap<QString, QStringList> &place);
345
};
346
347
bool ParsableBlock::eval(QMakeProject *p, QMap<QString, QStringList> &place)
348
{
349
    //save state
350
    parser_info pi = parser;
351
    const int block_count = p->scope_blocks.count();
352
353
    //execute
354
    bool ret = true;
355
    for(int i = 0; i < parselist.count(); i++) {
356
        parser = parselist.at(i).pi;
357
        if(!(ret = p->parse(parselist.at(i).text, place)) || !continueBlock())
358
            break;
359
    }
360
361
    //restore state
362
    parser = pi;
363
    while(p->scope_blocks.count() > block_count)
364
        p->scope_blocks.pop();
365
    return ret;
366
}
367
368
//defined functions
369
struct FunctionBlock : public ParsableBlock
370
{
371
    FunctionBlock() : calling_place(0), scope_level(1), cause_return(false) { }
372
373
    QMap<QString, QStringList> vars;
374
    QMap<QString, QStringList> *calling_place;
375
    QStringList return_value;
376
    int scope_level;
377
    bool cause_return;
378
379
    bool exec(const QList<QStringList> &args,
380
              QMakeProject *p, QMap<QString, QStringList> &place, QStringList &functionReturn);
381
    virtual bool continueBlock() { return !cause_return; }
382
};
383
384
bool FunctionBlock::exec(const QList<QStringList> &args,
385
                         QMakeProject *proj, QMap<QString, QStringList> &place,
386
                         QStringList &functionReturn)
387
{
388
    //save state
389
#if 1
390
    calling_place = &place;
391
#else
392
    calling_place = &proj->variables();
393
#endif
394
    return_value.clear();
395
    cause_return = false;
396
397
    //execute
398
#if 0
399
    vars = proj->variables(); // should be place so that local variables can be inherited
400
#else
401
    vars = place;
402
#endif
403
    vars["ARGS"].clear();
404
    for(int i = 0; i < args.count(); i++) {
405
        vars["ARGS"] += args[i];
406
        vars[QString::number(i+1)] = args[i];
407
    }
408
    bool ret = ParsableBlock::eval(proj, vars);
409
    functionReturn = return_value;
410
411
    //restore state
412
    calling_place = 0;
413
    return_value.clear();
414
    vars.clear();
415
    return ret;
416
}
417
418
//loops
419
struct IteratorBlock : public ParsableBlock
420
{
421
    IteratorBlock() : scope_level(1), loop_forever(false), cause_break(false), cause_next(false) { }
422
423
    int scope_level;
424
425
    struct Test {
426
        QString func;
427
        QStringList args;
428
        bool invert;
429
        parser_info pi;
430
        Test(const QString &f, QStringList &a, bool i) : func(f), args(a), invert(i) { pi = parser; }
431
    };
432
    QList<Test> test;
433
434
    QString variable;
435
436
    bool loop_forever, cause_break, cause_next;
437
    QStringList list;
438
439
    bool exec(QMakeProject *p, QMap<QString, QStringList> &place);
440
    virtual bool continueBlock() { return !cause_next && !cause_break; }
441
};
442
bool IteratorBlock::exec(QMakeProject *p, QMap<QString, QStringList> &place)
443
{
444
    bool ret = true;
445
    QStringList::Iterator it;
446
    if(!loop_forever)
447
        it = list.begin();
448
    int iterate_count = 0;
449
    //save state
450
    IteratorBlock *saved_iterator = p->iterator;
451
    p->iterator = this;
452
453
    //do the loop
454
    while(loop_forever || it != list.end()) {
455
        cause_next = cause_break = false;
456
        if(!loop_forever && (*it).isEmpty()) { //ignore empty items
457
            ++it;
458
            continue;
459
        }
460
461
        //set up the loop variable
462
        QStringList va;
463
        if(!variable.isEmpty()) {
464
            va = place[variable];
465
            if(loop_forever)
466
                place[variable] = QStringList(QString::number(iterate_count));
467
            else
468
                place[variable] = QStringList(*it);
469
        }
470
        //do the iterations
471
        bool succeed = true;
472
        for(QList<Test>::Iterator test_it = test.begin(); test_it != test.end(); ++test_it) {
473
            parser = (*test_it).pi;
474
            succeed = p->doProjectTest((*test_it).func, (*test_it).args, place);
475
            if((*test_it).invert)
476
                succeed = !succeed;
477
            if(!succeed)
478
                break;
479
        }
480
        if(succeed)
481
            ret = ParsableBlock::eval(p, place);
482
        //restore the variable in the map
483
        if(!variable.isEmpty())
484
            place[variable] = va;
485
        //loop counters
486
        if(!loop_forever)
487
            ++it;
488
        iterate_count++;
489
        if(!ret || cause_break)
490
            break;
491
    }
492
493
    //restore state
494
    p->iterator = saved_iterator;
495
    return ret;
496
}
497
498
QMakeProject::ScopeBlock::~ScopeBlock()
499
{
500
#if 0
501
    if(iterate)
502
        delete iterate;
503
#endif
504
}
505
506
static void qmake_error_msg(const QString &msg)
507
{
508
    fprintf(stderr, "%s:%d: %s\n", parser.file.toLatin1().constData(), parser.line_no,
509
            msg.toLatin1().constData());
510
}
511
512
enum isForSymbian_enum {
513
    isForSymbian_NOT_SET = -1,
514
    isForSymbian_FALSE = 0,
515
    isForSymbian_ABLD = 1,
516
    isForSymbian_SBSV2 = 2,
517
};
518
519
static isForSymbian_enum isForSymbian_value = isForSymbian_NOT_SET;
520
521
// Checking for symbian build is primarily determined from the qmake spec,
522
// but if that is not specified, detect if symbian is the default spec
523
// by checking the MAKEFILE_GENERATOR variable value.
524
static void init_symbian(const QMap<QString, QStringList>& vars)
525
{
526
    if (isForSymbian_value != isForSymbian_NOT_SET)
527
        return;
528
529
    QString spec = QFileInfo(Option::mkfile::qmakespec).fileName();
530
    if (spec.startsWith("symbian-abld", Qt::CaseInsensitive)) {
531
        isForSymbian_value = isForSymbian_ABLD;
532
    } else if (spec.startsWith("symbian-sbsv2", Qt::CaseInsensitive)) {
533
        isForSymbian_value = isForSymbian_SBSV2;
534
    } else {
535
        QStringList generatorList = vars["MAKEFILE_GENERATOR"];
536
537
        if (!generatorList.isEmpty()) {
538
            QString generator = generatorList.first();
539
            if (generator.startsWith("SYMBIAN_ABLD"))
540
                isForSymbian_value = isForSymbian_ABLD;
541
            else if (generator.startsWith("SYMBIAN_SBSV2"))
542
                isForSymbian_value = isForSymbian_SBSV2;
543
            else
544
                isForSymbian_value = isForSymbian_FALSE;
545
        } else {
546
            isForSymbian_value = isForSymbian_FALSE;
547
        }
548
    }
549
550
    // Force recursive on Symbian, as non-recursive is not really a viable option there
551
    if (isForSymbian_value != isForSymbian_FALSE)
552
        Option::recursive = true;
553
}
554
555
bool isForSymbian()
556
{
557
    // If isForSymbian_value has not been initialized explicitly yet,
558
    // call initializer with dummy map to check qmake spec.
559
    if (isForSymbian_value == isForSymbian_NOT_SET)
560
        init_symbian(QMap<QString, QStringList>());
561
562
    return (isForSymbian_value != isForSymbian_FALSE);
563
}
564
565
bool isForSymbianSbsv2()
566
{
567
    // If isForSymbian_value has not been initialized explicitly yet,
568
    // call initializer with dummy map to check qmake spec.
569
    if (isForSymbian_value == isForSymbian_NOT_SET)
570
        init_symbian(QMap<QString, QStringList>());
571
572
    return (isForSymbian_value == isForSymbian_SBSV2);
573
}
574
575
/*
576
   1) environment variable QMAKEFEATURES (as separated by colons)
577
   2) property variable QMAKEFEATURES (as separated by colons)
578
   3) <project_root> (where .qmake.cache lives) + FEATURES_DIR
579
   4) environment variable QMAKEPATH (as separated by colons) + /mkspecs/FEATURES_DIR
580
   5) your QMAKESPEC/features dir
581
   6) your data_install/mkspecs/FEATURES_DIR
582
   7) your QMAKESPEC/../FEATURES_DIR dir
583
584
   FEATURES_DIR is defined as:
585
586
   1) features/(unix|win32|macx)/
587
   2) features/
588
*/
589
QStringList qmake_feature_paths(QMakeProperty *prop=0)
590
{
591
    QStringList concat;
592
    {
593
        const QString base_concat = QDir::separator() + QString("features");
594
        switch(Option::target_mode) {
595
        case Option::TARG_MACX_MODE:                     //also a unix
596
            concat << base_concat + QDir::separator() + "mac";
597
            concat << base_concat + QDir::separator() + "macx";
598
            concat << base_concat + QDir::separator() + "unix";
599
            break;
600
        case Option::TARG_UNIX_MODE:
601
            {
602
                if (isForSymbian())
603
                    concat << base_concat + QDir::separator() + "symbian";
604
                else
605
                    concat << base_concat + QDir::separator() + "unix";
606
                break;
607
            }
608
        case Option::TARG_WIN_MODE:
609
            {
610
                if (isForSymbian())
611
                    concat << base_concat + QDir::separator() + "symbian";
612
                else
613
                    concat << base_concat + QDir::separator() + "win32";
614
                break;
615
            }
616
        case Option::TARG_MAC9_MODE:
617
            concat << base_concat + QDir::separator() + "mac";
618
            concat << base_concat + QDir::separator() + "mac9";
619
            break;
620
        }
621
        concat << base_concat;
622
    }
623
    const QString mkspecs_concat = QDir::separator() + QString("mkspecs");
624
    QStringList feature_roots;
625
    QByteArray mkspec_path = qgetenv("QMAKEFEATURES");
626
    if(!mkspec_path.isNull())
627
        feature_roots += splitPathList(QString::fromLocal8Bit(mkspec_path));
628
    if(prop)
629
        feature_roots += splitPathList(prop->value("QMAKEFEATURES"));
630
    if(!Option::mkfile::cachefile.isEmpty()) {
631
        QString path;
632
        int last_slash = Option::mkfile::cachefile.lastIndexOf(Option::dir_sep);
633
        if(last_slash != -1)
634
            path = Option::fixPathToLocalOS(Option::mkfile::cachefile.left(last_slash));
635
        for(QStringList::Iterator concat_it = concat.begin();
636
            concat_it != concat.end(); ++concat_it)
637
            feature_roots << (path + (*concat_it));
638
    }
639
    QByteArray qmakepath = qgetenv("QMAKEPATH");
640
    if (!qmakepath.isNull()) {
641
        const QStringList lst = splitPathList(QString::fromLocal8Bit(qmakepath));
642
        for(QStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it) {
643
            for(QStringList::Iterator concat_it = concat.begin();
644
                concat_it != concat.end(); ++concat_it)
645
                    feature_roots << ((*it) + mkspecs_concat + (*concat_it));
646
        }
647
    }
648
    if(!Option::mkfile::qmakespec.isEmpty())
649
        feature_roots << Option::mkfile::qmakespec + QDir::separator() + "features";
650
    if(!Option::mkfile::qmakespec.isEmpty()) {
651
        QFileInfo specfi(Option::mkfile::qmakespec);
652
        QDir specdir(specfi.absoluteFilePath());
653
        while(!specdir.isRoot()) {
654
            if(!specdir.cdUp() || specdir.isRoot())
655
                break;
656
            if(QFile::exists(specdir.path() + QDir::separator() + "features")) {
657
                for(QStringList::Iterator concat_it = concat.begin();
658
                    concat_it != concat.end(); ++concat_it)
659
                    feature_roots << (specdir.path() + (*concat_it));
660
                break;
661
            }
662
        }
663
    }
664
    for(QStringList::Iterator concat_it = concat.begin();
665
        concat_it != concat.end(); ++concat_it)
666
        feature_roots << (QLibraryInfo::location(QLibraryInfo::PrefixPath) +
667
                          mkspecs_concat + (*concat_it));
668
    for(QStringList::Iterator concat_it = concat.begin();
669
        concat_it != concat.end(); ++concat_it)
670
        feature_roots << (QLibraryInfo::location(QLibraryInfo::DataPath) +
671
                          mkspecs_concat + (*concat_it));
672
    return feature_roots;
673
}
674
675
QStringList qmake_mkspec_paths()
676
{
677
    QStringList ret;
678
    const QString concat = QDir::separator() + QString("mkspecs");
679
    QByteArray qmakepath = qgetenv("QMAKEPATH");
680
    if (!qmakepath.isEmpty()) {
681
        const QStringList lst = splitPathList(QString::fromLocal8Bit(qmakepath));
682
        for(QStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it)
683
            ret << ((*it) + concat);
684
    }
685
    ret << QLibraryInfo::location(QLibraryInfo::DataPath) + concat;
686
687
    return ret;
688
}
689
690
class QMakeProjectEnv
691
{
692
    QStringList envs;
693
public:
694
    QMakeProjectEnv() { }
695
    QMakeProjectEnv(QMakeProject *p) { execute(p->variables()); }
696
    QMakeProjectEnv(const QMap<QString, QStringList> &values) { execute(values); }
697
698
    void execute(QMakeProject *p) { execute(p->variables()); }
699
    void execute(const QMap<QString, QStringList> &values) {
700
#ifdef Q_OS_UNIX
701
        for(QMap<QString, QStringList>::ConstIterator it = values.begin(); it != values.end(); ++it) {
702
            const QString var = it.key(), val = it.value().join(" ");
703
            if(!var.startsWith(".")) {
704
                const QString env_var = Option::sysenv_mod + var;
705
                if(!putenv(strdup(QString(env_var + "=" + val).toAscii().data())))
706
                    envs.append(env_var);
707
            }
708
        }
709
#else
710
        Q_UNUSED(values);
711
#endif
712
    }
713
    ~QMakeProjectEnv() {
714
#ifdef Q_OS_UNIX
715
        for(QStringList::ConstIterator it = envs.begin();it != envs.end(); ++it) {
716
            putenv(strdup(QString(*it + "=").toAscii().data()));
717
        }
718
#endif
719
    }
720
};
721
722
QMakeProject::~QMakeProject()
723
{
724
    if(own_prop)
725
        delete prop;
726
    for(QMap<QString, FunctionBlock*>::iterator it = replaceFunctions.begin(); it != replaceFunctions.end(); ++it) {
727
        if(!it.value()->deref())
728
            delete it.value();
729
    }
730
    replaceFunctions.clear();
731
    for(QMap<QString, FunctionBlock*>::iterator it = testFunctions.begin(); it != testFunctions.end(); ++it) {
732
        if(!it.value()->deref())
733
            delete it.value();
734
    }
735
    testFunctions.clear();
736
}
737
738
739
void
740
QMakeProject::init(QMakeProperty *p, const QMap<QString, QStringList> *vars)
741
{
742
    if(vars)
743
        base_vars = *vars;
744
    if(!p) {
745
        prop = new QMakeProperty;
746
        own_prop = true;
747
    } else {
748
        prop = p;
749
        own_prop = false;
750
    }
751
    reset();
752
}
753
754
QMakeProject::QMakeProject(QMakeProject *p, const QMap<QString, QStringList> *vars)
755
{
756
    init(p->properties(), vars ? vars : &p->variables());
757
    for(QMap<QString, FunctionBlock*>::iterator it = p->replaceFunctions.begin(); it != p->replaceFunctions.end(); ++it) {
758
        it.value()->ref();
759
        replaceFunctions.insert(it.key(), it.value());
760
    }
761
    for(QMap<QString, FunctionBlock*>::iterator it = p->testFunctions.begin(); it != p->testFunctions.end(); ++it) {
762
        it.value()->ref();
763
        testFunctions.insert(it.key(), it.value());
764
    }
765
}
766
767
void
768
QMakeProject::reset()
769
{
770
    // scope_blocks starts with one non-ignoring entity
771
    scope_blocks.clear();
772
    scope_blocks.push(ScopeBlock());
773
    iterator = 0;
774
    function = 0;
775
}
776
777
bool
778
QMakeProject::parse(const QString &t, QMap<QString, QStringList> &place, int numLines)
779
{
780
    QString s = t.simplified();
781
    int hash_mark = s.indexOf("#");
782
    if(hash_mark != -1) //good bye comments
783
        s = s.left(hash_mark);
784
    if(s.isEmpty()) // blank_line
785
        return true;
786
787
    if(scope_blocks.top().ignore) {
788
        bool continue_parsing = false;
789
        // adjust scope for each block which appears on a single line
790
        for(int i = 0; i < s.length(); i++) {
791
            if(s[i] == '{') {
792
                scope_blocks.push(ScopeBlock(true));
793
            } else if(s[i] == '}') {
794
                if(scope_blocks.count() == 1) {
795
                    fprintf(stderr, "Braces mismatch %s:%d\n", parser.file.toLatin1().constData(), parser.line_no);
796
                    return false;
797
                }
798
                ScopeBlock sb = scope_blocks.pop();
799
                if(sb.iterate) {
800
                    sb.iterate->exec(this, place);
801
                    delete sb.iterate;
802
                    sb.iterate = 0;
803
                }
804
                if(!scope_blocks.top().ignore) {
805
                    debug_msg(1, "Project Parser: %s:%d : Leaving block %d", parser.file.toLatin1().constData(),
806
                              parser.line_no, scope_blocks.count()+1);
807
                    s = s.mid(i+1).trimmed();
808
                    continue_parsing = !s.isEmpty();
809
                    break;
810
                }
811
            }
812
        }
813
        if(!continue_parsing) {
814
            debug_msg(1, "Project Parser: %s:%d : Ignored due to block being false.",
815
                      parser.file.toLatin1().constData(), parser.line_no);
816
            return true;
817
        }
818
    }
819
820
    if(function) {
821
        QString append;
822
        int d_off = 0;
823
        const QChar *d = s.unicode();
824
        bool function_finished = false;
825
        while(d_off < s.length()) {
826
            if(*(d+d_off) == QLatin1Char('}')) {
827
                function->scope_level--;
828
                if(!function->scope_level) {
829
                    function_finished = true;
830
                    break;
831
                }
832
            } else if(*(d+d_off) == QLatin1Char('{')) {
833
                function->scope_level++;
834
            }
835
            append += *(d+d_off);
836
            ++d_off;
837
        }
838
        if(!append.isEmpty())
839
            function->parselist.append(IteratorBlock::Parse(append));
840
        if(function_finished) {
841
            function = 0;
842
            s = QString(d+d_off, s.length()-d_off);
843
        } else {
844
            return true;
845
        }
846
    } else if(IteratorBlock *it = scope_blocks.top().iterate) {
847
        QString append;
848
        int d_off = 0;
849
        const QChar *d = s.unicode();
850
        bool iterate_finished = false;
851
        while(d_off < s.length()) {
852
            if(*(d+d_off) == QLatin1Char('}')) {
853
                it->scope_level--;
854
                if(!it->scope_level) {
855
                    iterate_finished = true;
856
                    break;
857
                }
858
            } else if(*(d+d_off) == QLatin1Char('{')) {
859
                it->scope_level++;
860
            }
861
            append += *(d+d_off);
862
            ++d_off;
863
        }
864
        if(!append.isEmpty())
865
            scope_blocks.top().iterate->parselist.append(IteratorBlock::Parse(append));
866
        if(iterate_finished) {
867
            scope_blocks.top().iterate = 0;
868
            bool ret = it->exec(this, place);
869
            delete it;
870
            if(!ret)
871
                return false;
872
            s = s.mid(d_off);
873
        } else {
874
            return true;
875
        }
876
    }
877
878
    QString scope, var, op;
879
    QStringList val;
880
#define SKIP_WS(d, o, l) while(o < l && (*(d+o) == QLatin1Char(' ') || *(d+o) == QLatin1Char('\t'))) ++o
881
    const QChar *d = s.unicode();
882
    int d_off = 0;
883
    SKIP_WS(d, d_off, s.length());
884
    IteratorBlock *iterator = 0;
885
    bool scope_failed = false, else_line = false, or_op=false;
886
    QChar quote = 0;
887
    int parens = 0, scope_count=0, start_block = 0;
888
    while(d_off < s.length()) {
889
        if(!parens) {
890
            if(*(d+d_off) == QLatin1Char('='))
891
                break;
892
            if(*(d+d_off) == QLatin1Char('+') || *(d+d_off) == QLatin1Char('-') ||
893
               *(d+d_off) == QLatin1Char('*') || *(d+d_off) == QLatin1Char('~')) {
894
                if(*(d+d_off+1) == QLatin1Char('=')) {
895
                    break;
896
                } else if(*(d+d_off+1) == QLatin1Char(' ')) {
897
                    const QChar *k = d+d_off+1;
898
                    int k_off = 0;
899
                    SKIP_WS(k, k_off, s.length()-d_off);
900
                    if(*(k+k_off) == QLatin1Char('=')) {
901
                        QString msg;
902
                        qmake_error_msg(QString(d+d_off, 1) + "must be followed immediately by =");
903
                        return false;
904
                    }
905
                }
906
            }
907
        }
908
909
        if(!quote.isNull()) {
910
            if(*(d+d_off) == quote)
911
                quote = QChar();
912
        } else if(*(d+d_off) == '(') {
913
            ++parens;
914
        } else if(*(d+d_off) == ')') {
915
            --parens;
916
        } else if(*(d+d_off) == '"' /*|| *(d+d_off) == '\''*/) {
917
            quote = *(d+d_off);
918
        }
919
920
        if(!parens && quote.isNull() &&
921
           (*(d+d_off) == QLatin1Char(':') || *(d+d_off) == QLatin1Char('{') ||
922
            *(d+d_off) == QLatin1Char(')') || *(d+d_off) == QLatin1Char('|'))) {
923
            scope_count++;
924
            scope = var.trimmed();
925
            if(*(d+d_off) == QLatin1Char(')'))
926
                scope += *(d+d_off); // need this
927
            var = "";
928
929
            bool test = scope_failed;
930
            if(scope.isEmpty()) {
931
                test = true;
932
            } else if(scope.toLower() == "else") { //else is a builtin scope here as it modifies state
933
                if(scope_count != 1 || scope_blocks.top().else_status == ScopeBlock::TestNone) {
934
                    qmake_error_msg(("Unexpected " + scope + " ('" + s + "')").toLatin1());
935
                    return false;
936
                }
937
                else_line = true;
938
                test = (scope_blocks.top().else_status == ScopeBlock::TestSeek);
939
                debug_msg(1, "Project Parser: %s:%d : Else%s %s.", parser.file.toLatin1().constData(), parser.line_no,
940
                          scope == "else" ? "" : QString(" (" + scope + ")").toLatin1().constData(),
941
                          test ? "considered" : "excluded");
942
            } else {
943
                QString comp_scope = scope;
944
                bool invert_test = (comp_scope.at(0) == QLatin1Char('!'));
945
                if(invert_test)
946
                    comp_scope = comp_scope.mid(1);
947
                int lparen = comp_scope.indexOf('(');
948
                if(or_op == scope_failed) {
949
                    if(lparen != -1) { // if there is an lparen in the scope, it IS a function
950
                        int rparen = comp_scope.lastIndexOf(')');
951
                        if(rparen == -1) {
952
                            qmake_error_msg("Function missing right paren: " + comp_scope);
953
                            return false;
954
                        }
955
                        QString func = comp_scope.left(lparen);
956
                        QStringList args = split_arg_list(comp_scope.mid(lparen+1, rparen - lparen - 1));
957
                        if(function) {
958
                            fprintf(stderr, "%s:%d: No tests can come after a function definition!\n",
959
                                    parser.file.toLatin1().constData(), parser.line_no);
960
                            return false;
961
                        } else if(func == "for") { //for is a builtin function here, as it modifies state
962
                            if(args.count() > 2 || args.count() < 1) {
963
                                fprintf(stderr, "%s:%d: for(iterate, list) requires two arguments.\n",
964
                                        parser.file.toLatin1().constData(), parser.line_no);
965
                                return false;
966
                            } else if(iterator) {
967
                                fprintf(stderr, "%s:%d unexpected nested for()\n",
968
                                        parser.file.toLatin1().constData(), parser.line_no);
969
                                return false;
970
                            }
971
972
                            iterator = new IteratorBlock;
973
                            QString it_list;
974
                            if(args.count() == 1) {
975
                                doVariableReplace(args[0], place);
976
                                it_list = args[0];
977
                                if(args[0] != "ever") {
978
                                    delete iterator;
979
                                    iterator = 0;
980
                                    fprintf(stderr, "%s:%d: for(iterate, list) requires two arguments.\n",
981
                                            parser.file.toLatin1().constData(), parser.line_no);
982
                                    return false;
983
                                }
984
                                it_list = "forever";
985
                            } else if(args.count() == 2) {
986
                                iterator->variable = args[0];
987
                                doVariableReplace(args[1], place);
988
                                it_list = args[1];
989
                            }
990
                            QStringList list = place[it_list];
991
                            if(list.isEmpty()) {
992
                                if(it_list == "forever") {
993
                                    iterator->loop_forever = true;
994
                                } else {
995
                                    int dotdot = it_list.indexOf("..");
996
                                    if(dotdot != -1) {
997
                                        bool ok;
998
                                        int start = it_list.left(dotdot).toInt(&ok);
999
                                        if(ok) {
1000
                                            int end = it_list.mid(dotdot+2).toInt(&ok);
1001
                                            if(ok) {
1002
                                                if(start < end) {
1003
                                                    for(int i = start; i <= end; i++)
1004
                                                        list << QString::number(i);
1005
                                                } else {
1006
                                                    for(int i = start; i >= end; i--)
1007
                                                        list << QString::number(i);
1008
                                                }
1009
                                            }
1010
                                        }
1011
                                    }
1012
                                }
1013
                            }
1014
                            iterator->list = list;
1015
                            test = !invert_test;
1016
                        } else if(iterator) {
1017
                            iterator->test.append(IteratorBlock::Test(func, args, invert_test));
1018
                            test = !invert_test;
1019
                        } else if(func == "defineTest" || func == "defineReplace") {
1020
                            if(!function_blocks.isEmpty()) {
1021
                                fprintf(stderr,
1022
                                        "%s:%d: cannot define a function within another definition.\n",
1023
                                        parser.file.toLatin1().constData(), parser.line_no);
1024
                                return false;
1025
                            }
1026
                            if(args.count() != 1) {
1027
                                fprintf(stderr, "%s:%d: %s(function_name) requires one argument.\n",
1028
                                        parser.file.toLatin1().constData(), parser.line_no, func.toLatin1().constData());
1029
                                return false;
1030
                            }
1031
                            QMap<QString, FunctionBlock*> *map = 0;
1032
                            if(func == "defineTest")
1033
                                map = &testFunctions;
1034
                            else
1035
                                map = &replaceFunctions;
1036
#if 0
1037
                            if(!map || map->contains(args[0])) {
1038
                                fprintf(stderr, "%s:%d: Function[%s] multiply defined.\n",
1039
                                        parser.file.toLatin1().constData(), parser.line_no, args[0].toLatin1().constData());
1040
                                return false;
1041
                            }
1042
#endif
1043
                            function = new FunctionBlock;
1044
                            map->insert(args[0], function);
1045
                            test = true;
1046
                        } else {
1047
                            test = doProjectTest(func, args, place);
1048
                            if(*(d+d_off) == QLatin1Char(')') && d_off == s.length()-1) {
1049
                                if(invert_test)
1050
                                    test = !test;
1051
                                scope_blocks.top().else_status =
1052
                                    (test ? ScopeBlock::TestFound : ScopeBlock::TestSeek);
1053
                                return true;  // assume we are done
1054
                            }
1055
                        }
1056
                    } else {
1057
                        QString cscope = comp_scope.trimmed();
1058
                        doVariableReplace(cscope, place);
1059
                        test = isActiveConfig(cscope.trimmed(), true, &place);
1060
                    }
1061
                    if(invert_test)
1062
                        test = !test;
1063
                }
1064
            }
1065
            if(!test && !scope_failed)
1066
                debug_msg(1, "Project Parser: %s:%d : Test (%s) failed.", parser.file.toLatin1().constData(),
1067
                          parser.line_no, scope.toLatin1().constData());
1068
            if(test == or_op)
1069
                scope_failed = !test;
1070
            or_op = (*(d+d_off) == QLatin1Char('|'));
1071
1072
            if(*(d+d_off) == QLatin1Char('{')) { // scoping block
1073
                start_block++;
1074
                if(iterator) {
1075
                    for(int off = 0, braces = 0; true; ++off) {
1076
                        if(*(d+d_off+off) == QLatin1Char('{'))
1077
                            ++braces;
1078
                        else if(*(d+d_off+off) == QLatin1Char('}') && braces)
1079
                            --braces;
1080
                        if(!braces || d_off+off == s.length()) {
1081
                            iterator->parselist.append(s.mid(d_off, off-1));
1082
                            if(braces > 1)
1083
                                iterator->scope_level += braces-1;
1084
                            d_off += off-1;
1085
                            break;
1086
                        }
1087
                    }
1088
                }
1089
            }
1090
        } else if(!parens && *(d+d_off) == QLatin1Char('}')) {
1091
            if(start_block) {
1092
                --start_block;
1093
            } else if(!scope_blocks.count()) {
1094
                warn_msg(WarnParser, "Possible braces mismatch %s:%d", parser.file.toLatin1().constData(), parser.line_no);
1095
            } else {
1096
                if(scope_blocks.count() == 1) {
1097
                    fprintf(stderr, "Braces mismatch %s:%d\n", parser.file.toLatin1().constData(), parser.line_no);
1098
                    return false;
1099
                }
1100
                debug_msg(1, "Project Parser: %s:%d : Leaving block %d", parser.file.toLatin1().constData(),
1101
                          parser.line_no, scope_blocks.count());
1102
                ScopeBlock sb = scope_blocks.pop();
1103
                if(sb.iterate)
1104
                    sb.iterate->exec(this, place);
1105
            }
1106
        } else {
1107
            var += *(d+d_off);
1108
        }
1109
        ++d_off;
1110
    }
1111
    var = var.trimmed();
1112
1113
    if(!else_line || (else_line && !scope_failed))
1114
        scope_blocks.top().else_status = (!scope_failed ? ScopeBlock::TestFound : ScopeBlock::TestSeek);
1115
    if(start_block) {
1116
        ScopeBlock next_block(scope_failed);
1117
        next_block.iterate = iterator;
1118
        if(iterator)
1119
            next_block.else_status = ScopeBlock::TestNone;
1120
        else if(scope_failed)
1121
            next_block.else_status = ScopeBlock::TestSeek;
1122
        else
1123
            next_block.else_status = ScopeBlock::TestFound;
1124
        scope_blocks.push(next_block);
1125
        debug_msg(1, "Project Parser: %s:%d : Entering block %d (%d). [%s]", parser.file.toLatin1().constData(),
1126
                  parser.line_no, scope_blocks.count(), scope_failed, s.toLatin1().constData());
1127
    } else if(iterator) {
1128
        iterator->parselist.append(var+s.mid(d_off));
1129
        bool ret = iterator->exec(this, place);
1130
        delete iterator;
1131
        return ret;
1132
    }
1133
1134
    if((!scope_count && !var.isEmpty()) || (scope_count == 1 && else_line))
1135
        scope_blocks.top().else_status = ScopeBlock::TestNone;
1136
    if(d_off == s.length()) {
1137
        if(!var.trimmed().isEmpty())
1138
            qmake_error_msg(("Parse Error ('" + s + "')").toLatin1());
1139
        return var.isEmpty(); // allow just a scope
1140
    }
1141
1142
    SKIP_WS(d, d_off, s.length());
1143
    for(; d_off < s.length() && op.indexOf('=') == -1; op += *(d+(d_off++)))
1144
        ;
1145
    op.replace(QRegExp("\\s"), "");
1146
1147
    SKIP_WS(d, d_off, s.length());
1148
    QString vals = s.mid(d_off); // vals now contains the space separated list of values
1149
    int rbraces = vals.count('}'), lbraces = vals.count('{');
1150
    if(scope_blocks.count() > 1 && rbraces - lbraces == 1) {
1151
        debug_msg(1, "Project Parser: %s:%d : Leaving block %d", parser.file.toLatin1().constData(),
1152
                  parser.line_no, scope_blocks.count());
1153
        ScopeBlock sb = scope_blocks.pop();
1154
        if(sb.iterate)
1155
            sb.iterate->exec(this, place);
1156
        vals.truncate(vals.length()-1);
1157
    } else if(rbraces != lbraces) {
1158
        warn_msg(WarnParser, "Possible braces mismatch {%s} %s:%d",
1159
                 vals.toLatin1().constData(), parser.file.toLatin1().constData(), parser.line_no);
1160
    }
1161
    if(scope_failed)
1162
        return true; // oh well
1163
#undef SKIP_WS
1164
1165
    doVariableReplace(var, place);
1166
    var = varMap(var); //backwards compatability
1167
    if(!var.isEmpty() && Option::mkfile::do_preprocess) {
1168
        static QString last_file("*none*");
1169
        if(parser.file != last_file) {
1170
            fprintf(stdout, "#file %s:%d\n", parser.file.toLatin1().constData(), parser.line_no);
1171
            last_file = parser.file;
1172
        }
1173
        fprintf(stdout, "%s %s %s\n", var.toLatin1().constData(), op.toLatin1().constData(), vals.toLatin1().constData());
1174
    }
1175
1176
    if(vals.contains('=') && numLines > 1)
1177
        warn_msg(WarnParser, "Detected possible line continuation: {%s} %s:%d",
1178
                 var.toLatin1().constData(), parser.file.toLatin1().constData(), parser.line_no);
1179
1180
    QStringList &varlist = place[var]; // varlist is the list in the symbol table
1181
1182
    if(Option::debug_level >= 1) {
1183
        QString tmp_vals = vals;
1184
        doVariableReplace(tmp_vals, place);
1185
        debug_msg(1, "Project Parser: %s:%d :%s: :%s: (%s)", parser.file.toLatin1().constData(), parser.line_no,
1186
                  var.toLatin1().constData(), op.toLatin1().constData(), tmp_vals.toLatin1().constData());
1187
    }
1188
1189
    // now do the operation
1190
    if(op == "~=") {
1191
        doVariableReplace(vals, place);
1192
        if(vals.length() < 4 || vals.at(0) != 's') {
1193
            qmake_error_msg(("~= operator only can handle s/// function ('" +
1194
                            s + "')").toLatin1());
1195
            return false;
1196
        }
1197
        QChar sep = vals.at(1);
1198
        QStringList func = vals.split(sep);
1199
        if(func.count() < 3 || func.count() > 4) {
1200
            qmake_error_msg(("~= operator only can handle s/// function ('" +
1201
                s + "')").toLatin1());
1202
            return false;
1203
        }
1204
        bool global = false, case_sense = true, quote = false;
1205
        if(func.count() == 4) {
1206
            global = func[3].indexOf('g') != -1;
1207
            case_sense = func[3].indexOf('i') == -1;
1208
            quote = func[3].indexOf('q') != -1;
1209
        }
1210
        QString from = func[1], to = func[2];
1211
        if(quote)
1212
            from = QRegExp::escape(from);
1213
        QRegExp regexp(from, case_sense ? Qt::CaseSensitive : Qt::CaseInsensitive);
1214
        for(QStringList::Iterator varit = varlist.begin(); varit != varlist.end();) {
1215
            if((*varit).contains(regexp)) {
1216
                (*varit) = (*varit).replace(regexp, to);
1217
                if ((*varit).isEmpty())
1218
                    varit = varlist.erase(varit);
1219
                else
1220
                    ++varit;
1221
                if(!global)
1222
                    break;
1223
            } else
1224
                ++varit;
1225
        }
1226
    } else {
1227
        QStringList vallist;
1228
        {
1229
            //doVariableReplace(vals, place);
1230
            QStringList tmp = split_value_list(vals, (var == "DEPENDPATH" || var == "INCLUDEPATH"));
1231
            for(int i = 0; i < tmp.size(); ++i)
1232
                vallist += doVariableReplaceExpand(tmp[i], place);
1233
        }
1234
1235
        if(op == "=") {
1236
            if(!varlist.isEmpty()) {
1237
                bool send_warning = false;
1238
                if(var != "TEMPLATE" && var != "TARGET") {
1239
                    QSet<QString> incoming_vals = vallist.toSet();
1240
                    for(int i = 0; i < varlist.size(); ++i) {
1241
                        const QString var = varlist.at(i).trimmed();
1242
                        if(!var.isEmpty() && !incoming_vals.contains(var)) {
1243
                            send_warning = true;
1244
                            break;
1245
                        }
1246
                    }
1247
                }
1248
                if(send_warning)
1249
                    warn_msg(WarnParser, "Operator=(%s) clears variables previously set: %s:%d",
1250
                             var.toLatin1().constData(), parser.file.toLatin1().constData(), parser.line_no);
1251
            }
1252
            varlist.clear();
1253
        }
1254
        for(QStringList::ConstIterator valit = vallist.begin();
1255
            valit != vallist.end(); ++valit) {
1256
            if((*valit).isEmpty())
1257
                continue;
1258
            if((op == "*=" && !varlist.contains((*valit))) ||
1259
               op == "=" || op == "+=")
1260
                varlist.append((*valit));
1261
            else if(op == "-=")
1262
                varlist.removeAll((*valit));
1263
        }
1264
        if(var == "REQUIRES") // special case to get communicated to backends!
1265
            doProjectCheckReqs(vallist, place);
1266
    }
1267
    return true;
1268
}
1269
1270
bool
1271
QMakeProject::read(QTextStream &file, QMap<QString, QStringList> &place)
1272
{
1273
    int numLines = 0;
1274
    bool ret = true;
1275
    QString s;
1276
    while(!file.atEnd()) {
1277
        parser.line_no++;
1278
        QString line = file.readLine().trimmed();
1279
        int prelen = line.length();
1280
1281
        int hash_mark = line.indexOf("#");
1282
        if(hash_mark != -1) //good bye comments
1283
            line = line.left(hash_mark).trimmed();
1284
        if(!line.isEmpty() && line.right(1) == "\\") {
1285
            if(!line.startsWith("#")) {
1286
                line.truncate(line.length() - 1);
1287
                s += line + Option::field_sep;
1288
                ++numLines;
1289
            }
1290
        } else if(!line.isEmpty() || (line.isEmpty() && !prelen)) {
1291
            if(s.isEmpty() && line.isEmpty())
1292
                continue;
1293
            if(!line.isEmpty()) {
1294
                s += line;
1295
                ++numLines;
1296
            }
1297
            if(!s.isEmpty()) {
1298
                if(!(ret = parse(s, place, numLines))) {
1299
                    s = "";
1300
                    numLines = 0;
1301
                    break;
1302
                }
1303
                s = "";
1304
                numLines = 0;
1305
            }
1306
        }
1307
    }
1308
    if (!s.isEmpty())
1309
        ret = parse(s, place, numLines);
1310
    return ret;
1311
}
1312
1313
bool
1314
QMakeProject::read(const QString &file, QMap<QString, QStringList> &place)
1315
{
1316
    parser_info pi = parser;
1317
    reset();
1318
1319
    const QString oldpwd = qmake_getpwd();
1320
    QString filename = Option::fixPathToLocalOS(file);
1321
    doVariableReplace(filename, place);
1322
    bool ret = false, using_stdin = false;
1323
    QFile qfile;
1324
    if(!strcmp(filename.toLatin1(), "-")) {
1325
        qfile.setFileName("");
1326
        ret = qfile.open(stdin, QIODevice::ReadOnly);
1327
        using_stdin = true;
1328
    } else if(QFileInfo(file).isDir()) {
1329
        return false;
1330
    } else {
1331
        qfile.setFileName(filename);
1332
        ret = qfile.open(QIODevice::ReadOnly);
1333
        qmake_setpwd(QFileInfo(filename).absolutePath());
1334
    }
1335
    if(ret) {
1336
        parser_info pi = parser;
1337
        parser.from_file = true;
1338
        parser.file = filename;
1339
        parser.line_no = 0;
1340
        QTextStream t(&qfile);
1341
        ret = read(t, place);
1342
        if(!using_stdin)
1343
            qfile.close();
1344
    }
1345
    if(scope_blocks.count() != 1) {
1346
        qmake_error_msg("Unterminated conditional block at end of file");
1347
        ret = false;
1348
    }
1349
    parser = pi;
1350
    qmake_setpwd(oldpwd);
1351
    return ret;
1352
}
1353
1354
bool
1355
QMakeProject::read(const QString &project, uchar cmd)
1356
{
1357
    pfile = QFileInfo(project).absoluteFilePath();
1358
    return read(cmd);
1359
}
1360
1361
bool
1362
QMakeProject::read(uchar cmd)
1363
{
1364
    if(cfile.isEmpty()) {
1365
        //find out where qmake (myself) lives
1366
        if (!base_vars.contains("QMAKE_QMAKE")) {
1367
            if (!Option::qmake_abslocation.isNull())
1368
                base_vars["QMAKE_QMAKE"] = QStringList(Option::qmake_abslocation);
1369
            else
1370
                base_vars["QMAKE_QMAKE"] = QStringList("qmake");
1371
        }
1372
1373
        // hack to get the Option stuff in there
1374
        base_vars["QMAKE_EXT_OBJ"] = QStringList(Option::obj_ext);
1375
        base_vars["QMAKE_EXT_CPP"] = Option::cpp_ext;
1376
        base_vars["QMAKE_EXT_C"] = Option::c_ext;
1377
        base_vars["QMAKE_EXT_H"] = Option::h_ext;
1378
        base_vars["QMAKE_SH"] = Option::shellPath;
1379
        if(!Option::user_template_prefix.isEmpty())
1380
            base_vars["TEMPLATE_PREFIX"] = QStringList(Option::user_template_prefix);
1381
1382
        if(cmd & ReadCache && Option::mkfile::do_cache) {        // parse the cache
1383
            int cache_depth = -1;
1384
            QString qmake_cache = Option::mkfile::cachefile;
1385
            if(qmake_cache.isEmpty())  { //find it as it has not been specified
1386
                QString dir = QDir::toNativeSeparators(Option::output_dir);
1387
                while(!QFile::exists((qmake_cache = dir + QDir::separator() + ".qmake.cache"))) {
1388
                    dir = dir.left(dir.lastIndexOf(QDir::separator()));
1389
                    if(dir.isEmpty() || dir.indexOf(QDir::separator()) == -1) {
1390
                        qmake_cache = "";
1391
                        break;
1392
                    }
1393
                    if(cache_depth == -1)
1394
                        cache_depth = 1;
1395
                    else
1396
                        cache_depth++;
1397
                }
1398
            } else {
1399
                QString abs_cache = QFileInfo(Option::mkfile::cachefile).absoluteDir().path();
1400
                if(Option::output_dir.startsWith(abs_cache))
1401
                    cache_depth = Option::output_dir.mid(abs_cache.length()).count('/');
1402
            }
1403
            if(!qmake_cache.isEmpty()) {
1404
                if(read(qmake_cache, cache)) {
1405
                    Option::mkfile::cachefile_depth = cache_depth;
1406
                    Option::mkfile::cachefile = qmake_cache;
1407
                    if(Option::mkfile::qmakespec.isEmpty() && !cache["QMAKESPEC"].isEmpty())
1408
                        Option::mkfile::qmakespec = cache["QMAKESPEC"].first();
1409
                }
1410
            }
1411
        }
1412
        if(cmd & ReadConf) {             // parse mkspec
1413
            QString qmakespec = fixEnvVariables(Option::mkfile::qmakespec);
1414
            QStringList mkspec_roots = qmake_mkspec_paths();
1415
            debug_msg(2, "Looking for mkspec %s in (%s)", qmakespec.toLatin1().constData(),
1416
                      mkspec_roots.join("::").toLatin1().constData());
1417
            if(qmakespec.isEmpty()) {
1418
                for(QStringList::ConstIterator it = mkspec_roots.begin(); it != mkspec_roots.end(); ++it) {
1419
                    QString mkspec = (*it) + QDir::separator() + "default";
1420
                    QFileInfo default_info(mkspec);
1421
                    if(default_info.exists() && default_info.isDir()) {
1422
                        qmakespec = mkspec;
1423
                        break;
1424
                    }
1425
                }
1426
                if(qmakespec.isEmpty()) {
1427
                    fprintf(stderr, "QMAKESPEC has not been set, so configuration cannot be deduced.\n");
1428
                    return false;
1429
                }
1430
                Option::mkfile::qmakespec = qmakespec;
1431
            }
1432
1433
            if(QDir::isRelativePath(qmakespec)) {
1434
                if (QFile::exists(qmakespec+"/qmake.conf")) {
1435
                    Option::mkfile::qmakespec = QFileInfo(Option::mkfile::qmakespec).absoluteFilePath();
1436
                } else if (QFile::exists(Option::output_dir+"/"+qmakespec+"/qmake.conf")) {
1437
                    qmakespec = Option::mkfile::qmakespec = QFileInfo(Option::output_dir+"/"+qmakespec).absoluteFilePath();
1438
                } else {
1439
                    bool found_mkspec = false;
1440
                    for(QStringList::ConstIterator it = mkspec_roots.begin(); it != mkspec_roots.end(); ++it) {
1441
                        QString mkspec = (*it) + QDir::separator() + qmakespec;
1442
                        if(QFile::exists(mkspec)) {
1443
                            found_mkspec = true;
1444
                            Option::mkfile::qmakespec = qmakespec = mkspec;
1445
                            break;
1446
                        }
1447
                    }
1448
                    if(!found_mkspec) {
1449
                        fprintf(stderr, "Could not find mkspecs for your QMAKESPEC(%s) after trying:\n\t%s\n",
1450
                                qmakespec.toLatin1().constData(), mkspec_roots.join("\n\t").toLatin1().constData());
1451
                        return false;
1452
                    }
1453
                }
1454
            }
1455
1456
            // parse qmake configuration
1457
            while(qmakespec.endsWith(QString(QChar(QDir::separator()))))
1458
                qmakespec.truncate(qmakespec.length()-1);
1459
            QString spec = qmakespec + QDir::separator() + "qmake.conf";
1460
            if(!QFile::exists(spec) &&
1461
               QFile::exists(qmakespec + QDir::separator() + "tmake.conf"))
1462
                spec = qmakespec + QDir::separator() + "tmake.conf";
1463
            debug_msg(1, "QMAKESPEC conf: reading %s", spec.toLatin1().constData());
1464
            if(!read(spec, base_vars)) {
1465
                fprintf(stderr, "Failure to read QMAKESPEC conf file %s.\n", spec.toLatin1().constData());
1466
                return false;
1467
            }
1468
1469
            init_symbian(base_vars);
1470
1471
            if(Option::mkfile::do_cache && !Option::mkfile::cachefile.isEmpty()) {
1472
                debug_msg(1, "QMAKECACHE file: reading %s", Option::mkfile::cachefile.toLatin1().constData());
1473
                read(Option::mkfile::cachefile, base_vars);
1474
            }
1475
        }
1476
1477
        if(cmd & ReadFeatures) {
1478
            debug_msg(1, "Processing default_pre: %s", vars["CONFIG"].join("::").toLatin1().constData());
1479
            if(doProjectInclude("default_pre", IncludeFlagFeature, base_vars) == IncludeNoExist)
1480
                doProjectInclude("default", IncludeFlagFeature, base_vars);
1481
        }
1482
    }
1483
1484
    vars = base_vars; // start with the base
1485
1486
    //get a default
1487
    if(pfile != "-" && vars["TARGET"].isEmpty())
1488
        vars["TARGET"].append(QFileInfo(pfile).baseName());
1489
1490
    //before commandline
1491
    if(cmd & ReadCmdLine) {
1492
        cfile = pfile;
1493
        parser.file = "(internal)";
1494
        parser.from_file = false;
1495
        parser.line_no = 1; //really arg count now.. duh
1496
        reset();
1497
        for(QStringList::ConstIterator it = Option::before_user_vars.begin();
1498
            it != Option::before_user_vars.end(); ++it) {
1499
            if(!parse((*it), vars)) {
1500
                fprintf(stderr, "Argument failed to parse: %s\n", (*it).toLatin1().constData());
1501
                return false;
1502
            }
1503
            parser.line_no++;
1504
        }
1505
    }
1506
1507
    //commandline configs
1508
    if(cmd & ReadConfigs && !Option::user_configs.isEmpty()) {
1509
        parser.file = "(configs)";
1510
        parser.from_file = false;
1511
        parser.line_no = 1; //really arg count now.. duh
1512
        parse("CONFIG += " + Option::user_configs.join(" "), vars);
1513
    }
1514
1515
    if(cmd & ReadProFile) { // parse project file
1516
        debug_msg(1, "Project file: reading %s", pfile.toLatin1().constData());
1517
        if(pfile != "-" && !QFile::exists(pfile) && !pfile.endsWith(Option::pro_ext))
1518
            pfile += Option::pro_ext;
1519
        if(!read(pfile, vars))
1520
            return false;
1521
    }
1522
1523
    if(cmd & ReadPostFiles) { // parse post files
1524
        const QStringList l = vars["QMAKE_POST_INCLUDE_FILES"];
1525
        for(QStringList::ConstIterator it = l.begin(); it != l.end(); ++it) {
1526
            if(read((*it), vars)) {
1527
                if(vars["QMAKE_INTERNAL_INCLUDED_FILES"].indexOf((*it)) == -1)
1528
                    vars["QMAKE_INTERNAL_INCLUDED_FILES"].append((*it));
1529
            }
1530
        }
1531
    }
1532
1533
    if(cmd & ReadCmdLine) {
1534
        parser.file = "(internal)";
1535
        parser.from_file = false;
1536
        parser.line_no = 1; //really arg count now.. duh
1537
        reset();
1538
        for(QStringList::ConstIterator it = Option::after_user_vars.begin();
1539
            it != Option::after_user_vars.end(); ++it) {
1540
            if(!parse((*it), vars)) {
1541
                fprintf(stderr, "Argument failed to parse: %s\n", (*it).toLatin1().constData());
1542
                return false;
1543
            }
1544
            parser.line_no++;
1545
        }
1546
    }
1547
1548
    //after configs (set in BUILDS)
1549
    if(cmd & ReadConfigs && !Option::after_user_configs.isEmpty()) {
1550
        parser.file = "(configs)";
1551
        parser.from_file = false;
1552
        parser.line_no = 1; //really arg count now.. duh
1553
        parse("CONFIG += " + Option::after_user_configs.join(" "), vars);
1554
    }
1555
1556
    if(pfile != "-" && vars["TARGET"].isEmpty())
1557
        vars["TARGET"].append(QFileInfo(pfile).baseName());
1558
1559
    if(cmd & ReadConfigs && !Option::user_configs.isEmpty()) {
1560
        parser.file = "(configs)";
1561
        parser.from_file = false;
1562
        parser.line_no = 1; //really arg count now.. duh
1563
        parse("CONFIG += " + Option::user_configs.join(" "), base_vars);
1564
    }
1565
1566
    if(cmd & ReadFeatures) {
1567
        debug_msg(1, "Processing default_post: %s", vars["CONFIG"].join("::").toLatin1().constData());
1568
        doProjectInclude("default_post", IncludeFlagFeature, vars);
1569
1570
        QHash<QString, bool> processed;
1571
        const QStringList &configs = vars["CONFIG"];
1572
        debug_msg(1, "Processing CONFIG features: %s", configs.join("::").toLatin1().constData());
1573
        while(1) {
1574
            bool finished = true;
1575
            for(int i = configs.size()-1; i >= 0; --i) {
1576
		const QString config = configs[i].toLower();
1577
                if(!processed.contains(config)) {
1578
                    processed.insert(config, true);
1579
                    if(doProjectInclude(config, IncludeFlagFeature, vars) == IncludeSuccess) {
1580
                        finished = false;
1581
                        break;
1582
                    }
1583
                }
1584
            }
1585
            if(finished)
1586
                break;
1587
        }
1588
    }
1589
    Option::postProcessProject(this);   // let Option post-process
1590
    return true;
1591
}
1592
1593
bool
1594
QMakeProject::isActiveConfig(const QString &x, bool regex, QMap<QString, QStringList> *place)
1595
{
1596
    if(x.isEmpty())
1597
        return true;
1598
1599
    //magic types for easy flipping
1600
    if(x == "true")
1601
        return true;
1602
    else if(x == "false")
1603
        return false;
1604
1605
    static QString spec;
1606
    if(spec.isEmpty())
1607
        spec = QFileInfo(Option::mkfile::qmakespec).fileName();
1608
1609
    // Symbian is an exception to how scopes are resolved. Since we do not
1610
    // have a separate target mode for Symbian, but we expect the scope to resolve
1611
    // on other platforms we base it entirely on the mkspec. This means that
1612
    // using a mkspec starting with 'symbian*' will resolve both the 'symbian'
1613
    // and the 'unix' (because of Open C) scopes to true.
1614
    if(isForSymbian() && (x == "symbian" || x == "unix"))
1615
        return true;
1616
1617
    //mkspecs
1618
    if((Option::target_mode == Option::TARG_MACX_MODE ||
1619
        Option::target_mode == Option::TARG_UNIX_MODE) && x == "unix")
1620
        return !isForSymbian();
1621
    else if(Option::target_mode == Option::TARG_MACX_MODE && x == "macx")
1622
        return !isForSymbian();
1623
    else if(Option::target_mode == Option::TARG_MAC9_MODE && x == "mac9")
1624
        return !isForSymbian();
1625
    else if((Option::target_mode == Option::TARG_MAC9_MODE || Option::target_mode == Option::TARG_MACX_MODE) &&
1626
            x == "mac")
1627
        return !isForSymbian();
1628
    else if(Option::target_mode == Option::TARG_WIN_MODE && x == "win32")
1629
        return !isForSymbian();
1630
    QRegExp re(x, Qt::CaseSensitive, QRegExp::Wildcard);
1631
    if((regex && re.exactMatch(spec)) || (!regex && spec == x))
1632
        return true;
1633
#ifdef Q_OS_UNIX
1634
    else if(spec == "default") {
1635
        static char *buffer = NULL;
1636
        if(!buffer) {
1637
            buffer = (char *)malloc(1024);
1638
            qmakeAddCacheClear(qmakeFreeCacheClear, (void**)&buffer);
1639
        }
1640
        int l = readlink(Option::mkfile::qmakespec.toLatin1(), buffer, 1024);
1641
        if(l != -1) {
1642
            buffer[l] = '\0';
1643
            QString r = buffer;
1644
            if(r.lastIndexOf('/') != -1)
1645
                r = r.mid(r.lastIndexOf('/') + 1);
1646
            if((regex && re.exactMatch(r)) || (!regex && r == x))
1647
                return true;
1648
        }
1649
    }
1650
#elif defined(Q_OS_WIN)
1651
    else if(spec == "default") {
1652
        // We can't resolve symlinks as they do on Unix, so configure.exe puts the source of the
1653
        // qmake.conf at the end of the default/qmake.conf in the QMAKESPEC_ORG variable.
1654
        const QStringList &spec_org = (place ? (*place)["QMAKESPEC_ORIGINAL"]
1655
                                             : vars["QMAKESPEC_ORIGINAL"]);
1656
        if (!spec_org.isEmpty()) {
1657
            spec = spec_org.at(0);
1658
            int lastSlash = spec.lastIndexOf('/');
1659
            if(lastSlash != -1)
1660
                spec = spec.mid(lastSlash + 1);
1661
            if((regex && re.exactMatch(spec)) || (!regex && spec == x))
1662
                return true;
1663
        }
1664
    }
1665
#endif
1666
1667
    //simple matching
1668
    const QStringList &configs = (place ? (*place)["CONFIG"] : vars["CONFIG"]);
1669
    for(QStringList::ConstIterator it = configs.begin(); it != configs.end(); ++it) {
1670
        if(((regex && re.exactMatch((*it))) || (!regex && (*it) == x)) && re.exactMatch((*it)))
1671
            return true;
1672
    }
1673
    return false;
1674
}
1675
1676
bool
1677
QMakeProject::doProjectTest(QString str, QMap<QString, QStringList> &place)
1678
{
1679
    QString chk = remove_quotes(str);
1680
    if(chk.isEmpty())
1681
        return true;
1682
    bool invert_test = (chk.left(1) == "!");
1683
    if(invert_test)
1684
        chk = chk.mid(1);
1685
1686
    bool test=false;
1687
    int lparen = chk.indexOf('(');
1688
    if(lparen != -1) { // if there is an lparen in the chk, it IS a function
1689
        int rparen = chk.indexOf(')', lparen);
1690
        if(rparen == -1) {
1691
            qmake_error_msg("Function missing right paren: " + chk);
1692
        } else {
1693
            QString func = chk.left(lparen);
1694
            test = doProjectTest(func, chk.mid(lparen+1, rparen - lparen - 1), place);
1695
        }
1696
    } else {
1697
        test = isActiveConfig(chk, true, &place);
1698
    }
1699
    if(invert_test)
1700
        return !test;
1701
    return test;
1702
}
1703
1704
bool
1705
QMakeProject::doProjectTest(QString func, const QString &params,
1706
                            QMap<QString, QStringList> &place)
1707
{
1708
    return doProjectTest(func, split_arg_list(params), place);
1709
}
1710
1711
QMakeProject::IncludeStatus
1712
QMakeProject::doProjectInclude(QString file, uchar flags, QMap<QString, QStringList> &place)
1713
{
1714
    enum { UnknownFormat, ProFormat, JSFormat } format = UnknownFormat;
1715
    if(flags & IncludeFlagFeature) {
1716
        if(!file.endsWith(Option::prf_ext))
1717
            file += Option::prf_ext;
1718
        if(file.indexOf(Option::dir_sep) == -1 || !QFile::exists(file)) {
1719
            static QStringList *feature_roots = 0;
1720
            if(!feature_roots) {
1721
                init_symbian(base_vars);
1722
                feature_roots = new QStringList(qmake_feature_paths(prop));
1723
                qmakeAddCacheClear(qmakeDeleteCacheClear_QStringList, (void**)&feature_roots);
1724
            }
1725
            debug_msg(2, "Looking for feature '%s' in (%s)", file.toLatin1().constData(),
1726
			feature_roots->join("::").toLatin1().constData());
1727
            int start_root = 0;
1728
            if(parser.from_file) {
1729
                QFileInfo currFile(parser.file), prfFile(file);
1730
                if(currFile.fileName() == prfFile.fileName()) {
1731
                    currFile = QFileInfo(currFile.canonicalFilePath());
1732
                    for(int root = 0; root < feature_roots->size(); ++root) {
1733
                        prfFile = QFileInfo(feature_roots->at(root) +
1734
                                            QDir::separator() + file).canonicalFilePath();
1735
                        if(prfFile == currFile) {
1736
                            start_root = root+1;
1737
                            break;
1738
                        }
1739
                    }
1740
                }
1741
            }
1742
            for(int root = start_root; root < feature_roots->size(); ++root) {
1743
                QString prf(feature_roots->at(root) + QDir::separator() + file);
1744
                if(QFile::exists(prf + Option::js_ext)) {
1745
                    format = JSFormat;
1746
                    file = prf + Option::js_ext;
1747
                    break;
1748
                } else if(QFile::exists(prf)) {
1749
                    format = ProFormat;
1750
                    file = prf;
1751
                    break;
1752
                }
1753
            }
1754
            if(format == UnknownFormat)
1755
                return IncludeNoExist;
1756
            if(place["QMAKE_INTERNAL_INCLUDED_FEATURES"].indexOf(file) != -1)
1757
                return IncludeFeatureAlreadyLoaded;
1758
            place["QMAKE_INTERNAL_INCLUDED_FEATURES"].append(file);
1759
        }
1760
    }
1761
    if(QDir::isRelativePath(file)) {
1762
        QStringList include_roots;
1763
        if(Option::output_dir != qmake_getpwd())
1764
            include_roots << qmake_getpwd();
1765
        include_roots << Option::output_dir;
1766
        for(int root = 0; root < include_roots.size(); ++root) {
1767
            QString testName = QDir::toNativeSeparators(include_roots[root]);
1768
            if (!testName.endsWith(QString(QDir::separator())))
1769
                testName += QDir::separator();
1770
            testName += file;
1771
            if(QFile::exists(testName)) {
1772
                file = testName;
1773
                break;
1774
            }
1775
        }
1776
    }
1777
    if(format == UnknownFormat) {
1778
        if(QFile::exists(file)) {
1779
            if(file.endsWith(Option::js_ext))
1780
                format = JSFormat;
1781
            else
1782
                format = ProFormat;
1783
        } else {
1784
            return IncludeNoExist;
1785
        }
1786
    }
1787
    if(Option::mkfile::do_preprocess) //nice to see this first..
1788
        fprintf(stderr, "#switching file %s(%s) - %s:%d\n", (flags & IncludeFlagFeature) ? "load" : "include",
1789
                file.toLatin1().constData(),
1790
                parser.file.toLatin1().constData(), parser.line_no);
1791
    debug_msg(1, "Project Parser: %s'ing file %s.", (flags & IncludeFlagFeature) ? "load" : "include",
1792
              file.toLatin1().constData());
1793
1794
    QString orig_file = file;
1795
    int di = file.lastIndexOf(QDir::separator());
1796
    QString oldpwd = qmake_getpwd();
1797
    if(di != -1) {
1798
        if(!qmake_setpwd(file.left(file.lastIndexOf(QDir::separator())))) {
1799
            fprintf(stderr, "Cannot find directory: %s\n", file.left(di).toLatin1().constData());
1800
            return IncludeFailure;
1801
        }
1802
        file = file.right(file.length() - di - 1);
1803
    }
1804
    bool parsed = false;
1805
    parser_info pi = parser;
1806
    if(format == JSFormat) {
1807
        warn_msg(WarnParser, "%s:%d: QtScript support disabled for %s.",
1808
                 pi.file.toLatin1().constData(), pi.line_no, orig_file.toLatin1().constData());
1809
    } else {
1810
        QStack<ScopeBlock> sc = scope_blocks;
1811
        IteratorBlock *it = iterator;
1812
        FunctionBlock *fu = function;
1813
        if(flags & (IncludeFlagNewProject|IncludeFlagNewParser)) {
1814
            // The "project's variables" are used in other places (eg. export()) so it's not
1815
            // possible to use "place" everywhere. Instead just set variables and grab them later
1816
            QMakeProject proj(this, &place);
1817
            if(flags & IncludeFlagNewParser) {
1818
#if 1
1819
                if(proj.doProjectInclude("default_pre", IncludeFlagFeature, proj.variables()) == IncludeNoExist)
1820
                    proj.doProjectInclude("default", IncludeFlagFeature, proj.variables());
1821
#endif
1822
                parsed = proj.read(file, proj.variables());
1823
            } else {
1824
                parsed = proj.read(file);
1825
            }
1826
            place = proj.variables();
1827
        } else {
1828
            parsed = read(file, place);
1829
        }
1830
        iterator = it;
1831
        function = fu;
1832
        scope_blocks = sc;
1833
    }
1834
    if(parsed) {
1835
        if(place["QMAKE_INTERNAL_INCLUDED_FILES"].indexOf(orig_file) == -1)
1836
            place["QMAKE_INTERNAL_INCLUDED_FILES"].append(orig_file);
1837
    } else {
1838
        warn_msg(WarnParser, "%s:%d: Failure to include file %s.",
1839
                 pi.file.toLatin1().constData(), pi.line_no, orig_file.toLatin1().constData());
1840
    }
1841
    parser = pi;
1842
    qmake_setpwd(oldpwd);
1843
    if(!parsed)
1844
        return IncludeParseFailure;
1845
    return IncludeSuccess;
1846
}
1847
1848
QStringList
1849
QMakeProject::doProjectExpand(QString func, const QString &params,
1850
                              QMap<QString, QStringList> &place)
1851
{
1852
    return doProjectExpand(func, split_arg_list(params), place);
1853
}
1854
1855
QStringList
1856
QMakeProject::doProjectExpand(QString func, QStringList args,
1857
                              QMap<QString, QStringList> &place)
1858
{
1859
    QList<QStringList> args_list;
1860
    for(int i = 0; i < args.size(); ++i) {
1861
        QStringList arg = split_value_list(args[i]), tmp;
1862
        for(int i = 0; i < arg.size(); ++i)
1863
            tmp += doVariableReplaceExpand(arg[i], place);;
1864
        args_list += tmp;
1865
    }
1866
    return doProjectExpand(func, args_list, place);
1867
}
1868
1869
// defined in symbian generator
1870
extern QString generate_test_uid(const QString& target);
1871
1872
QStringList
1873
QMakeProject::doProjectExpand(QString func, QList<QStringList> args_list,
1874
                              QMap<QString, QStringList> &place)
1875
{
1876
    func = func.trimmed();
1877
    if(replaceFunctions.contains(func)) {
1878
        FunctionBlock *defined = replaceFunctions[func];
1879
        function_blocks.push(defined);
1880
        QStringList ret;
1881
        defined->exec(args_list, this, place, ret);
1882
        Q_ASSERT(function_blocks.pop() == defined);
1883
        return ret;
1884
    }
1885
1886
    QStringList args; //why don't the builtin functions just use args_list? --Sam
1887
    for(int i = 0; i < args_list.size(); ++i)
1888
        args += args_list[i].join(QString(Option::field_sep));
1889
1890
    ExpandFunc func_t = qmake_expandFunctions().value(func.toLower());
1891
    debug_msg(1, "Running project expand: %s(%s) [%d]",
1892
              func.toLatin1().constData(), args.join("::").toLatin1().constData(), func_t);
1893
1894
    QStringList ret;
1895
    switch(func_t) {
1896
    case E_MEMBER: {
1897
        if(args.count() < 1 || args.count() > 3) {
1898
            fprintf(stderr, "%s:%d: member(var, start, end) requires three arguments.\n",
1899
                    parser.file.toLatin1().constData(), parser.line_no);
1900
        } else {
1901
            bool ok = true;
1902
            const QStringList &var = values(args.first(), place);
1903
            int start = 0, end = 0;
1904
            if(args.count() >= 2) {
1905
                QString start_str = args[1];
1906
                start = start_str.toInt(&ok);
1907
                if(!ok) {
1908
                    if(args.count() == 2) {
1909
                        int dotdot = start_str.indexOf("..");
1910
                        if(dotdot != -1) {
1911
                            start = start_str.left(dotdot).toInt(&ok);
1912
                            if(ok)
1913
                                end = start_str.mid(dotdot+2).toInt(&ok);
1914
                        }
1915
                    }
1916
                    if(!ok)
1917
                        fprintf(stderr, "%s:%d: member() argument 2 (start) '%s' invalid.\n",
1918
                                parser.file.toLatin1().constData(), parser.line_no,
1919
                                start_str.toLatin1().constData());
1920
                } else {
1921
                    end = start;
1922
                    if(args.count() == 3)
1923
                        end = args[2].toInt(&ok);
1924
                    if(!ok)
1925
                        fprintf(stderr, "%s:%d: member() argument 3 (end) '%s' invalid.\n",
1926
                                parser.file.toLatin1().constData(), parser.line_no,
1927
                                args[2].toLatin1().constData());
1928
                }
1929
            }
1930
            if(ok) {
1931
                if(start < 0)
1932
                    start += var.count();
1933
                if(end < 0)
1934
                    end += var.count();
1935
                if(start < 0 || start >= var.count() || end < 0 || end >= var.count()) {
1936
                    //nothing
1937
                } else if(start < end) {
1938
                    for(int i = start; i <= end && (int)var.count() >= i; i++)
1939
                        ret += var[i];
1940
                } else {
1941
                    for(int i = start; i >= end && (int)var.count() >= i && i >= 0; i--)
1942
                        ret += var[i];
1943
                }
1944
            }
1945
        }
1946
        break; }
1947
    case E_FIRST:
1948
    case E_LAST: {
1949
        if(args.count() != 1) {
1950
            fprintf(stderr, "%s:%d: %s(var) requires one argument.\n",
1951
                    parser.file.toLatin1().constData(), parser.line_no, func.toLatin1().constData());
1952
        } else {
1953
            const QStringList &var = values(args.first(), place);
1954
            if(!var.isEmpty()) {
1955
                if(func_t == E_FIRST)
1956
                    ret = QStringList(var[0]);
1957
                else
1958
                    ret = QStringList(var[var.size()-1]);
1959
            }
1960
        }
1961
        break; }
1962
    case E_CAT: {
1963
        if(args.count() < 1 || args.count() > 2) {
1964
            fprintf(stderr, "%s:%d: cat(file) requires one argument.\n",
1965
                    parser.file.toLatin1().constData(), parser.line_no);
1966
        } else {
1967
            QString file = args[0];
1968
            file = Option::fixPathToLocalOS(file);
1969
1970
            bool singleLine = true;
1971
            if(args.count() > 1)
1972
                singleLine = (args[1].toLower() == "true");
1973
1974
            QFile qfile(file);
1975
            if(qfile.open(QIODevice::ReadOnly)) {
1976
                QTextStream stream(&qfile);
1977
                while(!stream.atEnd()) {
1978
                    ret += split_value_list(stream.readLine().trimmed());
1979
                    if(!singleLine)
1980
                        ret += "\n";
1981
                }
1982
                qfile.close();
1983
            }
1984
        }
1985
        break; }
1986
    case E_FROMFILE: {
1987
        if(args.count() != 2) {
1988
            fprintf(stderr, "%s:%d: fromfile(file, variable) requires two arguments.\n",
1989
                    parser.file.toLatin1().constData(), parser.line_no);
1990
        } else {
1991
            QString file = args[0], seek_var = args[1];
1992
            file = Option::fixPathToLocalOS(file);
1993
1994
            QMap<QString, QStringList> tmp;
1995
            if(doProjectInclude(file, IncludeFlagNewParser, tmp) == IncludeSuccess) {
1996
                if(tmp.contains("QMAKE_INTERNAL_INCLUDED_FILES")) {
1997
                    QStringList &out = place["QMAKE_INTERNAL_INCLUDED_FILES"];
1998
                    const QStringList &in = tmp["QMAKE_INTERNAL_INCLUDED_FILES"];
1999
                    for(int i = 0; i < in.size(); ++i) {
2000
                        if(out.indexOf(in[i]) == -1)
2001
                            out += in[i];
2002
                    }
2003
                }
2004
                ret = tmp[seek_var];
2005
            }
2006
        }
2007
        break; }
2008
    case E_EVAL: {
2009
        if(args.count() < 1 || args.count() > 2) {
2010
            fprintf(stderr, "%s:%d: eval(variable) requires one argument.\n",
2011
                    parser.file.toLatin1().constData(), parser.line_no);
2012
2013
        } else {
2014
            const QMap<QString, QStringList> *source = &place;
2015
            if(args.count() == 2) {
2016
                if(args.at(1) == "Global") {
2017
                    source = &vars;
2018
                } else if(args.at(1) == "Local") {
2019
                    source = &place;
2020
                } else {
2021
                    fprintf(stderr, "%s:%d: unexpected source to eval.\n", parser.file.toLatin1().constData(),
2022
                            parser.line_no);
2023
                }
2024
            }
2025
            ret += source->value(args.at(0));
2026
        }
2027
        break; }
2028
    case E_LIST: {
2029
        static int x = 0;
2030
        QString tmp;
2031
        tmp.sprintf(".QMAKE_INTERNAL_TMP_VAR_%d", x++);
2032
        ret = QStringList(tmp);
2033
        QStringList &lst = (*((QMap<QString, QStringList>*)&place))[tmp];
2034
        lst.clear();
2035
        for(QStringList::ConstIterator arg_it = args.begin();
2036
            arg_it != args.end(); ++arg_it)
2037
            lst += split_value_list((*arg_it));
2038
        break; }
2039
    case E_SPRINTF: {
2040
        if(args.count() < 1) {
2041
            fprintf(stderr, "%s:%d: sprintf(format, ...) requires one argument.\n",
2042
                    parser.file.toLatin1().constData(), parser.line_no);
2043
        } else {
2044
            QString tmp = args.at(0);
2045
            for(int i = 1; i < args.count(); ++i)
2046
                tmp = tmp.arg(args.at(i));
2047
            ret = split_value_list(tmp);
2048
        }
2049
        break; }
2050
    case E_JOIN: {
2051
        if(args.count() < 1 || args.count() > 4) {
2052
            fprintf(stderr, "%s:%d: join(var, glue, before, after) requires four"
2053
                    "arguments.\n", parser.file.toLatin1().constData(), parser.line_no);
2054
        } else {
2055
            QString glue, before, after;
2056
            if(args.count() >= 2)
2057
                glue = args[1];
2058
            if(args.count() >= 3)
2059
                before = args[2];
2060
            if(args.count() == 4)
2061
                after = args[3];
2062
            const QStringList &var = values(args.first(), place);
2063
            if(!var.isEmpty())
2064
                ret = split_value_list(before + var.join(glue) + after);
2065
        }
2066
        break; }
2067
    case E_SPLIT: {
2068
        if(args.count() < 1 || args.count() > 2) {
2069
            fprintf(stderr, "%s:%d split(var, sep) requires one or two arguments\n",
2070
                    parser.file.toLatin1().constData(), parser.line_no);
2071
        } else {
2072
            QString sep = QString(Option::field_sep);
2073
            if(args.count() >= 2)
2074
                sep = args[1];
2075
            QStringList var = values(args.first(), place);
2076
            for(QStringList::ConstIterator vit = var.begin(); vit != var.end(); ++vit) {
2077
                QStringList lst = (*vit).split(sep);
2078
                for(QStringList::ConstIterator spltit = lst.begin(); spltit != lst.end(); ++spltit)
2079
                    ret += (*spltit);
2080
            }
2081
        }
2082
        break; }
2083
    case E_BASENAME:
2084
    case E_DIRNAME:
2085
    case E_SECTION: {
2086
        bool regexp = false;
2087
        QString sep, var;
2088
        int beg=0, end=-1;
2089
        if(func_t == E_SECTION) {
2090
            if(args.count() != 3 && args.count() != 4) {
2091
                fprintf(stderr, "%s:%d section(var, sep, begin, end) requires three argument\n",
2092
                        parser.file.toLatin1().constData(), parser.line_no);
2093
            } else {
2094
                var = args[0];
2095
                sep = args[1];
2096
                beg = args[2].toInt();
2097
                if(args.count() == 4)
2098
                    end = args[3].toInt();
2099
            }
2100
        } else {
2101
            if(args.count() != 1) {
2102
                fprintf(stderr, "%s:%d %s(var) requires one argument.\n",
2103
                        parser.file.toLatin1().constData(), parser.line_no, func.toLatin1().constData());
2104
            } else {
2105
                var = args[0];
2106
                regexp = true;
2107
                sep = "[" + QRegExp::escape(Option::dir_sep) + "/]";
2108
                if(func_t == E_DIRNAME)
2109
                    end = -2;
2110
                else
2111
                    beg = -1;
2112
            }
2113
        }
2114
        if(!var.isNull()) {
2115
            const QStringList &l = values(var, place);
2116
            for(QStringList::ConstIterator it = l.begin(); it != l.end(); ++it) {
2117
                QString separator = sep;
2118
                if(regexp)
2119
                    ret += (*it).section(QRegExp(separator), beg, end);
2120
                else
2121
                    ret += (*it).section(separator, beg, end);
2122
            }
2123
        }
2124
        break; }
2125
    case E_FIND: {
2126
        if(args.count() != 2) {
2127
            fprintf(stderr, "%s:%d find(var, str) requires two arguments\n",
2128
                    parser.file.toLatin1().constData(), parser.line_no);
2129
        } else {
2130
            QRegExp regx(args[1]);
2131
            const QStringList &var = values(args.first(), place);
2132
            for(QStringList::ConstIterator vit = var.begin();
2133
                vit != var.end(); ++vit) {
2134
                if(regx.indexIn(*vit) != -1)
2135
                    ret += (*vit);
2136
            }
2137
        }
2138
        break;  }
2139
    case E_SYSTEM: {
2140
        if(args.count() < 1 || args.count() > 2) {
2141
            fprintf(stderr, "%s:%d system(execut) requires one argument.\n",
2142
                    parser.file.toLatin1().constData(), parser.line_no);
2143
        } else {
2144
            QMakeProjectEnv env(place);
2145
            char buff[256];
2146
            bool singleLine = true;
2147
            if(args.count() > 1)
2148
                singleLine = (args[1].toLower() == "true");
2149
            QString output;
2150
            FILE *proc = QT_POPEN(args[0].toLatin1(), "r");
2151
            while(proc && !feof(proc)) {
2152
                int read_in = int(fread(buff, 1, 255, proc));
2153
                if(!read_in)
2154
                    break;
2155
                for(int i = 0; i < read_in; i++) {
2156
                    if((singleLine && buff[i] == '\n') || buff[i] == '\t')
2157
                        buff[i] = ' ';
2158
                }
2159
                buff[read_in] = '\0';
2160
                output += buff;
2161
            }
2162
            ret += split_value_list(output);
2163
            if(proc)
2164
                QT_PCLOSE(proc);
2165
        }
2166
        break; }
2167
    case E_UNIQUE: {
2168
        if(args.count() != 1) {
2169
            fprintf(stderr, "%s:%d unique(var) requires one argument.\n",
2170
                    parser.file.toLatin1().constData(), parser.line_no);
2171
        } else {
2172
            const QStringList &var = values(args.first(), place);
2173
            for(int i = 0; i < var.count(); i++) {
2174
                if(!ret.contains(var[i]))
2175
                    ret.append(var[i]);
2176
            }
2177
        }
2178
        break; }
2179
    case E_QUOTE:
2180
        ret = args;
2181
        break;
2182
    case E_ESCAPE_EXPAND: {
2183
        for(int i = 0; i < args.size(); ++i) {
2184
            QChar *i_data = args[i].data();
2185
            int i_len = args[i].length();
2186
            for(int x = 0; x < i_len; ++x) {
2187
                if(*(i_data+x) == '\\' && x < i_len-1) {
2188
                    if(*(i_data+x+1) == '\\') {
2189
                        ++x;
2190
                    } else {
2191
                        struct {
2192
                            char in, out;
2193
                        } mapped_quotes[] = {
2194
                            { 'n', '\n' },
2195
                            { 't', '\t' },
2196
                            { 'r', '\r' },
2197
                            { 0, 0 }
2198
                        };
2199
                        for(int i = 0; mapped_quotes[i].in; ++i) {
2200
                            if(*(i_data+x+1) == mapped_quotes[i].in) {
2201
                                *(i_data+x) = mapped_quotes[i].out;
2202
                                if(x < i_len-2)
2203
                                    memmove(i_data+x+1, i_data+x+2, (i_len-x-2)*sizeof(QChar));
2204
                                --i_len;
2205
                                break;
2206
                            }
2207
                        }
2208
                    }
2209
                }
2210
            }
2211
            ret.append(QString(i_data, i_len));
2212
        }
2213
        break; }
2214
    case E_RE_ESCAPE: {
2215
        for(int i = 0; i < args.size(); ++i)
2216
            ret += QRegExp::escape(args[i]);
2217
        break; }
2218
    case E_UPPER:
2219
    case E_LOWER: {
2220
        for(int i = 0; i < args.size(); ++i) {
2221
            if(func_t == E_UPPER)
2222
                ret += args[i].toUpper();
2223
            else
2224
                ret += args[i].toLower();
2225
        }
2226
        break; }
2227
    case E_FILES: {
2228
        if(args.count() != 1 && args.count() != 2) {
2229
            fprintf(stderr, "%s:%d files(pattern) requires one argument.\n",
2230
                    parser.file.toLatin1().constData(), parser.line_no);
2231
        } else {
2232
            bool recursive = false;
2233
            if(args.count() == 2)
2234
                recursive = (args[1].toLower() == "true" || args[1].toInt());
2235
            QStringList dirs;
2236
            QString r = Option::fixPathToLocalOS(args[0]);
2237
            int slash = r.lastIndexOf(QDir::separator());
2238
            if(slash != -1) {
2239
                dirs.append(r.left(slash));
2240
                r = r.mid(slash+1);
2241
            } else {
2242
                dirs.append("");
2243
            }
2244
2245
            const QRegExp regex(r, Qt::CaseSensitive, QRegExp::Wildcard);
2246
            for(int d = 0; d < dirs.count(); d++) {
2247
                QString dir = dirs[d];
2248
                if(!dir.isEmpty() && !dir.endsWith(Option::dir_sep))
2249
                    dir += "/";
2250
2251
                QDir qdir(dir);
2252
                for(int i = 0; i < (int)qdir.count(); ++i) {
2253
                    if(qdir[i] == "." || qdir[i] == "..")
2254
                        continue;
2255
                    QString fname = dir + qdir[i];
2256
                    if(QFileInfo(fname).isDir()) {
2257
                        if(recursive)
2258
                            dirs.append(fname);
2259
                    }
2260
                    if(regex.exactMatch(qdir[i]))
2261
                        ret += fname;
2262
                }
2263
            }
2264
        }
2265
        break; }
2266
    case E_PROMPT: {
2267
        if(args.count() != 1) {
2268
            fprintf(stderr, "%s:%d prompt(question) requires one argument.\n",
2269
                    parser.file.toLatin1().constData(), parser.line_no);
2270
        } else if(projectFile() == "-") {
2271
            fprintf(stderr, "%s:%d prompt(question) cannot be used when '-o -' is used.\n",
2272
                    parser.file.toLatin1().constData(), parser.line_no);
2273
        } else {
2274
            QString msg = fixEnvVariables(args.first());
2275
            if(!msg.endsWith("?"))
2276
                msg += "?";
2277
            fprintf(stderr, "Project %s: %s ", func.toUpper().toLatin1().constData(),
2278
                    msg.toLatin1().constData());
2279
2280
            QFile qfile;
2281
            if(qfile.open(stdin, QIODevice::ReadOnly)) {
2282
                QTextStream t(&qfile);
2283
                ret = split_value_list(t.readLine());
2284
            }
2285
        }
2286
        break; }
2287
    case E_REPLACE: {
2288
        if(args.count() != 3 ) {
2289
            fprintf(stderr, "%s:%d replace(var, before, after) requires three arguments\n",
2290
                    parser.file.toLatin1().constData(), parser.line_no);
2291
        } else {
2292
            const QRegExp before( args[1] );
2293
            const QString after( args[2] );
2294
            QStringList var = values(args.first(), place);
2295
            for(QStringList::Iterator it = var.begin(); it != var.end(); ++it)
2296
                ret += it->replace(before, after);
2297
        }
2298
        break; }
2299
    case E_SIZE: {
2300
        if(args.count() != 1) {
2301
            fprintf(stderr, "%s:%d: size(var) requires one argument.\n",
2302
                    parser.file.toLatin1().constData(), parser.line_no);
2303
        } else {
2304
            //QString target = args[0];
2305
            int size = values(args[0]).size();
2306
            ret += QString::number(size);
2307
        }
2308
        break; }
2309
    case E_GENERATE_UID:
2310
        if (args.count() != 1) {
2311
            fprintf(stderr, "%s:%d: generate_uid(var) requires one argument.\n",
2312
                    parser.file.toLatin1().constData(), parser.line_no);
2313
        } else {
2314
            ret += generate_test_uid(args.first());
2315
        }
2316
        break;
2317
    default: {
2318
        fprintf(stderr, "%s:%d: Unknown replace function: %s\n",
2319
                parser.file.toLatin1().constData(), parser.line_no,
2320
                func.toLatin1().constData());
2321
        break; }
2322
    }
2323
    return ret;
2324
}
2325
2326
bool
2327
QMakeProject::doProjectTest(QString func, QStringList args, QMap<QString, QStringList> &place)
2328
{
2329
    QList<QStringList> args_list;
2330
    for(int i = 0; i < args.size(); ++i) {
2331
        QStringList arg = split_value_list(args[i]), tmp;
2332
        for(int i = 0; i < arg.size(); ++i)
2333
            tmp += doVariableReplaceExpand(arg[i], place);
2334
        args_list += tmp;
2335
    }
2336
    return doProjectTest(func, args_list, place);
2337
}
2338
2339
bool
2340
QMakeProject::doProjectTest(QString func, QList<QStringList> args_list, QMap<QString, QStringList> &place)
2341
{
2342
    func = func.trimmed();
2343
2344
    if(testFunctions.contains(func)) {
2345
        FunctionBlock *defined = testFunctions[func];
2346
        QStringList ret;
2347
        function_blocks.push(defined);
2348
        defined->exec(args_list, this, place, ret);
2349
        Q_ASSERT(function_blocks.pop() == defined);
2350
2351
        if(ret.isEmpty()) {
2352
            return true;
2353
        } else {
2354
            if(ret.first() == "true") {
2355
                return true;
2356
            } else if(ret.first() == "false") {
2357
                return false;
2358
            } else {
2359
                bool ok;
2360
                int val = ret.first().toInt(&ok);
2361
                if(ok)
2362
                    return val;
2363
                fprintf(stderr, "%s:%d Unexpected return value from test %s [%s].\n",
2364
                        parser.file.toLatin1().constData(),
2365
                        parser.line_no, func.toLatin1().constData(),
2366
                        ret.join("::").toLatin1().constData());
2367
            }
2368
            return false;
2369
        }
2370
        return false;
2371
    }
2372
2373
    QStringList args; //why don't the builtin functions just use args_list? --Sam
2374
    for(int i = 0; i < args_list.size(); ++i)
2375
        args += args_list[i].join(QString(Option::field_sep));
2376
2377
    TestFunc func_t = qmake_testFunctions().value(func);
2378
    debug_msg(1, "Running project test: %s(%s) [%d]",
2379
              func.toLatin1().constData(), args.join("::").toLatin1().constData(), func_t);
2380
2381
    switch(func_t) {
2382
    case T_REQUIRES:
2383
        return doProjectCheckReqs(args, place);
2384
    case T_LESSTHAN:
2385
    case T_GREATERTHAN: {
2386
        if(args.count() != 2) {
2387
            fprintf(stderr, "%s:%d: %s(variable, value) requires two arguments.\n", parser.file.toLatin1().constData(),
2388
                    parser.line_no, func.toLatin1().constData());
2389
            return false;
2390
        }
2391
        QString rhs(args[1]), lhs(values(args[0], place).join(QString(Option::field_sep)));
2392
        bool ok;
2393
        int rhs_int = rhs.toInt(&ok);
2394
        if(ok) { // do integer compare
2395
            int lhs_int = lhs.toInt(&ok);
2396
            if(ok) {
2397
                if(func_t == T_GREATERTHAN)
2398
                    return lhs_int > rhs_int;
2399
                return lhs_int < rhs_int;
2400
            }
2401
        }
2402
        if(func_t == T_GREATERTHAN)
2403
            return lhs > rhs;
2404
        return lhs < rhs; }
2405
    case T_IF: {
2406
        if(args.count() != 1) {
2407
            fprintf(stderr, "%s:%d: if(condition) requires one argument.\n", parser.file.toLatin1().constData(),
2408
                    parser.line_no);
2409
            return false;
2410
        }
2411
        const QString cond = args.first();
2412
        const QChar *d = cond.unicode();
2413
        QChar quote = 0;
2414
        bool ret = true, or_op = false;
2415
        QString test;
2416
        for(int d_off = 0, parens = 0, d_len = cond.size(); d_off < d_len; ++d_off) {
2417
            if(!quote.isNull()) {
2418
                if(*(d+d_off) == quote)
2419
                    quote = QChar();
2420
            } else if(*(d+d_off) == '(') {
2421
                ++parens;
2422
            } else if(*(d+d_off) == ')') {
2423
                --parens;
2424
            } else if(*(d+d_off) == '"' /*|| *(d+d_off) == '\''*/) {
2425
                quote = *(d+d_off);
2426
            }
2427
            if(!parens && quote.isNull() && (*(d+d_off) == QLatin1Char(':') || *(d+d_off) == QLatin1Char('|') || d_off == d_len-1)) {
2428
                if(d_off == d_len-1)
2429
                    test += *(d+d_off);
2430
                if(!test.isEmpty()) {
2431
                    const bool success = doProjectTest(test, place);
2432
                    test = "";
2433
                    if(or_op)
2434
                        ret = ret || success;
2435
                    else
2436
                        ret = ret && success;
2437
                }
2438
                if(*(d+d_off) == QLatin1Char(':')) {
2439
                    or_op = false;
2440
                } else if(*(d+d_off) == QLatin1Char('|')) {
2441
                    or_op = true;
2442
                }
2443
            } else {
2444
                test += *(d+d_off);
2445
            }
2446
        }
2447
        return ret; }
2448
    case T_EQUALS:
2449
        if(args.count() != 2) {
2450
            fprintf(stderr, "%s:%d: %s(variable, value) requires two arguments.\n", parser.file.toLatin1().constData(),
2451
                    parser.line_no, func.toLatin1().constData());
2452
            return false;
2453
        }
2454
        return values(args[0], place).join(QString(Option::field_sep)) == args[1];
2455
    case T_EXISTS: {
2456
        if(args.count() != 1) {
2457
            fprintf(stderr, "%s:%d: exists(file) requires one argument.\n", parser.file.toLatin1().constData(),
2458
                    parser.line_no);
2459
            return false;
2460
        }
2461
        QString file = args.first();
2462
        file = Option::fixPathToLocalOS(file);
2463
2464
        if(QFile::exists(file))
2465
            return true;
2466
        //regular expression I guess
2467
        QString dirstr = qmake_getpwd();
2468
        int slsh = file.lastIndexOf(Option::dir_sep);
2469
        if(slsh != -1) {
2470
            dirstr = file.left(slsh+1);
2471
            file = file.right(file.length() - slsh - 1);
2472
        }
2473
        return QDir(dirstr).entryList(QStringList(file)).count(); }
2474
    case T_EXPORT:
2475
        if(args.count() != 1) {
2476
            fprintf(stderr, "%s:%d: export(variable) requires one argument.\n", parser.file.toLatin1().constData(),
2477
                    parser.line_no);
2478
            return false;
2479
        }
2480
        for(int i = 0; i < function_blocks.size(); ++i) {
2481
            FunctionBlock *f = function_blocks.at(i);
2482
            f->vars[args[0]] = values(args[0], place);
2483
            if(!i && f->calling_place)
2484
                (*f->calling_place)[args[0]] = values(args[0], place);
2485
        }
2486
        return true;
2487
    case T_CLEAR:
2488
        if(args.count() != 1) {
2489
            fprintf(stderr, "%s:%d: clear(variable) requires one argument.\n", parser.file.toLatin1().constData(),
2490
                    parser.line_no);
2491
            return false;
2492
        }
2493
        if(!place.contains(args[0]))
2494
            return false;
2495
        place[args[0]].clear();
2496
        return true;
2497
    case T_UNSET:
2498
        if(args.count() != 1) {
2499
            fprintf(stderr, "%s:%d: unset(variable) requires one argument.\n", parser.file.toLatin1().constData(),
2500
                    parser.line_no);
2501
            return false;
2502
        }
2503
        if(!place.contains(args[0]))
2504
            return false;
2505
        place.remove(args[0]);
2506
        return true;
2507
    case T_EVAL: {
2508
        if(args.count() < 1 && 0) {
2509
            fprintf(stderr, "%s:%d: eval(project) requires one argument.\n", parser.file.toLatin1().constData(),
2510
                    parser.line_no);
2511
            return false;
2512
        }
2513
        QString project = args.join(" ");
2514
        parser_info pi = parser;
2515
        parser.from_file = false;
2516
        parser.file = "(eval)";
2517
        parser.line_no = 0;
2518
        QTextStream t(&project, QIODevice::ReadOnly);
2519
        bool ret = read(t, place);
2520
        parser = pi;
2521
        return ret; }
2522
    case T_CONFIG: {
2523
        if(args.count() < 1 || args.count() > 2) {
2524
            fprintf(stderr, "%s:%d: CONFIG(config) requires one argument.\n", parser.file.toLatin1().constData(),
2525
                    parser.line_no);
2526
            return false;
2527
        }
2528
        if(args.count() == 1)
2529
            return isActiveConfig(args[0]);
2530
        const QStringList mutuals = args[1].split('|');
2531
        const QStringList &configs = values("CONFIG", place);
2532
        for(int i = configs.size()-1; i >= 0; i--) {
2533
            for(int mut = 0; mut < mutuals.count(); mut++) {
2534
                if(configs[i] == mutuals[mut].trimmed())
2535
                    return (configs[i] == args[0]);
2536
            }
2537
        }
2538
        return false; }
2539
    case T_SYSTEM: {
2540
        bool setup_env = true;
2541
        if(args.count() < 1 || args.count() > 2) {
2542
            fprintf(stderr, "%s:%d: system(exec) requires one argument.\n", parser.file.toLatin1().constData(),
2543
                    parser.line_no);
2544
            return false;
2545
        }
2546
        if(args.count() == 2) {
2547
            const QString sarg = args[1];
2548
            setup_env = (sarg.toLower() == "true" || sarg.toInt());
2549
        }
2550
        QMakeProjectEnv env;
2551
        if(setup_env)
2552
            env.execute(place);
2553
        bool ret = system(args[0].toLatin1().constData()) == 0;
2554
        return ret; }
2555
    case T_RETURN:
2556
        if(function_blocks.isEmpty()) {
2557
            fprintf(stderr, "%s:%d unexpected return()\n",
2558
                    parser.file.toLatin1().constData(), parser.line_no);
2559
        } else {
2560
            FunctionBlock *f = function_blocks.top();
2561
            f->cause_return = true;
2562
            if(args_list.count() >= 1)
2563
                f->return_value += args_list[0];
2564
        }
2565
        return true;
2566
    case T_BREAK:
2567
        if(iterator)
2568
            iterator->cause_break = true;
2569
        else if(!scope_blocks.isEmpty())
2570
            scope_blocks.top().ignore = true;
2571
        else
2572
            fprintf(stderr, "%s:%d unexpected break()\n",
2573
                    parser.file.toLatin1().constData(), parser.line_no);
2574
        return true;
2575
    case T_NEXT:
2576
        if(iterator)
2577
            iterator->cause_next = true;
2578
        else
2579
            fprintf(stderr, "%s:%d unexpected next()\n",
2580
                    parser.file.toLatin1().constData(), parser.line_no);
2581
        return true;
2582
    case T_DEFINED:
2583
        if(args.count() < 1 || args.count() > 2) {
2584
            fprintf(stderr, "%s:%d: defined(function) requires one argument.\n",
2585
                    parser.file.toLatin1().constData(), parser.line_no);
2586
        } else {
2587
           if(args.count() > 1) {
2588
               if(args[1] == "test")
2589
                   return testFunctions.contains(args[0]);
2590
               else if(args[1] == "replace")
2591
                   return replaceFunctions.contains(args[0]);
2592
               fprintf(stderr, "%s:%d: defined(function, type): unexpected type [%s].\n",
2593
                       parser.file.toLatin1().constData(), parser.line_no,
2594
                       args[1].toLatin1().constData());
2595
            } else {
2596
                if(replaceFunctions.contains(args[0]) || testFunctions.contains(args[0]))
2597
                    return true;
2598
            }
2599
        }
2600
        return false;
2601
    case T_CONTAINS: {
2602
        if(args.count() < 2 || args.count() > 3) {
2603
            fprintf(stderr, "%s:%d: contains(var, val) requires at lesat 2 arguments.\n",
2604
                    parser.file.toLatin1().constData(), parser.line_no);
2605
            return false;
2606
        }
2607
        QRegExp regx(args[1]);
2608
        const QStringList &l = values(args[0], place);
2609
        if(args.count() == 2) {
2610
            for(int i = 0; i < l.size(); ++i) {
2611
                const QString val = l[i];
2612
                if(regx.exactMatch(val) || val == args[1])
2613
                    return true;
2614
            }
2615
        } else {
2616
            const QStringList mutuals = args[2].split('|');
2617
            for(int i = l.size()-1; i >= 0; i--) {
2618
                const QString val = l[i];
2619
                for(int mut = 0; mut < mutuals.count(); mut++) {
2620
                    if(val == mutuals[mut].trimmed())
2621
                        return (regx.exactMatch(val) || val == args[1]);
2622
                }
2623
            }
2624
        }
2625
        return false; }
2626
    case T_INFILE: {
2627
        if(args.count() < 2 || args.count() > 3) {
2628
            fprintf(stderr, "%s:%d: infile(file, var, val) requires at least 2 arguments.\n",
2629
                    parser.file.toLatin1().constData(), parser.line_no);
2630
            return false;
2631
        }
2632
2633
        bool ret = false;
2634
        QMap<QString, QStringList> tmp;
2635
        if(doProjectInclude(Option::fixPathToLocalOS(args[0]), IncludeFlagNewParser, tmp) == IncludeSuccess) {
2636
            if(tmp.contains("QMAKE_INTERNAL_INCLUDED_FILES")) {
2637
                QStringList &out = place["QMAKE_INTERNAL_INCLUDED_FILES"];
2638
                const QStringList &in = tmp["QMAKE_INTERNAL_INCLUDED_FILES"];
2639
                for(int i = 0; i < in.size(); ++i) {
2640
                    if(out.indexOf(in[i]) == -1)
2641
                        out += in[i];
2642
                }
2643
            }
2644
            if(args.count() == 2) {
2645
                ret = tmp.contains(args[1]);
2646
            } else {
2647
                QRegExp regx(args[2]);
2648
                const QStringList &l = tmp[args[1]];
2649
                for(QStringList::ConstIterator it = l.begin(); it != l.end(); ++it) {
2650
                    if(regx.exactMatch((*it)) || (*it) == args[2]) {
2651
                        ret = true;
2652
                        break;
2653
                    }
2654
                }
2655
            }
2656
        }
2657
        return ret; }
2658
    case T_COUNT:
2659
        if(args.count() != 2 && args.count() != 3) {
2660
            fprintf(stderr, "%s:%d: count(var, count) requires two arguments.\n", parser.file.toLatin1().constData(),
2661
                    parser.line_no);
2662
            return false;
2663
        }
2664
        if(args.count() == 3) {
2665
            QString comp = args[2];
2666
            if(comp == ">" || comp == "greaterThan")
2667
                return values(args[0], place).count() > args[1].toInt();
2668
            if(comp == ">=")
2669
                return values(args[0], place).count() >= args[1].toInt();
2670
            if(comp == "<" || comp == "lessThan")
2671
                return values(args[0], place).count() < args[1].toInt();
2672
            if(comp == "<=")
2673
                return values(args[0], place).count() <= args[1].toInt();
2674
            if(comp == "equals" || comp == "isEqual" || comp == "=" || comp == "==")
2675
                return values(args[0], place).count() == args[1].toInt();
2676
            fprintf(stderr, "%s:%d: unexpected modifier to count(%s)\n", parser.file.toLatin1().constData(),
2677
                    parser.line_no, comp.toLatin1().constData());
2678
            return false;
2679
        }
2680
        return values(args[0], place).count() == args[1].toInt();
2681
    case T_ISEMPTY:
2682
        if(args.count() != 1) {
2683
            fprintf(stderr, "%s:%d: isEmpty(var) requires one argument.\n", parser.file.toLatin1().constData(),
2684
                    parser.line_no);
2685
            return false;
2686
        }
2687
        return values(args[0], place).isEmpty();
2688
    case T_INCLUDE:
2689
    case T_LOAD: {
2690
        QString parseInto;
2691
        const bool include_statement = (func_t == T_INCLUDE);
2692
        bool ignore_error = false;
2693
        if(args.count() >= 2) {
2694
            if(func_t == T_INCLUDE) {
2695
                parseInto = args[1];
2696
                if (args.count() == 3){
2697
                    QString sarg = args[2];
2698
                    if (sarg.toLower() == "true" || sarg.toInt())
2699
                        ignore_error = true;
2700
                }
2701
            } else {
2702
                QString sarg = args[1];
2703
                ignore_error = (sarg.toLower() == "true" || sarg.toInt());
2704
            }
2705
        } else if(args.count() != 1) {
2706
            QString func_desc = "load(feature)";
2707
            if(include_statement)
2708
                func_desc = "include(file)";
2709
            fprintf(stderr, "%s:%d: %s requires one argument.\n", parser.file.toLatin1().constData(),
2710
                    parser.line_no, func_desc.toLatin1().constData());
2711
            return false;
2712
        }
2713
        QString file = args.first();
2714
        file = Option::fixPathToLocalOS(file);
2715
        uchar flags = IncludeFlagNone;
2716
        if(!include_statement)
2717
            flags |= IncludeFlagFeature;
2718
        IncludeStatus stat = IncludeFailure;
2719
        if(!parseInto.isEmpty()) {
2720
            QMap<QString, QStringList> symbols;
2721
            stat = doProjectInclude(file, flags|IncludeFlagNewProject, symbols);
2722
            if(stat == IncludeSuccess) {
2723
                QMap<QString, QStringList> out_place;
2724
                for(QMap<QString, QStringList>::ConstIterator it = place.begin(); it != place.end(); ++it) {
2725
                    const QString var = it.key();
2726
                    if(var != parseInto && !var.startsWith(parseInto + "."))
2727
                        out_place.insert(var, it.value());
2728
                }
2729
                for(QMap<QString, QStringList>::ConstIterator it = symbols.begin(); it != symbols.end(); ++it) {
2730
                    const QString var = it.key();
2731
                    if(!var.startsWith("."))
2732
                        out_place.insert(parseInto + "." + it.key(), it.value());
2733
                }
2734
                place = out_place;
2735
            }
2736
        } else {
2737
            stat = doProjectInclude(file, flags, place);
2738
        }
2739
        if(stat == IncludeFeatureAlreadyLoaded) {
2740
            warn_msg(WarnParser, "%s:%d: Duplicate of loaded feature %s",
2741
                     parser.file.toLatin1().constData(), parser.line_no, file.toLatin1().constData());
2742
        } else if(stat == IncludeNoExist && !ignore_error) {
2743
            warn_msg(WarnAll, "%s:%d: Unable to find file for inclusion %s",
2744
                     parser.file.toLatin1().constData(), parser.line_no, file.toLatin1().constData());
2745
            return false;
2746
        } else if(stat >= IncludeFailure) {
2747
            if(!ignore_error) {
2748
                printf("Project LOAD(): Feature %s cannot be found.\n", file.toLatin1().constData());
2749
                if (!ignore_error)
2750
#if defined(QT_BUILD_QMAKE_LIBRARY)
2751
                    return false;
2752
#else
2753
                    exit(3);
2754
#endif
2755
            }
2756
            return false;
2757
        }
2758
        return true; }
2759
    case T_DEBUG: {
2760
        if(args.count() != 2) {
2761
            fprintf(stderr, "%s:%d: debug(level, message) requires one argument.\n", parser.file.toLatin1().constData(),
2762
                    parser.line_no);
2763
            return false;
2764
        }
2765
        QString msg = fixEnvVariables(args[1]);
2766
        debug_msg(args[0].toInt(), "Project DEBUG: %s", msg.toLatin1().constData());
2767
        return true; }
2768
    case T_ERROR:
2769
    case T_MESSAGE:
2770
    case T_WARNING: {
2771
        if(args.count() != 1) {
2772
            fprintf(stderr, "%s:%d: %s(message) requires one argument.\n", parser.file.toLatin1().constData(),
2773
                    parser.line_no, func.toLatin1().constData());
2774
            return false;
2775
        }
2776
        QString msg = fixEnvVariables(args.first());
2777
        fprintf(stderr, "Project %s: %s\n", func.toUpper().toLatin1().constData(), msg.toLatin1().constData());
2778
        if(func == "error")
2779
#if defined(QT_BUILD_QMAKE_LIBRARY)
2780
            return false;
2781
#else
2782
            exit(2);
2783
#endif
2784
        return true; }
2785
    default:
2786
        fprintf(stderr, "%s:%d: Unknown test function: %s\n", parser.file.toLatin1().constData(), parser.line_no,
2787
                func.toLatin1().constData());
2788
    }
2789
    return false;
2790
}
2791
2792
bool
2793
QMakeProject::doProjectCheckReqs(const QStringList &deps, QMap<QString, QStringList> &place)
2794
{
2795
    bool ret = false;
2796
    for(QStringList::ConstIterator it = deps.begin(); it != deps.end(); ++it) {
2797
        bool test = doProjectTest((*it), place);
2798
        if(!test) {
2799
            debug_msg(1, "Project Parser: %s:%d Failed test: REQUIRES = %s",
2800
                      parser.file.toLatin1().constData(), parser.line_no,
2801
                      (*it).toLatin1().constData());
2802
            place["QMAKE_FAILED_REQUIREMENTS"].append((*it));
2803
            ret = false;
2804
        }
2805
    }
2806
    return ret;
2807
}
2808
2809
bool
2810
QMakeProject::test(const QString &v)
2811
{
2812
    QMap<QString, QStringList> tmp = vars;
2813
    return doProjectTest(v, tmp);
2814
}
2815
2816
bool
2817
QMakeProject::test(const QString &func, const QList<QStringList> &args)
2818
{
2819
    QMap<QString, QStringList> tmp = vars;
2820
    return doProjectTest(func, args, tmp);
2821
}
2822
2823
QStringList
2824
QMakeProject::expand(const QString &str)
2825
{
2826
    bool ok;
2827
    QMap<QString, QStringList> tmp = vars;
2828
    const QStringList ret = doVariableReplaceExpand(str, tmp, &ok);
2829
    if(ok)
2830
        return ret;
2831
    return QStringList();
2832
}
2833
2834
QStringList
2835
QMakeProject::expand(const QString &func, const QList<QStringList> &args)
2836
{
2837
    QMap<QString, QStringList> tmp = vars;
2838
    return doProjectExpand(func, args, tmp);
2839
}
2840
2841
bool
2842
QMakeProject::doVariableReplace(QString &str, QMap<QString, QStringList> &place)
2843
{
2844
    bool ret;
2845
    str = doVariableReplaceExpand(str, place, &ret).join(QString(Option::field_sep));
2846
    return ret;
2847
}
2848
2849
QStringList
2850
QMakeProject::doVariableReplaceExpand(const QString &str, QMap<QString, QStringList> &place, bool *ok)
2851
{
2852
    QStringList ret;
2853
    if(ok)
2854
        *ok = true;
2855
    if(str.isEmpty())
2856
        return ret;
2857
2858
    const ushort LSQUARE = '[';
2859
    const ushort RSQUARE = ']';
2860
    const ushort LCURLY = '{';
2861
    const ushort RCURLY = '}';
2862
    const ushort LPAREN = '(';
2863
    const ushort RPAREN = ')';
2864
    const ushort DOLLAR = '$';
2865
    const ushort SLASH = '\\';
2866
    const ushort UNDERSCORE = '_';
2867
    const ushort DOT = '.';
2868
    const ushort SPACE = ' ';
2869
    const ushort TAB = '\t';
2870
    const ushort SINGLEQUOTE = '\'';
2871
    const ushort DOUBLEQUOTE = '"';
2872
2873
    ushort unicode, quote = 0;
2874
    const QChar *str_data = str.data();
2875
    const int str_len = str.length();
2876
2877
    ushort term;
2878
    QString var, args;
2879
2880
    int replaced = 0;
2881
    QString current;
2882
    for(int i = 0; i < str_len; ++i) {
2883
        unicode = str_data[i].unicode();
2884
        const int start_var = i;
2885
        if(unicode == DOLLAR && str_len > i+2) {
2886
            unicode = str_data[++i].unicode();
2887
            if(unicode == DOLLAR) {
2888
                term = 0;
2889
                var.clear();
2890
                args.clear();
2891
                enum { VAR, ENVIRON, FUNCTION, PROPERTY } var_type = VAR;
2892
                unicode = str_data[++i].unicode();
2893
                if(unicode == LSQUARE) {
2894
                    unicode = str_data[++i].unicode();
2895
                    term = RSQUARE;
2896
                    var_type = PROPERTY;
2897
                } else if(unicode == LCURLY) {
2898
                    unicode = str_data[++i].unicode();
2899
                    var_type = VAR;
2900
                    term = RCURLY;
2901
                } else if(unicode == LPAREN) {
2902
                    unicode = str_data[++i].unicode();
2903
                    var_type = ENVIRON;
2904
                    term = RPAREN;
2905
                }
2906
                while(1) {
2907
                    if(!(unicode & (0xFF<<8)) &&
2908
                       unicode != DOT && unicode != UNDERSCORE &&
2909
                       //unicode != SINGLEQUOTE && unicode != DOUBLEQUOTE &&
2910
                       (unicode < 'a' || unicode > 'z') && (unicode < 'A' || unicode > 'Z') &&
2911
                       (unicode < '0' || unicode > '9'))
2912
                        break;
2913
                    var.append(QChar(unicode));
2914
                    if(++i == str_len)
2915
                        break;
2916
                    unicode = str_data[i].unicode();
2917
                    // at this point, i points to either the 'term' or 'next' character (which is in unicode)
2918
                }
2919
                if(var_type == VAR && unicode == LPAREN) {
2920
                    var_type = FUNCTION;
2921
                    int depth = 0;
2922
                    while(1) {
2923
                        if(++i == str_len)
2924
                            break;
2925
                        unicode = str_data[i].unicode();
2926
                        if(unicode == LPAREN) {
2927
                            depth++;
2928
                        } else if(unicode == RPAREN) {
2929
                            if(!depth)
2930
                                break;
2931
                            --depth;
2932
                        }
2933
                        args.append(QChar(unicode));
2934
                    }
2935
                    if(++i < str_len)
2936
                        unicode = str_data[i].unicode();
2937
                    else
2938
                        unicode = 0;
2939
                    // at this point i is pointing to the 'next' character (which is in unicode)
2940
                    // this might actually be a term character since you can do $${func()}
2941
                }
2942
                if(term) {
2943
                    if(unicode != term) {
2944
                        qmake_error_msg("Missing " + QString(term) + " terminator [found " + (unicode?QString(unicode):QString("end-of-line")) + "]");
2945
                        if(ok)
2946
                            *ok = false;
2947
                        return QStringList();
2948
                    }
2949
                } else {
2950
                    // move the 'cursor' back to the last char of the thing we were looking at
2951
                    --i;
2952
                }
2953
                // since i never points to the 'next' character, there is no reason for this to be set
2954
                unicode = 0;
2955
2956
                QStringList replacement;
2957
                if(var_type == ENVIRON) {
2958
                    replacement = split_value_list(QString::fromLocal8Bit(qgetenv(var.toLatin1().constData())));
2959
                } else if(var_type == PROPERTY) {
2960
                    if(prop)
2961
                        replacement = split_value_list(prop->value(var));
2962
                } else if(var_type == FUNCTION) {
2963
                    replacement = doProjectExpand(var, args, place);
2964
                } else if(var_type == VAR) {
2965
                    replacement = values(var, place);
2966
                }
2967
                if(!(replaced++) && start_var)
2968
                    current = str.left(start_var);
2969
                if(!replacement.isEmpty()) {
2970
                    if(quote) {
2971
                        current += replacement.join(QString(Option::field_sep));
2972
                    } else {
2973
                        current += replacement.takeFirst();
2974
                        if(!replacement.isEmpty()) {
2975
                            if(!current.isEmpty())
2976
                                ret.append(current);
2977
                            current = replacement.takeLast();
2978
                            if(!replacement.isEmpty())
2979
                                ret += replacement;
2980
                        }
2981
                    }
2982
                }
2983
                debug_msg(2, "Project Parser [var replace]: %s -> %s",
2984
                          str.toLatin1().constData(), var.toLatin1().constData(),
2985
                          replacement.join("::").toLatin1().constData());
2986
            } else {
2987
                if(replaced)
2988
                    current.append("$");
2989
            }
2990
        }
2991
        if(quote && unicode == quote) {
2992
            unicode = 0;
2993
            quote = 0;
2994
        } else if(unicode == SLASH) {
2995
            bool escape = false;
2996
            const char *symbols = "[]{}()$\\'\"";
2997
            for(const char *s = symbols; *s; ++s) {
2998
                if(str_data[i+1].unicode() == (ushort)*s) {
2999
                    i++;
3000
                    escape = true;
3001
                    if(!(replaced++))
3002
                        current = str.left(start_var);
3003
                    current.append(str.at(i));
3004
                    break;
3005
                }
3006
            }
3007
            if(escape || !replaced)
3008
                unicode =0;
3009
        } else if(!quote && (unicode == SINGLEQUOTE || unicode == DOUBLEQUOTE)) {
3010
            quote = unicode;
3011
            unicode = 0;
3012
            if(!(replaced++) && i)
3013
                current = str.left(i);
3014
        } else if(!quote && (unicode == SPACE || unicode == TAB)) {
3015
            unicode = 0;
3016
            if(!current.isEmpty()) {
3017
                ret.append(current);
3018
                current.clear();
3019
            }
3020
        }
3021
        if(replaced && unicode)
3022
            current.append(QChar(unicode));
3023
    }
3024
    if(!replaced)
3025
        ret = QStringList(str);
3026
    else if(!current.isEmpty())
3027
        ret.append(current);
3028
    //qDebug() << "REPLACE" << str << ret;
3029
    return ret;
3030
}
3031
3032
QStringList &QMakeProject::values(const QString &_var, QMap<QString, QStringList> &place)
3033
{
3034
    QString var = varMap(_var);
3035
    if(var == QLatin1String("LITERAL_WHITESPACE")) { //a real space in a token)
3036
        var = ".BUILTIN." + var;
3037
        place[var] = QStringList(QLatin1String("\t"));
3038
    } else if(var == QLatin1String("LITERAL_DOLLAR")) { //a real $
3039
        var = ".BUILTIN." + var;
3040
        place[var] = QStringList(QLatin1String("$"));
3041
    } else if(var == QLatin1String("LITERAL_HASH")) { //a real #
3042
        var = ".BUILTIN." + var;
3043
        place[var] = QStringList("#");
3044
    } else if(var == QLatin1String("OUT_PWD")) { //the out going dir
3045
        var = ".BUILTIN." + var;
3046
        place[var] =  QStringList(Option::output_dir);
3047
    } else if(var == QLatin1String("PWD") ||  //current working dir (of _FILE_)
3048
              var == QLatin1String("IN_PWD")) {
3049
        var = ".BUILTIN." + var;
3050
        place[var] = QStringList(qmake_getpwd());
3051
    } else if(var == QLatin1String("DIR_SEPARATOR")) {
3052
        var = ".BUILTIN." + var;
3053
        place[var] =  QStringList(Option::dir_sep);
3054
    } else if(var == QLatin1String("DIRLIST_SEPARATOR")) {
3055
        var = ".BUILTIN." + var;
3056
        place[var] = QStringList(Option::dirlist_sep);
3057
    } else if(var == QLatin1String("_LINE_")) { //parser line number
3058
        var = ".BUILTIN." + var;
3059
        place[var] = QStringList(QString::number(parser.line_no));
3060
    } else if(var == QLatin1String("_FILE_")) { //parser file
3061
        var = ".BUILTIN." + var;
3062
        place[var] = QStringList(parser.file);
3063
    } else if(var == QLatin1String("_DATE_")) { //current date/time
3064
        var = ".BUILTIN." + var;
3065
        place[var] = QStringList(QDateTime::currentDateTime().toString());
3066
    } else if(var == QLatin1String("_PRO_FILE_")) {
3067
        var = ".BUILTIN." + var;
3068
        place[var] = QStringList(pfile);
3069
    } else if(var == QLatin1String("_PRO_FILE_PWD_")) {
3070
        var = ".BUILTIN." + var;
3071
        place[var] = QStringList(pfile.isEmpty() ? qmake_getpwd() : QFileInfo(pfile).absolutePath());
3072
    } else if(var == QLatin1String("_QMAKE_CACHE_")) {
3073
        var = ".BUILTIN." + var;
3074
        if(Option::mkfile::do_cache)
3075
            place[var] = QStringList(Option::mkfile::cachefile);
3076
    } else if(var == QLatin1String("TEMPLATE")) {
3077
        if(!Option::user_template.isEmpty()) {
3078
            var = ".BUILTIN.USER." + var;
3079
            place[var] =  QStringList(Option::user_template);
3080
        } else if(!place[var].isEmpty()) {
3081
            QString orig_template = place["TEMPLATE"].first(), real_template;
3082
            if(!Option::user_template_prefix.isEmpty() && !orig_template.startsWith(Option::user_template_prefix))
3083
                real_template = Option::user_template_prefix + orig_template;
3084
            if(real_template.endsWith(".t"))
3085
                real_template = real_template.left(real_template.length()-2);
3086
            if(!real_template.isEmpty()) {
3087
                var = ".BUILTIN." + var;
3088
                place[var] = QStringList(real_template);
3089
            }
3090
        } else {
3091
            var = ".BUILTIN." + var;
3092
            place[var] =  QStringList("app");
3093
        }
3094
    } else if(var.startsWith(QLatin1String("QMAKE_HOST."))) {
3095
        QString ret, type = var.mid(11);
3096
#if defined(Q_OS_WIN32)
3097
        if(type == "os") {
3098
            ret = "Windows";
3099
        } else if(type == "name") {
3100
            DWORD name_length = 1024;
3101
            wchar_t name[1024];
3102
            if (GetComputerName(name, &name_length))
3103
                ret = QString::fromWCharArray(name);
3104
        } else if(type == "version" || type == "version_string") {
3105
            QSysInfo::WinVersion ver = QSysInfo::WindowsVersion;
3106
            if(type == "version")
3107
                ret = QString::number(ver);
3108
            else if(ver == QSysInfo::WV_Me)
3109
                ret = "WinMe";
3110
            else if(ver == QSysInfo::WV_95)
3111
                ret = "Win95";
3112
            else if(ver == QSysInfo::WV_98)
3113
                ret = "Win98";
3114
            else if(ver == QSysInfo::WV_NT)
3115
                ret = "WinNT";
3116
            else if(ver == QSysInfo::WV_2000)
3117
                ret = "Win2000";
3118
            else if(ver == QSysInfo::WV_2000)
3119
                ret = "Win2003";
3120
            else if(ver == QSysInfo::WV_XP)
3121
                ret = "WinXP";
3122
            else if(ver == QSysInfo::WV_VISTA)
3123
                ret = "WinVista";
3124
            else
3125
                ret = "Unknown";
3126
        } else if(type == "arch") {
3127
            SYSTEM_INFO info;
3128
            GetSystemInfo(&info);
3129
            switch(info.wProcessorArchitecture) {
3130
#ifdef PROCESSOR_ARCHITECTURE_AMD64
3131
            case PROCESSOR_ARCHITECTURE_AMD64:
3132
                ret = "x86_64";
3133
                break;
3134
#endif
3135
            case PROCESSOR_ARCHITECTURE_INTEL:
3136
                ret = "x86";
3137
                break;
3138
            case PROCESSOR_ARCHITECTURE_IA64:
3139
#ifdef PROCESSOR_ARCHITECTURE_IA32_ON_WIN64
3140
            case PROCESSOR_ARCHITECTURE_IA32_ON_WIN64:
3141
#endif
3142
                ret = "IA64";
3143
                break;
3144
            default:
3145
                ret = "Unknown";
3146
                break;
3147
            }
3148
        }
3149
#elif defined(Q_OS_UNIX)
3150
        struct utsname name;
3151
        if(!uname(&name)) {
3152
            if(type == "os")
3153
                ret = name.sysname;
3154
            else if(type == "name")
3155
                ret = name.nodename;
3156
            else if(type == "version")
3157
                ret = name.release;
3158
            else if(type == "version_string")
3159
                ret = name.version;
3160
            else if(type == "arch")
3161
                ret = name.machine;
3162
        }
3163
#endif
3164
        var = ".BUILTIN.HOST." + type;
3165
        place[var] = QStringList(ret);
3166
    } else if (var == QLatin1String("QMAKE_DIR_SEP")) {
3167
        if (place[var].isEmpty())
3168
            return values("DIR_SEPARATOR", place);
3169
    } else if (var == QLatin1String("EPOCROOT")) {
3170
        if (place[var].isEmpty())
3171
            place[var] = QStringList(epocRoot());
3172
    }
3173
    //qDebug("REPLACE [%s]->[%s]", qPrintable(var), qPrintable(place[var].join("::")));
3174
    return place[var];
3175
}
3176
3177
QT_END_NAMESPACE