aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/osspecificaspects.cpp
blob: 2d04e195f1aed89dc724108a0211f0ff3d0cb092 (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
// 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 "osspecificaspects.h"

#include <algorithm>

namespace Utils {

QString osTypeToString(OsType osType)
{
    switch (osType) {
    case OsTypeWindows:
        return "Windows";
    case OsTypeLinux:
        return "Linux";
    case OsTypeMac:
        return "Mac";
    case OsTypeOtherUnix:
        return "Other Unix";
    case OsTypeOther:
    default:
        return "Other";
    }
}

Result<OsType> osTypeFromString(const QString &string)
{
    if (string.compare("windows", Qt::CaseInsensitive) == 0)
        return OsTypeWindows;

    if (string.compare("linux", Qt::CaseInsensitive) == 0)
        return OsTypeLinux;

    if (string.compare("mac", Qt::CaseInsensitive) == 0
        || string.compare("darwin", Qt::CaseInsensitive) == 0
        || string.compare("macos", Qt::CaseInsensitive) == 0)
        return OsTypeMac;

    if (string.compare("other unix", Qt::CaseInsensitive) == 0
        || string.compare("qnx", Qt::CaseInsensitive) == 0)
        return OsTypeOtherUnix;

    return ResultError(QString::fromLatin1("Unknown os type: %1").arg(string));
}

Result<OsArch> osArchFromString(const QString &architecture)
{
    if (architecture == QLatin1String("x86_64") || architecture == QLatin1String("amd64"))
        return OsArchAMD64;
    if (architecture == QLatin1String("x86"))
        return OsArchX86;
    if (architecture == QLatin1String("ia64"))
        return OsArchItanium;
    if (architecture == QLatin1String("arm") || architecture.startsWith(QLatin1String("armv")))
        return OsArchArm;
    if (architecture == QLatin1String("arm64") || architecture == QLatin1String("aarch64"))
        return OsArchArm64;

    return Utils::ResultError(QString::fromLatin1("Unknown architecture: %1").arg(architecture));
}

namespace OsSpecificAspects {

QString withExecutableSuffix(OsType osType, const QString &executable)
{
    QString finalName = executable;
    if (osType == OsTypeWindows && !finalName.endsWith(QTC_WIN_EXE_SUFFIX))
        finalName += QLatin1String(QTC_WIN_EXE_SUFFIX);
    return finalName;
}

QString pathWithNativeSeparators(OsType osType, const QString &pathName)
{
    if (osType == OsTypeWindows) {
        const int pos = pathName.indexOf('/');
        if (pos >= 0) {
            QString n = pathName;
            std::replace(std::begin(n) + pos, std::end(n), '/', '\\');
            return n;
        }
    } else {
        const int pos = pathName.indexOf('\\');
        if (pos >= 0) {
            QString n = pathName;
            std::replace(std::begin(n) + pos, std::end(n), '\\', '/');
            return n;
        }
    }
    return pathName;
}

} // namespace OsSpecificAspects
} // namespace Utils