Skip to content

Commit df5f0c7

Browse files
committed
dsit: Compilable WASM build
This commit makes it possible to compile QPrompt for WASM. It removes the KCoreAddons dependency, leaving a gap, and depends on a single tweak to Kirigami. The following compiler flags must also be set: -DWASM:BOOL=ON -DKF_IGNORE_PLATFORM_CHECK:BOOL=ON -DKCOREADDONS_USE_QML:BOOL=OFF -DBUILD_SHARED_LIBS:BOOL=OFF -DBUILD_TESTING:BOOL=OFF -DUSE_DBUS:BOOL=OFF Doesn't work with single-threaded WebAssembly, only multi-threaded. Resulting build loads in Firefox but fails to run due to static build being unable to locate KirigamiPlugin. This is the same bug that afflicts static MacOS builds, and probably all static builds but Android. The changes made here not only bring the WASM build closer to fruition, but also Android and iOS builds as well.
1 parent 0831464 commit df5f0c7

File tree

6 files changed

+94
-39
lines changed

6 files changed

+94
-39
lines changed

CMakeLists.txt

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ project(qprompt
3535
HOMEPAGE_URL "https://qprompt.app/"
3636
LANGUAGES CXX)
3737

38+
set(WASM ON)
39+
set(KF_IGNORE_PLATFORM_CHECK ON)
40+
3841
# Find includes in corresponding build directories
3942
set(CMAKE_INCLUDE_CURRENT_DIR ON)
4043
# Instruct CMake to create code from Qt designer ui files
@@ -138,43 +141,48 @@ find_package(Qt${QT_VERSION_MAJOR} ${QT_MIN_VERSION} REQUIRED NO_MODULE COMPONEN
138141
LinguistTools
139142
)
140143

141-
if (ANDROID OR IOS OR WASM)
144+
if(ANDROID OR IOS OR WASM)
142145
set(BUILD_SHARED_LIBS OFF)
143146
endif()
144147

145-
if (WIN32 OR APPLE OR WASM)
148+
if(WIN32 OR APPLE OR WASM)
146149
set(BUILD_TESTING OFF)
147-
FetchContent_Declare(
148-
CoreAddons
149-
GIT_REPOSITORY "${CMAKE_SOURCE_DIR}/3rdparty/kcoreaddons"
150-
GIT_TAG "v${KF_PREFFERED_VERSION}"
151-
SOURCE_DIR submodules/kcoreaddons
152-
)
150+
if(NOT WASM)
151+
FetchContent_Declare(
152+
CoreAddons
153+
GIT_REPOSITORY "${CMAKE_SOURCE_DIR}/3rdparty/kcoreaddons"
154+
GIT_TAG "v${KF_PREFFERED_VERSION}"
155+
SOURCE_DIR submodules/kcoreaddons
156+
)
157+
FetchContent_MakeAvailable(CoreAddons)
158+
endif()
153159
FetchContent_Declare(
154160
Kirigami
155161
GIT_REPOSITORY "${CMAKE_SOURCE_DIR}/3rdparty/kirigami"
156162
GIT_TAG "v${KF_PREFFERED_VERSION}"
157163
SOURCE_DIR submodules/kirigami
158164
)
159-
FetchContent_MakeAvailable(CoreAddons Kirigami)
165+
FetchContent_MakeAvailable(Kirigami)
160166
else()
161167
find_package(KF${QT_VERSION_MAJOR} ${KF_MIN_VERSION} REQUIRED COMPONENTS
162168
Kirigami
163169
CoreAddons
164170
)
165171
endif()
166172

167-
if(NOT BSD)
168-
set(BUILD_SHARED_LIBS ON)
169-
FetchContent_Declare(
170-
QHotkey
171-
GIT_REPOSITORY "${CMAKE_SOURCE_DIR}/3rdparty/QHotkey"
172-
GIT_TAG "1231c4cc9649ec0132d939cb7e139a82b8edeca0"
173-
SOURCE_DIR submodules/QHotkey
174-
)
175-
FetchContent_MakeAvailable(QHotkey)
176-
else()
177-
find_package(QHotkey)
173+
if(NOT ANDROID AND NOT IOS AND NOT WASM)
174+
if(NOT BSD)
175+
set(BUILD_SHARED_LIBS ON)
176+
FetchContent_Declare(
177+
QHotkey
178+
GIT_REPOSITORY "${CMAKE_SOURCE_DIR}/3rdparty/QHotkey"
179+
GIT_TAG "1231c4cc9649ec0132d939cb7e139a82b8edeca0"
180+
SOURCE_DIR submodules/QHotkey
181+
)
182+
FetchContent_MakeAvailable(QHotkey)
183+
else()
184+
find_package(QHotkey)
185+
endif()
178186
endif()
179187
# Desktop only dependencies
180188
if(NOT ANDROID AND NOT IOS AND NOT WASM)
@@ -356,7 +364,9 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
356364
include(CPack)
357365
endif()
358366

