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
|
/****************************************************************************
**
** 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.
**
****************************************************************************/
#include "commandlinefrontend.h"
#include "application.h"
#include "consoleprogressobserver.h"
#include "showproperties.h"
#include "status.h"
#include <qbs.h>
#include <api/runenvironment.h>
#include <logging/translator.h>
#include <QDir>
#include <QProcessEnvironment>
#include <cstdlib>
namespace qbs {
CommandLineFrontend::CommandLineFrontend(const CommandLineParser &parser, QObject *parent)
: QObject(parent), m_parser(parser), m_observer(0), m_canceled(false)
{
}
void CommandLineFrontend::cancel()
{
if (m_resolveJobs.isEmpty() && m_buildJobs.isEmpty())
std::exit(EXIT_FAILURE);
foreach (AbstractJob * const job, m_resolveJobs)
job->cancel();
foreach (AbstractJob * const job, m_buildJobs)
job->cancel();
m_canceled = true;
}
void CommandLineFrontend::start()
{
try {
switch (m_parser.command()) {
case RunCommandType:
case ShellCommandType:
if (m_parser.products().count() > 1) {
throw Error(Tr::tr("Invalid use of command '%1': Cannot use more than one "
"product.\nUsage: %2")
.arg(m_parser.commandName(), m_parser.commandDescription()));
}
// Fall-through intended.
case PropertiesCommandType:
case StatusCommandType:
if (m_parser.buildConfigurations().count() > 1) {
QString error = Tr::tr("Invalid use of command '%1': There can be only one "
"build configuration.\n").arg(m_parser.commandName());
error += Tr::tr("Usage: %1").arg(m_parser.commandDescription());
throw Error(error);
}
break;
default:
break;
}
if (m_parser.showProgress())
m_observer = new ConsoleProgressObserver;
foreach (const QVariantMap &buildConfig, m_parser.buildConfigurations()) {
SetupProjectJob * const job = Project::setupProject(m_parser.projectFilePath(),
buildConfig, QDir::currentPath(), this);
connectJob(job);
m_resolveJobs << job;
}
/*
* Progress reporting on the terminal gets a bit tricky when resolving several projects
* concurrently, since we cannot show multiple progress bars at the same time. Instead,
* we just set the total effort to the number of projects and increase the progress
* every time one of them finishes, ingoring the progress reports from the jobs themselves.
* (Yes, that does mean it will take disproportionately long for the first progress
* notification to arrive.)
*/
if (m_parser.showProgress() && resolvingMultipleProjects())
m_observer->initialize(tr("Setting up projects"), m_resolveJobs.count());
} catch (const Error &error) {
qbsError() << error.toString();
if (m_buildJobs.isEmpty() && m_resolveJobs.isEmpty())
qApp->exit(EXIT_FAILURE);
else
cancel();
}
}
void CommandLineFrontend::handleJobFinished(bool success, AbstractJob *job)
{
job->deleteLater();
if (!success) {
qbsError() << job->error().toString();
m_resolveJobs.removeOne(job);
m_buildJobs.removeOne(job);
if (m_resolveJobs.isEmpty() && m_buildJobs.isEmpty()) {
qApp->exit(EXIT_FAILURE);
return;
}
cancel();
} else if (SetupProjectJob * const setupJob = qobject_cast<SetupProjectJob *>(job)) {
m_resolveJobs.removeOne(job);
m_projects << setupJob->project();
if (m_observer && resolvingMultipleProjects())
m_observer->incrementProgressValue();
if (m_resolveJobs.isEmpty())
handleProjectsResolved();
} else { // Build or clean.
m_buildJobs.removeOne(job);
if (m_buildJobs.isEmpty()) {
if (m_parser.command() == RunCommandType)
qApp->exit(runTarget());
else
qApp->quit();
}
}
}
void CommandLineFrontend::handleNewTaskStarted(const QString &description, int totalEffort)
{
if (isBuilding()) {
m_totalBuildEffort += totalEffort;
if (++m_buildEffortsRetrieved == m_buildEffortsNeeded) {
m_observer->initialize(tr("Building"), m_totalBuildEffort);
if (m_currentBuildEffort > 0)
m_observer->setProgressValue(m_currentBuildEffort);
}
} else if (!resolvingMultipleProjects()) {
m_observer->initialize(description, totalEffort);
}
}
void CommandLineFrontend::handleTaskProgress(int value, AbstractJob *job)
{
if (isBuilding()) {
int ¤tJobEffort = m_buildEfforts[job];
m_currentBuildEffort += value - currentJobEffort;
currentJobEffort = value;
if (m_buildEffortsRetrieved == m_buildEffortsNeeded)
m_observer->setProgressValue(m_currentBuildEffort);
} else if (!resolvingMultipleProjects()) {
m_observer->setProgressValue(value);
}
}
bool CommandLineFrontend::resolvingMultipleProjects() const
{
return isResolving() && m_resolveJobs.count() + m_projects.count() > 1;
}
bool CommandLineFrontend::isResolving() const
{
return !m_resolveJobs.isEmpty();
}
bool CommandLineFrontend::isBuilding() const
{
return !m_buildJobs.isEmpty();
}
CommandLineFrontend::ProductMap CommandLineFrontend::productsToUse() const
{
ProductMap products;
QStringList productNames;
const bool useAll = m_parser.products().isEmpty();
foreach (const Project &project, m_projects) {
QList<ProductData> &productList = products[project];
const ProjectData projectData = project.projectData();
foreach (const ProductData &product, projectData.products()) {
if (useAll || m_parser.products().contains(product.name())) {
productList << product;
productNames << product.name();
}
}
}
foreach (const QString &productName, m_parser.products()) {
if (!productNames.contains(productName))
throw Error(Tr::tr("No such product '%1'.").arg(productName));
}
return products;
}
void CommandLineFrontend::handleProjectsResolved()
{
try {
if (m_canceled)
throw Error(Tr::tr("Execution canceled due to user request."));
switch (m_parser.command()) {
case CleanCommandType:
makeClean();
break;
case ShellCommandType:
checkForExactlyOneProduct();
qApp->exit(runShell());
break;
case StatusCommandType: {
qApp->exit(printStatus(m_projects.first().projectData()));
break;
}
case PropertiesCommandType: {
QList<ProductData> products;
const ProductMap &p = productsToUse();
foreach (const QList<ProductData> &pProducts, p)
products << pProducts;
qApp->exit(showProperties(products));
break;
}
case BuildCommandType:
case RunCommandType:
build();
break;
case UpdateTimestampsCommandType:
updateTimestamps();
qApp->quit();
break;
case HelpCommandType:
Q_ASSERT_X(false, Q_FUNC_INFO, "Impossible.");
}
} catch (const Error &error) {
qbsError() << error.toString();
qApp->exit(EXIT_FAILURE);
}
}
void CommandLineFrontend::makeClean()
{
const Project::CleanType cleanType = m_parser.cleanAll()
? Project::CleanupAll : Project::CleanupTemporaries;
if (m_parser.products().isEmpty()) {
foreach (const Project &project, m_projects) {
m_buildJobs << project.cleanAllProducts(m_parser.buildOptions(), cleanType, this);
}
} else {
const ProductMap &products = productsToUse();
for (ProductMap::ConstIterator it = products.begin(); it != products.end(); ++it) {
m_buildJobs << it.key().cleanSomeProducts(it.value(), m_parser.buildOptions(),
cleanType, this);
}
}
connectBuildJobs();
}
int CommandLineFrontend::runShell()
{
const ProductMap &productMap = productsToUse();
Q_ASSERT(productMap.count() == 1);
const Project &project = productMap.begin().key();
const QList<ProductData> &products = productMap.begin().value();
Q_ASSERT(products.count() == 1);
RunEnvironment runEnvironment = project.getRunEnvironment(products.first(),
QProcessEnvironment::systemEnvironment());
return runEnvironment.runShell();
}
void CommandLineFrontend::build()
{
if (m_parser.products().isEmpty()) {
foreach (const Project &project, m_projects)
m_buildJobs << project.buildAllProducts(m_parser.buildOptions(), this);
} else {
const ProductMap &products = productsToUse();
for (ProductMap::ConstIterator it = products.begin(); it != products.end(); ++it)
m_buildJobs << it.key().buildSomeProducts(it.value(), m_parser.buildOptions(), this);
}
connectBuildJobs();
/*
* Progress reporting for the build jobs works as follows: We know that for every job,
* the newTaskStarted() signal is emitted exactly once (unless there's an error). So we add up
* the respective total efforts as they come in. Once all jobs have reported their total
* efforts, we can start the overall progress report.
*/
m_buildEffortsNeeded = m_buildJobs.count();
m_buildEffortsRetrieved = 0;
m_totalBuildEffort = 0;
m_currentBuildEffort = 0;
}
int CommandLineFrontend::runTarget()
{
try {
checkForExactlyOneProduct();
const ProductMap &productMap = productsToUse();
Q_ASSERT(productMap.count() == 1);
const Project &project = productMap.begin().key();
const QList<ProductData> &products = productMap.begin().value();
Q_ASSERT(products.count() == 1);
const ProductData productToRun = products.first();
const QString executableFilePath = project.targetExecutable(productToRun);
if (executableFilePath.isEmpty()) {
throw Error(Tr::tr("Cannot run: Product '%1' is not an application.")
.arg(productToRun.name()));
}
RunEnvironment runEnvironment = project.getRunEnvironment(productToRun,
QProcessEnvironment::systemEnvironment());
return runEnvironment.runTarget(executableFilePath, m_parser.runArgs());
} catch (const Error &error) {
qbsError() << error.toString();
return EXIT_FAILURE;
}
}
void CommandLineFrontend::updateTimestamps()
{
const ProductMap &products = productsToUse();
for (ProductMap::ConstIterator it = products.constBegin(); it != products.constEnd(); ++it) {
Project p = it.key();
p.updateTimestamps(it.value());
}
}
void CommandLineFrontend::connectBuildJobs()
{
foreach (AbstractJob * const job, m_buildJobs)
connectJob(job);
}
void CommandLineFrontend::connectJob(AbstractJob *job)
{
connect(job, SIGNAL(finished(bool, qbs::AbstractJob*)),
SLOT(handleJobFinished(bool, qbs::AbstractJob*)));
if (m_parser.showProgress()) {
connect(job, SIGNAL(taskStarted(QString,int,qbs::AbstractJob*)),
SLOT(handleNewTaskStarted(QString,int)));
connect(job, SIGNAL(taskProgress(int,qbs::AbstractJob*)),
SLOT(handleTaskProgress(int,qbs::AbstractJob*)));
}
}
void CommandLineFrontend::checkForExactlyOneProduct()
{
if (m_parser.products().count() == 0
&& m_projects.first().projectData().products().count() > 1) {
throw Error(Tr::tr("Ambiguous use of command '%1': No product given for project "
"with more than one product.\nUsage: %2")
.arg(m_parser.commandName(), m_parser.commandDescription()));
}
}
} // namespace qbs
|