buildstep.cpp 6.64 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11
** 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
12 13 14
** 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.
15
**
16 17 18 19 20 21 22
** 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.
con's avatar
con committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
hjk's avatar
hjk committed
25

con's avatar
con committed
26
#include "buildstep.h"
hjk's avatar
hjk committed
27

Tobias Hunger's avatar
Tobias Hunger committed
28 29
#include "buildconfiguration.h"
#include "buildsteplist.h"
30
#include "deployconfiguration.h"
Tobias Hunger's avatar
Tobias Hunger committed
31 32
#include "target.h"

33 34
#include <utils/algorithm.h>

35 36 37
/*!
    \class ProjectExplorer::BuildStep

38
    \brief The BuildStep class provides build steps for projects.
39

40 41
    Build steps are the primary way plugin developers can customize
    how their projects (or projects from other plugins) are built.
42

43 44
    Projects are built by taking the list of build steps
    from the project and calling first \c init() and then \c run() on them.
45

46 47
    To change the way your project is built, reimplement
    this class and add your build step to the build step list of the project.
48

49 50 51
    \note The projects own the build step. Do not delete them yourself.

    \c init() is called in the GUI thread and can be used to query the
52 53
    project for any information you need.

Eike Ziller's avatar
Eike Ziller committed
54
    \c run() is run via Utils::runAsync in a separate thread. If you need an
55
    event loop, you need to create it yourself.
56 57 58 59 60
*/

/*!
    \fn bool ProjectExplorer::BuildStep::init()

61 62
    This function is run in the GUI thread. Use it to retrieve any information
    that you need in the run() function.
63 64 65 66 67
*/

/*!
    \fn void ProjectExplorer::BuildStep::run(QFutureInterface<bool> &fi)

68 69
    Reimplement this function. It is called when the target is built.
    By default, this function is NOT run in the GUI thread, but runs in its
70 71
    own thread. If you need an event loop, you need to create one.
    This function should block until the task is done
72 73 74 75 76

    The absolute minimal implementation is:
    \code
    fi.reportResult(true);
    \endcode
77

78 79
    By returning \c true from runInGuiThread(), this function is called in
    the GUI thread. Then the function should not block and instead the
80
    finished() signal should be emitted.
81

82
    \sa runInGuiThread()
83 84 85
*/

/*!
86
    \fn BuildStepConfigWidget *ProjectExplorer::BuildStep::createConfigWidget()
87

88 89
    Returns the Widget shown in the target settings dialog for this build step.
    Ownership is transferred to the caller.
90 91 92 93
*/

/*!
    \fn  void ProjectExplorer::BuildStep::addTask(const ProjectExplorer::Task &task)
94
    Adds \a task.
95 96 97
*/

/*!
98 99
    \fn  void ProjectExplorer::BuildStep::addOutput(const QString &string, ProjectExplorer::BuildStep::OutputFormat format,
              ProjectExplorer::BuildStep::OutputNewlineSetting newlineSetting = DoAppendNewline) const
100

101
    The \a string is added to the generated output, usually in the output pane.
102 103 104
    It should be in plain text, with the format in the parameter.
*/

105 106
/*!
    \fn  void ProjectExplorer::BuildStep::finished()
107
    This signal needs to be emitted if the build step runs in the GUI thread.
108 109
*/

Daniel Teske's avatar
Daniel Teske committed
110 111
static const char buildStepEnabledKey[] = "ProjectExplorer.BuildStep.Enabled";

112
using namespace ProjectExplorer;
con's avatar
con committed
113

114
BuildStep::BuildStep(BuildStepList *bsl, Core::Id id) :
115
    ProjectConfiguration(bsl), m_enabled(true)
con's avatar
con committed
116
{
117
    initialize(id);
Tobias Hunger's avatar
Tobias Hunger committed
118
    Q_ASSERT(bsl);
119
    ctor();
con's avatar
con committed
120 121
}

Tobias Hunger's avatar
Tobias Hunger committed
122
BuildStep::BuildStep(BuildStepList *bsl, BuildStep *bs) :
123
    ProjectConfiguration(bsl), m_enabled(bs->m_enabled)
