aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/remotelinux/makeinstallstep.cpp
blob: 8b678bea9619ebaec9c3d3612f35fa15aa11e007 (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
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "makeinstallstep.h"

#include "remotelinux_constants.h"
#include "remotelinuxtr.h"

#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/buildsteplist.h>
#include <projectexplorer/buildsystem.h>
#include <projectexplorer/deployconfiguration.h>
#include <projectexplorer/devicesupport/idevice.h>
#include <projectexplorer/kitaspects.h>
#include <projectexplorer/makestep.h>
#include <projectexplorer/processparameters.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/runconfigurationaspects.h>
#include <projectexplorer/target.h>
#include <projectexplorer/task.h>

#include <utils/algorithm.h>
#include <utils/fileutils.h>
#include <utils/qtcprocess.h>
#include <utils/qtcassert.h>

#include <QSet>
#include <QTemporaryDir>

using namespace ProjectExplorer;
using namespace Utils;

namespace RemoteLinux::Internal {

class MakeInstallStep : public MakeStep
{
public:
    MakeInstallStep(BuildStepList *parent, Id id);

private:
    void fromMap(const Store &map) override;
    QWidget *createConfigWidget() override;
    bool init() override;
    Tasking::GroupItem runRecipe() final;
    bool isJobCountSupported() const override { return false; }

    void updateCommandFromAspect();
    void updateArgsFromAspect();
    void updateFullCommandLine();
    void updateFromCustomCommandLineAspect();

    ExecutableAspect m_makeBinary{this};
    FilePathAspect m_installRoot{this};
    BoolAspect m_cleanInstallRoot{this};
    StringAspect m_fullCommand{this};
    StringAspect m_customCommand{this};

    DeploymentData m_deploymentData;
    bool m_noInstallTarget = false;
    bool m_isCmakeProject = false;
};

MakeInstallStep::MakeInstallStep(BuildStepList *parent, Id id) : MakeStep(parent, id)
{
    m_makeBinary.setVisible(false);
    m_buildTargetsAspect.setVisible(false);
    m_userArgumentsAspect.setVisible(false);
    m_overrideMakeflagsAspect.setVisible(false);
    m_nonOverrideWarning.setVisible(false);
    m_jobCountAspect.setVisible(false);
    m_disabledForSubdirsAspect.setVisible(false);

    // FIXME: Hack, Part#1: If the build device is not local, start with a temp dir
    // inside the build dir. On Docker that's typically shared with the host.
    const IDevice::ConstPtr device = BuildDeviceKitAspect::device(target()->kit());
    const bool hack = device && device->type() != ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE;
    FilePath rootPath;
    if (hack) {
        rootPath = buildDirectory().pathAppended(".tmp-root");
    } else {
        QTemporaryDir tmpDir;
        rootPath = FilePath::fromString(tmpDir.path());
    }

    m_makeBinary.setDeviceSelector(parent->target(), ExecutableAspect::BuildDevice);
    m_makeBinary.setSettingsKey("RemoteLinux.MakeInstall.Make");
    m_makeBinary.setReadOnly(false);
    m_makeBinary.setLabelText(Tr::tr("Command:"));
    connect(&m_makeBinary, &BaseAspect::changed,
            this, &MakeInstallStep::updateCommandFromAspect);

    m_installRoot.setSettingsKey("RemoteLinux.MakeInstall.InstallRoot");
    m_installRoot.setExpectedKind(PathChooser::Directory);
    m_installRoot.setLabelText(Tr::tr("Install root:"));
    m_installRoot.setValue(rootPath);
    connect(&m_installRoot, &BaseAspect::changed,
            this, &MakeInstallStep::updateArgsFromAspect);

    m_cleanInstallRoot.setSettingsKey("RemoteLinux.MakeInstall.CleanInstallRoot");
    m_cleanInstallRoot.setLabelText(Tr::tr("Clean install root first:"));
    m_cleanInstallRoot.setLabelPlacement(BoolAspect::LabelPlacement::InExtraLabel);
    m_cleanInstallRoot.setDefaultValue(true);

    m_fullCommand.setDisplayStyle(StringAspect::LabelDisplay);
    m_fullCommand.setLabelText(Tr::tr("Full command line:"));

    m_customCommand.setSettingsKey("RemoteLinux.MakeInstall.CustomCommandLine");
    m_customCommand.setDisplayStyle(StringAspect::LineEditDisplay);
    m_customCommand.setLabelText(Tr::tr("Custom command line:"));
    m_customCommand.makeCheckable(CheckBoxPlacement::Top,
                                  Tr::tr("Use custom command line instead:"),
                                  "RemoteLinux.MakeInstall.EnableCustomCommandLine");
    const auto updateCommand = [this] {
        updateCommandFromAspect();
        updateArgsFromAspect();
        updateFromCustomCommandLineAspect();
    };

    connect(&m_customCommand, &StringAspect::checkedChanged, this, updateCommand);
    connect(&m_customCommand, &StringAspect::changed,
            this, &MakeInstallStep::updateFromCustomCommandLineAspect);

    connect(target(), &Target::buildSystemUpdated, this, updateCommand);

    const MakeInstallCommand cmd = buildSystem()->makeInstallCommand(rootPath);
    QTC_ASSERT(!cmd.command.isEmpty(), return);
    m_makeBinary.setExecutable(cmd.command.executable());

    connect(this, &BuildStep::addOutput, this, [this](const QString &string, OutputFormat format) {
        // When using Makefiles: "No rule to make target 'install'"
        // When using ninja: "ninja: error: unknown target 'install'"
        if (format == OutputFormat::Stderr && string.contains("target 'install'"))
            m_noInstallTarget = true;
    });
}

QWidget *MakeInstallStep::createConfigWidget()
{
    // Note: this intentionally skips the MakeStep::createConfigWidget() level.
    return BuildStep::createConfigWidget();
}

bool MakeInstallStep::init()
{
    if (!MakeStep::init())
        return false;

    const FilePath rootDir = makeCommand().withNewPath(m_installRoot().path()); // FIXME: Needed?
    if (rootDir.isEmpty()) {
        emit addTask(BuildSystemTask(Task::Error, Tr::tr("You must provide an install root.")));
        return false;
    }
    if (m_cleanInstallRoot() && !rootDir.removeRecursively()) {
        emit addTask(BuildSystemTask(Task::Error,
                                        Tr::tr("The install root \"%1\" could not be cleaned.")
                                            .arg(rootDir.displayName())));
        return false;
    }
    if (!rootDir.exists() && !rootDir.createDir()) {
        emit addTask(BuildSystemTask(Task::Error,
                                        Tr::tr("The install root \"%1\" could not be created.")
                                            .arg(rootDir.displayName())));
        return false;
    }
    if (this == deployConfiguration()->stepList()->steps().last()) {
        emit addTask(BuildSystemTask(Task::Warning,
                                        Tr::tr("The \"make install\" step should probably not be "
                                            "last in the list of deploy steps. "
                                            "Consider moving it up.")));
    }

    const MakeInstallCommand cmd = buildSystem()->makeInstallCommand(rootDir);
    if (cmd.environment.hasChanges()) {
        Environment env = processParameters()->environment();
        cmd.environment.forEachEntry([&](const QString &key, const QString &value, bool enabled) {
            if (enabled)
                env.set(key, cmd.environment.expandVariables(value));
        });
        processParameters()->setEnvironment(env);
    }
    m_noInstallTarget = false;

    const auto buildStep = buildConfiguration()->buildSteps()->firstOfType<AbstractProcessStep>();
    m_isCmakeProject = buildStep
            && buildStep->processParameters()->command().executable().toString()
            .contains("cmake");

    return true;
}

Tasking::GroupItem MakeInstallStep::runRecipe()
{
    using namespace Tasking;

    const auto onDone = [this](DoneWith result) {
        if (result != DoneWith::Success) {
            if (m_noInstallTarget && m_isCmakeProject) {
                emit addTask(DeploymentTask(Task::Warning, Tr::tr("You need to add an install "
                                                                  "statement to your CMakeLists.txt file for deployment to work.")));
            }
            return;
        }
        const FilePath rootDir = makeCommand().withNewPath(m_installRoot().path()); // FIXME: Needed?

        m_deploymentData = DeploymentData();
        m_deploymentData.setLocalInstallRoot(rootDir);

        const int startPos = rootDir.path().length();

        const auto appFileNames = transform<QSet<QString>>(buildSystem()->applicationTargets(),
            [](const BuildTargetInfo &appTarget) { return appTarget.targetFilePath.fileName(); });

        auto handleFile = [this, &appFileNames, startPos](const FilePath &filePath) {
            const DeployableFile::Type type = appFileNames.contains(filePath.fileName())
                ? DeployableFile::TypeExecutable : DeployableFile::TypeNormal;
            const QString targetDir = filePath.parentDir().path().mid(startPos);
            m_deploymentData.addFile(filePath, targetDir, type);
            return IterationPolicy::Continue;
        };
        rootDir.iterateDirectory(
            handleFile, {{}, QDir::Files | QDir::Hidden, QDirIterator::Subdirectories});

        buildSystem()->setDeploymentData(m_deploymentData);
    };

    return Group { onGroupDone(onDone), defaultProcessTask() };
}

void MakeInstallStep::updateCommandFromAspect()
{
    if (m_customCommand.isChecked())
        return;
    setMakeCommand(m_makeBinary());
    updateFullCommandLine();
}

void MakeInstallStep::updateArgsFromAspect()
{
    if (m_customCommand.isChecked())
        return;
    const CommandLine cmd = buildSystem()->makeInstallCommand(m_installRoot()).command;
    setUserArguments(cmd.arguments());
    updateFullCommandLine();
}

void MakeInstallStep::updateFullCommandLine()
{
    const CommandLine cmd{makeExecutable(), userArguments(), CommandLine::Raw};
    m_fullCommand.setValue(cmd.toUserOutput());
}

void MakeInstallStep::updateFromCustomCommandLineAspect()
{
    if (m_customCommand.isChecked())
        return;
    const QStringList tokens = ProcessArgs::splitArgs(m_customCommand(), HostOsInfo::hostOs());
    setMakeCommand(tokens.isEmpty() ? FilePath() : FilePath::fromString(tokens.first()));
    setUserArguments(ProcessArgs::joinArgs(tokens.mid(1)));
}

void MakeInstallStep::fromMap(const Store &map)
{
    MakeStep::fromMap(map);
    if (hasError())
        return;
    updateCommandFromAspect();
    updateArgsFromAspect();
    updateFromCustomCommandLineAspect();
}

// Factory

MakeInstallStepFactory::MakeInstallStepFactory()
{
    registerStep<MakeInstallStep>(Constants::MakeInstallStepId);
    setDisplayName(Tr::tr("Install into temporary host directory"));
}

} // RemoteLinux::Internal