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
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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 General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** 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-3.0.html.
**
****************************************************************************/
#include "cmakebuildstep.h"
#include "cmakebuildconfiguration.h"
#include "cmakekitinformation.h"
#include "cmakeparser.h"
#include "cmakeprojectconstants.h"
#include "cmakeproject.h"
#include "cmakerunconfiguration.h"
#include "cmaketool.h"
#include <projectexplorer/buildsteplist.h>
#include <projectexplorer/deployconfiguration.h>
#include <projectexplorer/gnumakeparser.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/processparameters.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/target.h>
#include <projectexplorer/toolchain.h>
#include <qtsupport/qtkitinformation.h>
#include <qtsupport/qtparser.h>
#include <coreplugin/find/itemviewfind.h>
#include <utils/algorithm.h>
#include <utils/qtcprocess.h>
#include <utils/pathchooser.h>
#include <QFormLayout>
#include <QGroupBox>
#include <QCheckBox>
#include <QLineEdit>
#include <QListWidget>
using namespace CMakeProjectManager;
using namespace CMakeProjectManager::Internal;
using namespace ProjectExplorer;
namespace {
const char BUILD_TARGETS_KEY[] = "CMakeProjectManager.MakeStep.BuildTargets";
const char TOOL_ARGUMENTS_KEY[] = "CMakeProjectManager.MakeStep.AdditionalArguments";
const char ADD_RUNCONFIGURATION_ARGUMENT_KEY[] = "CMakeProjectManager.MakeStep.AddRunConfigurationArgument";
const char ADD_RUNCONFIGURATION_TEXT[] = "Current executable";
}
static bool isCurrentExecutableTarget(const QString &target)
{
return target == ADD_RUNCONFIGURATION_TEXT;
}
CMakeBuildStep::CMakeBuildStep(BuildStepList *bsl) :
AbstractProcessStep(bsl, Constants::CMAKE_BUILD_STEP_ID)
{
m_percentProgress = QRegExp("^\\[\\s*(\\d*)%\\]");
m_ninjaProgress = QRegExp("^\\[\\s*(\\d*)/\\s*(\\d*)");
m_ninjaProgressString = "[%f/%t "; // ninja: [33/100
//: Default display name for the cmake make step.
setDefaultDisplayName(tr("CMake Build"));
auto bc = qobject_cast<CMakeBuildConfiguration *>(bsl->parent());
if (!bc) {
auto t = qobject_cast<Target *>(bsl->parent()->parent());
QTC_ASSERT(t, return);
bc = qobject_cast<CMakeBuildConfiguration *>(t->activeBuildConfiguration());
}
// Set a good default build target:
if (m_buildTarget.isEmpty()) {
if (bsl->id() == ProjectExplorer::Constants::BUILDSTEPS_CLEAN)
setBuildTarget(cleanTarget());
else if (bsl->id() == ProjectExplorer::Constants::BUILDSTEPS_DEPLOY)
setBuildTarget(installTarget());
else
setBuildTarget(allTarget());
}
connect(target(), &Target::kitChanged, this, &CMakeBuildStep::cmakeCommandChanged);
connect(project(), &Project::parsingFinished,
this, &CMakeBuildStep::handleBuildTargetChanges);
}
CMakeBuildConfiguration *CMakeBuildStep::cmakeBuildConfiguration() const
{
return static_cast<CMakeBuildConfiguration *>(buildConfiguration());
}
CMakeRunConfiguration *CMakeBuildStep::targetsActiveRunConfiguration() const
{
return qobject_cast<CMakeRunConfiguration *>(target()->activeRunConfiguration());
}
void CMakeBuildStep::handleBuildTargetChanges(bool success)
{
if (!success)
return; // Do not change when parsing failed.
if (isCurrentExecutableTarget(m_buildTarget))
return; // Do not change just because a different set of build targets is there...
if (!static_cast<CMakeProject *>(project())->buildTargetTitles().contains(m_buildTarget))
setBuildTarget(allTarget());
emit buildTargetsChanged();
}
QVariantMap CMakeBuildStep::toMap() const
{
QVariantMap map(AbstractProcessStep::toMap());
// Use QStringList for compatibility with old files
map.insert(BUILD_TARGETS_KEY, QStringList(m_buildTarget));
map.insert(TOOL_ARGUMENTS_KEY, m_toolArguments);
return map;
}
bool CMakeBuildStep::fromMap(const QVariantMap &map)
{
const QStringList targetList = map.value(BUILD_TARGETS_KEY).toStringList();
if (!targetList.isEmpty())
m_buildTarget = targetList.last();
m_toolArguments = map.value(TOOL_ARGUMENTS_KEY).toString();
if (map.value(ADD_RUNCONFIGURATION_ARGUMENT_KEY, false).toBool())
m_buildTarget = ADD_RUNCONFIGURATION_TEXT;
return BuildStep::fromMap(map);
}
bool CMakeBuildStep::init()
{
bool canInit = true;
CMakeBuildConfiguration *bc = cmakeBuildConfiguration();
if (!bc) {
emit addTask(Task::buildConfigurationMissingTask());
canInit = false;
}
if (bc && !bc->isEnabled()) {
emit addTask(Task(Task::Error,
QCoreApplication::translate("CMakeProjectManager::CMakeBuildStep",
"The build configuration is currently disabled."),
Utils::FileName(), -1, ProjectExplorer::Constants::TASK_CATEGORY_BUILDSYSTEM));
canInit = false;
}
CMakeTool *tool = CMakeKitInformation::cmakeTool(target()->kit());
if (!tool || !tool->isValid()) {
emit addTask(Task(Task::Error,
tr("A CMake tool must be set up for building. "
"Configure a CMake tool in the kit options."),
Utils::FileName(), -1,
ProjectExplorer::Constants::TASK_CATEGORY_BUILDSYSTEM));
canInit = false;
}
CMakeRunConfiguration *rc = targetsActiveRunConfiguration();
if (isCurrentExecutableTarget(m_buildTarget) && (!rc || rc->buildKey().isEmpty())) {
emit addTask(Task(Task::Error,
QCoreApplication::translate("ProjectExplorer::Task",
"You asked to build the current Run Configuration's build target only, "
"but it is not associated with a build target. "
"Update the Make Step in your build settings."),
Utils::FileName(), -1,
ProjectExplorer::Constants::TASK_CATEGORY_BUILDSYSTEM));
canInit = false;
}
if (!canInit) {
emitFaultyConfigurationMessage();
return false;
}
// Warn if doing out-of-source builds with a CMakeCache.txt is the source directory
const Utils::FileName projectDirectory = bc->target()->project()->projectDirectory();
if (bc->buildDirectory() != projectDirectory) {
Utils::FileName cmc = projectDirectory;
cmc.appendPath("CMakeCache.txt");
if (cmc.exists()) {
emit addTask(Task(Task::Warning,
tr("There is a CMakeCache.txt file in \"%1\", which suggest an "
"in-source build was done before. You are now building in \"%2\", "
"and the CMakeCache.txt file might confuse CMake.")
.arg(projectDirectory.toUserOutput(), bc->buildDirectory().toUserOutput()),
Utils::FileName(), -1,
ProjectExplorer::Constants::TASK_CATEGORY_BUILDSYSTEM));
}
}
QString arguments = allArguments(rc);
setIgnoreReturnValue(m_buildTarget == CMakeBuildStep::cleanTarget());
ProcessParameters *pp = processParameters();
pp->setMacroExpander(bc->macroExpander());
Utils::Environment env = bc->environment();
Utils::Environment::setupEnglishOutput(&env);
if (!env.value("NINJA_STATUS").startsWith(m_ninjaProgressString))
env.set("NINJA_STATUS", m_ninjaProgressString + "%o/sec] ");
pp->setEnvironment(env);
pp->setWorkingDirectory(bc->buildDirectory().toString());
pp->setCommand(cmakeCommand());
pp->setArguments(arguments);
pp->resolveAll();
setOutputParser(new CMakeParser);
appendOutputParser(new GnuMakeParser);
IOutputParser *parser = target()->kit()->createOutputParser();
if (parser)
appendOutputParser(parser);
outputParser()->setWorkingDirectory(pp->effectiveWorkingDirectory());
return AbstractProcessStep::init();
}
void CMakeBuildStep::doRun()
{
// Make sure CMake state was written to disk before trying to build:
CMakeBuildConfiguration *bc = cmakeBuildConfiguration();
QTC_ASSERT(bc, return);
m_waiting = false;
auto p = static_cast<CMakeProject *>(bc->project());
if (p->persistCMakeState()) {
emit addOutput(tr("Persisting CMake state..."), BuildStep::OutputFormat::NormalMessage);
m_waiting = true;
} else if (p->mustUpdateCMakeStateBeforeBuild()) {
emit addOutput(tr("Running CMake in preparation to build..."), BuildStep::OutputFormat::NormalMessage);
m_waiting = true;
}
if (m_waiting) {
m_runTrigger = connect(project(), &Project::parsingFinished,
this, [this](bool success) { handleProjectWasParsed(success); });
} else {
runImpl();
}
}
void CMakeBuildStep::runImpl()
{
// Do the actual build:
AbstractProcessStep::doRun();
}
void CMakeBuildStep::handleProjectWasParsed(bool success)
{
m_waiting = false;
disconnect(m_runTrigger);
if (isCanceled()) {
emit finished(false);
} else if (success) {
runImpl();
} else {
AbstractProcessStep::stdError(tr("Project did not parse successfully, cannot build."));
emit finished(false);
}
}
BuildStepConfigWidget *CMakeBuildStep::createConfigWidget()
{
return new CMakeBuildStepConfigWidget(this);
}
void CMakeBuildStep::stdOutput(const QString &line)
{
if (m_percentProgress.indexIn(line) != -1) {
AbstractProcessStep::stdOutput(line);
bool ok = false;
int percent = m_percentProgress.cap(1).toInt(&ok);
if (ok)
emit progress(percent, QString());
return;
} else if (m_ninjaProgress.indexIn(line) != -1) {
AbstractProcessStep::stdOutput(line);
m_useNinja = true;
bool ok = false;
int done = m_ninjaProgress.cap(1).toInt(&ok);
if (ok) {
int all = m_ninjaProgress.cap(2).toInt(&ok);
if (ok && all != 0) {
const int percent = static_cast<int>(100.0 * done/all);
emit progress(percent, QString());
}
}
return;
}
if (m_useNinja)
AbstractProcessStep::stdError(line);
else
AbstractProcessStep::stdOutput(line);
}
QString CMakeBuildStep::buildTarget() const
{
return m_buildTarget;
}
bool CMakeBuildStep::buildsBuildTarget(const QString &target) const
{
return target == m_buildTarget;
}
void CMakeBuildStep::setBuildTarget(const QString &buildTarget)
{
if (m_buildTarget == buildTarget)
return;
m_buildTarget = buildTarget;
emit targetToBuildChanged();
}
void CMakeBuildStep::clearBuildTargets()
{
m_buildTarget.clear();
}
QString CMakeBuildStep::toolArguments() const
{
return m_toolArguments;
}
void CMakeBuildStep::setToolArguments(const QString &list)
{
m_toolArguments = list;
}
QString CMakeBuildStep::allArguments(const CMakeRunConfiguration *rc) const
{
QString arguments;
Utils::QtcProcess::addArg(&arguments, "--build");
Utils::QtcProcess::addArg(&arguments, ".");
QString target;
if (isCurrentExecutableTarget(m_buildTarget)) {
if (rc)
target = rc->buildKey().section('\n', 0, 0);
else
target = "<i><" + tr(ADD_RUNCONFIGURATION_TEXT) + "></i>";
} else {
target = m_buildTarget;
}
Utils::QtcProcess::addArg(&arguments, "--target");
Utils::QtcProcess::addArg(&arguments, target);
if (!m_toolArguments.isEmpty()) {
Utils::QtcProcess::addArg(&arguments, "--");
arguments += ' ' + m_toolArguments;
}
return arguments;
}
QString CMakeBuildStep::cmakeCommand() const
{
CMakeTool *tool = CMakeKitInformation::cmakeTool(target()->kit());
return tool ? tool->cmakeExecutable().toString() : QString();
}
QString CMakeBuildStep::cleanTarget()
{
return QString("clean");
}
QString CMakeBuildStep::allTarget()
{
return QString("all");
}
QString CMakeBuildStep::installTarget()
{
return QString("install");
}
QString CMakeBuildStep::testTarget()
{
return QString("test");
}
QStringList CMakeBuildStep::specialTargets()
{
return { allTarget(), cleanTarget(), installTarget(), testTarget() };
}
//
// CMakeBuildStepConfigWidget
//
CMakeBuildStepConfigWidget::CMakeBuildStepConfigWidget(CMakeBuildStep *buildStep) :
BuildStepConfigWidget(buildStep),
m_buildStep(buildStep),
m_toolArguments(new QLineEdit),
m_buildTargetsList(new QListWidget)
{
setDisplayName(tr("Build", "CMakeProjectManager::CMakeBuildStepConfigWidget display name."));
auto fl = new QFormLayout(this);
fl->setMargin(0);
fl->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
setLayout(fl);
fl->addRow(tr("Tool arguments:"), m_toolArguments);
m_toolArguments->setText(m_buildStep->toolArguments());
m_buildTargetsList->setFrameStyle(QFrame::NoFrame);
m_buildTargetsList->setMinimumHeight(200);
auto frame = new QFrame(this);
frame->setFrameStyle(QFrame::StyledPanel);
auto frameLayout = new QVBoxLayout(frame);
frameLayout->setMargin(0);
frameLayout->addWidget(Core::ItemViewFind::createSearchableWrapper(m_buildTargetsList,
Core::ItemViewFind::LightColored));
fl->addRow(tr("Targets:"), frame);
buildTargetsChanged();
updateDetails();
connect(m_toolArguments, &QLineEdit::textEdited, this, &CMakeBuildStepConfigWidget::toolArgumentsEdited);
connect(m_buildTargetsList, &QListWidget::itemChanged, this, &CMakeBuildStepConfigWidget::itemChanged);
connect(ProjectExplorerPlugin::instance(), &ProjectExplorerPlugin::settingsChanged,
this, &CMakeBuildStepConfigWidget::updateDetails);
connect(m_buildStep, &CMakeBuildStep::buildTargetsChanged, this, &CMakeBuildStepConfigWidget::buildTargetsChanged);
connect(m_buildStep, &CMakeBuildStep::targetToBuildChanged, this, &CMakeBuildStepConfigWidget::selectedBuildTargetsChanged);
m_buildStep->project()->subscribeSignal(&BuildConfiguration::environmentChanged, this, [this]() {
if (static_cast<BuildConfiguration *>(sender())->isActive())
updateDetails();
});
connect(m_buildStep->project(), &Project::activeProjectConfigurationChanged,
this, [this](ProjectConfiguration *pc) {
if (pc && pc->isActive())
updateDetails();
});
}
void CMakeBuildStepConfigWidget::toolArgumentsEdited()
{
m_buildStep->setToolArguments(m_toolArguments->text());
updateDetails();
}
void CMakeBuildStepConfigWidget::itemChanged(QListWidgetItem *item)
{
const QString target =
(item->checkState() == Qt::Checked) ? item->data(Qt::UserRole).toString() : CMakeBuildStep::allTarget();
m_buildStep->setBuildTarget(target);
updateDetails();
}
void CMakeBuildStepConfigWidget::buildTargetsChanged()
{
{
QSignalBlocker blocker(m_buildTargetsList);
m_buildTargetsList->clear();
auto pro = static_cast<CMakeProject *>(m_buildStep->project());
QStringList targetList = pro->buildTargetTitles();
targetList.sort();
QFont italics;
italics.setItalic(true);
auto exeItem = new QListWidgetItem(tr(ADD_RUNCONFIGURATION_TEXT), m_buildTargetsList);
exeItem->setData(Qt::UserRole, ADD_RUNCONFIGURATION_TEXT);
foreach (const QString &buildTarget, targetList) {
auto item = new QListWidgetItem(buildTarget, m_buildTargetsList);
item->setData(Qt::UserRole, buildTarget);
}
for (int i = 0; i < m_buildTargetsList->count(); ++i) {
QListWidgetItem *item = m_buildTargetsList->item(i);
const QString title = item->data(Qt::UserRole).toString();
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setCheckState(m_buildStep->buildsBuildTarget(title) ? Qt::Checked : Qt::Unchecked);
// Print utility targets in italics:
if (CMakeBuildStep::specialTargets().contains(title) || title == ADD_RUNCONFIGURATION_TEXT)
item->setFont(italics);
}
}
updateDetails();
}
void CMakeBuildStepConfigWidget::selectedBuildTargetsChanged()
{
{
QSignalBlocker blocker(m_buildTargetsList);
for (int y = 0; y < m_buildTargetsList->count(); ++y) {
QListWidgetItem *item = m_buildTargetsList->item(y);
item->setCheckState(m_buildStep->buildsBuildTarget(item->data(Qt::UserRole).toString())
? Qt::Checked : Qt::Unchecked);
}
}
updateDetails();
}
void CMakeBuildStepConfigWidget::updateDetails()
{
BuildConfiguration *bc = m_buildStep->buildConfiguration();
if (!bc) {
setSummaryText(tr("<b>No build configuration found on this kit.</b>"));
return;
}
ProcessParameters param;
param.setMacroExpander(bc->macroExpander());
param.setEnvironment(bc->environment());
param.setWorkingDirectory(bc->buildDirectory().toString());
param.setCommand(m_buildStep->cmakeCommand());
param.setArguments(m_buildStep->allArguments(nullptr));
setSummaryText(param.summary(displayName()));
}
//
// CMakeBuildStepFactory
//
CMakeBuildStepFactory::CMakeBuildStepFactory()
{
registerStep<CMakeBuildStep>(Constants::CMAKE_BUILD_STEP_ID);
setDisplayName(CMakeBuildStep::tr("Build", "Display name for CMakeProjectManager::CMakeBuildStep id."));
setSupportedProjectType(Constants::CMAKEPROJECT_ID);
}
void CMakeBuildStep::processStarted()
{
m_useNinja = false;
AbstractProcessStep::processStarted();
}
void CMakeBuildStep::processFinished(int exitCode, QProcess::ExitStatus status)
{
AbstractProcessStep::processFinished(exitCode, status);
emit progress(100, QString());
}
|