aboutsummaryrefslogtreecommitdiffstats
path: root/src/app/qbs/parser/parsercommand.cpp
blob: 9485b087808b2978ff9a134d482edfca8c98fca2 (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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/****************************************************************************
**
** 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 "parsercommand.h"

#include "commandlineoption.h"
#include "commandlineoptionpool.h"

#include <logging/translator.h>
#include <tools/error.h>
#include <tools/hostosinfo.h>
#include <tools/qbsassert.h>
#include <tools/stlutils.h>
#include <tools/qttools.h>

#include <QtCore/qmap.h>

namespace qbs {
using namespace Internal;

Command::~Command()
{
}

void Command::parse(QStringList &input)
{
    while (!input.empty())
        parseNext(input);
}

bool Command::canResolve() const
{
    return supportedOptions().contains(CommandLineOption::FileOptionType);
}

void Command::parsePropertyAssignment(const QString &argument)
{
    const auto throwError = [argument](const QString &msgTemplate) {
        ErrorInfo error(msgTemplate.arg(argument));
        error.append(QLatin1String("Expected an assignment of the form <property>:<value>, "
                                   "profile:<profile-name> or config:<configuration-name>."));
        throw error;
    };
    if (argument.startsWith(QLatin1Char('-')))
        throwError(Tr::tr("Unexpected option '%1'."));
    const int sepPos = argument.indexOf(QLatin1Char(':'));
    if (sepPos == -1)
        throwError(Tr::tr("Unexpected command line parameter '%1'."));
    if (sepPos == 0)
        throwError(Tr::tr("Empty key not allowed in assignment '%1'."));
    if (!canResolve() && argument.contains(QLatin1Char(':'))
            && !argument.startsWith(QLatin1String("config:"))) {
        throw ErrorInfo(Tr::tr("The '%1' command does not support property assignments.")
                        .arg(representation()));
    }
    m_additionalArguments << argument;
}

QList<CommandLineOption::Type> Command::actualSupportedOptions() const
{
    QList<CommandLineOption::Type> options = supportedOptions();
    if (type() != HelpCommandType)
        options.push_back(CommandLineOption::SettingsDirOptionType); // Valid for almost all commands.
    return options;
}

void Command::parseOption(QStringList &input)
{
    const QString optionString = input.front();
    QBS_CHECK(optionString.startsWith(QLatin1Char('-')));
    input.removeFirst();
    if (optionString.size() == 1)
        throwError(Tr::tr("Empty options are not allowed."));

    // Split up grouped short options.
    if (optionString.at(1) != QLatin1Char('-') && optionString.size() > 2) {
        QString parameter;
        for (int i = optionString.size(); --i > 0;) {
            const QChar c = optionString.at(i);
            if (c.isDigit()) {
                parameter.prepend(c);
            } else {
                if (!parameter.isEmpty()) {
                    input.prepend(parameter);
                    parameter.clear();
                }
                input.prepend(QLatin1Char('-') + c);
            }
        }
        if (!parameter.isEmpty())
            throwError(Tr::tr("Unknown numeric option '%1'.").arg(parameter));
        return;
    }

    bool matchFound = false;
    const auto optionTypes = actualSupportedOptions();
    for (const CommandLineOption::Type optionType : optionTypes) {
        CommandLineOption * const option = optionPool().getOption(optionType);
        if (option->shortRepresentation() != optionString
                && option->longRepresentation() != optionString) {
            continue;
        }
        if (contains(m_usedOptions, option) && !option->canAppearMoreThanOnce())
            throwError(Tr::tr("Option '%1' cannot appear more than once.").arg(optionString));
        option->parse(type(), optionString, input);
        m_usedOptions.insert(option);
        matchFound = true;
        break;
    }
    if (!matchFound)
        throwError(Tr::tr("Unknown option '%1'.").arg(optionString));
}

void Command::parseNext(QStringList &input)
{
    QBS_CHECK(!input.empty());
    if (input.front().startsWith(QLatin1Char('-')))
        parseOption(input);
    else
        parsePropertyAssignment(input.takeFirst());
}

QString Command::supportedOptionsDescription() const
{
    // Sorting the options by name is nicer for the user.
    QMap<QString, const CommandLineOption *> optionMap;
    const auto opTypes = actualSupportedOptions();
    for (const CommandLineOption::Type opType : opTypes) {
        const CommandLineOption * const option = optionPool().getOption(opType);
        optionMap.insert(option->longRepresentation(), option);
    }

    QString s = Tr::tr("The possible options are:\n");
    for (const CommandLineOption *option : qAsConst(optionMap))
        s += option->description(type());
    return s;
}

void Command::throwError(const QString &reason)
{
    ErrorInfo error(Tr::tr("Invalid use of command '%1': %2").arg(representation(), reason));
    error.append(Tr::tr("Type 'qbs help %1' to see how to use this command.")
                 .arg(representation()));
    throw error;
}


QString ResolveCommand::shortDescription() const
{
    return Tr::tr("Resolve a project without building it.");
}

QString ResolveCommand::longDescription() const
{
    QString description = Tr::tr(
                "qbs %1 [options] [[config:<configuration-name>] [<property>:<value>] ...] ...\n")
            .arg(representation());
    description += Tr::tr("Resolves a project in one or more configuration(s).\n");
    return description += supportedOptionsDescription();
}

QString ResolveCommand::representation() const
{
    return QStringLiteral("resolve");
}

static QList<CommandLineOption::Type> resolveOptions()
{
    return {CommandLineOption::FileOptionType,
            CommandLineOption::BuildDirectoryOptionType,
            CommandLineOption::LogLevelOptionType,
            CommandLineOption::VerboseOptionType,
            CommandLineOption::QuietOptionType,
            CommandLineOption::ShowProgressOptionType,
            CommandLineOption::DryRunOptionType,
            CommandLineOption::ForceProbesOptionType,
            CommandLineOption::LogTimeOptionType,
            CommandLineOption::DisableFallbackProviderType};
}

QList<CommandLineOption::Type> ResolveCommand::supportedOptions() const
{
    return resolveOptions();
}

QString GenerateCommand::shortDescription() const
{
    return Tr::tr("Generate project files for another build tool.");
}

QString GenerateCommand::longDescription() const
{
    QString description = Tr::tr(
                "qbs %1 [options] [[config:<configuration-name>] [<property>:<value>] ...] ...\n")
            .arg(representation());
    description += Tr::tr("Generates files to build the project using another build tool.\n");
    return description += supportedOptionsDescription();
}

QString GenerateCommand::representation() const
{
    return QStringLiteral("generate");
}

QList<CommandLineOption::Type> GenerateCommand::supportedOptions() const
{
    return {CommandLineOption::FileOptionType,
            CommandLineOption::BuildDirectoryOptionType,
            CommandLineOption::LogLevelOptionType,
            CommandLineOption::VerboseOptionType,
            CommandLineOption::QuietOptionType,
            CommandLineOption::ShowProgressOptionType,
            CommandLineOption::InstallRootOptionType,
            CommandLineOption::LogTimeOptionType,
            CommandLineOption::GeneratorOptionType};
}

QString BuildCommand::shortDescription() const
{
    return Tr::tr("Build (parts of) a project. This is the default command.");
}

QString BuildCommand::longDescription() const
{
    QString description = Tr::tr(
                "qbs %1 [options] [[config:<configuration-name>] [<property>:<value>] ...] ...\n")
            .arg(representation());
    description += Tr::tr("Builds a project in one or more configuration(s).\n");
    return description += supportedOptionsDescription();
}

static QString buildCommandRepresentation() { return QStringLiteral("build"); }

QString BuildCommand::representation() const
{
    return buildCommandRepresentation();
}

static QList<CommandLineOption::Type> buildOptions()
{
    QList<CommandLineOption::Type> options = resolveOptions();
    return options << CommandLineOption::KeepGoingOptionType
            << CommandLineOption::ProductsOptionType
            << CommandLineOption::ChangedFilesOptionType
            << CommandLineOption::ForceTimestampCheckOptionType
            << CommandLineOption::ForceOutputCheckOptionType
            << CommandLineOption::BuildNonDefaultOptionType
            << CommandLineOption::JobsOptionType
            << CommandLineOption::CommandEchoModeOptionType
            << CommandLineOption::NoInstallOptionType
            << CommandLineOption::RemoveFirstOptionType
            << CommandLineOption::JobLimitsOptionType
            << CommandLineOption::RespectProjectJobLimitsOptionType
            << CommandLineOption::WaitLockOptionType;
}

QList<CommandLineOption::Type> BuildCommand::supportedOptions() const
{
    return buildOptions();
}

QString CleanCommand::shortDescription() const
{
    return Tr::tr("Remove the files generated during a build.");
}

QString CleanCommand::longDescription() const
{
    QString description = Tr::tr(
                "qbs %1 [options] [config:<configuration-name>] ...\n")
            .arg(representation());
    description += Tr::tr("Removes build artifacts for the given configuration(s).\n");
    return description += supportedOptionsDescription();
}

QString CleanCommand::representation() const
{
    return QStringLiteral("clean");
}

QList<CommandLineOption::Type> CleanCommand::supportedOptions() const
{
    return {CommandLineOption::BuildDirectoryOptionType,
            CommandLineOption::DryRunOptionType,
            CommandLineOption::KeepGoingOptionType,
            CommandLineOption::LogTimeOptionType,
            CommandLineOption::ProductsOptionType,
            CommandLineOption::QuietOptionType,
            CommandLineOption::SettingsDirOptionType,
            CommandLineOption::ShowProgressOptionType,
            CommandLineOption::VerboseOptionType};
}

QString InstallCommand::shortDescription() const
{
    return Tr::tr("Install (parts of) a project.");
}

QString InstallCommand::longDescription() const
{
    QString description = Tr::tr(
                "qbs %1 [options] [[config:<configuration-name>] [<property>:<value>] ...]\n")
            .arg(representation());
    description += Tr::tr("Install all files marked as installable "
                          "to their respective destinations.\n"
                          "The project is built first, if necessary, unless the '%1' option "
                          "is given.\n").arg(optionPool().noBuildOption()->longRepresentation());
    return description += supportedOptionsDescription();
}

QString InstallCommand::representation() const
{
    return QStringLiteral("install");
}

QList<CommandLineOption::Type> installOptions()
{
    QList<CommandLineOption::Type> options = buildOptions()
            << CommandLineOption::InstallRootOptionType
            << CommandLineOption::NoBuildOptionType;
    options.removeOne(CommandLineOption::NoInstallOptionType);
    return options;
}

QList<CommandLineOption::Type> InstallCommand::supportedOptions() const
{
    return installOptions();
}

QString RunCommand::shortDescription() const
{
    return QStringLiteral("Run an executable generated by building a project.");
}

QString RunCommand::longDescription() const
{
    QString description = Tr::tr(
                "qbs %1 [options] [config:<configuration-name>] [<property>:<value>] ... "
                                 "[ -- <arguments>]\n").arg(representation());
    description += Tr::tr("Run the specified product's executable with the specified arguments.\n");
    description += Tr::tr("If the project has only one product, the '%1' option may be omitted.\n")
            .arg(optionPool().productsOption()->longRepresentation());
    description += Tr::tr("The product will be built if it is not up to date; "
                          "see the '%2' command.\n").arg(buildCommandRepresentation());
    return description += supportedOptionsDescription();
}

QString RunCommand::representation() const
{
    return QStringLiteral("run");
}

QList<CommandLineOption::Type> RunCommand::supportedOptions() const
{
    return QList<CommandLineOption::Type>() << installOptions()
                                            << CommandLineOption::RunEnvConfigOptionType;
}

void RunCommand::parseNext(QStringList &input)
{
    QBS_CHECK(!input.empty());
    if (input.front() != QLatin1String("--")) {
        Command::parseNext(input);
        return;
    }
    input.removeFirst();
    m_targetParameters = input;
    input.clear();
}

QString ShellCommand::shortDescription() const
{
    return Tr::tr("Open a shell with a product's environment.");
}

QString ShellCommand::longDescription() const
{
    QString description = Tr::tr(
                "qbs %1 [options] [config:<configuration-name>] [<property>:<value>] ...\n")
            .arg(representation());
    description += Tr::tr("Opens a shell in the same environment that a build with the given "
                          "parameters would use.\n");
    return description += supportedOptionsDescription();
}

QString ShellCommand::representation() const
{
    return QStringLiteral("shell");
}

QList<CommandLineOption::Type> ShellCommand::supportedOptions() const
{
    return {CommandLineOption::FileOptionType,
            CommandLineOption::BuildDirectoryOptionType,
            CommandLineOption::ProductsOptionType};
}

QString StatusCommand::shortDescription() const
{
    return Tr::tr("Show the status of files in the project directory.");
}

QString StatusCommand::longDescription() const
{
    QString description = Tr::tr("qbs %1 [options] [config:<configuration-name>]\n")
            .arg(representation());
    description += Tr::tr("Lists all the files in the project directory and shows whether "
                          "they are known to qbs in the respective configuration.\n");
    return description += supportedOptionsDescription();
}

QString StatusCommand::representation() const
{
    return QStringLiteral("status");
}

QList<CommandLineOption::Type> StatusCommand::supportedOptions() const
{
    return {CommandLineOption::BuildDirectoryOptionType};
}

QString UpdateTimestampsCommand::shortDescription() const
{
    return Tr::tr("Mark the build as up to date.");
}

QString UpdateTimestampsCommand::longDescription() const
{
    QString description = Tr::tr(
                "qbs %1 [options] [config:<configuration-name>] ...\n")
            .arg(representation());
    description += Tr::tr("Update the timestamps of all build artifacts, causing the next "
            "builds of the project to do nothing if no updates to source files happen in between.\n"
            "This functionality is useful if you know that the current changes to source files "
            "are irrelevant to the build.\n"
            "NOTE: Doing this causes a discrepancy between the \"real world\" and the information "
            "in the build graph, so use with care.\n");
    return description += supportedOptionsDescription();
}

QString UpdateTimestampsCommand::representation() const
{
    return QStringLiteral("update-timestamps");
}

QList<CommandLineOption::Type> UpdateTimestampsCommand::supportedOptions() const
{
    return {CommandLineOption::BuildDirectoryOptionType,
            CommandLineOption::LogLevelOptionType,
            CommandLineOption::VerboseOptionType,
            CommandLineOption::QuietOptionType,
            CommandLineOption::ProductsOptionType};
}

QString DumpNodesTreeCommand::shortDescription() const
{
    return Tr::tr("Dumps the nodes in the build graph to stdout.");
}

QString DumpNodesTreeCommand::longDescription() const
{
    QString description = Tr::tr("qbs %1 [options] [config:<configuration-name>] ...\n")
            .arg(representation());
    description += Tr::tr("Internal command; for debugging purposes only.\n");
    return description += supportedOptionsDescription();
}

QString DumpNodesTreeCommand::representation() const
{
    return QStringLiteral("dump-nodes-tree");
}

QList<CommandLineOption::Type> DumpNodesTreeCommand::supportedOptions() const
{
    return {CommandLineOption::BuildDirectoryOptionType,
            CommandLineOption::ProductsOptionType};
}

QString ListProductsCommand::shortDescription() const
{
    return Tr::tr("Lists all products in the project, including sub-projects.");
}

QString ListProductsCommand::longDescription() const
{
    QString description = Tr::tr("qbs %1 [options] [[config:<configuration-name>] "
                                 "[<property>:<value>] ...] ...\n").arg(representation());
    return description += supportedOptionsDescription();
}

QString ListProductsCommand::representation() const
{
    return QStringLiteral("list-products");
}

QList<CommandLineOption::Type> ListProductsCommand::supportedOptions() const
{
    return {CommandLineOption::FileOptionType,
            CommandLineOption::BuildDirectoryOptionType};
}

QString HelpCommand::shortDescription() const
{
    return Tr::tr("Show general or command-specific help.");
}

QString HelpCommand::longDescription() const
{
    QString description = Tr::tr("qbs %1 [<command>]\n").arg(representation());
    return description += Tr::tr("Shows either the general help or a description of "
                                 "the given command.\n");
}

QString HelpCommand::representation() const
{
    return QStringLiteral("help");
}

QList<CommandLineOption::Type> HelpCommand::supportedOptions() const
{
    return {};
}

void HelpCommand::parseNext(QStringList &input)
{
    if (input.empty())
        return;
    if (input.size() > 1)
        throwError(Tr::tr("Cannot describe more than one command."));
    m_command = input.takeFirst();
    QBS_CHECK(input.empty());
}

QString VersionCommand::shortDescription() const
{
    return Tr::tr("Print the Qbs version number to stdout.");
}

QString VersionCommand::longDescription() const
{
    QString description = Tr::tr("qbs %1\n").arg(representation());
    return description += Tr::tr("%1\n").arg(shortDescription());
}

QString VersionCommand::representation() const
{
    return QStringLiteral("show-version");
}

QList<CommandLineOption::Type> VersionCommand::supportedOptions() const
{
    return {};
}

void VersionCommand::parseNext(QStringList &input)
{
    QBS_CHECK(!input.empty());
    throwError(Tr::tr("This command takes no arguments."));
}

} // namespace qbs