aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/webassembly/webassemblyemsdk.cpp
blob: 42f8738a8e73905322308358259ffc1de272b612 (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
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "webassemblyemsdk.h"

#include "webassemblyconstants.h"

#include <coreplugin/icore.h>
#include <utils/environment.h>
#include <utils/process.h>
#include <utils/hostosinfo.h>

#include <QCache>
#include <QSettings>

using namespace Utils;

namespace WebAssembly::Internal::WebAssemblyEmSdk {

using EmSdkEnvCache = QCache<QString, QString>;
Q_GLOBAL_STATIC_WITH_ARGS(EmSdkEnvCache, emSdkEnvCache, (10))
using EmSdkVersionCache = QCache<QString, QVersionNumber>;
Q_GLOBAL_STATIC_WITH_ARGS(EmSdkVersionCache, emSdkVersionCache, (10))

static QString emSdkEnvOutput(const FilePath &sdkRoot)
{
    const QString cacheKey = sdkRoot.toString();
    const bool isWindows = sdkRoot.osType() == OsTypeWindows;
    if (!emSdkEnvCache()->contains(cacheKey)) {
        const FilePath scriptFile = sdkRoot.pathAppended(QLatin1String("emsdk_env") +
                                        (isWindows ? ".bat" : ".sh"));
        Process emSdkEnv;
        if (isWindows) {
            emSdkEnv.setCommand(CommandLine(scriptFile));
        } else {
            // File needs to be source'd, not executed.
            emSdkEnv.setCommand({sdkRoot.withNewPath("bash"), {"-c", ". " + scriptFile.path()}});
        }
        emSdkEnv.runBlocking();
        const QString output = emSdkEnv.allOutput();
        emSdkEnvCache()->insert(cacheKey, new QString(output));
    }
    return *emSdkEnvCache()->object(cacheKey);
}

void parseEmSdkEnvOutputAndAddToEnv(const QString &output, Environment &env)
{
    const QStringList lines = output.split('\n');

    for (const QString &line : lines) {
        const QStringList prependParts = line.trimmed().split(" += ");
        if (prependParts.count() == 2)
            env.prependOrSetPath(FilePath::fromUserInput(prependParts.last()));

        const QStringList setParts = line.trimmed().split(" = ");
        if (setParts.count() == 2) {
            if (setParts.first() != "PATH") // Path was already extended above
                env.set(setParts.first(), setParts.last());
            continue;
        }
    }

    // QTCREATORBUG-26199: Wrapper scripts (e.g. emcc.bat) of older emsdks might not find python
    const QString emsdkPython = env.value("EMSDK_PYTHON");
    if (!emsdkPython.isEmpty())
        env.appendOrSetPath(FilePath::fromUserInput(emsdkPython).parentDir());
}

bool isValid(const FilePath &sdkRoot)
{
    return !version(sdkRoot).isNull();
}

void addToEnvironment(const FilePath &sdkRoot, Environment &env)
{
    if (sdkRoot.exists())
        parseEmSdkEnvOutputAndAddToEnv(emSdkEnvOutput(sdkRoot), env);
}

QVersionNumber version(const FilePath &sdkRoot)
{
    if (!sdkRoot.exists())
        return {};
    const QString cacheKey = sdkRoot.toString();
    if (!emSdkVersionCache()->contains(cacheKey)) {
        Environment env = sdkRoot.deviceEnvironment();
        addToEnvironment(sdkRoot, env);
        QLatin1String scriptFile{sdkRoot.osType() == OsType::OsTypeWindows ? "emcc.bat" : "emcc"};
        FilePath script = sdkRoot.withNewPath(scriptFile).searchInDirectories(env.path());
        const CommandLine command(script, {"-dumpversion"});
        Process emcc;
        emcc.setCommand(command);
        emcc.setEnvironment(env);
        emcc.runBlocking();
        const QString version = emcc.cleanedStdOut();
        emSdkVersionCache()->insert(cacheKey,
                                    new QVersionNumber(QVersionNumber::fromString(version)));
    }
    return *emSdkVersionCache()->object(cacheKey);
}

void registerEmSdk(const FilePath &sdkRoot)
{
    QSettings *s = Core::ICore::settings();
    s->setValue(QLatin1String(Constants::SETTINGS_GROUP) + '/'
                + QLatin1String(Constants::SETTINGS_KEY_EMSDK), sdkRoot.toString());
}

FilePath registeredEmSdk()
{
    QSettings *s = Core::ICore::settings();
    const QString path = s->value(QLatin1String(Constants::SETTINGS_GROUP) + '/'
                     + QLatin1String(Constants::SETTINGS_KEY_EMSDK)).toString();
    return FilePath::fromUserInput(path);
}

void clearCaches()
{
    emSdkEnvCache()->clear();
    emSdkVersionCache()->clear();
}

} // namespace WebAssembly::Internal::WebAssemblyEmSdk