con's avatar
con committed
124
{
125
    copyFrom(bs);
Tobias Hunger's avatar
Tobias Hunger committed
126
    Q_ASSERT(bsl);
127
    setDisplayName(bs->displayName());
128 129 130 131 132 133 134 135 136
    ctor();
}

void BuildStep::ctor()
{
    Utils::MacroExpander *expander = macroExpander();
    expander->setDisplayName(tr("Build Step"));
    expander->setAccumulating(true);
    expander->registerSubProvider([this] { return projectConfiguration()->macroExpander(); });
con's avatar
con committed
137 138
}

Daniel Teske's avatar
Daniel Teske committed
139 140
bool BuildStep::fromMap(const QVariantMap &map)
{
141
    m_enabled = map.value(buildStepEnabledKey, true).toBool();
Daniel Teske's avatar
Daniel Teske committed
142 143 144 145 146 147
    return ProjectConfiguration::fromMap(map);
}

QVariantMap BuildStep::toMap() const
{
    QVariantMap map = ProjectConfiguration::toMap();
148
    map.insert(buildStepEnabledKey, m_enabled);
Daniel Teske's avatar
Daniel Teske committed
149 150 151
    return map;
}

152
BuildConfiguration *BuildStep::buildConfiguration() const
con's avatar
con committed
153
{
154
    return qobject_cast<BuildConfiguration *>(parent()->parent());
Tobias Hunger's avatar
Tobias Hunger committed
155 156
}

157 158
DeployConfiguration *BuildStep::deployConfiguration() const
{
159
    return qobject_cast<DeployConfiguration *>(parent()->parent());
160 161
}

Daniel Teske's avatar
Daniel Teske committed
162 163 164 165 166
ProjectConfiguration *BuildStep::projectConfiguration() const
{
    return static_cast<ProjectConfiguration *>(parent()->parent());
}

Tobias Hunger's avatar
Tobias Hunger committed
167 168 169
Target *BuildStep::target() const
{
    return qobject_cast<Target *>(parent()->parent()->parent());
con's avatar
con committed
170 171
}

172 173 174 175 176
Project *BuildStep::project() const
{
    return target()->project();
}

177 178 179 180 181 182
void BuildStep::reportRunResult(QFutureInterface<bool> &fi, bool success)
{
    fi.reportResult(success);
    fi.reportFinished();
}

183 184 185 186 187
bool BuildStep::isActive() const
{
    return projectConfiguration()->isActive();
}

188 189 190 191 192 193
/*!
    If this function returns \c true, the user cannot delete this build step for
    this target and the user is prevented from changing the order in which
    immutable steps are run. The default implementation returns \c false.
*/

con's avatar
con committed
194 195 196 197 198
bool BuildStep::immutable() const
{
    return false;
}

199 200 201 202 203
bool BuildStep::runInGuiThread() const
{
    return false;
}

204 205 206 207 208 209
/*!
    This function needs to be reimplemented only for build steps that return
    \c false from runInGuiThread().

    \sa runInGuiThread()
*/
210 211 212 213 214
void BuildStep::cancel()
{
    // Do nothing
}

Daniel Teske's avatar
Daniel Teske committed
215 216 217 218 219 220 221 222 223 224 225 226 227
void BuildStep::setEnabled(bool b)
{
    if (m_enabled == b)
        return;
    m_enabled = b;
    emit enabledChanged();
}

bool BuildStep::enabled() const
{
    return m_enabled;
}

228 229
IBuildStepFactory::IBuildStepFactory(QObject *parent) :
    QObject(parent)
Tobias Hunger's avatar
Tobias Hunger committed
230
{ }
231 232 233 234 235 236 237 238 239 240 241

BuildStep *IBuildStepFactory::restore(BuildStepList *parent, const QVariantMap &map)
{
    const Core::Id id = idFromMap(map);
    BuildStep *bs = create(parent, id);
    if (bs->fromMap(map))
        return bs;
    delete bs;
    return nullptr;
}