aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/buildgraph/buildgraph.h
blob: 28c1d9ca66e81089622b71c75e062df21ab9e8b1 (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
/****************************************************************************
**
** 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 BUILDGRAPH_H
#define BUILDGRAPH_H

#include "artifactlist.h"

#include <language/language.h>
#include <tools/error.h>
#include <tools/persistentobject.h>
#include <tools/weakpointer.h>

#include <QDir>
#include <QScriptValue>
#include <QSet>
#include <QSharedPointer>
#include <QStringList>
#include <QVariant>
#include <QVector>

namespace qbs {
class ProgressObserver;

namespace Internal {
class Artifact;
class Transformer;
class BuildProject;

typedef QMap<QString, ArtifactList> ArtifactsPerFileTagMap;

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

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

    ~BuildProduct();

    void dump() const;
    const QList<Rule::ConstPtr> &topSortedRules() const;
    Artifact *lookupArtifact(const QString &dirPath, const QString &fileName) const;
    Artifact *lookupArtifact(const QString &filePath) const;

    WeakPointer<BuildProject> project;
    ResolvedProduct::Ptr rProduct;
    QSet<Artifact *> targetArtifacts;
    QList<BuildProduct::Ptr> dependencies;
    ArtifactList artifacts;

private:
    BuildProduct();

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

private:
    mutable QList<Rule::ConstPtr> m_topSortedRules;
};

class BuildGraph;

class BuildProject : public PersistentObject
{
    friend class BuildGraph;
public:
    typedef QSharedPointer<BuildProject> Ptr;
    typedef QSharedPointer<const BuildProject> ConstPtr;

    BuildProject(BuildGraph *bg);
    ~BuildProject();

    struct LoadResult
    {
        ResolvedProject::Ptr changedResolvedProject;
        BuildProject::Ptr loadedProject;
        bool discardLoadedProject;
    };

    static LoadResult load(const QString &projectFilePath, BuildGraph *bg, const QString &buildRoot,
                           const QVariantMap &cfg, const QStringList &loaderSearchPaths);
    void store() const;
    static QString deriveBuildGraphFilePath(const QString &buildDir, const QString projectId);
    QString buildGraphFilePath() const;

    BuildGraph *buildGraph() const;
    ResolvedProject::Ptr resolvedProject() const;
    QSet<BuildProduct::Ptr> buildProducts() const;
    bool dirty() const;
    void markDirty();
    void insertIntoArtifactLookupTable(Artifact *artifact);
    void removeFromArtifactLookupTable(Artifact *artifact);
    QList<Artifact *> lookupArtifacts(const QString &filePath) const;
    QList<Artifact *> lookupArtifacts(const QString &dirPath, const QString &fileName) const;
    void insertFileDependency(Artifact *artifact);
    void rescueDependencies(const BuildProject::Ptr &other);

    void onProductRemoved(const BuildProduct::Ptr &product);

private:
    void load(PersistentPool &pool);
    void store(PersistentPool &pool) const;
    void addBuildProduct(const BuildProduct::Ptr &product);
    void setResolvedProject(const ResolvedProject::Ptr & resolvedProject);

private:
    BuildGraph *m_buildGraph;
    ResolvedProject::Ptr m_resolvedProject;
    QSet<BuildProduct::Ptr> m_buildProducts;
    ArtifactList m_dependencyArtifacts;
    QHash<QString, QHash<QString, QList<Artifact *> > > m_artifactLookupTable;
    mutable bool m_dirty;
};

/**
 * N artifact, T transformer, parent -> child
 * parent depends on child, child is a dependency of parent,
 * parent is a dependent of child.
 *
 * N a.out -> N main.o -> N main.cpp
 *
 * Every artifact can point to a transformer which contains the commands.
 * Multiple artifacts can point to the same transformer.
 */
class BuildGraph
{
public:
    BuildGraph(ScriptEngine *engine);
    ~BuildGraph();

    ScriptEngine *engine() { return m_engine; }

    BuildProject::Ptr resolveProject(ResolvedProject::Ptr);

    void applyRules(BuildProduct *product, ArtifactsPerFileTagMap &artifactsPerFileTag);

    void setProgressObserver(ProgressObserver *observer);

    static bool findPath(Artifact *u, Artifact *v, QList<Artifact*> &path);
    static void connect(Artifact *p, Artifact *c);
    static void loggedConnect(Artifact *u, Artifact *v);
    static bool safeConnect(Artifact *u, Artifact *v);
    void insert(BuildProduct::Ptr target, Artifact *n) const;
    void insert(BuildProduct *target, Artifact *n) const;
    void remove(Artifact *artifact) const;
    void removeArtifactAndExclusiveDependents(Artifact *artifact, QList<Artifact*> *removedArtifacts = 0);
    static void removeGeneratedArtifactFromDisk(Artifact *artifact);

    void onProductChanged(BuildProduct::Ptr product, ResolvedProduct::Ptr changedProduct, bool *discardStoredProject);
    void updateNodesThatMustGetNewTransformer();
    void removeFromArtifactsThatMustGetNewTransformers(Artifact *a) {
        m_artifactsThatMustGetNewTransformers -= a;
    }
    void createTransformerCommands(const PrepareScript::ConstPtr &script, Transformer *transformer);

    static void setupScriptEngineForProduct(ScriptEngine *scriptEngine,
                                            const ResolvedProduct::ConstPtr &product,
                                            Rule::ConstPtr rule, QScriptValue targetObject);
    static void disconnect(Artifact *u, Artifact *v);
    static void disconnectChildren(Artifact *u);
    static void disconnectParents(Artifact *u);
    static void disconnectAll(Artifact *u);

private:
    void initEngine();
    void cleanupEngine();
    BuildProduct::Ptr resolveProduct(const BuildProject::Ptr &project,
                                     const ResolvedProduct::Ptr &rProduct);
    Artifact *createArtifact(BuildProduct::Ptr product, SourceArtifact::ConstPtr sourceArtifact);
    void updateNodeThatMustGetNewTransformer(Artifact *artifact);

    class EngineInitializer
    {
    public:
        EngineInitializer(BuildGraph *bg);
        ~EngineInitializer();

    private:
        BuildGraph *buildGraph;
    };

    ProgressObserver *m_progressObserver;
    ScriptEngine *m_engine;
    unsigned int m_initEngineCalls;
    QScriptValue m_scope;
    QScriptValue m_prepareScriptScope;
    QHash<ResolvedProduct::Ptr, BuildProduct::Ptr> m_productCache;
    QHash<QString, QScriptProgram> m_scriptProgramCache;
    mutable QSet<Artifact *> m_artifactsThatMustGetNewTransformers;

    friend class EngineInitializer;
};

class RulesApplicator
{
public:
    RulesApplicator(BuildProduct *product, ArtifactsPerFileTagMap &artifactsPerFileTag,
            BuildGraph *bg);
    void applyAllRules();
    void applyRule(const Rule::ConstPtr &rule);

private:
    void doApply(const ArtifactList &inputArtifacts);
    void setupScriptEngineForArtifact(Artifact *artifact);
    Artifact *createOutputArtifact(const RuleArtifact::ConstPtr &ruleArtifact,
            const ArtifactList &inputArtifacts);
    QString resolveOutPath(const QString &path) const;

    QScriptValue scope() const;
    ScriptEngine *engine() const { return m_buildGraph->engine(); }

    BuildProduct * const m_buildProduct;
    ArtifactsPerFileTagMap &m_artifactsPerFileTag;
    BuildGraph * const m_buildGraph;

    Rule::ConstPtr m_rule;
    QSharedPointer<Transformer> m_transformer;
};

// debugging helper
QString fileName(Artifact *n);

// debugging helper
template <typename T>
static QStringList toStringList(const T &artifactContainer)
{
    QStringList l;
    foreach (Artifact *n, artifactContainer)
        l.append(fileName(n));
    return l;
}

char **createCFileTags(const QSet<QString> &fileTags);
void freeCFileTags(char **cFileTags, int numFileTags);

} // namespace Internal
} // namespace qbs

#endif // BUILDGRAPH_H