Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support emscripten build #1145

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ option(ENABLE_XDGAUTOSTART "Enable xdg autostart desktop file installation" On)
option(USE_FLATPAK_ICON "Use flatpak icon name for desktop files" Off)
option(ENABLE_EMOJI "Enable emoji module" On)
option(ENABLE_LIBUUID "Use libuuid for uuid generation" On)
option(BUILD_SPELL_DICT "Build en_dict.fscd for English spell check" On)
set(NO_PREEDIT_APPS "gvim.*,wps.*,wpp.*,et.*" CACHE STRING "Disable preedit for follwing app by default.")

if (ENABLE_EMOJI)
Expand Down Expand Up @@ -71,7 +72,7 @@ if (NOT TARGET Systemd::Systemd)

if (NOT LIBUV_TARGET)
if (NOT (TARGET PkgConfig::LibUV))
pkg_check_modules(LibUV REQUIRED IMPORTED_TARGET "libuv")
pkg_check_modules(LibUV IMPORTED_TARGET "libuv")
endif()
set(LIBUV_TARGET PkgConfig::LibUV)
endif()
Expand Down
2 changes: 1 addition & 1 deletion cmake/Fcitx5CompilerSettings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set(CMAKE_C_STANDARD 99)
set(CMAKE_C_FLAGS "-Wall -Wextra ${CMAKE_C_FLAGS}")
set(CMAKE_CXX_FLAGS "-Wall -Wextra ${CMAKE_CXX_FLAGS}")

if(NOT APPLE)
if(NOT APPLE AND NOT EMSCRIPTEN)
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-undefined -Wl,--as-needed ${CMAKE_SHARED_LINKER_FLAGS}")
set(CMAKE_MODULE_LINKER_FLAGS "-Wl,--no-undefined -Wl,--as-needed ${CMAKE_MODULE_LINKER_FLAGS}")
endif()
Expand Down
20 changes: 13 additions & 7 deletions src/lib/fcitx-utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ if (ENABLE_DBUS)
endif()
endif()

if (NOT TARGET Systemd::Systemd)
if (TARGET Systemd::Systemd)
set(FCITX_UTILS_SOURCES
${FCITX_UTILS_SOURCES}
event_libuv.cpp)
else()
event_sdevent.cpp)
elseif (TARGET ${LIBUV_TARGET})
set(FCITX_UTILS_SOURCES
${FCITX_UTILS_SOURCES}
event_sdevent.cpp)
event_libuv.cpp)
else()
list(APPEND FCITX_UTILS_SOURCES event_stub.cpp)
endif()