359-
feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) # INCLUDE_QUIET_PACKAGES
367+
if(NOT WASM)
368+
feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) # INCLUDE_QUIET_PACKAGES
369+
endif()
360370

361371
file(GLOB_RECURSE ALL_CLANG_FORMAT_SOURCE_FILES *.cpp *.h *.hpp)
362372
kde_clang_format(${ALL_CLANG_FORMAT_SOURCE_FILES})

src/CMakeLists.txt

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,24 @@ endif()
2626

2727
qt_standard_project_setup(I18N_TRANSLATED_LANGUAGES ar cs de es fi fr he it ja ko nl oc pl pt_BR pt_PT ru tr uk zh)
2828

29-
qt_add_executable(${PROJECT_NAME}
30-
main.cpp
31-
systemfontchooserdialog.h
32-
systemfontchooserdialog.cpp
33-
)
29+
if (ANDROID OR IOS OR WASM)
30+
qt_add_executable(${PROJECT_NAME}
31+
main.cpp
32+
)
33+
else()
34+
qt_add_executable(${PROJECT_NAME}
35+
main.cpp
36+
systemfontchooserdialog.h
37+
systemfontchooserdialog.cpp
38+
)
39+
endif()
3440
if (NOT ANDROID)
3541
qt_wrap_ui(${PROJECT_NAME}
3642
qt/systemfontchooserdialog.ui
3743
)
3844
endif()
3945

