OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / cmakeprojectmanager / makestep.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "makestep.h"
35
36 #include "cmakeprojectconstants.h"
37 #include "cmakeproject.h"
38 #include "cmaketarget.h"
39 #include "cmakebuildconfiguration.h"
40
41 #include <projectexplorer/buildsteplist.h>
42 #include <projectexplorer/toolchain.h>
43 #include <projectexplorer/projectexplorer.h>
44 #include <projectexplorer/gnumakeparser.h>
45
46 #include <utils/qtcprocess.h>
47
48 #include <QtGui/QFormLayout>
49 #include <QtGui/QGroupBox>
50 #include <QtGui/QCheckBox>
51 #include <QtGui/QLineEdit>
52 #include <QtGui/QListWidget>
53
54 using namespace CMakeProjectManager;
55 using namespace CMakeProjectManager::Internal;
56 using namespace ProjectExplorer;
57
58 namespace {
59 const char * const MS_ID("CMakeProjectManager.MakeStep");
60
61 const char * const CLEAN_KEY("CMakeProjectManager.MakeStep.Clean");
62 const char * const BUILD_TARGETS_KEY("CMakeProjectManager.MakeStep.BuildTargets");
63 const char * const ADDITIONAL_ARGUMENTS_KEY("CMakeProjectManager.MakeStep.AdditionalArguments");
64 }
65
66 // TODO: Move progress information into an IOutputParser!
67
68 MakeStep::MakeStep(BuildStepList *bsl) :
69     AbstractProcessStep(bsl, QLatin1String(MS_ID)), m_clean(false),
70     m_futureInterface(0)
71 {
72     ctor();
73 }
74
75 MakeStep::MakeStep(BuildStepList *bsl, const QString &id) :
76     AbstractProcessStep(bsl, id), m_clean(false),
77     m_futureInterface(0)
78 {
79     ctor();
80 }
81
82 MakeStep::MakeStep(BuildStepList *bsl, MakeStep *bs) :
83     AbstractProcessStep(bsl, bs),
84     m_clean(bs->m_clean),
85     m_futureInterface(0),
86     m_buildTargets(bs->m_buildTargets),
87     m_additionalArguments(Utils::QtcProcess::joinArgs(bs->m_buildTargets))
88 {
89     ctor();
90 }
91
92 void MakeStep::ctor()
93 {
94     m_percentProgress = QRegExp("^\\[\\s*(\\d*)%\\]");
95     //: Default display name for the cmake make step.
96     setDefaultDisplayName(tr("Make"));
97 }
98
99 MakeStep::~MakeStep()
100 {
101 }
102
103 CMakeBuildConfiguration *MakeStep::cmakeBuildConfiguration() const
104 {
105     return static_cast<CMakeBuildConfiguration *>(buildConfiguration());
106 }
107
108 void MakeStep::setClean(bool clean)
109 {
110     m_clean = clean;
111 }
112
113 QVariantMap MakeStep::toMap() const
114 {
115     QVariantMap map(AbstractProcessStep::toMap());
116     map.insert(QLatin1String(CLEAN_KEY), m_clean);
117     map.insert(QLatin1String(BUILD_TARGETS_KEY), m_buildTargets);
118     map.insert(QLatin1String(ADDITIONAL_ARGUMENTS_KEY), m_additionalArguments);
119     return map;
120 }
121
122 bool MakeStep::fromMap(const QVariantMap &map)
123 {
124     m_clean = map.value(QLatin1String(CLEAN_KEY)).toBool();
125     m_buildTargets = map.value(QLatin1String(BUILD_TARGETS_KEY)).toStringList();
126     m_additionalArguments = map.value(QLatin1String(ADDITIONAL_ARGUMENTS_KEY)).toString();
127
128     return BuildStep::fromMap(map);
129 }
130
131
132 bool MakeStep::init()
133 {
134     CMakeBuildConfiguration *bc = cmakeBuildConfiguration();
135
136     QString arguments = Utils::QtcProcess::joinArgs(m_buildTargets);
137     Utils::QtcProcess::addArgs(&arguments, additionalArguments());
138
139     setEnabled(true);
140     setIgnoreReturnValue(m_clean);
141
142     ProcessParameters *pp = processParameters();
143     pp->setMacroExpander(bc->macroExpander());
144     pp->setEnvironment(bc->environment());
145     pp->setWorkingDirectory(bc->buildDirectory());
146     pp->setCommand(bc->toolChain()->makeCommand());
147     pp->setArguments(arguments);
148
149     setOutputParser(new ProjectExplorer::GnuMakeParser());
150     if (bc->toolChain())
151         appendOutputParser(bc->toolChain()->outputParser());
152     outputParser()->setWorkingDirectory(pp->effectiveWorkingDirectory());
153
154     return AbstractProcessStep::init();
155 }
156
157 void MakeStep::run(QFutureInterface<bool> &fi)
158 {
159     m_futureInterface = &fi;
160     m_futureInterface->setProgressRange(0, 100);
161     AbstractProcessStep::run(fi);
162     m_futureInterface->setProgressValue(100);
163     m_futureInterface->reportFinished();
164     m_futureInterface = 0;
165 }
166
167 BuildStepConfigWidget *MakeStep::createConfigWidget()
168 {
169     return new MakeStepConfigWidget(this);
170 }
171
172 bool MakeStep::immutable() const
173 {
174     return false;
175 }
176
177 void MakeStep::stdOutput(const QString &line)
178 {
179     if (m_percentProgress.indexIn(line) != -1) {
180         bool ok = false;
181         int percent = m_percentProgress.cap(1).toInt(&ok);;
182         if (ok)
183             m_futureInterface->setProgressValue(percent);
184     }
185     AbstractProcessStep::stdOutput(line);
186 }
187
188 bool MakeStep::buildsBuildTarget(const QString &target) const
189 {
190     return m_buildTargets.contains(target);
191 }
192
193 void MakeStep::setBuildTarget(const QString &buildTarget, bool on)
194 {
195     QStringList old = m_buildTargets;
196     if (on && !old.contains(buildTarget))
197         old << buildTarget;
198     else if(!on && old.contains(buildTarget))
199         old.removeOne(buildTarget);
200     m_buildTargets = old;
201 }
202
203 QString MakeStep::additionalArguments() const
204 {
205     return m_additionalArguments;
206 }
207
208 void MakeStep::setAdditionalArguments(const QString &list)
209 {
210     m_additionalArguments = list;
211 }
212
213 //
214 // MakeStepConfigWidget
215 //
216
217 MakeStepConfigWidget::MakeStepConfigWidget(MakeStep *makeStep)
218     : m_makeStep(makeStep)
219 {
220     QFormLayout *fl = new QFormLayout(this);
221     fl->setMargin(0);
222     fl->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
223     setLayout(fl);
224
225     m_additionalArguments = new QLineEdit(this);
226     fl->addRow(tr("Additional arguments:"), m_additionalArguments);
227
228     connect(m_additionalArguments, SIGNAL(textEdited(const QString &)), this, SLOT(additionalArgumentsEdited()));
229
230     m_buildTargetsList = new QListWidget;
231     m_buildTargetsList->setMinimumHeight(200);
232     fl->addRow(tr("Targets:"), m_buildTargetsList);
233
234     // TODO update this list also on rescans of the CMakeLists.txt
235     // TODO shouldn't be accessing project
236     CMakeProject *pro = m_makeStep->cmakeBuildConfiguration()->cmakeTarget()->cmakeProject();
237     foreach(const QString& buildTarget, pro->buildTargetTitles()) {
238         QListWidgetItem *item = new QListWidgetItem(buildTarget, m_buildTargetsList);
239         item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
240         item->setCheckState(Qt::Unchecked);
241     }
242
243     connect(m_buildTargetsList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(itemChanged(QListWidgetItem*)));
244     connect(ProjectExplorer::ProjectExplorerPlugin::instance(), SIGNAL(settingsChanged()),
245             this, SLOT(updateDetails()));
246
247 }
248
249 void MakeStepConfigWidget::additionalArgumentsEdited()
250 {
251     m_makeStep->setAdditionalArguments(m_additionalArguments->text());
252     updateDetails();
253 }
254
255 void MakeStepConfigWidget::itemChanged(QListWidgetItem *item)
256 {
257     m_makeStep->setBuildTarget(item->text(), item->checkState() & Qt::Checked);
258     updateDetails();
259 }
260
261 QString MakeStepConfigWidget::displayName() const
262 {
263     return tr("Make", "CMakeProjectManager::MakeStepConfigWidget display name.");
264 }
265
266 void MakeStepConfigWidget::init()
267 {
268     // disconnect to make the changes to the items
269     disconnect(m_buildTargetsList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(itemChanged(QListWidgetItem*)));
270     int count = m_buildTargetsList->count();
271     for(int i = 0; i < count; ++i) {
272         QListWidgetItem *item = m_buildTargetsList->item(i);
273         item->setCheckState(m_makeStep->buildsBuildTarget(item->text()) ? Qt::Checked : Qt::Unchecked);
274     }
275     // and connect again
276     connect(m_buildTargetsList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(itemChanged(QListWidgetItem*)));
277
278     m_additionalArguments->setText(m_makeStep->additionalArguments());
279     updateDetails();
280
281     CMakeProject *pro = m_makeStep->cmakeBuildConfiguration()->cmakeTarget()->cmakeProject();
282     connect(pro, SIGNAL(buildTargetsChanged()),
283             this, SLOT(buildTargetsChanged()));
284 }
285
286 void MakeStepConfigWidget::buildTargetsChanged()
287 {
288     disconnect(m_buildTargetsList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(itemChanged(QListWidgetItem*)));
289     m_buildTargetsList->clear();
290     CMakeProject *pro = m_makeStep->cmakeBuildConfiguration()->cmakeTarget()->cmakeProject();
291     foreach(const QString& buildTarget, pro->buildTargetTitles()) {
292         QListWidgetItem *item = new QListWidgetItem(buildTarget, m_buildTargetsList);
293         item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
294         item->setCheckState(m_makeStep->buildsBuildTarget(item->text()) ? Qt::Checked : Qt::Unchecked);
295     }
296     connect(m_buildTargetsList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(itemChanged(QListWidgetItem*)));
297     updateSummary();
298 }
299
300 void MakeStepConfigWidget::updateDetails()
301 {
302     CMakeBuildConfiguration *bc = m_makeStep->cmakeBuildConfiguration();
303     ProjectExplorer::ToolChain *tc = bc->toolChain();
304     if (tc) {
305         QString arguments = Utils::QtcProcess::joinArgs(m_makeStep->m_buildTargets);
306         Utils::QtcProcess::addArgs(&arguments, m_makeStep->additionalArguments());
307
308         ProcessParameters param;
309         param.setMacroExpander(bc->macroExpander());
310         param.setEnvironment(bc->environment());
311         param.setWorkingDirectory(bc->buildDirectory());
312         param.setCommand(tc->makeCommand());
313         param.setArguments(arguments);
314         m_summaryText = param.summary(displayName());
315     } else {
316         m_summaryText = tr("<b>Unknown Toolchain</b>");
317     }
318     emit updateSummary();
319 }
320
321 QString MakeStepConfigWidget::summaryText() const
322 {
323     return m_summaryText;
324 }
325
326 //
327 // MakeStepFactory
328 //
329
330 MakeStepFactory::MakeStepFactory(QObject *parent) :
331     ProjectExplorer::IBuildStepFactory(parent)
332 {
333 }
334
335 MakeStepFactory::~MakeStepFactory()
336 {
337 }
338
339 bool MakeStepFactory::canCreate(BuildStepList *parent, const QString &id) const
340 {
341     if (parent->target()->project()->id() != QLatin1String(Constants::CMAKEPROJECT_ID))
342         return false;
343     return QLatin1String(MS_ID) == id;
344 }
345
346 BuildStep *MakeStepFactory::create(BuildStepList *parent, const QString &id)
347 {
348     if (!canCreate(parent, id))
349         return 0;
350     return new MakeStep(parent);
351 }
352
353 bool MakeStepFactory::canClone(BuildStepList *parent, BuildStep *source) const
354 {
355     return canCreate(parent, source->id());
356 }
357
358 BuildStep *MakeStepFactory::clone(BuildStepList *parent, BuildStep *source)
359 {
360     if (!canClone(parent, source))
361         return 0;
362     return new MakeStep(parent, static_cast<MakeStep *>(source));
363 }
364
365 bool MakeStepFactory::canRestore(BuildStepList *parent, const QVariantMap &map) const
366 {
367     QString id(ProjectExplorer::idFromMap(map));
368     return canCreate(parent, id);
369 }
370
371 BuildStep *MakeStepFactory::restore(BuildStepList *parent, const QVariantMap &map)
372 {
373     if (!canRestore(parent, map))
374         return 0;
375     MakeStep *bs(new MakeStep(parent));
376     if (bs->fromMap(map))
377         return bs;
378     delete bs;
379     return 0;
380 }
381
382 QStringList MakeStepFactory::availableCreationIds(ProjectExplorer::BuildStepList *parent) const
383 {
384     if (parent->target()->project()->id() == QLatin1String(Constants::CMAKEPROJECT_ID))
385         return QStringList() << QLatin1String(MS_ID);
386     return QStringList();
387 }
388
389 QString MakeStepFactory::displayNameForId(const QString &id) const
390 {
391     if (id == QLatin1String(MS_ID))
392         return tr("Make", "Display name for CMakeProjectManager::MakeStep id.");
393     return QString();
394 }