set(FCITX_UTILS_SOURCES
Expand Down Expand Up @@ -104,6 +106,10 @@ set(FCITX_UTILS_HEADERS
${CMAKE_CURRENT_BINARY_DIR}/fcitxutils_export.h
)

if (NOT TARGET Systemd::Systemd AND NOT TARGET ${LIBUV_TARGET})
list(APPEND FCITX_UTILS_HEADERS event_impl.h)
endif()

set(FCITX_UTILS_DBUS_HEADERS
dbus/message.h
dbus/objectvtable.h
Expand Down Expand Up @@ -134,13 +140,13 @@ if(LIBKVM_FOUND)
target_link_libraries(Fcitx5Utils PRIVATE LibKVM::LibKVM)
endif()

if (NOT TARGET Systemd::Systemd)
if (TARGET Systemd::Systemd)
target_link_libraries(Fcitx5Utils PRIVATE Systemd::Systemd)
elseif (TARGET ${LIBUV_TARGET})
target_link_libraries(Fcitx5Utils PRIVATE ${LIBUV_TARGET})
if (ENABLE_DBUS)
target_link_libraries(Fcitx5Utils PRIVATE PkgConfig::DBus)
endif()
else()
target_link_libraries(Fcitx5Utils PRIVATE Systemd::Systemd)
endif()

configure_file(Fcitx5Utils.pc.in ${CMAKE_CURRENT_BINARY_DIR}/Fcitx5Utils.pc @ONLY)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/fcitx-utils/endian_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define _FCITX_UTILS_ENDIAN_P_H_

#include <cstdint>
#if defined(__linux__) || defined(__GLIBC__)
#if defined(__linux__) || defined(__GLIBC__) || defined(__EMSCRIPTEN__)
#include <endian.h>
#elif defined(__APPLE__)

Expand Down
29 changes: 29 additions & 0 deletions src/lib/fcitx-utils/event_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* SPDX-FileCopyrightText: 2024 Qijia Liu <[email protected]>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
*/
#ifndef _FCITX_UTILS_EVENT_IMPL_H_
#define _FCITX_UTILS_EVENT_IMPL_H_

#include <fcitx-utils/event.h>
namespace fcitx {
class FCITXUTILS_EXPORT EventLoopImpl {
public:
EventLoopImpl() = default;
virtual ~EventLoopImpl() = default;
virtual std::unique_ptr<EventSourceIO>
addIOEvent(int fd, IOEventFlags flags, IOCallback callback);
virtual std::unique_ptr<EventSourceTime>
addTimeEvent(clockid_t clock, uint64_t usec, uint64_t accuracy,
TimeCallback callback);
virtual std::unique_ptr<EventSource> addExitEvent(EventCallback callback);
virtual std::unique_ptr<EventSource> addDeferEvent(EventCallback callback);
virtual std::unique_ptr<EventSource> addPostEvent(EventCallback callback);
};

FCITXUTILS_EXPORT void setEventLoopImpl(std::unique_ptr<EventLoopImpl> factory);
} // namespace fcitx

#endif // _FCITX_UTILS_EVENT_H_
154 changes: 154 additions & 0 deletions src/lib/fcitx-utils/event_stub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* SPDX-FileCopyrightText: 2024 Qijia Liu <[email protected]>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
*/
#include "fcitx-utils/event.h"
#include "fcitx-utils/log.h"
#include "event_impl.h"

namespace fcitx {

template <typename Interface>
struct StubEventSourceBase : public Interface {
public:
~StubEventSourceBase() override {}

bool isEnabled() const override { return false; }

void setEnabled(bool enabled) override { FCITX_UNUSED(enabled); }

bool isOneShot() const override { return false; }

void setOneShot() override {}
};

struct StubEventSource : public StubEventSourceBase<EventSource> {
StubEventSource() {}
};

struct StubEventSourceIO : public StubEventSourceBase<EventSourceIO> {
StubEventSourceIO() {}

int fd() const override { return 0; }

void setFd(int fd) override { FCITX_UNUSED(fd); }

IOEventFlags events() const override { return IOEventFlag::In; }

void setEvents(IOEventFlags flags) override { FCITX_UNUSED(flags); }

IOEventFlags revents() const override { return IOEventFlag::In; }
};

struct StubEventSourceTime : public StubEventSourceBase<EventSourceTime> {
StubEventSourceTime() {}

uint64_t time() const override { return 0; }

void setTime(uint64_t time) override { FCITX_UNUSED(time); }

uint64_t accuracy() const override { return 0; }

void setAccuracy(uint64_t time) override { FCITX_UNUSED(time); }

clockid_t clock() const override { return 0; }
};

static std::shared_ptr<EventLoopImpl> eventLoopImpl = nullptr;

void setEventLoopImpl(std::unique_ptr<EventLoopImpl> impl) {
eventLoopImpl = std::move(impl);
}

class EventLoopPrivate {
public:
EventLoopPrivate() {
if (!eventLoopImpl) {
FCITX_WARN() << "Using stub event loop implementation.";
eventLoopImpl = std::make_shared<EventLoopImpl>();
}
impl_ = eventLoopImpl;
}
~EventLoopPrivate() {}

std::shared_ptr<EventLoopImpl> impl_;
};

EventLoop::EventLoop() : d_ptr(std::make_unique<EventLoopPrivate>()) {}

EventLoop::~EventLoop() = default;

const char *EventLoop::impl() { return "stub"; }

void *EventLoop::nativeHandle() { return nullptr; }

bool EventLoop::exec() { return true; }

void EventLoop::exit() {}

std::unique_ptr<EventSourceIO> EventLoop::addIOEvent(int fd, IOEventFlags flags,
IOCallback callback) {
FCITX_D();
return d->impl_->addIOEvent(fd, flags, std::move(callback));
}

std::unique_ptr<EventSourceTime>
EventLoop::addTimeEvent(clockid_t clock, uint64_t usec, uint64_t accuracy,
TimeCallback callback) {
FCITX_D();
return d->impl_->addTimeEvent(clock, usec, accuracy, std::move(callback));
}

std::unique_ptr<EventSource> EventLoop::addExitEvent(EventCallback callback) {
FCITX_D();
return d->impl_->addExitEvent(std::move(callback));
}

std::unique_ptr<EventSource> EventLoop::addDeferEvent(EventCallback callback) {
FCITX_D();
return d->impl_->addDeferEvent(std::move(callback));
}

std::unique_ptr<EventSource> EventLoop::addPostEvent(EventCallback callback) {
FCITX_D();
return d->impl_->addPostEvent(std::move(callback));
}

std::unique_ptr<EventSourceIO>
EventLoopImpl::addIOEvent(int fd, IOEventFlags flags, IOCallback callback) {
FCITX_UNUSED(fd);
FCITX_UNUSED(flags);
FCITX_UNUSED(callback);
return std::make_unique<StubEventSourceIO>();
}

std::unique_ptr<EventSourceTime>
EventLoopImpl::addTimeEvent(clockid_t clock, uint64_t usec, uint64_t accuracy,
TimeCallback callback) {
FCITX_UNUSED(clock);
FCITX_UNUSED(usec);
FCITX_UNUSED(accuracy);
FCITX_UNUSED(callback);
return std::make_unique<StubEventSourceTime>();
}

std::unique_ptr<EventSource>
EventLoopImpl::addExitEvent(EventCallback callback) {
FCITX_UNUSED(callback);
return std::make_unique<StubEventSource>();
}

std::unique_ptr<EventSource>
EventLoopImpl::addDeferEvent(EventCallback callback) {
FCITX_UNUSED(callback);
return std::make_unique<StubEventSource>();
}

std::unique_ptr<EventSource>
EventLoopImpl::addPostEvent(EventCallback callback) {
FCITX_UNUSED(callback);
return std::make_unique<StubEventSource>();
}
} // namespace fcitx
2 changes: 2 additions & 0 deletions src/modules/spell/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ install(FILES "${CMAKE_CURRENT_BINARY_DIR}/spell.conf" DESTINATION "${FCITX_INST
COMPONENT config)
fcitx5_export_module(Spell TARGET spell BUILD_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}" HEADERS spell_public.h INSTALL)

if (BUILD_SPELL_DICT)
set(DICT_COMP_SRC
comp_spell_dict.cpp
)
Expand Down Expand Up @@ -46,3 +47,4 @@ add_custom_command(
"${SPELL_EN_DICT_SRC}" "${SPELL_EN_DICT}")
add_custom_target(spell_en_dict ALL DEPENDS "${SPELL_EN_DICT}")
install(FILES "${SPELL_EN_DICT}" DESTINATION "${FCITX_INSTALL_PKGDATADIR}/spell")
endif()
Loading