aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/corelib/tools/buildoptions.cpp
blob: 75417ab0bce64efa20cf12c47e4f094eb4791a7b (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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/****************************************************************************
**
** 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 "buildoptions.h"

#include "jsonhelper.h"

#include <QtCore/qjsonobject.h>
#include <QtCore/qshareddata.h>
#include <QtCore/qthread.h>

namespace qbs {
namespace Internal {

class BuildOptionsPrivate : public QSharedData
{
public:
    BuildOptionsPrivate()
        : maxJobCount(0), dryRun(false), keepGoing(false), forceTimestampCheck(false),
          forceOutputCheck(false),
          logElapsedTime(false), echoMode(defaultCommandEchoMode()), install(true),
          removeExistingInstallation(false), onlyExecuteRules(false)
    {
    }

    QStringList changedFiles;
    QStringList filesToConsider;
    QStringList activeFileTags;
    JobLimits jobLimits;
    QString settingsDir;
    int maxJobCount;
    bool dryRun;
    bool keepGoing;
    bool forceTimestampCheck;
    bool forceOutputCheck;
    bool logElapsedTime;
    CommandEchoMode echoMode;
    bool install;
    bool removeExistingInstallation;
    bool onlyExecuteRules;
    bool jobLimitsFromProjectTakePrecedence = false;
};

} // namespace Internal

/*!
 * \class BuildOptions
 * \brief The \c BuildOptions class comprises parameters that influence the behavior of
 * build and clean operations.
 */

/*!
 * \brief Creates a \c BuildOptions object and initializes its members to sensible default values.
 */
BuildOptions::BuildOptions() : d(new Internal::BuildOptionsPrivate)
{
}

BuildOptions::BuildOptions(const BuildOptions &other) : d(other.d)
{
}

BuildOptions &BuildOptions::operator=(const BuildOptions &other)
{
    d = other.d;
    return *this;
}

BuildOptions::~BuildOptions()
{
}

/*!
 * \brief If non-empty, qbs pretends that only these files have changed.
 * By default, this list is empty.
 */
QStringList BuildOptions::changedFiles() const
{
    return d->changedFiles;
}

/*!
 * \brief If the given list is empty, qbs will pretend only the listed files are changed.
 * \note The list elements must be absolute file paths.
 */
void BuildOptions::setChangedFiles(const QStringList &changedFiles)
{
    d->changedFiles = changedFiles;
}

/*!
 * \brief The list of files to consider.
 * \sa setFilesToConsider.
 * By default, this list is empty.
 */
QStringList BuildOptions::filesToConsider() const
{
    return d->filesToConsider;
}

/*!
 * \brief If the given list is non-empty, qbs will run only commands whose rule has at least one
 *        of these files as an input.
 * \note The list elements must be absolute file paths.
 */
void BuildOptions::setFilesToConsider(const QStringList &files)
{
    d->filesToConsider = files;
}

/*!
 * \brief The list of active file tags.
 * \sa setActiveFileTags
 */
QStringList BuildOptions::activeFileTags() const
{
    return d->activeFileTags;
}

/*!
 * \brief Set the list of active file tags.
 * If this list is non-empty, then every transformer with non-matching output file tags is skipped.
 * E.g. call \c setFilesToConsider() with "foo.cpp" and \c setActiveFileTags() with "obj" to
 * run the compiler on foo.cpp without further processing like linking.
 * \sa activeFileTags
 */
void BuildOptions::setActiveFileTags(const QStringList &fileTags)
{
    d->activeFileTags = fileTags;
}

/*!
 * \brief Returns the default value for \c maxJobCount.
 * This value will be used when \c maxJobCount has not been set explicitly.
 */
int BuildOptions::defaultMaxJobCount()
{
    return QThread::idealThreadCount();
}

/*!
 * \brief Returns the maximum number of build commands to run concurrently.
 * If the value is not valid (i.e. <= 0), a sensible one will be derived at build time
 * from the number of available processor cores at build time.
 * The default is 0.
 * \sa BuildOptions::defaultMaxJobCount
 */
int BuildOptions::maxJobCount() const
{
    return d->maxJobCount;
}

/*!
 * \brief Controls how many build commands can be run in parallel.
 * A value <= 0 leaves the decision to qbs.
 */
void BuildOptions::setMaxJobCount(int jobCount)
{
    d->maxJobCount = jobCount;
}

/*!
 * \brief The base directory for qbs settings.
 * This value is used to locate profiles and preferences.
 */
QString BuildOptions::settingsDirectory() const
{
    return d->settingsDir;
}

/*!
 * \brief Sets the base directory for qbs settings.
 * \param settingsBaseDir Will be used to locate profiles and preferences.
 */
void BuildOptions::setSettingsDirectory(const QString &settingsBaseDir)
{
    d->settingsDir = settingsBaseDir;
}

JobLimits BuildOptions::jobLimits() const
{
    return d->jobLimits;
}

void BuildOptions::setJobLimits(const JobLimits &jobLimits)
{
    d->jobLimits = jobLimits;
}

bool BuildOptions::projectJobLimitsTakePrecedence() const
{
    return d->jobLimitsFromProjectTakePrecedence;
}

void BuildOptions::setProjectJobLimitsTakePrecedence(bool toggle)
{
    d->jobLimitsFromProjectTakePrecedence = toggle;
}

/*!
 * \brief Returns true iff qbs will not actually execute any commands, but just show what
 *        would happen.
 * The default is false.
 */
bool BuildOptions::dryRun() const
{
    return d->dryRun;
}

/*!
 * \brief Controls whether qbs will actually build something.
 * If the argument is true, qbs will just emit information about what it would do. Otherwise,
 * the build is actually done.
 * \note After you build with this setting enabled, the next call to \c build() on the same
 * \c Project object will do nothing, since the internal state needs to be updated the same way
 * as if an actual build had happened. You'll need to create a new \c Project object to do
 * a real build afterwards.
 */
void BuildOptions::setDryRun(bool dryRun)
{
    d->dryRun = dryRun;
}

/*!
 * \brief Returns true iff a build will continue after an error.
 * E.g. a failed compile command will result in a warning message being printed, instead of
 * stopping the build process right away. However, there might still be fatal errors after which the
 * build process cannot continue.
 * The default is \c false.
 */
bool BuildOptions::keepGoing() const
{
    return d->keepGoing;
}

/*!
 * \brief Controls whether a qbs will try to continue building after an error has occurred.
 */
void BuildOptions::setKeepGoing(bool keepGoing)
{
    d->keepGoing = keepGoing;
}

/*!
 * \brief Returns true if qbs is to use physical timestamps instead of the timestamps stored in the
 * build graph.
 * The default is \c false.
 */
bool BuildOptions::forceTimestampCheck() const
{
    return d->forceTimestampCheck;
}

/*!
 * \brief Controls whether qbs should use physical timestamps for up-to-date checks.
 */
void BuildOptions::setForceTimestampCheck(bool enabled)
{
    d->forceTimestampCheck = enabled;
}

/*!
 * \brief Returns true if qbs will test whether rules actually create their
 * declared output artifacts.
 * The default is \c false.
 */
bool BuildOptions::forceOutputCheck() const
{
    return d->forceOutputCheck;
}

/*!
 * \brief Controls whether qbs should test whether rules actually create their
 * declared output artifacts. Enabling this may introduce some small I/O overhead during the build.
 */
void BuildOptions::setForceOutputCheck(bool enabled)
{
    d->forceOutputCheck = enabled;
}

/*!
 * \brief Returns true iff the time the operation takes will be logged.
 * The default is \c false.
 */
bool BuildOptions::logElapsedTime() const
{
    return d->logElapsedTime;
}

/*!
 * \brief Controls whether the build time will be measured and logged.
 */
void BuildOptions::setLogElapsedTime(bool log)
{
    d->logElapsedTime = log;
}

/*!
 * \brief The kind of output that is displayed when executing commands.
 */
CommandEchoMode BuildOptions::echoMode() const
{
    return d->echoMode;
}

/*!
 * \brief Controls the kind of output that is displayed when executing commands.
 */
void BuildOptions::setEchoMode(CommandEchoMode echoMode)
{
    d->echoMode = echoMode;
}

/*!
 * \brief Returns true iff installation should happen as part of the build.
 * The default is \c true.
 */
bool BuildOptions::install() const
{
    return d->install;
}

/*!
 * \brief Controls whether to install artifacts as part of the build process.
 */
void BuildOptions::setInstall(bool install)
{
    d->install = install;
}

/*!
 * \brief Returns true iff an existing installation will be removed prior to building.
 * The default is false.
 */
bool BuildOptions::removeExistingInstallation() const
{
    return d->removeExistingInstallation;
}

/*!
 * Controls whether to remove an existing installation before installing.
 * \note qbs may do some safety checks and refuse to remove certain directories such as
 *       a user's home directory. You should still be careful with this option, since it
 *       deletes recursively.
 */
void BuildOptions::setRemoveExistingInstallation(bool removeExisting)
{
    d->removeExistingInstallation = removeExisting;
}

/*!
 * \brief Returns true iff instead of a full build, only the rules of the project will be run.
 * The default is false.
 */
bool BuildOptions::executeRulesOnly() const
{
    return d->onlyExecuteRules;
}

/*!
 * If \a onlyRules is \c true, then no artifacts are built, but only rules are being executed.
 * \note If the project contains highly dynamic rules that depend on output artifacts of child
 *       rules being already present, then the associated build job may fail even though
 *       the project is perfectly valid. Callers need to take this into consideration.
 */
void BuildOptions::setExecuteRulesOnly(bool onlyRules)
{
    d->onlyExecuteRules = onlyRules;
}


bool operator==(const BuildOptions &bo1, const BuildOptions &bo2)
{
    return bo1.changedFiles() == bo2.changedFiles()
            && bo1.dryRun() == bo2.dryRun()
            && bo1.keepGoing() == bo2.keepGoing()
            && bo1.logElapsedTime() == bo2.logElapsedTime()
            && bo1.echoMode() == bo2.echoMode()
            && bo1.maxJobCount() == bo2.maxJobCount()
            && bo1.install() == bo2.install()
            && bo1.removeExistingInstallation() == bo2.removeExistingInstallation();
}

namespace Internal {
template<> JobLimits fromJson(const QJsonValue &limitsData)
{
    JobLimits limits;
    const QJsonArray &limitsArray = limitsData.toArray();
    for (const QJsonValue &v : limitsArray) {
        const QJsonObject limitData = v.toObject();
        QString pool;
        int limit = 0;
        setValueFromJson(pool, limitData, "pool");
        setValueFromJson(limit, limitData, "limit");
        if (!pool.isEmpty() && limit > 0)
            limits.setJobLimit(pool, limit);
    }
    return limits;
}

template<> CommandEchoMode fromJson(const QJsonValue &modeData)
{
    const QString modeString = modeData.toString();
    if (modeString == QLatin1String("silent"))
        return CommandEchoModeSilent;
    if (modeString == QLatin1String("command-line"))
        return CommandEchoModeCommandLine;
    if (modeString == QLatin1String("command-line-with-environment"))
        return CommandEchoModeCommandLineWithEnvironment;
    return CommandEchoModeSummary;
}
} // namespace Internal

qbs::BuildOptions qbs::BuildOptions::fromJson(const QJsonObject &data)
{
    using namespace Internal;
    BuildOptions opt;
    setValueFromJson(opt.d->changedFiles, data, "changed-files");
    setValueFromJson(opt.d->filesToConsider, data, "files-to-consider");
    setValueFromJson(opt.d->activeFileTags, data, "active-file-tags");
    setValueFromJson(opt.d->jobLimits, data, "job-limits");
    setValueFromJson(opt.d->maxJobCount, data, "max-job-count");
    setValueFromJson(opt.d->dryRun, data, "dry-run");
    setValueFromJson(opt.d->keepGoing, data, "keep-going");
    setValueFromJson(opt.d->forceTimestampCheck, data, "check-timestamps");
    setValueFromJson(opt.d->forceOutputCheck, data, "check-outputs");
    setValueFromJson(opt.d->logElapsedTime, data, "log-time");
    setValueFromJson(opt.d->echoMode, data, "command-echo-mode");
    setValueFromJson(opt.d->install, data, "install");
    setValueFromJson(opt.d->removeExistingInstallation, data, "clean-install-root");
    setValueFromJson(opt.d->onlyExecuteRules, data, "only-execute-rules");
    setValueFromJson(opt.d->jobLimitsFromProjectTakePrecedence, data, "enforce-project-job-limits");
    return opt;
}

} // namespace qbs