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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
// 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 "common.h"
#include "jsontools.h"
#include "wasmbinary.h"
#include <QCoreApplication>
#include <QDir>
#include <QDirListing>
#include <QDirIterator>
#include <QtGlobal>
#include <QLibraryInfo>
#include <QJsonDocument>
#include <QStringList>
#include <QtCore/QCommandLineOption>
#include <QtCore/QCommandLineParser>
#include <QtCore/QProcess>
#include <QQueue>
#include <QMap>
#include <QSet>
#include <optional>
#include <iostream>
#include <ostream>
struct Parameters
{
std::optional<QString> argAppPath;
QString appWasmPath;
std::optional<QDir> qtHostDir;
std::optional<QDir> qtWasmDir;
QList<QDir> libPaths;
std::optional<QDir> qmlRootPath;
QSet<QString> loadedQtLibraries;
};
bool parseArguments(Parameters ¶ms)
{
QCoreApplication::setApplicationName("wasmdeployqt");
QCoreApplication::setApplicationVersion("1.0");
QCommandLineParser parser;
parser.setApplicationDescription(
QStringLiteral("Qt for WebAssembly deployment tool \n\n"
"Example:\n"
"wasmdeployqt app.wasm --qml-root-path=repo/myapp "
"--qt-wasm-dir=/home/user/qt/shared-qt-wasm/bin"));
parser.addHelpOption();
QStringList args = QCoreApplication::arguments();
parser.addPositionalArgument("app", "Path to the application.");
QCommandLineOption libPathOption("lib-path", "Colon-separated list of library directories.",
"paths");
parser.addOption(libPathOption);
QCommandLineOption qtWasmDirOption("qt-wasm-dir", "Path to the Qt for WebAssembly directory.",
"dir");
parser.addOption(qtWasmDirOption);
QCommandLineOption qtHostDirOption("qt-host-dir", "Path to the Qt host directory.", "dir");
parser.addOption(qtHostDirOption);
QCommandLineOption qmlRootPathOption("qml-root-path", "Root directory for QML files.", "dir");
parser.addOption(qmlRootPathOption);
parser.process(args);
const QStringList positionalArgs = parser.positionalArguments();
if (positionalArgs.size() > 1) {
std::cout << "ERROR: Expected only one positional argument with path to the app. Received: "
<< positionalArgs.join(" ").toStdString() << std::endl;
return false;
}
if (!positionalArgs.isEmpty()) {
params.argAppPath = positionalArgs.first();
}
if (parser.isSet(libPathOption)) {
QStringList paths = parser.value(libPathOption).split(';', Qt::SkipEmptyParts);
for (const QString &path : paths) {
QDir dir(path);
if (dir.exists()) {
params.libPaths.append(dir);
} else {
std::cout << "ERROR: Directory does not exist: " << path.toStdString() << std::endl;
return false;
}
}
}
if (parser.isSet(qtWasmDirOption)) {
QDir dir(parser.value(qtWasmDirOption));
if (dir.cdUp() && dir.exists())
params.qtWasmDir = dir;
else {
std::cout << "ERROR: Directory does not exist: " << dir.absolutePath().toStdString()
<< std::endl;
return false;
}
}
if (parser.isSet(qtHostDirOption)) {
QDir dir(parser.value(qtHostDirOption));
if (dir.cdUp() && dir.exists())
params.qtHostDir = dir;
else {
std::cout << "ERROR: Directory does not exist: " << dir.absolutePath().toStdString()
<< std::endl;
return false;
}
}
if (parser.isSet(qmlRootPathOption)) {
QDir dir(parser.value(qmlRootPathOption));
if (dir.exists()) {
params.qmlRootPath = dir;
} else {
std::cout << "ERROR: Directory specified for qml-root-path does not exist: "
<< dir.absolutePath().toStdString() << std::endl;
return false;
}
}
return true;
}
std::optional<QString> detectAppName()
{
QDirIterator it(QDir::currentPath(), QStringList() << "*.html" << "*.wasm" << "*.js",
QDir::NoFilter);
QMap<QString, QSet<QString>> fileGroups;
while (it.hasNext()) {
QFileInfo fileInfo(it.next());
QString baseName = fileInfo.completeBaseName();
QString suffix = fileInfo.suffix();
fileGroups[baseName].insert(suffix);
}
for (auto it = fileGroups.constBegin(); it != fileGroups.constEnd(); ++it) {
const QSet<QString> &extensions = it.value();
if (extensions.contains("html") && extensions.contains("js")
&& extensions.contains("wasm")) {
return it.key();
}
}
return std::nullopt;
}
bool verifyPaths(Parameters ¶ms)
{
if (params.argAppPath) {
QFileInfo fileInfo(*params.argAppPath);
if (!fileInfo.exists()) {
std::cout << "ERROR: Cannot find " << params.argAppPath->toStdString() << std::endl;
std::cout << "Make sure that the path is valid." << std::endl;
return false;
}
params.appWasmPath = fileInfo.absoluteFilePath();
} else {
auto appName = detectAppName();
if (!appName) {
std::cout << "ERROR: Cannot find the application in current directory. Specify the "
"path as an argument:"
"wasmdeployqt <path-to-app-wasm-binary>"
<< std::endl;
return false;
}
params.appWasmPath = QDir::current().filePath(*appName + ".wasm");
std::cout << "Automatically detected " << params.appWasmPath.toStdString() << std::endl;
}
if (!params.qtWasmDir) {
std::cout << "ERROR: Please set path to Qt WebAssembly installation as "
"--qt-wasm-dir=<path_to_qt_wasm_bin>"
<< std::endl;
return false;
}
if (!params.qtHostDir) {
auto qtHostPath = QLibraryInfo::path(QLibraryInfo::BinariesPath);
if (qtHostPath.length() == 0) {
std::cout << "ERROR: Cannot read Qt host path or detect it from environment. Please "
"pass it explicitly with --qt-host-dir=<path>. "
<< std::endl;
} else {
auto qtHostDir = QDir(qtHostPath);
if (!qtHostDir.cdUp()) {
std::cout << "ERROR: Invalid Qt host path: "
<< qtHostDir.absolutePath().toStdString() << std::endl;
return false;
}
params.qtHostDir = qtHostDir;
}
}
params.libPaths.push_front(params.qtWasmDir->filePath("lib"));
params.libPaths.push_front(*params.qtWasmDir);
return true;
}
bool copyFile(QString srcPath, QString destPath)
{
auto file = QFile(destPath);
if (file.exists()) {
file.remove();
}
QFileInfo destInfo(destPath);
if (!QDir().mkpath(destInfo.path())) {
std::cout << "ERROR: Cannot create path " << destInfo.path().toStdString() << std::endl;
return false;
}
if (!QFile::copy(srcPath, destPath)) {
std::cout << "ERROR: Failed to copy " << srcPath.toStdString() << " to "
<< destPath.toStdString() << std::endl;
return false;
}
return true;
}
bool copyDirectDependencies(QList<QString> dependencies, const Parameters ¶ms)
{
for (auto &&depFilename : dependencies) {
if (params.loadedQtLibraries.contains(depFilename)) {
continue; // dont copy library that has been already copied
}
std::optional<QString> libPath;
for (auto &&libDir : params.libPaths) {
auto path = libDir.filePath(depFilename);
QFileInfo file(path);
if (file.exists()) {
libPath = path;
}
}
if (!libPath) {
std::cout << "ERROR: Cannot find required library " << depFilename.toStdString()
<< std::endl;
return false;
}
if (!copyFile(*libPath, QDir::current().filePath(depFilename)))
return false;
}
std::cout << "INFO: Succesfully copied direct dependencies." << std::endl;
return true;
}
QStringList findSoFiles(const QString &directory)
{
QStringList soFiles;
QDir baseDir(directory);
if (!baseDir.exists())
return soFiles;
QDirIterator it(directory, QStringList() << "*.so", QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) {
it.next();
QString absPath = it.filePath();
QString filePath = baseDir.relativeFilePath(absPath);
soFiles.append(filePath);
}
return soFiles;
}
bool copyQtLibs(Parameters ¶ms)
{
Q_ASSERT(params.qtWasmDir);
auto qtLibDir = *params.qtWasmDir;
if (!qtLibDir.cd("lib")) {
std::cout << "ERROR: Cannot find lib directory in Qt installation." << std::endl;
return false;
}
auto qtLibTargetDir = QDir(QDir(QDir::current().filePath("qt")).filePath("lib"));
auto soFiles = findSoFiles(qtLibDir.absolutePath());
for (auto &&soFilePath : soFiles) {
auto relativeFilePath = QDir("lib").filePath(soFilePath);
auto srcPath = qtLibDir.absoluteFilePath(soFilePath);
auto destPath = qtLibTargetDir.absoluteFilePath(soFilePath);
if (!copyFile(srcPath, destPath))
return false;
params.loadedQtLibraries.insert(QFileInfo(srcPath).fileName());
}
std::cout << "INFO: Succesfully deployed qt lib shared objects." << std::endl;
return true;
}
bool copyPreloadPlugins(Parameters ¶ms)
{
Q_ASSERT(params.qtWasmDir);
auto qtPluginsDir = *params.qtWasmDir;
if (!qtPluginsDir.cd("plugins")) {
std::cout << "ERROR: Cannot find plugins directory in Qt installation." << std::endl;
return false;
}
auto qtPluginsTargetDir = QDir(QDir(QDir::current().filePath("qt")).filePath("plugins"));
// copy files
auto soFiles = findSoFiles(qtPluginsDir.absolutePath());
for (auto &&soFilePath : soFiles) {
auto relativeFilePath = QDir("plugins").filePath(soFilePath);
params.loadedQtLibraries.insert(QFileInfo(relativeFilePath).fileName());
auto srcPath = qtPluginsDir.absoluteFilePath(soFilePath);
auto destPath = qtPluginsTargetDir.absoluteFilePath(soFilePath);
if (!copyFile(srcPath, destPath))
return false;
}
// qt_plugins.json
QSet<PreloadEntry> preload{ { { "qt.conf" }, { "/qt.conf" } } };
for (auto &&plugin : soFiles) {
PreloadEntry entry;
entry.source = QDir("$QTDIR").filePath("plugins") + QDir::separator()
+ QDir(qtPluginsDir).relativeFilePath(plugin);
entry.destination = "/qt/plugins/" + QDir(qtPluginsTargetDir).relativeFilePath(plugin);
preload.insert(entry);
}
JsonTools::savePreloadFile(preload, QDir::current().filePath("qt_plugins.json"));
QString qtconfContent = "[Paths]\nPrefix = /qt\n";
QString filePath = QDir::current().filePath("qt.conf");
QFile file(filePath);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
out << qtconfContent;
if (!file.flush()) {
std::cout << "ERROR: Failed flushing the file :" << file.fileName().toStdString()
<< std::endl;
return false;
}
file.close();
} else {
std::cout << "ERROR: Failed to write to qt.conf." << std::endl;
return false;
}
std::cout << "INFO: Succesfully deployed qt plugins." << std::endl;
return true;
}
bool copyPreloadQmlImports(Parameters ¶ms)
{
Q_ASSERT(params.qtWasmDir);
if (!params.qmlRootPath) {
std::cout << "WARNING: qml-root-path not specified. Skipping generating preloads for QML "
"imports."
<< std::endl;
std::cout << "WARNING: This may lead to erronous behaviour if applications requires QML "
"imports."
<< std::endl;
QSet<PreloadEntry> preload;
JsonTools::savePreloadFile(preload, QDir::current().filePath("qt_qml_imports.json"));
return true;
}
auto qmlImportScannerPath = params.qtHostDir
? QDir(params.qtHostDir->filePath("libexec")).filePath("qmlimportscanner")
: "qmlimportscanner";
QProcess process;
auto qmlImportPath = *params.qtWasmDir;
qmlImportPath.cd("qml");
if (!qmlImportPath.exists()) {
std::cout << "ERROR: Cannot find qml import path: "
<< qmlImportPath.absolutePath().toStdString() << std::endl;
return false;
}
QStringList args{ "-rootPath", params.qmlRootPath->absolutePath(), "-importPath",
qmlImportPath.absolutePath() };
process.start(qmlImportScannerPath, args);
if (!process.waitForFinished()) {
std::cout << "ERROR: Failed to execute qmlImportScanner." << std::endl;
return false;
}
QString stdoutOutput = process.readAllStandardOutput();
auto qmlImports = JsonTools::getPreloadsFromQmlImportScannerOutput(stdoutOutput);
if (!qmlImports) {
return false;
}
JsonTools::savePreloadFile(*qmlImports, QDir::current().filePath("qt_qml_imports.json"));
for (const PreloadEntry &import : *qmlImports) {
auto relativePath = import.source;
relativePath.remove("$QTDIR/");
auto srcPath = params.qtWasmDir->absoluteFilePath(relativePath);
auto destPath = QDir(QDir::current().filePath("qt")).absoluteFilePath(relativePath);
if (!copyFile(srcPath, destPath))
return false;
}
std::cout << "INFO: Succesfully deployed qml imports." << std::endl;
return true;
}
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
Parameters params;
if (!parseArguments(params)) {
return -1;
}
if (!verifyPaths(params)) {
return -1;
}
std::cout << "INFO: Target: " << params.appWasmPath.toStdString() << std::endl;
WasmBinary wasmBinary(params.appWasmPath);
if (wasmBinary.type == WasmBinary::Type::INVALID) {
return -1;
} else if (wasmBinary.type == WasmBinary::Type::STATIC) {
std::cout << "INFO: This is statically linked WebAssembly binary." << std::endl;
std::cout << "INFO: No extra steps required!" << std::endl;
return 0;
}
std::cout << "INFO: Verified as shared module." << std::endl;
if (!copyQtLibs(params))
return -1;
if (!copyPreloadPlugins(params))
return -1;
if (!copyPreloadQmlImports(params))
return -1;
if (!copyDirectDependencies(wasmBinary.dependencies, params))
return -1;
std::cout << "INFO: Deployment done!" << std::endl;
return 0;
}
|