aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6/ApiExtractor/clangparser/triplet.cpp
blob: ff93a61ca8e43b84b648bf3f74bb168983b591aa (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "triplet.h"

#include <QtCore/qdebug.h>
#include <QtCore/qlibraryinfo.h>
#include <QtCore/qoperatingsystemversion.h>

using namespace Qt::StringLiterals;

// from CMAKE_SYSTEM_PROCESSOR or target triplet
static Architecture parseArchitecture(QStringView a)
{
    if (a == "AMD64"_L1 || a == "IA64"_L1 // Windows
        || a == "x86_64"_L1) {
        return Architecture::X64;
    }
    if (a.compare("x86"_L1, Qt::CaseInsensitive) == 0
        || a.compare("i386"_L1, Qt::CaseInsensitive) == 0
        || a.compare("i486"_L1, Qt::CaseInsensitive) == 0
        || a.compare("i586"_L1, Qt::CaseInsensitive) == 0
        || a.compare("i686"_L1, Qt::CaseInsensitive) == 0) {
        return Architecture::X86;
    }
    if (a.startsWith("armv7"_L1, Qt::CaseInsensitive))
        return Architecture::Arm32;
    if (a.startsWith("arm"_L1, Qt::CaseInsensitive)
        || a.startsWith("aarch64"_L1, Qt::CaseInsensitive)) {
        return Architecture::Arm64;
    }
    return Architecture::Unknown;
}

static Architecture hostArchitecture()
{
    // src/corelib/global/archdetect.cpp, "Qt 6.9.2 (x86_64-little_endian-lp64..."
    std::string_view build = QLibraryInfo::build();
    auto startPos = build.find('(');
    auto dashPos = build.find('-');
    if (startPos != std::string_view::npos && dashPos != std::string_view::npos) {
        ++startPos;
        build = build.substr(startPos, dashPos - startPos);
        if (build == "x86_64")
            return Architecture::X64;
        if (build == "i386")
            return Architecture::X86;
        if (build == "arm64")
            return Architecture::Arm64;
        if (build == "arm")
            return Architecture::Arm32;
    }
    return Architecture::Unknown;
}

// from CMAKE_SYSTEM_NAME / legacy lower case name or target triplet
static Platform parsePlatform(QStringView name)
{
    if (name.compare("unix"_L1, Qt::CaseInsensitive) == 0)
        return Platform::Unix;
    if (name.compare("linux"_L1, Qt::CaseInsensitive) == 0)
        return Platform::Linux;
    if (name.compare("windows"_L1, Qt::CaseInsensitive) == 0)
        return Platform::Windows;
    if (name.compare("darwin"_L1, Qt::CaseInsensitive) == 0
        || name.compare("macosx"_L1, Qt::CaseInsensitive) == 0) {
        return Platform::macOS;
    }
    if (name.startsWith("android"_L1, Qt::CaseInsensitive))
        return Platform::Android; // "androideabi"
    if (name.compare("ios"_L1, Qt::CaseInsensitive) == 0)
        return Platform::iOS;
    return Platform::Unknown;
}

// CMAKE_CXX_COMPILER_ID or triplet name
static Compiler parseCompiler(QStringView name)
{
    if (name.compare("msvc"_L1, Qt::CaseInsensitive) == 0)
        return Compiler::Msvc;
    if (name.compare("g++"_L1, Qt::CaseInsensitive) == 0 || name.compare("gnu"_L1, Qt::CaseInsensitive) == 0)
        return Compiler::Gpp;
    if (name.compare("clang"_L1, Qt::CaseInsensitive) == 0)
        return Compiler::Clang;
    return Compiler::Unknown;
}

static Compiler hostCompiler()
{
#if defined (Q_CC_CLANG)
    return Compiler::Clang;
#elif defined (Q_CC_MSVC)
    return Compiler::Msvc;
#else
    return Compiler::Gpp;
#endif
}

static Platform hostPlatform()
{
#if defined (Q_OS_DARWIN)
    return Platform::macOS;
#elif defined (Q_OS_WIN)
    return Platform::Windows;
#elif defined (Q_OS_LINUX)
    return Platform::Linux;
#else
    return Platform::Unix;
#endif
}

static QVersionNumber hostPlatformVersion()
{
    auto ov = QOperatingSystemVersion::current();
    return ov.type() != QOperatingSystemVersionBase::Unknown ? ov.version() : QVersionNumber{};
}

Triplet::Triplet() = default;

bool Triplet::isValid() const
{
    return m_architecture != Architecture::Unknown
           && m_platform != Platform::Unknown;
}

QByteArray Triplet::architectureTripletValue() const
{
    switch (m_architecture) {
    case Architecture::X64:
        return "x86_64"_ba;
    case Architecture::X86:
        return "i586"_ba;
    case Architecture::Arm32:
        return "armv7a"_ba;
    case Architecture::Arm64:
        return m_platform == Platform::Android ? "aarch64"_ba : "arm64"_ba;
    case Architecture::Unknown:
        break;
    }
    return {};
}

void Triplet::setArchitecture(Architecture newArchitecture)
{
    m_architecture = newArchitecture;
}

bool Triplet::setArchitectureString(QStringView v)
{
    const auto arch = parseArchitecture(v);
    const bool ok = arch != Architecture::Unknown;
    if (ok)
        m_architecture = arch;
    return ok;
}

QByteArray Triplet::platformTripletValue() const
{
    switch (m_platform) {
    case Platform::Unix:
        return "unknown-unix"_ba;
    case Platform::Linux:
        return "unknown-linux"_ba;
    case Platform::Windows:
        return "pc-windows"_ba;
    case Platform::macOS:
        return "apple-macosx"_ba;
    case Platform::Android:
        return "unknown-linux-android"_ba;
        break;
    case Platform::iOS:
        return "apple-ios"_ba;
    case Platform::Unknown:
        break;
    }
    return {};
}

void Triplet::setPlatform(Platform newPlatform)
{
    m_platform = newPlatform;
}

QByteArray Triplet::compilerTripletValue() const
{
    switch (m_compiler) {
    case Compiler::Clang:
        return "clang"_ba;
    case Compiler::Msvc:
        return "msvc"_ba;
    case Compiler::Gpp:
        return "gnu"_ba;
        break;
    case Compiler::Unknown:
        break;
    }
    return {};
}

void Triplet::setCompiler(Compiler newCompiler)
{
    m_compiler = newCompiler;
}

bool Triplet::setCompilerString(QStringView v)
{
    const auto comp = parseCompiler(v);
    const bool ok = comp != Compiler::Unknown;
    if (ok)
        m_compiler = comp;
    return ok;
}

bool Triplet::setPlatformString(QStringView v)
{
    const auto p = parsePlatform(v);
    const bool ok = p != Platform::Unknown;
    if (ok)
        m_platform = p;
    return ok;
}

void Triplet::setPlatformVersion(const QVersionNumber &newPlatformVersion)
{
    m_platformVersion = newPlatformVersion;
}

bool Triplet::equals(const Triplet &rhs) const noexcept
{
    if (m_architecture != rhs.m_architecture
        || m_platform != rhs.m_platform
        || m_compiler != rhs.m_compiler) {
        return false;
    }
    const bool lhsHasVersion = hasPlatformVersion();
    const bool rhsHasVersion = rhs.hasPlatformVersion();;
    if (lhsHasVersion != rhsHasVersion)
        return false;
    return !lhsHasVersion || m_platformVersion == rhs.m_platformVersion;
}

QByteArray Triplet::toByteArray() const
{
    if (!isValid())
        return {};

    QByteArray result = architectureTripletValue() + '-' + platformTripletValue();

    if (m_platform != Platform::Unix && m_platform != Platform::Unknown
        && !m_platformVersion.isNull()) {
        result += m_platformVersion.toString().toUtf8();
    }

    switch (m_platform) {
    case Platform::Linux:
    case Platform::Windows:
        if (m_compiler != Compiler::Unknown)
            result += '-' + compilerTripletValue();
        break;
    default:
        break;
    }

    return result;
}

// Parsing triplets
static inline bool isVersionChar(QChar c)
{
    return c.isDigit() || c == u'.';
}

// "macosx15.0" -> "macosx"
QStringView stripTrailingVersion(QStringView s)
{
    while (!s.isEmpty() && isVersionChar(s.at(s.size() - 1)))
        s.chop(1);
    return s;
}

std::optional<Triplet> Triplet::fromString(QStringView name)
{
    auto values = name.split(u'-');
    if (values.size() < 2)
        return std::nullopt;

    const auto arch = parseArchitecture(values.constFirst());
    if (arch == Architecture::Unknown)
        return std::nullopt;;
    // Try a trailing compiler?
    const Compiler comp = parseCompiler(stripTrailingVersion(values.constLast()));
    if (comp != Compiler::Unknown)
        values.removeLast();

    const QStringView &fullPlatform = values.constLast();
    QStringView platformName = stripTrailingVersion(fullPlatform);
    const Platform platform = parsePlatform(platformName);
    if (platform == Platform::Unknown)
        return std::nullopt;

    Triplet result;
    result.setArchitecture(arch);
    result.setPlatform(platform);
    if (comp != Compiler::Unknown)
        result.setCompiler(comp);

    QVersionNumber platformVersion;
    if (platformName.size() < fullPlatform.size()) {
        const QVersionNumber platformVersion = QVersionNumber::fromString(fullPlatform.sliced(platformName.size()));
        if (!platformVersion.isNull())
            result.setPlatformVersion(platformVersion);
    }

    return result;
}

Triplet Triplet::fromHost()
{
    Triplet result;
    result.setArchitecture(hostArchitecture());
    result.setPlatform(hostPlatform());
    result.setCompiler(hostCompiler());
    const auto hv = hostPlatformVersion();
    if (!hv.isNull())
        result.setPlatformVersion(hv);
    return result;
}

QDebug operator<<(QDebug debug, const Triplet &t)
{
    QDebugStateSaver saver(debug);
    debug.noquote();
    debug.nospace();
    debug << "Triplet(";
    if (t.isValid()) {
        debug << '"' << t.toByteArray() << '"';
    } else {
        debug << "invalid";
    }
    debug << ')';
    return debug;
}