1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qbs.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QBS_BUILDGRAPHEXECUTOR_H
41 #define QBS_BUILDGRAPHEXECUTOR_H
42 
43 #include "forward_decls.h"
44 #include "buildgraphvisitor.h"
45 #include <buildgraph/artifact.h>
46 #include <language/forward_decls.h>
47 
48 #include <logging/logger.h>
49 #include <tools/buildoptions.h>
50 #include <tools/error.h>
51 #include <tools/qttools.h>
52 
53 #include <QtCore/qobject.h>
54 
55 #include <queue>
56 #include <unordered_map>
57 
58 QT_BEGIN_NAMESPACE
59 class QTimer;
60 QT_END_NAMESPACE
61 
62 namespace qbs {
63 class ProcessResult;
64 
65 namespace Internal {
66 class ExecutorJob;
67 class FileTime;
68 class InputArtifactScannerContext;
69 class ProductInstaller;
70 class ProgressObserver;
71 class RuleNode;
72 
73 class Executor : public QObject, private BuildGraphVisitor
74 {
75     Q_OBJECT
76 
77 public:
78     void build();
79 
80     Executor(Logger logger, QObject *parent = nullptr);
81     ~Executor() override;
82 
83     void setProject(const TopLevelProjectPtr &project);
84     void setProducts(const QVector<ResolvedProductPtr> &productsToBuild);
85     void setBuildOptions(const BuildOptions &buildOptions);
setProgressObserver(ProgressObserver * observer)86     void setProgressObserver(ProgressObserver *observer) { m_progressObserver = observer; }
87 
error()88     ErrorInfo error() const { return m_error; }
89 
90 signals:
91     void reportCommandDescription(const QString &highlight, const QString &message);
92     void reportProcessResult(const qbs::ProcessResult &result);
93 
94     void finished();
95 
96 private:
97     void onJobFinished(const qbs::ErrorInfo &err);
98     void finish();
99     void checkForCancellation();
100 
101     // BuildGraphVisitor implementation
102     bool visit(Artifact *artifact) override;
103     bool visit(RuleNode *ruleNode) override;
104 
105     enum ExecutorState { ExecutorIdle, ExecutorRunning, ExecutorCanceling };
106 
107     struct ComparePriority
108     {
109         bool operator() (const BuildGraphNode *x, const BuildGraphNode *y) const;
110     };
111 
112     using Leaves = std::priority_queue<BuildGraphNode *, std::vector<BuildGraphNode *>,
113                                        ComparePriority>;
114 
115     void doBuild();
116     void prepareAllNodes();
117     void syncFileDependencies();
118     void prepareArtifact(Artifact *artifact);
119     void setupForBuildingSelectedFiles(const BuildGraphNode *node);
120     void prepareReachableNodes();
121     void prepareReachableNodes_impl(BuildGraphNode *node);
122     void prepareProducts();
123     void setupRootNodes();
124     void initLeaves();
125     void updateLeaves(const NodeSet &nodes);
126     void updateLeaves(BuildGraphNode *node, NodeSet &seenNodes);
127     bool scheduleJobs();
128     void buildArtifact(Artifact *artifact);
129     void executeRuleNode(RuleNode *ruleNode);
130     void finishJob(ExecutorJob *job, bool success);
131     void finishNode(BuildGraphNode *leaf);
132     void finishArtifact(Artifact *artifact);
133     void setState(ExecutorState);
134     void addExecutorJobs();
135     void cancelJobs();
136     void setupProgressObserver();
137     void doSanityChecks();
138     void handleError(const ErrorInfo &error);
139     void rescueOldBuildData(Artifact *artifact, bool *childrenAdded);
140     bool checkForUnbuiltDependencies(Artifact *artifact);
141     void potentiallyRunTransformer(const TransformerPtr &transformer);
142     void runTransformer(const TransformerPtr &transformer);
143     void finishTransformer(const TransformerPtr &transformer);
144     void possiblyInstallArtifact(const Artifact *artifact);
145     void checkForUnbuiltProducts();
146     bool checkNodeProduct(BuildGraphNode *node);
147 
148     bool mustExecuteTransformer(const TransformerPtr &transformer) const;
149     bool isUpToDate(Artifact *artifact) const;
150     void retrieveSourceFileTimestamp(Artifact *artifact) const;
151     FileTime recursiveFileTime(const QString &filePath) const;
152     QString configString() const;
153     bool transformerHasMatchingOutputTags(const TransformerConstPtr &transformer) const;
154     bool artifactHasMatchingOutputTags(const Artifact *artifact) const;
155     bool transformerHasMatchingInputFiles(const TransformerConstPtr &transformer) const;
156 
157     void setupJobLimits();
158     void updateJobCounts(const Transformer *transformer, int diff);
159     bool schedulingBlockedByJobLimit(const BuildGraphNode *node);
160 
161     using JobMap = QHash<ExecutorJob *, TransformerPtr>;
162     JobMap m_processingJobs;
163 
164     ProductInstaller *m_productInstaller;
165     RulesEvaluationContextPtr m_evalContext;
166     BuildOptions m_buildOptions;
167     Logger m_logger;
168     ProgressObserver *m_progressObserver;
169     std::vector<std::unique_ptr<ExecutorJob>> m_allJobs;
170     QList<ExecutorJob*> m_availableJobs;
171     ExecutorState m_state;
172     TopLevelProjectPtr m_project;
173     QVector<ResolvedProductPtr> m_productsToBuild;
174     std::vector<ResolvedProductPtr> m_allProducts;
175     std::unordered_map<QString, const ResolvedProduct *> m_productsByName;
176     std::unordered_map<QString, const ResolvedProject *> m_projectsByName;
177     std::unordered_map<QString, int> m_jobCountPerPool;
178     std::unordered_map<const ResolvedProduct *, JobLimits> m_jobLimitsPerProduct;
179     std::unordered_map<const Rule *, int> m_pendingTransformersPerRule;
180     NodeSet m_roots;
181     Leaves m_leaves;
182     InputArtifactScannerContext *m_inputArtifactScanContext;
183     ErrorInfo m_error;
184     bool m_explicitlyCanceled = false;
185     FileTags m_activeFileTags;
186     FileTags m_tagsOfFilesToConsider;
187     FileTags m_tagsNeededForFilesToConsider;
188     QList<ResolvedProductPtr> m_productsOfFilesToConsider;
189     QTimer * const m_cancelationTimer;
190     QStringList m_artifactsRemovedFromDisk;
191     bool m_partialBuild = false;
192     qint64 m_elapsedTimeRules = 0;
193     qint64 m_elapsedTimeScanners = 0;
194     qint64 m_elapsedTimeInstalling = 0;
195 };
196 
197 } // namespace Internal
198 } // namespace qbs
199 
200 #endif // QBS_BUILDGRAPHEXECUTOR_H
201