40-
if (NOT ANDROID)
46+
if (NOT ANDROID AND NOT WASM)
4147
set(ICONS_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/icons/hicolor)
4248
set(RASTER_ICONS
4349
${ICONS_FOLDER}/16-apps-com.cuperino.qprompt.png
@@ -328,11 +334,7 @@ qt_add_translations(${PROJECT_NAME} SOURCE_TARGETS ${PROJECT_NAME}
328334
)
329335

330336
# Libraries
331-
if (ANDROID OR IOS OR WASM)
332-
if (WASM)
333-
target_compile_options(${PROJECT_NAME} PRIVATE -oz -flto)
334-
target_link_options(${PROJECT_NAME} PRIVATE -flto)
335-
endif()
337+
if (ANDROID OR IOS)
336338
target_link_libraries(${PROJECT_NAME} PRIVATE
337339
Qt${QT_VERSION_MAJOR}::Core
338340
Qt${QT_VERSION_MAJOR}::Quick
@@ -344,6 +346,19 @@ if (ANDROID OR IOS OR WASM)
344346
KF${QT_VERSION_MAJOR}::CoreAddons
345347
KF${QT_VERSION_MAJOR}::Kirigami
346348
)
349+
elseif (WASM)
350+
target_compile_options(${PROJECT_NAME} PRIVATE -O3 -flto -sDISABLE_EXCEPTION_THROWING=0 -sDISABLE_EXCEPTION_CATCHING=0)
351+
target_link_options(${PROJECT_NAME} PRIVATE -flto -fno-except -sASYNCIFY -Os -sDISABLE_EXCEPTION_THROWING=0 -sDISABLE_EXCEPTION_CATCHING=0)
352+
target_link_libraries(${PROJECT_NAME} PRIVATE
353+
Qt${QT_VERSION_MAJOR}::Core
354+
Qt${QT_VERSION_MAJOR}::Quick
355+
Qt${QT_VERSION_MAJOR}::Svg
356+
Qt${QT_VERSION_MAJOR}::Gui
357+
Qt${QT_VERSION_MAJOR}::Qml
358+
Qt${QT_VERSION_MAJOR}::QuickControls2
359+
Qt${QT_VERSION_MAJOR}::Network
360+
KF${QT_VERSION_MAJOR}::Kirigami
361+
)
347362
elseif(HAIKU)
348363
target_link_libraries(${PROJECT_NAME} PRIVATE
349364
Qt${QT_VERSION_MAJOR}::Core

src/documenthandler.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,21 @@ DocumentHandler::DocumentHandler(QObject *parent)
122122
_fileSystemWatcher = new QFileSystemWatcher();
123123
pdf_importer = QString::fromUtf8("TextExtraction");
124124

125-
m_fontDialog = new SystemFontChooserDialog();
126125
m_network = new QNetworkAccessManager(this);
127126
m_cache = new QTemporaryFile(this);
128-
connect(m_fontDialog, &SystemFontChooserDialog::fontFamilyChanged, this, &DocumentHandler::setFontFamily);
129127
connect(m_network, &QNetworkAccessManager::finished, this, &DocumentHandler::loadFromNetworkFinihed);
128+
129+
#if !(defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(Q_OS_WASM) || defined(Q_OS_WATCHOS))
130+
m_fontDialog = new SystemFontChooserDialog();
131+
connect(m_fontDialog, &SystemFontChooserDialog::fontFamilyChanged, this, &DocumentHandler::setFontFamily);
132+
#endif
130133
}
131134

132135
DocumentHandler::~DocumentHandler()
133136
{
137+
#if !(defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(Q_OS_WASM) || defined(Q_OS_WATCHOS))
134138
delete m_fontDialog;
139+
#endif
135140
}
136141

137142
QQuickTextDocument *DocumentHandler::document() const
@@ -417,6 +422,7 @@ void DocumentHandler::setKeyMarker(QString keyCodeString = QString::fromUtf8("")
417422
Q_EMIT markerChanged();
418423
}
419424

425+
#if !(defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(Q_OS_WASM) || defined(Q_OS_WATCHOS))
420426
bool DocumentHandler::showFontDialog()
421427
{
422428
QTextCursor cursor = textCursor();
@@ -436,6 +442,7 @@ bool DocumentHandler::showFontDialog()
436442
m_fontDialog->show(fontFamily(), text);
437443
return false;
438444
}
445+
#endif
439446

440447
QString DocumentHandler::getMarkerKey()
441448
{
@@ -610,6 +617,7 @@ void DocumentHandler::load(const QUrl &fileUrl)
610617
#endif
611618
// File formats imported using external software
612619
else {
620+
#if !(defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(Q_OS_WASM) || defined(Q_OS_WATCHOS))
613621
ImportFormat type = NONE;
614622
if (mime.inherits(QString::fromUtf8("application/pdf")))
615623
type = PDF;
@@ -649,6 +657,7 @@ void DocumentHandler::load(const QUrl &fileUrl)
649657
}
650658
// Read as raw or text file
651659
else {
660+
#endif
652661
// Interpret RAW data using Qt's auto detection
653662
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
654663
// I doubt that this is a proper conversion. Needs proper testing.
@@ -657,7 +666,9 @@ void DocumentHandler::load(const QUrl &fileUrl)
657666
QTextCodec *codec = QTextCodec::codecForName("utf-8");
658667
updateContents(codec->toUnicode(data));
659668
#endif
669+
#if !(defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(Q_OS_WASM) || defined(Q_OS_WATCHOS))
660670
}
671+
#endif
661672
}
662673
doc->setModified(false);
663674
}
@@ -685,6 +696,7 @@ void DocumentHandler::load(const QUrl &fileUrl)
685696
Q_EMIT fileUrlChanged();
686697
}
687698

699+
#if !(defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(Q_OS_WASM) || defined(Q_OS_WATCHOS))
688700
QString DocumentHandler::import(const QString &fileName, ImportFormat type)
689701
{
690702
QString program = QString::fromUtf8("");
@@ -743,6 +755,7 @@ QString DocumentHandler::import(const QString &fileName, ImportFormat type)
743755
// return filterHtml(html, true);
744756
return filterHtml(html, false);
745757
}
758+
#endif
746759

