aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/boot2qt/qdbplugin.cpp
blob: 64be4075ee83c636d7749ebd7b8ae2f9e7857459 (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
// 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 "qdbplugin.h"

#include "device-detection/devicedetector.h"
#include "qdbconstants.h"
#include "qdbdevice.h"
#include "qdbstopapplicationstep.h"
#include "qdbmakedefaultappstep.h"
#include "qdbdevicedebugsupport.h"
#include "qdbqtversion.h"
#include "qdbrunconfiguration.h"
#include "qdbutils.h"
#include "qdbtr.h"

#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/icore.h>

#include <projectexplorer/deployconfiguration.h>
#include <projectexplorer/devicesupport/devicemanager.h>
#include <projectexplorer/kitaspects.h>
#include <projectexplorer/kitmanager.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/target.h>

#include <qtsupport/qtversionfactory.h>

#include <remotelinux/remotelinux_constants.h>

#include <utils/fileutils.h>
#include <utils/hostosinfo.h>
#include <utils/process.h>

#include <QAction>

using namespace ProjectExplorer;
using namespace Utils;

namespace Qdb::Internal {

static FilePath flashWizardFilePath()
{
    return findTool(QdbTool::FlashingWizard);
}

static void startFlashingWizard()
{
    const FilePath filePath = flashWizardFilePath();
    if (HostOsInfo::isWindowsHost()) {
        if (Process::startDetached({"explorer.exe", {filePath.toUserOutput()}}))
            return;
    } else if (Process::startDetached({filePath, {}})) {
        return;
    }
    const QString message = Tr::tr("Flash wizard \"%1\" failed to start.");
    showMessage(message.arg(filePath.toUserOutput()), true);
}

static bool isFlashActionDisabled()
{
    QtcSettings * const settings = Core::ICore::settings();
    settings->beginGroup(settingsGroupKey());
    bool disabled = settings->value("flashActionDisabled", false).toBool();
    settings->endGroup();
    return disabled;
}

void registerFlashAction(QObject *parentForAction)
{
    if (isFlashActionDisabled())
        return;
    const FilePath fileName = flashWizardFilePath();
    if (!fileName.exists()) {
        const QString message = Tr::tr("Flash wizard executable \"%1\" not found.");
        showMessage(message.arg(fileName.toUserOutput()));
        return;
    }

    const char flashActionId[] = "Qdb.FlashAction";
    if (Core::ActionManager::command(flashActionId))
        return; // The action has already been registered.

    Core::ActionContainer *toolsContainer =
        Core::ActionManager::actionContainer(Core::Constants::M_TOOLS);
    toolsContainer->insertGroup(Core::Constants::G_TOOLS_DEBUG, flashActionId);

    Core::Context globalContext(Core::Constants::C_GLOBAL);

    QAction *flashAction = new QAction(Tr::tr("Flash Boot to Qt Device"), parentForAction);
    Core::Command *flashCommand = Core::ActionManager::registerAction(flashAction,
                                                                      flashActionId,
                                                                      globalContext);
    QObject::connect(flashAction, &QAction::triggered, startFlashingWizard);
    toolsContainer->addAction(flashCommand, flashActionId);
}

class QdbDeployStepFactory : public BuildStepFactory
{
public:
    explicit QdbDeployStepFactory(Id existingStepId)
    {
        cloneStepCreator(existingStepId);
        setSupportedConfiguration(Constants::QdbDeployConfigurationId);
        setSupportedStepList(ProjectExplorer::Constants::BUILDSTEPS_DEPLOY);
    }
};

class QdbDeployConfigurationFactory final : public DeployConfigurationFactory
{
public:
    QdbDeployConfigurationFactory()
    {
        setConfigBaseId(Constants::QdbDeployConfigurationId);
        addSupportedTargetDeviceType(Constants::QdbLinuxOsType);
        setDefaultDisplayName(Tr::tr("Deploy to Boot2Qt target"));
        setUseDeploymentDataView();

        addInitialStep(RemoteLinux::Constants::MakeInstallStepId, [](Target *target) {
            const Project * const prj = target->project();
            return prj->deploymentKnowledge() == DeploymentKnowledge::Bad
                   && prj->hasMakeInstallEquivalent();
        });
        addInitialStep(Qdb::Constants::QdbStopApplicationStepId);
        addInitialStep(RemoteLinux::Constants::RsyncDeployStepId, [](Target *target) {
            auto device = DeviceKitAspect::device(target->kit());
            auto buildDevice = BuildDeviceKitAspect::device(target->kit());
            if (buildDevice && buildDevice->rootPath().needsDevice())
                return false;
            return !device || (device
                   && device->extraData(ProjectExplorer::Constants::SUPPORTS_RSYNC).toBool());
        });
        addInitialStep(RemoteLinux::Constants::DirectUploadStepId, [](Target *target) {
            auto device = DeviceKitAspect::device(target->kit());
            auto buildDevice = BuildDeviceKitAspect::device(target->kit());
            if (buildDevice && buildDevice->rootPath().needsDevice())
                return false;
            return device && !device->extraData(ProjectExplorer::Constants::SUPPORTS_RSYNC).toBool();
        });
        // This step is for:
        // a) A remote build device, as they do not support real rsync yet.
        // b) If there is no target device setup yet.
        addInitialStep(RemoteLinux::Constants::DirectUploadStepId, [](Target *target) {
            auto device = DeviceKitAspect::device(target->kit());
            auto buildDevice = BuildDeviceKitAspect::device(target->kit());
            if (buildDevice && buildDevice->rootPath().needsDevice())
                return true;
            return false;
        });
    }
};

class QdbPluginPrivate : public QObject
{
public:
    void setupDeviceDetection();

    QdbLinuxDeviceFactory m_qdbDeviceFactory;
    QdbQtVersionFactory m_qtVersionFactory;
    QdbDeployConfigurationFactory m_deployConfigFactory;
    QdbRunConfigurationFactory m_runConfigFactory;
    QdbStopApplicationStepFactory m_stopApplicationStepFactory;
    QdbMakeDefaultAppStepFactory m_makeDefaultAppStepFactory;

    QdbDeployStepFactory m_directUploadStepFactory{RemoteLinux::Constants::DirectUploadStepId};
    QdbDeployStepFactory m_rsyncDeployStepFactory{RemoteLinux::Constants::RsyncDeployStepId};
    QdbDeployStepFactory m_makeInstallStepFactory{RemoteLinux::Constants::MakeInstallStepId};

    const QList<Id> supportedRunConfigs {
        m_runConfigFactory.runConfigurationId(),
        "QmlProjectManager.QmlRunConfiguration"
    };

    QdbRunWorkerFactory runWorkerFactory{supportedRunConfigs};
    QdbDebugWorkerFactory debugWorkerFactory{supportedRunConfigs};
    QdbQmlToolingWorkerFactory qmlToolingWorkerFactory{supportedRunConfigs};
    QdbPerfProfilerWorkerFactory perfRecorderWorkerFactory;

    DeviceDetector m_deviceDetector;
};

QdbPlugin::~QdbPlugin()
{
    delete d;
}

void QdbPlugin::initialize()
{
    d = new QdbPluginPrivate;

    registerFlashAction(this);
}

void QdbPlugin::extensionsInitialized()
{
    DeviceManager * const dm = DeviceManager::instance();
    if (dm->isLoaded()) {
        d->setupDeviceDetection();
    } else {
        connect(dm, &DeviceManager::devicesLoaded,
                d, &QdbPluginPrivate::setupDeviceDetection);
    }
}

ExtensionSystem::IPlugin::ShutdownFlag QdbPlugin::aboutToShutdown()
{
    d->m_deviceDetector.stop();

    return SynchronousShutdown;
}

void QdbPluginPrivate::setupDeviceDetection()
{
    m_deviceDetector.start();
}

} // Qdb::Internal