aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/corelib/tools/clangclinfo.cpp
blob: a9a1cb44927c2856dde4e807ce8abeac19abcdfc (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
/****************************************************************************
**
** Copyright (C) 2020 Ivan Komissarov (abbapoh@gmail.com)
** Contact: http://www.qt.io/licensing
**
** This file is part of Qbs.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "clangclinfo.h"

#include "hostosinfo.h"
#include "msvcinfo.h"
#include "stlutils.h"

namespace qbs {
namespace Internal {

static std::vector<MSVCInstallInfo> compatibleMsvcs(Logger &logger)
{
    auto msvcs = MSVCInstallInfo::installedMSVCs(logger);
    auto filter = [](const MSVCInstallInfo &info)
    {
        const auto versions = info.version.split(QLatin1Char('.'));
        if (versions.empty())
            return true;
        bool ok = false;
        const int major = versions.at(0).toInt(&ok);
        return !(ok && major >= 15); // support MSVC2017 and above
    };
    Internal::removeIf(msvcs, filter);
    for (const auto &msvc: msvcs) {
        auto vcvarsallPath = msvc.findVcvarsallBat();
        if (vcvarsallPath.isEmpty())
            continue;
    }
    return msvcs;
}

static QString findCompatibleVcsarsallBat(const std::vector<MSVCInstallInfo> &msvcs)
{
    for (const auto &msvc: msvcs) {
        const auto vcvarsallPath = msvc.findVcvarsallBat();
        if (!vcvarsallPath.isEmpty())
            return vcvarsallPath;
    }
    return {};
}

static QString wow6432Key()
{
#ifdef Q_OS_WIN64
    return QStringLiteral("\\Wow6432Node");
#else
    return {};
#endif
}

static QString getToolchainInstallPath(const QFileInfo &compiler)
{
    return compiler.path(); // 1 level up
}

QVariantMap ClangClInfo::toVariantMap() const
{
    return {
        {QStringLiteral("toolchainInstallPath"), toolchainInstallPath},
        {QStringLiteral("vcvarsallPath"), vcvarsallPath},
    };
}

ClangClInfo ClangClInfo::fromCompilerFilePath(const QString &path, Logger &logger)
{
    const auto compilerName = QStringLiteral("clang-cl");
    const auto vcvarsallPath = findCompatibleVcsarsallBat(compatibleMsvcs(logger));
    if (vcvarsallPath.isEmpty()) {
        logger.qbsWarning()
                << Tr::tr("%1 requires installed Visual Studio 2017 or newer, but none was found.")
                   .arg(compilerName);
        return {};
    }

    return {getToolchainInstallPath(QFileInfo(path)), vcvarsallPath};
}

std::vector<ClangClInfo> ClangClInfo::installedCompilers(
        const std::vector<QString> &extraPaths, Logger &logger)
{
    std::vector<QString> compilerPaths;
    compilerPaths.reserve(extraPaths.size());
    std::copy_if(extraPaths.begin(), extraPaths.end(),
                 std::back_inserter(compilerPaths),
                 [](const QString &path){ return !path.isEmpty(); });
    const auto compilerName = HostOsInfo::appendExecutableSuffix(QStringLiteral("clang-cl"));

    const QSettings registry(
            QStringLiteral("HKEY_LOCAL_MACHINE\\SOFTWARE%1\\LLVM\\LLVM").arg(wow6432Key()),
            QSettings::NativeFormat);
    const auto key = QStringLiteral(".");
    if (registry.contains(key)) {
        const auto compilerPath = QDir::fromNativeSeparators(registry.value(key).toString())
                + QStringLiteral("/bin/") + compilerName;
        if (QFileInfo::exists(compilerPath))
            compilerPaths.push_back(compilerPath);
    }

    // this branch can be useful in case user had two LLVM installations (e.g. 32bit & 64bit)
    // but uninstalled one - in that case, registry will be empty
    static const char * const envVarCandidates[] = {"ProgramFiles", "ProgramFiles(x86)"};
    for (const auto &envVar : envVarCandidates) {
        const auto value
                = QDir::fromNativeSeparators(QString::fromLocal8Bit(qgetenv(envVar)));
        const auto compilerPath = value + QStringLiteral("/LLVM/bin/") + compilerName;
        if (QFileInfo::exists(compilerPath) && !contains(compilerPaths, compilerPath))
            compilerPaths.push_back(compilerPath);
    }

    const auto msvcs = compatibleMsvcs(logger);
    const auto vcvarsallPath = findCompatibleVcsarsallBat(msvcs);
    if (vcvarsallPath.isEmpty()) {
        logger.qbsWarning()
                << Tr::tr("%1 requires installed Visual Studio 2017 or newer, but none was found.")
                        .arg(compilerName);
        return {};
    }

    std::vector<ClangClInfo> result;
    result.reserve(compilerPaths.size() + msvcs.size());
    transform(compilerPaths, result, [&vcvarsallPath](const auto &path) {
        return ClangClInfo{getToolchainInstallPath(QFileInfo(path)), vcvarsallPath}; });

    // If we didn't find custom LLVM installation, try to find if it's installed with Visual Studio
    for (const auto &msvc : msvcs) {
        const auto compilerPath = QStringLiteral("%1/VC/Tools/Llvm/bin/%2")
                .arg(msvc.installDir, compilerName);
        if (QFileInfo::exists(compilerPath)) {
            const auto vcvarsallPath = msvc.findVcvarsallBat();
            if (vcvarsallPath.isEmpty()) {
                logger.qbsWarning()
                        << Tr::tr("Found LLVM in %1, but vcvarsall.bat is missing.")
                                .arg(msvc.installDir);
            }

            result.push_back({getToolchainInstallPath(QFileInfo(compilerPath)), vcvarsallPath});
        }
    }

    return result;
}

} // namespace Internal
} // namespace qbs