WebAssembly: Revamp Emscripten SDK registration and toolchain handling

Qt Creator's ability to register Emscripten toolchains depended on an
SDK being activated "globally", and on the presence of the ~/.emscripten
file. Qt Creator would parse that file in order to determine the
location of the compiler and the necessary environment variables that
have to be set.

As of recently, the Emscripten SDK does neither activate gobally
anymore, nor is the ~/.emscripten generated. This change here addresses
the new situation in a couple of ways:

- Instead of trying to silently detect everything, add a UI
(IOptionsPageWidget) that lets the user select the Emscripten SDK root.
- Instead of parsing the ~/.emscripten file, parse the output of the
emsdk_env tool to determine the toolchain environment
(ToolChain::addToEnvironment). The parsing is cached. A test for the
parsing is included.
- Instead of registering the underlying clang as compiler, register the
emcc/em++ wrapper scripts, which are (also as of recently) compatible
with Qt Creator's way of determining gcc's predefined macros and built-
in header paths.

One Emscripten SDK is registered globally in Qt Creator. When changing
that, the previous Emscripten toolchains are removed, the new ones are
registered and the kit are "fixed" to use those.

On startup, an InfoBar entry is shown if Qt for Webassembly kits exist
and no Emscripten toolchains are present. That's the case for first-time
use after installing Qt for Webassembly via Qt SDK installer. The
InfoBar entry opens up the IOptionsPageWidget.

Qt 5.15.0 for WebAssembly and Emscripten SDK 1.39.0 are the minimum
supported versions. The new UI will show warnings accordingly.

Task-number: QTCREATORBUG-24811
Fixes: QTCREATORBUG-24822
Fixes: QTCREATORBUG-24814
Fixes: QTCREATORBUG-23741
Fixes: QTCREATORBUG-23561
Fixes: QTCREATORBUG-23160
Fixes: QTCREATORBUG-23126
Change-Id: I017c61586b17e815bb20a90e3f305a6bf705da36
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Alessandro Portale
2020-11-03 00:20:43 +01:00
parent 783a06b2ee
commit b057ea1ab8
15 changed files with 712 additions and 110 deletions

View File