747760
void DocumentHandler::updateContents(const QString &text, Qt::TextFormat format) {
748761
QTextCursor cursor = textCursor();

src/documenthandler.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@
8080
#include <QUrl>
8181

8282
#include "markersmodel.h"
83+
#if !(defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(Q_OS_WASM) || defined(Q_OS_WATCHOS))
8384
#include "systemfontchooserdialog.h"
85+
#endif
8486
#include <QFont>
8587
#include <QQuickTextDocument>
8688
#include <QTextCursor>
@@ -211,7 +213,9 @@ class DocumentHandler : public QObject
211213

212214
Q_INVOKABLE bool preventSleep(bool prevent);
213215

216+
#if !(defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(Q_OS_WASM) || defined(Q_OS_WATCHOS))
214217
Q_INVOKABLE bool showFontDialog();
218+
#endif
215219

216220
Q_INVOKABLE void loadFromNetwork(const QUrl &url);
217221

@@ -264,7 +268,9 @@ public Q_SLOTS:
264268

265269
enum ImportFormat { NONE, PDF, ODT, DOCX, DOC, RTF, ABW, EPUB, MOBI, AZW, PAGES, PAGESX };
266270
void updateContents(const QString &text, Qt::TextFormat format);
271+
#if !(defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(Q_OS_WASM) || defined(Q_OS_WATCHOS))
267272
QString import(const QString &fileName, ImportFormat);
273+
#endif
268274

269275
QQuickTextDocument *m_document;
270276

@@ -277,7 +283,9 @@ public Q_SLOTS:
277283
MarkersModel *_markersModel;
278284
QFileSystemWatcher *_fileSystemWatcher;
279285

286+
#if !(defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(Q_OS_WASM) || defined(Q_OS_WATCHOS))
280287
SystemFontChooserDialog *m_fontDialog;
288+
#endif
281289
QFont m_font;
282290
QUrl m_fileUrl;
283291
QString pdf_importer;

src/main.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
#endif
4545
// #include <KLocalizedContext>
4646
// #include <KLocalizedString>
47+
#ifndef Q_OS_WASM
4748
#include <kaboutdata.h>
49+
#endif
4850

4951
#if defined(KF6Crash_FOUND)
5052
#include <KCrash>
@@ -132,6 +134,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
132134
QString copyrightYear = QString::number(currentYear);
133135
QString copyrightStatement1 = QStringLiteral("© 2020 Javier O. Cordero Pérez");
134136
QString copyrightStatement2 = QStringLiteral("© 2020-2025 Javier O. Cordero Pérez"); // , copyrightYear);
137+
#ifndef Q_OS_WASM
135138
KAboutData aboutData(QLatin1String("qprompt"),
136139
QLatin1String("QPrompt"),
137140
QPROMPT_VERSION_STRING " (" + QString::fromUtf8(GIT_BRANCH) + "/" + QString::fromUtf8(GIT_COMMIT_HASH) + ")",
@@ -161,7 +164,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
161164
//);
162165
// Set the application metadata
163166
KAboutData::setApplicationData(aboutData);
164-
167+
#endif
165168
// qmlRegisterType<PrompterTimer>(QPROMPT_URI + ".promptertimer", 1, 0, "PrompterTimer");
166169
// qmlRegisterType<DocumentHandler>(QPROMPT_URI ".document", 1, 0, "DocumentHandler");
167170
// qmlRegisterType<MarkersModel>(QPROMPT_URI ".markers", 1, 0, "MarkersModel");
@@ -172,9 +175,9 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
172175

173176
// qRegisterMetaType<Marker>();
174177

175-
// #ifdef Q_OS_ANDROID
176-
// KirigamiPlugin::getInstance().registerTypes();
177-
// #endif
178+
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(Q_OS_WASM)
179+
// KirigamiPlugin::getInstance().registerTypes();
180+
#endif
178181

179182
#if defined(Q_OS_MACOS)
180183
// Enable automatic display of dialog prompts on the touchbar.
@@ -233,7 +236,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
233236
// Un-comment to force RightToLeft Layout for debugging purposes
234237
// app.setLayoutDirection(Qt::RightToLeft);
235238

236-
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(Q_OS_QNX)
239+
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
237240
app.setWindowIcon(QIcon(":/qt/qml/com/cuperino/qprompt/images/qprompt-logo-wireframe.png"));
238241
#else
239242
app.setWindowIcon(QIcon(":/qt/qml/com/cuperino/qprompt/images/qprompt.png"));
@@ -255,7 +258,9 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
255258
engine.addImportPath(QStringLiteral("../Resources/qml/"));
256259
// Send context data from C++ to QML
257260
// engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
261+
#ifndef Q_OS_WASM
258262
engine.rootContext()->setContextProperty(QStringLiteral("aboutData"), QVariant::fromValue(KAboutData::applicationData()));
263+
#endif
259264
if (positionalArguments.length())
260265
engine.rootContext()->setContextProperty(QStringLiteral("fileToOpen"), fileToOpen);
261266
#if defined(Q_OS_MACOS)

src/qmlutil.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
#include <QKeySequence>
3434
#include <QObject>
3535
#include <QPixmap>
36+
#if !(defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(Q_OS_WASM) || defined(Q_OS_WATCHOS))
3637
#include <QProcess>
38+
#endif
3739
#include <QQmlEngine>
3840
#include <QSettings>
3941

@@ -75,7 +77,9 @@ class QmlUtil : public QObject
7577
}
7678
Q_INVOKABLE void restartApplication()
7779
{
80+
#if !(defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(Q_OS_WASM) || defined(Q_OS_WATCHOS))
7881
QProcess::startDetached(QCoreApplication::applicationFilePath(), {"-q"});
82+
#endif
7983
QCoreApplication::quit();
8084
}
8185
Q_INVOKABLE void hideCursor()

0 commit comments

Comments
 (0)