Skip to content

Commit e0404d4

Browse files
rockaiovxw
andauthored
rust libchewing v0.11.0 (#39)
Co-authored-by: iovxw <iovxw@outlook.com>
1 parent 3ad4247 commit e0404d4

File tree

5 files changed

+119
-66
lines changed

5 files changed

+119
-66
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ jobs:
1414
NDK_VERSION: "28.0.13004108"
1515
CMAKE_VERSION: "3.31.6"
1616
ANDROID_PLATFORM: "23"
17+
RUST_VERSION: "1.92.0"
1718

1819
steps:
1920
- name: Install build dependencies
2021
# android-actions/setup-android needs unzip
2122
# sdkmanager needs jre
2223
# libime needs boost (build host tools)
2324
# opencc needs python (scripts)
25+
# libchewing needs rustup (build)
2426
# libiconv needs gperf (build)
2527
run: |
2628
pacman -Syu --needed --noconfirm \
@@ -29,6 +31,7 @@ jobs:
2931
cmake extra-cmake-modules ninja \
3032
boost \
3133
python \
34+
rustup \
3235
gperf
3336
3437
- name: Fetch source code
@@ -44,6 +47,8 @@ jobs:
4447
git config --global --add safe.directory '*'
4548
# fetch libime submodule kenlm since it's not in .gitmodules
4649
git submodule update --init --recursive libime
50+
# fetch libchewing-data submodule
51+
git submodule update --init --recursive libchewing
4752
4853
- name: Setup Android NDK
4954
uses: android-actions/setup-android@v3
@@ -53,6 +58,7 @@ jobs:
5358
- name: Build everything
5459
env:
5560
ANDROID_NDK_ROOT: ${{ env.ANDROID_HOME }}/ndk/${{ env.NDK_VERSION }}
61+
RUST_VERSION: ${{ env.RUST_VERSION }}
5662
run: |
5763
# use archlinux's dynamic only ghc
5864
cabal configure --disable-library-vanilla --enable-shared --enable-executable-dynamic --ghc-options=-dynamic

libchewing

Submodule libchewing updated 202 files

patches/libchewing.patch

Lines changed: 76 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,85 @@
11
diff --git a/CMakeLists.txt b/CMakeLists.txt
2-
index ae1d14b7..5d03df35 100644
2+
index 59e012d8..48dca4c0 100644
33
--- a/CMakeLists.txt
44
+++ b/CMakeLists.txt
5-
@@ -1,2 +1,2 @@
6-
-cmake_minimum_required(VERSION 3.24.0)
7-
+cmake_minimum_required(VERSION 3.22.1)
8-
project(libchewing LANGUAGES C)
9-
@@ -216,8 +216,6 @@ if(WITH_RUST)
10-
list(APPEND ALL_INC ${INC_DIR}/chewing_rs.h)
11-
endif()
12-
13-
-add_subdirectory(doc)
14-
-add_subdirectory(data)
5+
@@ -77,7 +77,7 @@ corrosion_import_crate(MANIFEST_PATH Cargo.toml CRATES chewing_capi)
156
if(BUILD_TESTING)
16-
add_subdirectory(tests)
7+
corrosion_import_crate(MANIFEST_PATH Cargo.toml CRATES chewing_testhelper)
178
endif()
18-
@@ -228,14 +226,20 @@ add_library(common OBJECT
9+
-corrosion_import_crate(MANIFEST_PATH Cargo.toml CRATES chewing-cli)
10+
+corrosion_add_target_rustflags(chewing_capi "--remap-path-prefix=${PROJECT_SOURCE_DIR}=.")
1911

20-
src/porting_layer/src/asprintf.c
21-
)
22-
+target_compile_options(common PRIVATE
23-
+ "-ffile-prefix-map=${CMAKE_SOURCE_DIR}=."
24-
+)
25-
target_compile_definitions(common PRIVATE
26-
- CHEWING_DATADIR=\"${CMAKE_INSTALL_FULL_DATADIR}/libchewing\"
27-
+ CHEWING_DATADIR=\"./libchewing\"
12+
if(WITH_SQLITE3)
13+
corrosion_set_features(chewing_capi FEATURES sqlite)
14+
@@ -191,7 +191,7 @@ target_include_directories(libchewing
2815
)
2916

30-
add_library(libchewing ${ALL_INC} src/chewing.c)
31-
set_target_properties(libchewing PROPERTIES LINKER_LANGUAGE C)
32-
+target_compile_options(libchewing PRIVATE
33-
+ "-ffile-prefix-map=${CMAKE_SOURCE_DIR}=."
34-
+)
35-
target_compile_definitions(libchewing PRIVATE
36-
- CHEWING_DATADIR=\"${CMAKE_INSTALL_FULL_DATADIR}/libchewing\"
37-
+ CHEWING_DATADIR=\"./libchewing\"
17+
corrosion_set_env_vars(chewing_capi
18+
- CHEWING_DATADIR=${CMAKE_INSTALL_FULL_DATADIR}/libchewing
19+
+ CHEWING_DATADIR="./libchewing"
3820
)
39-
if(NOT WITH_RUST)
40-
target_sources(common PRIVATE
41-
@@ -288,6 +292,9 @@ if(NOT WITH_RUST)
42-
${SRC_DIR}/userphrase-hash.c
43-
)
44-
endif()
45-
+ target_compile_options(userphrase PRIVATE
46-
+ "-ffile-prefix-map=${CMAKE_SOURCE_DIR}=."
47-
+ )
48-
target_link_libraries(libchewing
49-
PRIVATE common
50-
PRIVATE userphrase)
21+
target_link_libraries(libchewing PRIVATE chewing_capi)
22+
target_link_libraries(chewing_capi INTERFACE ${SQLite3_LIBRARIES})
23+
@@ -239,15 +239,17 @@ else()
24+
)
25+
endif()
26+
27+
-install(FILES ${ALL_INC} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/chewing)
28+
+install(FILES ${ALL_INC} DESTINATION include/chewing)
29+
install(FILES ${PROJECT_BINARY_DIR}/chewing.pc
30+
- DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
31+
-install(TARGETS libchewing
32+
+ DESTINATION lib/pkgconfig)
33+
+install(TARGETS chewing_capi
34+
EXPORT libchewingTargets
35+
- ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
36+
- INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
37+
- LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
38+
- RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
39+
+ ARCHIVE DESTINATION lib
40+
+ INCLUDES DESTINATION include
41+
+ LIBRARY DESTINATION lib
42+
+ RUNTIME DESTINATION bin)
43+
+install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libchewing_capi.a
44+
+ DESTINATION lib)
45+
46+
if(BUILD_DATA)
47+
install(
48+
@@ -261,7 +263,6 @@ if(BUILD_DATA)
49+
)
50+
endif()
51+
52+
-install(IMPORTED_RUNTIME_ARTIFACTS chewing-cli DESTINATION ${CMAKE_INSTALL_BINDIR})
53+
54+
# generate CMake Config files
55+
include(CMakePackageConfigHelpers)
56+
diff --git a/Cargo.toml b/Cargo.toml
57+
index d546d772..448f6b1d 100644
58+
--- a/Cargo.toml
59+
+++ b/Cargo.toml
60+
@@ -41,10 +41,10 @@ tempfile = "3.10.1"
61+
62+
[profile.release]
63+
lto = true
64+
-opt-level = 3
65+
+opt-level = "s"
66+
panic = "abort"
67+
codegen-units = 1
68+
-debug = true
69+
+debug = false
70+
71+
[package.metadata.docs.rs]
72+
features = ["sqlite"]
73+
diff --git a/capi/Cargo.toml b/capi/Cargo.toml
74+
index 517dd9e2..12fefb9e 100644
75+
--- a/capi/Cargo.toml
76+
+++ b/capi/Cargo.toml
77+
@@ -7,7 +7,7 @@ rust-version = "1.88"
78+
edition = "2024"
79+
80+
[lib]
81+
-crate-type = ["rlib", "staticlib"]
82+
+crate-type = ["staticlib"]
83+
84+
[dependencies]
85+
chewing = { version = "0.11.0", path = ".." }

src/Main.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ data ToolchainVersions = ToolchainVersions
177177
{ prebuilderRev :: String,
178178
ndkVersion :: String,
179179
platformVersion :: Int,
180-
cmakeVersion :: String
180+
cmakeVersion :: String,
181+
rustVersion :: String
181182
}
182183

183184
instance A.ToJSON ToolchainVersions where
@@ -186,7 +187,8 @@ instance A.ToJSON ToolchainVersions where
186187
[ fromString "prebuilder" A..= prebuilderRev,
187188
fromString "ndk" A..= ndkVersion,
188189
fromString "platform" A..= platformVersion,
189-
fromString "cmake" A..= cmakeVersion
190+
fromString "cmake" A..= cmakeVersion,
191+
fromString "rust" A..= rustVersion
190192
]
191193

192194
getToolchainVersions :: Action ToolchainVersions
@@ -200,4 +202,5 @@ getToolchainVersions = do
200202
pure $ dropWhileEnd (== ' ') ndkVersion
201203
| otherwise -> fail "Failed to parse Pkg.Revision"
202204
Nothing -> fail "Pkg.Revision not found in source.properties"
205+
rustVersion <- fromMaybeM (fail "RUST_VERSION not set") (getEnv "RUST_VERSION")
203206
pure ToolchainVersions {..}

src/Rules/LibChewing.hs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,34 @@ libchewingRule = do
2323
useCMake $
2424
(cmakeBuilder "libchewing")
2525
{ preBuild = BuildAction $ \_ src -> do
26-
-- CMakeLists is changed in last build
26+
-- install rust toolchain
27+
cmd_ Shell "rustup toolchain install $RUST_VERSION"
28+
-- CMakeLists.txt changed in last build
2729
cmd_ (Cwd src) Shell "git checkout ."
28-
-- skip data and shared lib
29-
-- merge libuserphrase.a into libchewing.a
30-
-- remove absolute path by CHEWING_DATADIR macro
31-
-- remove absolute path by __FILE__ macro
30+
-- disable data/doc, remove absolute path, optimize library size
3231
cmd_ (Cwd src) "git apply ../patches/libchewing.patch",
33-
cmakeFlags =
34-
const
35-
[ "-DBUILD_SHARED_LIBS=OFF",
36-
"-DBUILD_TESTING=OFF",
37-
"-DWITH_SQLITE3=OFF",
38-
"-DWITH_RUST=OFF"
39-
]
32+
preBuildEachABI = BuildActionABI $ \_ env -> do
33+
-- install rust target for this abi
34+
let targetName = case (buildEnvABI env) of
35+
"armeabi-v7a" -> "armv7-linux-androideabi"
36+
"arm64-v8a" -> "aarch64-linux-android"
37+
"x86" -> "i686-linux-android"
38+
"x86_64" -> "x86_64-linux-android"
39+
_ -> fail "Unknown Android ABI"
40+
cmd_ "rustup" "target" "add" targetName,
41+
cmakeFlags =
42+
const
43+
[ "-DBUILD_SHARED_LIBS=OFF",
44+
"-DBUILD_TESTING=OFF",
45+
"-DBUILD_DATA=OFF",
46+
"-DBUILD_DOC=OFF",
47+
"-DWITH_SQLITE3=OFF"
48+
]
4049
}
4150

4251
"chewing-dict" ~> do
52+
-- install rust
53+
cmd_ Shell "rustup toolchain install $RUST_VERSION"
4354
let libchewingBuildHost = outputDir </> "libchewing-build-host"
4455
dictSrcDir = libchewingBuildHost </> "data"
4556
cmd_ (Cwd libchewingSrc) Shell "git checkout ."
@@ -49,25 +60,23 @@ libchewingRule = do
4960
libchewingBuildHost
5061
"-G"
5162
"Ninja"
52-
[ "-DBUILD_SHARED_LIBS=OFF",
53-
"-DBUILD_TESTING=OFF",
63+
[ "-DBUILD_TESTING=OFF",
5464
"-DWITH_SQLITE3=OFF",
55-
"-DWITH_RUST=OFF"
65+
"-DBUILD_DOC=OFF"
5666
]
5767
libchewingSrc
5868
cmd_
5969
"cmake"
6070
"--build"
6171
libchewingBuildHost
6272
"--target"
63-
[ "data",
64-
"all_static_data"
73+
[ "dict_chewing",
74+
"misc"
6575
]
66-
copyFile' (dictSrcDir </> "dictionary.dat") (dictOutputDir </> "dictionary.dat")
67-
copyFile' (dictSrcDir </> "index_tree.dat") (dictOutputDir </> "index_tree.dat")
68-
copyFile' (dictSrcDir </> "pinyin.tab") (dictOutputDir </> "pinyin.tab")
69-
copyFile' (dictSrcDir </> "swkb.dat") (dictOutputDir </> "swkb.dat")
70-
copyFile' (dictSrcDir </> "symbols.dat") (dictOutputDir </> "symbols.dat")
76+
copyFile' (dictSrcDir </> "dict" </> "chewing" </> "tsi.dat") (dictOutputDir </> "tsi.dat")
77+
copyFile' (dictSrcDir </> "dict" </> "chewing" </> "word.dat") (dictOutputDir </> "word.dat")
78+
copyFile' (dictSrcDir </> "misc" </> "swkb.dat") (dictOutputDir </> "swkb.dat")
79+
copyFile' (dictSrcDir </> "misc" </> "symbols.dat") (dictOutputDir </> "symbols.dat")
7180

7281
"libchewing" ~> do
7382
need ["chewing-dict"]

0 commit comments

Comments
 (0)