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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qbs.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "internaljobs.h"
#include "jobs.h"
#include <buildgraph/artifactcleaner.h>
#include <buildgraph/buildgraph.h>
#include <buildgraph/buildgraphloader.h>
#include <buildgraph/productbuilddata.h>
#include <buildgraph/projectbuilddata.h>
#include <buildgraph/executor.h>
#include <buildgraph/productinstaller.h>
#include <buildgraph/rulesevaluationcontext.h>
#include <language/language.h>
#include <language/loader.h>
#include <logging/logger.h>
#include <logging/translator.h>
#include <tools/buildgraphlocker.h>
#include <tools/error.h>
#include <tools/profiling.h>
#include <tools/progressobserver.h>
#include <tools/preferences.h>
#include <tools/qbsassert.h>
#include <QtCore/qeventloop.h>
#include <QtCore/qscopedpointer.h>
#include <QtCore/qtimer.h>
namespace qbs {
namespace Internal {
class JobObserver : public ProgressObserver
{
public:
JobObserver(InternalJob *job) : m_canceled(false), m_job(job), m_timedLogger(0) { }
~JobObserver() { delete m_timedLogger; }
void cancel() { m_canceled = true; }
private:
void initialize(const QString &task, int maximum)
{
QBS_ASSERT(!m_timedLogger, delete m_timedLogger);
if (m_job->timed())
m_timedLogger = new TimedActivityLogger(m_job->logger(), task, true);
m_value = 0;
m_maximum = maximum;
m_canceled = false;
emit m_job->newTaskStarted(task, maximum, m_job);
}
void setMaximum(int maximum)
{
m_maximum = maximum;
emit m_job->totalEffortChanged(maximum, m_job);
}
void setProgressValue(int value)
{
//QBS_ASSERT(value >= m_value, qDebug("old value = %d, new value = %d", m_value, value));
//QBS_ASSERT(value <= m_maximum, qDebug("value = %d, maximum = %d", value, m_maximum));
m_value = value;
if (value == m_maximum) {
delete m_timedLogger;
m_timedLogger = 0;
}
emit m_job->taskProgress(value, m_job);
}
int progressValue() { return m_value; }
int maximum() const { return m_maximum; }
bool canceled() const { return m_canceled; }
int m_value;
int m_maximum;
bool m_canceled;
InternalJob * const m_job;
TimedActivityLogger *m_timedLogger;
};
InternalJob::InternalJob(const Logger &logger, QObject *parent)
: QObject(parent)
, m_observer(new JobObserver(this))
, m_ownsObserver(true)
, m_logger(logger)
, m_timed(false)
{
}
InternalJob::~InternalJob()
{
if (m_ownsObserver)
delete m_observer;
}
void InternalJob::cancel()
{
m_observer->cancel();
}
void InternalJob::shareObserverWith(InternalJob *otherJob)
{
if (m_ownsObserver) {
delete m_observer;
m_ownsObserver = false;
}
m_observer = otherJob->m_observer;
}
void InternalJob::storeBuildGraph(const TopLevelProjectPtr &project)
{
try {
doSanityChecks(project, logger());
TimedActivityLogger storeTimer(m_logger, Tr::tr("Storing build graph"), timed());
project->store(logger());
} catch (const ErrorInfo &error) {
logger().printWarning(error);
}
}
/**
* Construct a new thread wrapper for a synchronous job.
* This object takes over ownership of the synchronous job.
*/
InternalJobThreadWrapper::InternalJobThreadWrapper(InternalJob *synchronousJob, QObject *parent)
: InternalJob(synchronousJob->logger(), parent)
, m_job(synchronousJob)
, m_running(false)
{
synchronousJob->shareObserverWith(this);
m_job->moveToThread(&m_thread);
connect(m_job, &InternalJob::finished, this, &InternalJobThreadWrapper::handleFinished);
connect(m_job, &InternalJob::newTaskStarted,
this, &InternalJob::newTaskStarted);
connect(m_job, &InternalJob::taskProgress,
this, &InternalJob::taskProgress);
connect(m_job, &InternalJob::totalEffortChanged,
this, &InternalJob::totalEffortChanged);
connect(this, &InternalJobThreadWrapper::startRequested, m_job, &InternalJob::start);
}
InternalJobThreadWrapper::~InternalJobThreadWrapper()
{
if (m_running) {
QEventLoop loop;
connect(m_job, &InternalJob::finished, &loop, &QEventLoop::quit);
cancel();
loop.exec();
}
m_thread.quit();
m_thread.wait();
delete m_job;
}
void InternalJobThreadWrapper::start()
{
setTimed(m_job->timed());
m_thread.start();
m_running = true;
emit startRequested();
}
void InternalJobThreadWrapper::handleFinished()
{
m_running = false;
setError(m_job->error());
emit finished(this);
}
InternalSetupProjectJob::InternalSetupProjectJob(const Logger &logger)
: InternalJob(logger)
{
}
InternalSetupProjectJob::~InternalSetupProjectJob()
{
}
void InternalSetupProjectJob::init(const TopLevelProjectPtr &existingProject,
const SetupProjectParameters ¶meters)
{
m_existingProject = existingProject;
m_parameters = parameters;
setTimed(parameters.logElapsedTime());
}
void InternalSetupProjectJob::reportError(const ErrorInfo &error)
{
setError(error);
emit finished(this);
}
TopLevelProjectPtr InternalSetupProjectJob::project() const
{
return m_newProject;
}
void InternalSetupProjectJob::start()
{
BuildGraphLocker *bgLocker = m_existingProject ? m_existingProject->bgLocker : 0;
bool deleteLocker = false;
try {
const ErrorInfo err = m_parameters.expandBuildConfiguration();
if (err.hasError())
throw err;
const QString projectId = TopLevelProject::deriveId(
m_parameters.finalBuildConfigurationTree());
const QString buildDir
= TopLevelProject::deriveBuildDirectory(m_parameters.buildRoot(), projectId);
if (m_existingProject && m_existingProject->buildDirectory != buildDir)
m_existingProject.clear();
if (!m_existingProject) {
bgLocker = new BuildGraphLocker(ProjectBuildData::deriveBuildGraphFilePath(buildDir,
projectId),
logger(), m_parameters.waitLockBuildGraph(), observer());
deleteLocker = true;
}
execute();
if (m_existingProject)
m_existingProject->bgLocker = 0;
m_newProject->bgLocker = bgLocker;
deleteLocker = false;
} catch (const ErrorInfo &error) {
m_newProject.clear();
setError(error);
// Delete the build graph locker if and only if we allocated it here.
if (deleteLocker)
delete bgLocker;
}
emit finished(this);
}
void InternalSetupProjectJob::execute()
{
RulesEvaluationContextPtr evalContext(new RulesEvaluationContext(logger()));
evalContext->setObserver(observer());
switch (m_parameters.restoreBehavior()) {
case SetupProjectParameters::ResolveOnly:
resolveProjectFromScratch(evalContext->engine());
resolveBuildDataFromScratch(evalContext);
break;
case SetupProjectParameters::RestoreOnly:
m_newProject = restoreProject(evalContext).loadedProject;
break;
case SetupProjectParameters::RestoreAndTrackChanges: {
const BuildGraphLoadResult loadResult = restoreProject(evalContext);
m_newProject = loadResult.newlyResolvedProject;
if (!m_newProject)
m_newProject = loadResult.loadedProject;
if (!m_newProject) {
resolveProjectFromScratch(evalContext->engine());
resolveBuildDataFromScratch(evalContext);
} else {
QBS_CHECK(m_newProject->buildData);
}
break;
}
}
if (!m_parameters.dryRun())
storeBuildGraph(m_newProject);
// The evalutation context cannot be re-used for building, which runs in a different thread.
m_newProject->buildData->evaluationContext.clear();
}
void InternalSetupProjectJob::resolveProjectFromScratch(ScriptEngine *engine)
{
Loader loader(engine, logger());
loader.setSearchPaths(m_parameters.searchPaths());
loader.setProgressObserver(observer());
m_newProject = loader.loadProject(m_parameters);
QBS_CHECK(m_newProject);
}
void InternalSetupProjectJob::resolveBuildDataFromScratch(const RulesEvaluationContextPtr &evalContext)
{
TimedActivityLogger resolveLogger(logger(), QLatin1String("Resolving build project"), timed());
BuildDataResolver(logger()).resolveBuildData(m_newProject, evalContext);
}
BuildGraphLoadResult InternalSetupProjectJob::restoreProject(const RulesEvaluationContextPtr &evalContext)
{
BuildGraphLoader bgLoader(logger());
const BuildGraphLoadResult loadResult
= bgLoader.load(m_existingProject, m_parameters, evalContext);
return loadResult;
}
BuildGraphTouchingJob::BuildGraphTouchingJob(const Logger &logger, QObject *parent)
: InternalJob(logger, parent), m_dryRun(false)
{
}
BuildGraphTouchingJob::~BuildGraphTouchingJob()
{
}
void BuildGraphTouchingJob::setup(const TopLevelProjectPtr &project,
const QList<ResolvedProductPtr> &products, bool dryRun)
{
m_project = project;
m_products = products;
m_dryRun = dryRun;
}
void BuildGraphTouchingJob::storeBuildGraph()
{
if (!m_dryRun && !error().isInternalError())
InternalJob::storeBuildGraph(m_project);
}
InternalBuildJob::InternalBuildJob(const Logger &logger, QObject *parent)
: BuildGraphTouchingJob(logger, parent), m_executor(0)
{
}
void InternalBuildJob::build(const TopLevelProjectPtr &project,
const QList<ResolvedProductPtr> &products, const BuildOptions &buildOptions)
{
setup(project, products, buildOptions.dryRun());
setTimed(buildOptions.logElapsedTime());
m_executor = new Executor(logger());
m_executor->setProject(project);
m_executor->setProducts(products);
m_executor->setBuildOptions(buildOptions);
m_executor->setProgressObserver(observer());
QThread * const executorThread = new QThread(this);
m_executor->moveToThread(executorThread);
connect(m_executor, &Executor::reportCommandDescription,
this, &BuildGraphTouchingJob::reportCommandDescription);
connect(m_executor, &Executor::reportProcessResult,
this, &BuildGraphTouchingJob::reportProcessResult);
connect(executorThread, &QThread::started, m_executor, &Executor::build);
connect(m_executor, &Executor::finished, this, &InternalBuildJob::handleFinished);
connect(m_executor, &QObject::destroyed, executorThread, &QThread::quit);
connect(executorThread, &QThread::finished, this, &InternalBuildJob::emitFinished);
executorThread->start();
}
void InternalBuildJob::handleFinished()
{
setError(m_executor->error());
project()->buildData->evaluationContext.clear();
storeBuildGraph();
m_executor->deleteLater();
}
void InternalBuildJob::emitFinished()
{
emit finished(this);
}
InternalCleanJob::InternalCleanJob(const Logger &logger, QObject *parent)
: BuildGraphTouchingJob(logger, parent)
{
}
void InternalCleanJob::init(const TopLevelProjectPtr &project,
const QList<ResolvedProductPtr> &products, const CleanOptions &options)
{
setup(project, products, options.dryRun());
setTimed(options.logElapsedTime());
m_options = options;
}
void InternalCleanJob::start()
{
try {
ArtifactCleaner cleaner(logger(), observer());
cleaner.cleanup(project(), products(), m_options);
} catch (const ErrorInfo &error) {
setError(error);
}
storeBuildGraph();
emit finished(this);
}
InternalInstallJob::InternalInstallJob(const Logger &logger)
: InternalJob(logger)
{
}
InternalInstallJob::~InternalInstallJob()
{
}
void InternalInstallJob::init(const TopLevelProjectPtr &project,
const QList<ResolvedProductPtr> &products, const InstallOptions &options)
{
m_project = project;
m_products = products;
m_options = options;
setTimed(options.logElapsedTime());
}
void InternalInstallJob::start()
{
try {
ProductInstaller(m_project, m_products, m_options, observer(), logger()).install();
} catch (const ErrorInfo &error) {
setError(error);
}
emit finished(this);
}
} // namespace Internal
} // namespace qbs
|