Skip to content

Commit 120e2ae

Browse files
authored
Fix: GitHub CI. Feature: JNI Binding
1 parent 5d81fd6 commit 120e2ae

File tree

3 files changed

+62
-26
lines changed

3 files changed

+62
-26
lines changed

.github/workflows/build.yaml

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
run: |
5454
rustup target add --toolchain nightly "${{ matrix.target }}"
5555
rustup component add rust-src --toolchain "nightly-$(rustc --print host-tuple)"
56-
cargo +nightly build --release --target "${{ matrix.target }}" \
56+
cargo +nightly build --release --bin terracotta --target "${{ matrix.target }}" \
5757
-Z build-std=core,std,alloc,proc_macro,panic_abort \
5858
-Z build-std-features=default,optimize_for_size
5959
env:
@@ -90,25 +90,40 @@ jobs:
9090
os: freebsd
9191
arch: x86_64
9292
target: x86_64-unknown-freebsd
93+
- runs-on: ubuntu-24.04
94+
os: android
95+
target: aarch64-linux-android
9396
runs-on: ${{ matrix.runs-on }}
9497
name: Build (${{ matrix.target }})
9598
steps:
9699
- name: Checkout
97100
uses: actions/checkout@v4
98101
- name: Install GitHub Artifact Client
99102
uses: lhotari/gh-actions-artifact-client@v2
100-
- name: Install musl libc (linux x86_64 | linux arm64)
101-
if: ${{ contains(fromJSON('["x86_64", "arm64"]'), matrix.arch) && matrix.os == 'linux' }}
102-
run: |
103-
sudo apt update
104-
sudo apt install --yes --no-install-recommends build-essential musl-tools musl-dev
105103
- name: Compute Version
106104
run: |
107105
if [ -z "${{ github.event.inputs.version }}" ]; then
108106
echo "TERRACOTTA_VERSION=${GITHUB_SHA::7}" >> $GITHUB_ENV
109107
else
110108
echo "TERRACOTTA_VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
111109
fi
110+
- name: Setup Android SDK (Android)
111+
if: ${{ matrix.os == 'android' }}
112+
uses: android-actions/setup-android@v3
113+
with:
114+
cmdline-tools-version: 11076708
115+
packages: 'build-tools;34.0.0 ndk;26.0.10792818 tools platform-tools platforms;android-34 '
116+
- name: Install cargo-ndk and protobuf-compiler (Android)
117+
if: ${{ matrix.os == 'android' }}
118+
run: |
119+
sudo apt update
120+
sudo apt install --yes --no-install-recommends protobuf-compiler libprotobuf-dev
121+
cargo install cargo-ndk --force
122+
- name: Install musl libc (linux x86_64 | linux arm64)
123+
if: ${{ contains(fromJSON('["x86_64", "arm64"]'), matrix.arch) && matrix.os == 'linux' }}
124+
run: |
125+
sudo apt update
126+
sudo apt install --yes --no-install-recommends build-essential musl-tools musl-dev
112127
- name: Install Zig (linux riscv64 | linux loongarch64)
113128
if: ${{ contains(fromJSON('["riscv64", "loongarch64"]'), matrix.arch) && matrix.os == 'linux' }}
114129
uses: mlugg/setup-zig@v2
@@ -120,7 +135,7 @@ jobs:
120135
run: |
121136
rustup target add --toolchain nightly "${{ matrix.target }}"
122137
rustup component add rust-src --toolchain "nightly-$(rustc --print host-tuple)"
123-
cargo +nightly build --release --target "${{ matrix.target }}" \
138+
cargo +nightly build --release --bin terracotta --target "${{ matrix.target }}" \
124139
-Z build-std=core,std,alloc,proc_macro,panic_abort \
125140
-Z build-std-features=default,optimize_for_size
126141
env:
@@ -129,7 +144,7 @@ jobs:
129144
if: ${{ contains(fromJSON('["riscv64", "loongarch64"]'), matrix.arch) && matrix.os == 'linux' }}
130145
run: |
131146
rustup target add --toolchain nightly "${{ matrix.target }}"
132-
cargo +nightly zigbuild --release --target "${{ matrix.target }}.${{ matrix.glibc-version }}"
147+
cargo +nightly zigbuild --release --bin terracotta --target "${{ matrix.target }}.${{ matrix.glibc-version }}"
133148
- name: Build (freebsd x86_64)
134149
if: ${{ contains(fromJSON('["x86_64"]'), matrix.arch) && matrix.os == 'freebsd' }}
135150
uses: cross-platform-actions/action@v0.29.0
@@ -145,11 +160,28 @@ jobs:
145160
export CXX=clang++
146161
. $HOME/.cargo/env
147162
rustup target add --toolchain nightly "${{ matrix.target }}"
148-
cargo +nightly build --release --target "${{ matrix.target }}"
163+
cargo +nightly build --release --bin terracotta --target "${{ matrix.target }}"
164+
- name: Build (Android)
165+
if: ${{ matrix.os == 'android' }}
166+
run: |
167+
export PATH="$PATH:$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin"
168+
export NDK_HOME="$ANDROID_NDK_HOME"
169+
170+
rustup target add --toolchain nightly "${{ matrix.target }}"
171+
rustup component add rust-src --toolchain "nightly-$(rustc --print host-tuple)"
172+
cargo +nightly ndk build --release --lib --target "${{ matrix.target }}" \
173+
-Z build-std=core,std,alloc,proc_macro,panic_abort \
174+
-Z build-std-features=default,optimize_for_size
149175
- name: Upload Artifact
150176
run: |
151-
source="terracotta-${TERRACOTTA_VERSION}-${{ matrix.os }}-${{ matrix.arch }}"
152-
cp "target/${{ matrix.target }}/release/terracotta" "$source"
177+
if [ "${{ matrix.os }}" = "android" ]; then
178+
source="terracotta-${TERRACOTTA_VERSION}-${{ matrix.os }}-${{ matrix.arch }}.so"
179+
cp "target/${{ matrix.target }}/release/libterracotta.so" "$source"
180+
else
181+
source="terracotta-${TERRACOTTA_VERSION}-${{ matrix.os }}-${{ matrix.arch }}"
182+
cp "target/${{ matrix.target }}/release/terracotta" "$source"
183+
fi
184+
153185
chmod 755 "$source"
154186
tar -cf "$source.tar" "$source"
155187
zip -q - "$source.tar" | gh-actions-artifact-client.js upload -r 90 "$source.tar"
@@ -184,7 +216,7 @@ jobs:
184216
run: |
185217
rustup target add --toolchain nightly "${{ matrix.target }}"
186218
rustup component add rust-src --toolchain "nightly-$(rustc --print host-tuple)"
187-
cargo +nightly build --release --target "${{ matrix.target }}" \
219+
cargo +nightly build --release --bin terracotta --target "${{ matrix.target }}" \
188220
-Z build-std=core,std,alloc,proc_macro,panic_abort \
189221
-Z build-std-features=default,optimize_for_size
190222
env:

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ easytier = { git = "https://github.com/burningtnt/EasyTier.git", branch = "devel
5353
uuid = "1.18.1" # This library are the necessities to interact with EasyTier. DO NOT upgrade their version.
5454
toml = "0.9.8"
5555
tokio = "1.48.0"
56+
jni-sys = "0.4.0"
5657

5758
[build-dependencies]
5859
winresource = "0.1"

src/lib.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::{
2121
thread,
2222
time::SystemTime,
2323
};
24+
use jni_sys::{jint, jobject, JNIEnv};
2425

2526
pub mod controller;
2627
pub mod easytier;
@@ -96,8 +97,12 @@ lazy_static! {
9697
static ref LOGGING_FILE: std::path::PathBuf = WORKING_DIR.join("application.log");
9798
}
9899

99-
#[rocket::main]
100-
async fn main() {
100+
#[no_mangle]
101+
pub extern "system" fn Java_net_burningtnt_terracotta_TerracottaAndroidAPI_start(_env: *mut JNIEnv) -> jint {
102+
run() as jint
103+
}
104+
105+
fn run() -> i16 {
101106
cfg_if::cfg_if! {
102107
if #[cfg(debug_assertions)] {
103108
std::panic::set_backtrace_style(std::panic::BacktraceStyle::Short);
@@ -127,23 +132,21 @@ async fn main() {
127132
env!("CARGO_CFG_TARGET_ENV"),
128133
);
129134

130-
let future = server::server_main(port_callback);
135+
tokio::runtime::Builder::new_multi_thread()
136+
.enable_all()
137+
.build()
138+
.unwrap()
139+
.spawn(server::server_main(port_callback));
140+
131141
thread::spawn(|| {
132142
lazy_static::initialize(&controller::SCAFFOLDING_PORT);
133143
lazy_static::initialize(&easytier::FACTORY);
134144
});
135145

136-
thread::spawn(move || {
137-
let port = port_receiver.recv().unwrap();
138-
if port != 0 {
139-
let _ = open::that(format!("http://127.0.0.1:{}/", port));
140-
}
141-
});
142-
143-
future.await;
144-
let _ = port_callback2.send(0);
145-
146-
easytier::FACTORY.remove();
146+
return match port_receiver.recv() {
147+
Ok(port) => port as i16,
148+
Err(_) => -1,
149+
};
147150
}
148151

149152
fn redirect_std(file: &'static std::path::PathBuf) {

0 commit comments

Comments
 (0)