@@ -0,0 +1,181 @@
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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 https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "webassemblyconstants.h"
#include "webassemblyemsdk.h"
#include <coreplugin/icore.h>
#include <utils/environment.h>
#include <utils/qtcprocess.h>
#include <utils/hostosinfo.h>
#include <QSettings>
#ifdef WITH_TESTS
# include <QTest>
# include "webassemblyplugin.h"
#endif // WITH_TESTS
using namespace Utils;
namespace WebAssembly {
namespace Internal {
using EmSdkEnvCache = QCache<QString, QByteArray>;
Q_GLOBAL_STATIC_WITH_ARGS(EmSdkEnvCache, emSdkEnvCache, (10))
using EmSdkVersionCache = QCache<QString, QVersionNumber>;
Q_GLOBAL_STATIC_WITH_ARGS(EmSdkVersionCache, emSdkVersionCache, (10))
static QByteArray emSdkEnvOutput(const FilePath &sdkRoot)
{
const QString cacheKey = sdkRoot.toString();
if (!emSdkEnvCache()->contains(cacheKey)) {
const QString scriptFile = sdkRoot.pathAppended(QLatin1String("emsdk_env") +
(HostOsInfo::isWindowsHost() ? ".bat" : ".sh")).toString();
QtcProcess emSdkEnv;
if (HostOsInfo::isWindowsHost()) {
emSdkEnv.setCommand(CommandLine(scriptFile));
} else {
// File needs to be source'd, not executed.
emSdkEnv.setCommand({"bash", {"-c", ". " + scriptFile}});
}
emSdkEnv.start();
if (!emSdkEnv.waitForFinished())
return {};
emSdkEnvCache()->insert(cacheKey, new QByteArray(emSdkEnv.readAllStandardError()));
}
return *emSdkEnvCache()->object(cacheKey);
}
static void parseEmSdkEnvOutputAndAddToEnv(const QByteArray &output, Environment &env)
{
const QStringList lines = QString::fromLocal8Bit(output).split('\n');
for (const QString &line : lines) {
const QStringList prependParts = line.trimmed().split(" += ");
if (prependParts.count() == 2)
env.prependOrSetPath(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;
}
}
}
bool WebAssemblyEmSdk::isValid(const FilePath &sdkRoot)
{
return !version(sdkRoot).isNull();
}
void WebAssemblyEmSdk::addToEnvironment(const FilePath &sdkRoot, Environment &env)
{
if (sdkRoot.exists())
parseEmSdkEnvOutputAndAddToEnv(emSdkEnvOutput(sdkRoot), env);
}
QVersionNumber WebAssemblyEmSdk::version(const FilePath &sdkRoot)
{
if (!sdkRoot.exists())
return {};
const QString cacheKey = sdkRoot.toString();
if (!emSdkVersionCache()->contains(cacheKey)) {
Environment env;
// Non-Windows: Need python in path (not provided by emsdk), thus use systemEnvironment
if (!HostOsInfo::isWindowsHost())
env = Environment::systemEnvironment();
WebAssemblyEmSdk::addToEnvironment(sdkRoot, env);
const QString scriptFile =
QLatin1String("emcc") + QLatin1String(HostOsInfo::isWindowsHost() ? ".bat" : "");
const CommandLine command(env.searchInPath(scriptFile), {"-dumpversion"});
QtcProcess emcc;
emcc.setCommand(command);
emcc.setEnvironment(env);
emcc.start();
if (!emcc.waitForFinished())
return {};
const QString version = QLatin1String(emcc.readAllStandardOutput());
emSdkVersionCache()->insert(cacheKey,
new QVersionNumber(QVersionNumber::fromString(version)));
}
return *emSdkVersionCache()->object(cacheKey);
}
void WebAssemblyEmSdk::registerEmSdk(const FilePath &sdkRoot)
{
QSettings *s = Core::ICore::settings();
s->setValue(QLatin1String(Constants::SETTINGS_GROUP) + '/'
+ QLatin1String(Constants::SETTINGS_KEY_EMSDK), sdkRoot.toString());
}
FilePath WebAssemblyEmSdk::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 WebAssemblyEmSdk::clearCaches()
{
emSdkEnvCache()->clear();
emSdkVersionCache()->clear();
}
// Unit tests:
#ifdef WITH_TESTS
void WebAssemblyPlugin::testEmSdkEnvParsing()
{
// Output of "emsdk_env"
const QByteArray emSdkEnvOutput = R"(
Adding directories to PATH:
PATH += C:\Users\user\dev\emsdk
PATH += C:\Users\user\dev\emsdk\upstream\emscripten
PATH += C:\Users\user\dev\emsdk\node\12.18.1_64bit\bin
PATH += C:\Users\user\dev\emsdk\python\3.7.4-pywin32_64bit
PATH += C:\Users\user\dev\emsdk\java\8.152_64bit\bin
Setting environment variables:
PATH = C:\Users\user\dev\emsdk;C:\Users\user\dev\emsdk\upstream\emscripten;C:\Users\user\dev\emsdk\node\12.18.1_64bit\bin;C:\Users\user\dev\emsdk\python\3.7.4-pywin32_64bit;C:\Users\user\dev\emsdk\java\8.152_64bit\bin;...other_stuff...
EMSDK = C:/Users/user/dev/emsdk
EM_CONFIG = C:\Users\user\dev\emsdk\.emscripten
EM_CACHE = C:/Users/user/dev/emsdk/upstream/emscripten\cache
EMSDK_NODE = C:\Users\user\dev\emsdk\node\12.18.1_64bit\bin\node.exe
EMSDK_PYTHON = C:\Users\user\dev\emsdk\python\3.7.4-pywin32_64bit\python.exe
JAVA_HOME = C:\Users\user\dev\emsdk\java\8.152_64bit
)";
Environment env;
parseEmSdkEnvOutputAndAddToEnv(emSdkEnvOutput, env);
QVERIFY(env.path().count() == 5);
QCOMPARE(env.value("EMSDK"), "C:/Users/user/dev/emsdk");
QCOMPARE(env.value("EM_CONFIG"), "C:\\Users\\user\\dev\\emsdk\\.emscripten");
}
#endif // WITH_TESTS
} // namespace Internal
} // namespace WebAssembly