aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/baremetal/debugservers/uvsc/uvproject.cpp
blob: d393641251a7e63dfdff74cb0381b806e945aee2 (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
// Copyright (C) 2020 Denis Shienkov <denis.shienkov@gmail.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "uvproject.h"
#include "uvscserverprovider.h"

#include <cppeditor/cppmodelmanager.h>

#include <debugger/debuggerkitinformation.h>
#include <debugger/debuggerruncontrol.h>

#include <projectexplorer/runcontrol.h>
#include <projectexplorer/session.h>
#include <projectexplorer/target.h>

#include <QFileInfo>
#include <QDir>

using namespace CppEditor;
using namespace Debugger;
using namespace ProjectExplorer;
using namespace Utils;

namespace BareMetal {
namespace Internal {
namespace Uv {

const char kProjectSchema[] = "2.1";

static QString buildToolsetNumber(int number)
{
    return QStringLiteral("0x%1").arg(QString::number(number, 16));
}

static QString buildPackageId(const DeviceSelection::Package &package)
{
    return QStringLiteral("%1.%2.%3").arg(package.vendorName, package.name, package.version);
}

static QString buildCpu(const DeviceSelection &device)
{
    QString cpu;
    for (const DeviceSelection::Memory &memory : device.memories) {
        const QString id = (memory.id == "IRAM1")
                ? "IRAM" : ((memory.id == "IROM1") ? "IROM" : memory.id);
        cpu += id + '(' +  memory.start + ',' +  memory.size + ") ";
    }
    cpu += "CPUTYPE(\"" + device.cpu.core + "\")";
    return cpu;
}

static QString buildCpuDllName(const DriverSelection &driver)
{
    if (driver.cpuDllIndex < 0 || driver.cpuDllIndex >= driver.cpuDlls.count())
        return {};
    return driver.cpuDlls.at(driver.cpuDllIndex);
}

static QString buildCpuDllParameters(bool isSimulator)
{
    QString params = " -MPU";
    if (isSimulator)
        params.prepend(" -REMAP");
    return params;
}

static void extractAllFiles(const DebuggerRunTool *runTool, QStringList &includes,
                            FilePaths &headers, FilePaths &sources, FilePaths &assemblers)
{
    const auto project = runTool->runControl()->project();
    const CppEditor::ProjectInfo::ConstPtr info = CppModelManager::instance()->projectInfo(project);
    if (!info)
        return;
    const QVector<ProjectPart::ConstPtr> parts = info->projectParts();
    for (const ProjectPart::ConstPtr &part : parts) {
        for (const ProjectFile &file : qAsConst(part->files)) {
            if (!file.active)
                continue;
            const auto path = FilePath::fromString(file.path);
            if (file.isHeader() && !headers.contains(path))
                headers.push_back(path);
            else if (file.isSource() && !sources.contains(path))
                sources.push_back(path);
            else if (file.path.endsWith(".s") && !assemblers.contains(path))
                assemblers.push_back(path);
        }
        for (const HeaderPath &include : qAsConst(part->headerPaths)) {
            if (!includes.contains(include.path))
                includes.push_back(include.path);
        }
    }
}

QString buildPackageId(const DeviceSelection &selection)
{
    return buildPackageId(selection.package);
}

// Project

Project::Project(const UvscServerProvider *provider, DebuggerRunTool *runTool)
{
    appendProperty("SchemaVersion", kProjectSchema);
    appendProperty("Header", "### uVision Project, generated by QtCreator");

    const auto targets = appendChild<Gen::Xml::PropertyGroup>("Targets");
    // Fill 'Target' group.
    m_target = targets->appendPropertyGroup("Target");
    m_target->appendProperty("TargetName", "Template");
    const int toolsetNo = provider->toolsetNumber();
    const QString toolsetNumber = buildToolsetNumber(toolsetNo);
    m_target->appendProperty("ToolsetNumber", toolsetNumber);
    // Fill 'TargetOption' group.
    const auto targetOption = m_target->appendPropertyGroup("TargetOption");

    // Fill 'TargetCommonOption' group.
    const auto targetCommonOption = targetOption->appendPropertyGroup("TargetCommonOption");
    const DeviceSelection device = provider->deviceSelection();
    targetCommonOption->appendProperty("Device", device.name);
    targetCommonOption->appendProperty("Vendor", device.vendorName);
    const QString packageId = buildPackageId(device.package);
    targetCommonOption->appendProperty("PackID", packageId);
    targetCommonOption->appendProperty("PackURL", device.package.url);
    const QString cpu = buildCpu(device);
    targetCommonOption->appendProperty("Cpu", cpu);

    // Fill 'DllOption' group (required for debugging).
    const auto dllOption = targetOption->appendPropertyGroup("DllOption");
    const DriverSelection driver = provider->driverSelection();

    const QString cpuDllName = buildCpuDllName(driver);
    const QString simulatorCpuDllParameters = buildCpuDllParameters(true);
    const QString targetCpuDllParameters = buildCpuDllParameters(false);
    dllOption->appendProperty("SimDllName", cpuDllName);
    dllOption->appendProperty("SimDllArguments", simulatorCpuDllParameters);
    dllOption->appendProperty("TargetDllName", cpuDllName);
    dllOption->appendProperty("TargetDllArguments", targetCpuDllParameters);

    QStringList includes;
    FilePaths headers;
    FilePaths sources;
    FilePaths assemblers;
    extractAllFiles(runTool, includes, headers, sources, assemblers);

    if (toolsetNo == UvscServerProvider::ArmAdsToolsetNumber) {
        // Fill 'TargetArmAds' group (required for debugging).
        const auto targetArmAds = targetOption->appendPropertyGroup("TargetArmAds");
        // Fill 'ArmAdsMisc' group.
        const auto armAdsMisc = targetArmAds->appendPropertyGroup("ArmAdsMisc");
        // Fill 'OnChipMemories' group.
        const auto onChipMemories = armAdsMisc->appendPropertyGroup("OnChipMemories");

        static const struct OnChipEntry {
            QString id;
            QByteArray name;
            int type = -1;
        } entries[] = {
            {{"IROM1"}, "OCR_RVCT4",  1},
            {{"IROM2"}, "OCR_RVCT5",  1},
            {{"IRAM1"}, "OCR_RVCT9",  0},
            {{"IRAM2"}, "OCR_RVCT10", 0},
        };

        const auto entryBegin = std::cbegin(entries);
        const auto entryEnd = std::cend(entries);
        for (const DeviceSelection::Memory &memory : device.memories) {
            const auto entryIt = std::find_if(entryBegin, entryEnd,
                                              [memory](const OnChipEntry &entry) {
                return memory.id == entry.id;
            });
            if (entryIt == entryEnd)
                continue;

            // Fill 'OCR_RVCTn' group.
            const auto ocrRvct = onChipMemories->appendPropertyGroup(entryIt->name);
            ocrRvct->appendProperty("Type", entryIt->type);
            ocrRvct->appendProperty("StartAddress", memory.start);
            ocrRvct->appendProperty("Size", memory.size);
        }

        // Fill 'Cads' group.
        const auto cAds = targetArmAds->appendPropertyGroup("Cads");
        const auto variousControls = cAds->appendPropertyGroup("VariousControls");
        variousControls->appendMultiLineProperty("IncludePath", includes, ';');
    }

    fillAllFiles(headers, sources, assemblers);
}

void Project::fillAllFiles(const FilePaths &headers, const FilePaths &sources,
                           const FilePaths &assemblers)
{
    // Add headers, seources, and assembler files.
    const auto groups = m_target->appendPropertyGroup("Groups");
    const auto group = groups->appendPropertyGroup("Group");
    group->appendProperty("GroupName", "All Files");
    const auto filesGroup = group->appendPropertyGroup("Files");

    enum FileType { SourceFile = 1, AssemblerFile = 2, HeaderFile = 5 };
    auto fillFile = [filesGroup](const FilePath &filePath, FileType fileType) {
        const auto fileGroup = filesGroup->appendPropertyGroup("File");
        fileGroup->appendProperty("FileName", filePath.fileName());
        fileGroup->appendProperty("FileType", fileType);
        fileGroup->appendProperty("FilePath", filePath.toString());
    };

    // Add headers.
    for (const auto &header : headers)
        fillFile(header, FileType::HeaderFile);
    // Add sources.
    for (const auto &source : sources)
        fillFile(source, FileType::SourceFile);
    // Add assemblers.
    for (const auto &assembler : assemblers)
        fillFile(assembler, FileType::AssemblerFile);
}

// ProjectOptions

ProjectOptions::ProjectOptions(const UvscServerProvider *provider)
{
    appendProperty("SchemaVersion", kProjectSchema);
    appendProperty("Header", "### uVision Project, generated by QtCreator");

    // Fill 'Target' group.
    const auto target = appendChild<Gen::Xml::PropertyGroup>("Target");
    target->appendProperty("TargetName", "Template");
    const int toolsetNo = provider->toolsetNumber();
    const QString toolsetNumber = buildToolsetNumber(toolsetNo);
    target->appendProperty("ToolsetNumber", toolsetNumber);

    m_targetOption = target->appendPropertyGroup("TargetOption");

    // Fill 'DebugOpt' group (required for dedugging).
    m_debugOpt = m_targetOption->appendPropertyGroup("DebugOpt");
    const bool useSimulator = provider->isSimulator();
    m_debugOpt->appendProperty("uSim", int(useSimulator));
    m_debugOpt->appendProperty("uTrg", int(!useSimulator));
}

} // namespace Uv
} // namespace Internal
} // namespace BareMetal