aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/language/language.h
blob: de3b35f11314046d0e64c95142bbc895186a128d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Build Suite.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#ifndef L2_LANGUAGE_HPP
#define L2_LANGUAGE_HPP

#include "jsimports.h"
#include <tools/codelocation.h>
#include <tools/fileinfo.h>
#include <tools/persistentobject.h>
#include <tools/settings.h>
#include <tools/weakpointer.h>

#include <QDataStream>
#include <QMutex>
#include <QProcessEnvironment>
#include <QScriptProgram>
#include <QScriptValue>
#include <QSet>
#include <QSharedPointer>
#include <QString>
#include <QStringList>
#include <QVariant>

QT_BEGIN_NAMESPACE
class QScriptEngine;
QT_END_NAMESPACE

namespace qbs {
namespace Internal {

class PropertyMap: public PersistentObject
{
public:
    typedef QSharedPointer<PropertyMap> Ptr;
    typedef QSharedPointer<const PropertyMap> ConstPtr;

    static Ptr create() { return Ptr(new PropertyMap); }
    Ptr clone() const { return Ptr(new PropertyMap(*this)); }

    const QVariantMap &value() const { return m_value; }
    void setValue(const QVariantMap &value);
    QScriptValue toScriptValue(QScriptEngine *scriptEngine) const;

private:
    PropertyMap();
    PropertyMap(const PropertyMap &other);

    void load(PersistentPool &);
    void store(PersistentPool &) const;

    QVariantMap m_value;
    mutable QHash<QScriptEngine *, QScriptValue> m_scriptValueCache;
    mutable QMutex m_scriptValueCacheMutex;
};

class FileTagger : public PersistentObject
{
public:
    typedef QSharedPointer<FileTagger> Ptr;
    typedef QSharedPointer<const FileTagger> ConstPtr;

    static Ptr create() { return Ptr(new FileTagger); }
    static Ptr create(const QRegExp &artifactExpression, const QStringList &fileTags) {
        return Ptr(new FileTagger(artifactExpression, fileTags));
    }

    const QRegExp &artifactExpression() const { return m_artifactExpression; }
    const QStringList &fileTags() const { return m_fileTags; }

private:
    FileTagger(const QRegExp &artifactExpression, const QStringList &fileTags)
        : m_artifactExpression(artifactExpression), m_fileTags(fileTags)
    { }

    FileTagger()
        : m_artifactExpression(QString(), Qt::CaseSensitive, QRegExp::Wildcard)
    {}

    void load(PersistentPool &);
    void store(PersistentPool &) const;

    QRegExp m_artifactExpression;
    QStringList m_fileTags;
};

class RuleArtifact : public PersistentObject
{
public:
    typedef QSharedPointer<RuleArtifact> Ptr;
    typedef QSharedPointer<const RuleArtifact> ConstPtr;

    static Ptr create() { return Ptr(new RuleArtifact); }

    QString fileName;
    QStringList fileTags;
    bool alwaysUpdated;

    struct Binding
    {
        QStringList name;
        QString code;
        CodeLocation location;
    };

    QVector<Binding> bindings;

private:
    RuleArtifact()
        : alwaysUpdated(true)
    {}

    void load(PersistentPool &pool);
    void store(PersistentPool &pool) const;
};

class SourceArtifact : public PersistentObject
{
public:
    typedef QSharedPointer<SourceArtifact> Ptr;
    typedef QSharedPointer<const SourceArtifact> ConstPtr;

    static Ptr create() { return Ptr(new SourceArtifact); }

    QString absoluteFilePath;
    QSet<QString> fileTags;
    bool overrideFileTags;
    PropertyMap::Ptr properties;

private:
    SourceArtifact() : overrideFileTags(true) {}

    void load(PersistentPool &pool);
    void store(PersistentPool &pool) const;
};

class SourceWildCards : public PersistentObject
{
public:
    typedef QSharedPointer<SourceWildCards> Ptr;
    typedef QSharedPointer<const SourceWildCards> ConstPtr;

    static Ptr create() { return Ptr(new SourceWildCards); }

    QSet<QString> expandPatterns(const QString &baseDir) const;

    // TODO: Use back pointer to Group instead?
    bool recursive;
    QString prefix;

    QStringList patterns;
    QStringList excludePatterns;
    QList<SourceArtifact::Ptr> files;

private:
    SourceWildCards() : recursive(false) {}

    QSet<QString> expandPatterns(const QStringList &patterns, const QString &baseDir) const;
    void expandPatterns(QSet<QString> &files, const QString &baseDir, bool useRecursion,
                      const QStringList &parts, int index = 0) const;

    void load(PersistentPool &pool);
    void store(PersistentPool &pool) const;
};

class ResolvedGroup : public PersistentObject
{
public:
    typedef QSharedPointer<ResolvedGroup> Ptr;
    typedef QSharedPointer<const ResolvedGroup> ConstPtr;

    static Ptr create() { return Ptr(new ResolvedGroup); }

    int qbsLine;

    QString name;
    QList<SourceArtifact::Ptr> files;
    SourceWildCards::Ptr wildcards;
    PropertyMap::Ptr properties;

    QList<SourceArtifact::Ptr> allFiles() const;

private:
    ResolvedGroup() {}

