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

Add wasm support #754

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 25 additions & 0 deletions .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: WASM CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Install emsdk
run: |
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest
- name: Build wasm
run: |
. emsdk/emsdk_env.sh
bash wasm/build.sh
12 changes: 8 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
-std=c++14
-Wall
)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
if (NOT EMSCRIPTEN)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
endif ()
if (CMAKE_BUILD_TYPE MATCHES Debug)
add_definitions(-O0 -g3)
endif ()
Expand Down Expand Up @@ -196,9 +198,11 @@ endif()
######## Subdirectories

add_subdirectory(src)
add_subdirectory(doc)
add_subdirectory(data)
add_subdirectory(test)
if (NOT EMSCRIPTEN)
add_subdirectory(doc)
add_subdirectory(data)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那 txt 转 ocd2 的工作如何在编译时进行?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasm/build.sh里,先本地编译,然后把ocd2产物共享给wasm

add_subdirectory(test)
endif ()

######## Testing

Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[![C/C++ CI](https://github.com/BYVoid/OpenCC/actions/workflows/cmake.yml/badge.svg)](https://github.com/BYVoid/OpenCC/actions/workflows/cmake.yml)
[![Node.js CI](https://github.com/BYVoid/OpenCC/actions/workflows/nodejs.yml/badge.svg)](https://github.com/BYVoid/OpenCC/actions/workflows/nodejs.yml)
[![Python CI](https://github.com/BYVoid/OpenCC/actions/workflows/python.yml/badge.svg)](https://github.com/BYVoid/OpenCC/actions/workflows/python.yml)
[![WASM CI](https://github.com/BYVoid/OpenCC/actions/workflows/wasm.yml/badge.svg)](https://github.com/BYVoid/OpenCC/actions/workflows/wasm.yml)

## Introduction 介紹

Expand Down Expand Up @@ -99,6 +100,10 @@ int main() {

```

### WASM

See [wasm.yml](https://github.com/BYVoid/OpenCC/blob/master/.github/workflows/wasm.yml).

Document 文檔: https://byvoid.github.io/OpenCC/

### Command Line
Expand All @@ -115,7 +120,7 @@ Document 文檔: https://byvoid.github.io/OpenCC/
* Android: [android-opencc](https://github.com/qichuan/android-opencc)
* PHP: [opencc4php](https://github.com/nauxliu/opencc4php)
* Pure JavaScript: [opencc-js](https://github.com/nk2028/opencc-js)
* WebAssembly: [wasm-opencc](https://github.com/oyyd/wasm-opencc)
* asm.js: [wasm-opencc](https://github.com/oyyd/wasm-opencc)
* Browser Extension: [opencc-extension](https://github.com/tnychn/opencc-extension)
* Go (Pure): [OpenCC for Go](https://github.com/longbridgeapp/opencc)

Expand Down Expand Up @@ -279,5 +284,6 @@ All these libraries are statically linked by default.
* [Prcuvu](https://github.com/Prcuvu)
* [Tony Able](https://github.com/TonyAble)
* [Xiao Liang](https://github.com/yxliang01)
* [Qijia Liu](https://github.com/eagleoflqj)

Please feel free to update this list if you have contributed OpenCC.
3 changes: 3 additions & 0 deletions wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
demo.data
demo.js
demo.wasm
35 changes: 35 additions & 0 deletions wasm/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
root=$PWD

# build data and share with wasm
cmake . -B build/native \
-DCMAKE_INSTALL_PREFIX:PATH=$root/build/sysroot/usr
make -C build/native/data install

# build wasm libopencc.a and libmarisa.a
emcmake cmake . -B build/wasm \
-DBUILD_SHARED_LIBS:BOOL=OFF \
-DCMAKE_BUILD_TYPE:STRING="Release" \
-DCMAKE_INSTALL_PREFIX:PATH=/usr
make DESTDIR=$root/build/sysroot -C build/wasm install
cp build/wasm/deps/marisa-0.2.6/libmarisa.a build/sysroot/usr/lib

# build demo.js
em++ -std=c++14 \
-I build/sysroot/usr/include/opencc \
--preload-file build/sysroot/usr/share/opencc@/usr/share/opencc \
-o wasm/demo.js \
wasm/demo.cpp \
-L build/sysroot/usr/lib \
-l opencc -l marisa

# test demo.js
expected="網路"
actual=$(cd wasm; node demo.js)
if [[ $actual != $expected ]]; then
echo Test failed.
echo expected: $expected
echo actual: $actual
exit 1
else
echo Test passed.
fi
8 changes: 8 additions & 0 deletions wasm/demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <iostream>
#include "opencc.h"

int main() {
const opencc::SimpleConverter converter("s2twp.json");
std::cout << converter.Convert("网络") << std::endl; // 網路
return 0;
}