    void load(PersistentPool &pool);
    void store(PersistentPool &pool) const;
};

class PrepareScript: public PersistentObject
{
public:
    typedef QSharedPointer<PrepareScript> Ptr;
    typedef QSharedPointer<const PrepareScript> ConstPtr;

    static Ptr create() { return Ptr(new PrepareScript); }

    QString script;
    CodeLocation location;

private:
    PrepareScript() {}

    void load(PersistentPool &);
    void store(PersistentPool &) const;
};

class ResolvedModule : public PersistentObject
{
public:
    typedef QSharedPointer<ResolvedModule> Ptr;
    typedef QSharedPointer<const ResolvedModule> ConstPtr;

    static Ptr create() { return Ptr(new ResolvedModule); }

    QString name;
    QStringList moduleDependencies;
    JsImports jsImports;
    QString setupBuildEnvironmentScript;
    QString setupRunEnvironmentScript;

private:
    ResolvedModule() {}

    void load(PersistentPool &pool);
    void store(PersistentPool &pool) const;
};

/**
  * Per default each rule is a "non-multiplex rule".
  *
  * A "multiplex rule" creates one transformer that takes all
  * input artifacts with the matching input file tag and creates
  * one or more artifacts. (e.g. linker rule)
  *
  * A "non-multiplex rule" creates one transformer per matching input file.
  */
class Rule : public PersistentObject
{
public:
    typedef QSharedPointer<Rule> Ptr;
    typedef QSharedPointer<const Rule> ConstPtr;

    static Ptr create() { return Ptr(new Rule); }

    ResolvedModule::ConstPtr module;
    JsImports jsImports;
    PrepareScript::ConstPtr script;
    QStringList inputs;
    QStringList usings;
    QStringList explicitlyDependsOn;
    bool multiplex;
    QList<RuleArtifact::ConstPtr> artifacts;

    // members that we don't need to save
    int ruleGraphId;

    QString toString() const;
    QStringList outputFileTags() const;

private:
    Rule() : multiplex(false), ruleGraphId(-1) {}

    void load(PersistentPool &pool);
    void store(PersistentPool &pool) const;
};

class ResolvedTransformer
{
public:
    typedef QSharedPointer<ResolvedTransformer> Ptr;

    static Ptr create() { return Ptr(new ResolvedTransformer); }

    ResolvedModule::ConstPtr module;
    QStringList inputs;
    QList<SourceArtifact::Ptr> outputs;
    PrepareScript::ConstPtr transform;
    JsImports jsImports;

private:
    ResolvedTransformer() {}
};

class ResolvedProject;
class ScriptEngine;

class ResolvedProduct: public PersistentObject
{
public:
    typedef QSharedPointer<ResolvedProduct> Ptr;
    typedef QSharedPointer<const ResolvedProduct> ConstPtr;

    static Ptr create() { return Ptr(new ResolvedProduct); }

    QStringList fileTags;
    QStringList additionalFileTags;
    QString name;
    QString targetName;
    QString sourceDirectory;
    QString destinationDirectory;
    QString qbsFile;
    int qbsLine;
    WeakPointer<ResolvedProject> project;
    PropertyMap::Ptr properties;
    QSet<Rule::Ptr> rules;
    QSet<ResolvedProduct::Ptr> dependencies;
    QSet<FileTagger::ConstPtr> fileTaggers;
    QList<ResolvedModule::ConstPtr> modules;
    QList<ResolvedTransformer::Ptr> transformers;
    QList<ResolvedGroup::Ptr> groups;

    mutable QProcessEnvironment buildEnvironment; // must not be saved
    mutable QProcessEnvironment runEnvironment; // must not be saved
    QHash<QString, QString> executablePathCache;

    QList<SourceArtifact::Ptr> allFiles() const;
    QSet<QString> fileTagsForFileName(const QString &fileName) const;
    void setupBuildEnvironment(ScriptEngine *scriptEngine, const QProcessEnvironment &systemEnvironment) const;
    void setupRunEnvironment(ScriptEngine *scriptEngine, const QProcessEnvironment &systemEnvironment) const;

private:
    ResolvedProduct();

    void load(PersistentPool &pool);
    void store(PersistentPool &pool) const;
};

class ResolvedProject: public PersistentObject
{
public:
    typedef QSharedPointer<ResolvedProject> Ptr;
    typedef QSharedPointer<const ResolvedProject> ConstPtr;

    static Ptr create() { return Ptr(new ResolvedProject); }

    static QString deriveId(const QVariantMap &config);
    static QString deriveBuildDirectory(const QString &buildRoot, const QString &id);

    QString qbsFile; // Not saved.
    QString buildDirectory; // Not saved
    QVariantMap platformEnvironment;
    QList<ResolvedProduct::Ptr> products;

    void setBuildConfiguration(const QVariantMap &config);
    const QVariantMap &buildConfiguration() const { return m_buildConfiguration; }
    QString id() const { return m_id; }

private:
    ResolvedProject() {}

    void load(PersistentPool &pool);
    void store(PersistentPool &pool) const;

    QVariantMap m_buildConfiguration;
    QString m_id;
};

} // namespace Internal
} // namespace qbs

QT_BEGIN_NAMESPACE
Q_DECLARE_TYPEINFO(qbs::Internal::JsImport, Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(qbs::Internal::RuleArtifact::Binding, Q_MOVABLE_TYPE);
QT_END_NAMESPACE

#endif