feat(rustadapter): using rifgen and flapigen-rs to generate interface
This commit is contained in:
parent
69b9f018b7
commit
e292058b5c
15 changed files with 1724 additions and 30 deletions
20
rust-adapter/.gitignore
vendored
Normal file
20
rust-adapter/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# Created by https://www.toptal.com/developers/gitignore/api/rust
|
||||
# Edit at https://www.toptal.com/developers/gitignore?templates=rust
|
||||
|
||||
### Rust ###
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
|
||||
# End of https://www.toptal.com/developers/gitignore/api/rust
|
||||
21
rust-adapter/Cargo.toml
Normal file
21
rust-adapter/Cargo.toml
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
[package]
|
||||
name = "rust-adapter"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
name = "rustadapter"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
jni = "0.21"
|
||||
rifgen = "0.1"
|
||||
popequer_rust = { path = "../../../popequer_rust" }
|
||||
jni-sys = "0.3"
|
||||
log = "0.4.20"
|
||||
|
||||
[build-dependencies]
|
||||
rifgen = "0.1"
|
||||
flapigen = "0.6.0-pre9"
|
||||
|
||||
[target.'cfg(target_os = "android")'.dependencies]
|
||||
9
rust-adapter/README.md
Normal file
9
rust-adapter/README.md
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# Rust Android JNI adapter
|
||||
|
||||
## how to build
|
||||
|
||||
```
|
||||
export ANDROID_NDK_HOME=~/Android/Sdk/ndk/26.1.10909125/
|
||||
cargo ndk -t arm64-v8a -o ../app/src/main/jniLibs build
|
||||
```
|
||||
|
||||
29
rust-adapter/build.rs
Normal file
29
rust-adapter/build.rs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
use std::path::Path;
|
||||
use std::env;
|
||||
|
||||
fn main() {
|
||||
let source_folder = "/mnt/extramedia3/mbess/workspace/popequer/android_app/rust-adapter/src"; //use your projects folder
|
||||
let out_file = "/mnt/extramedia3/mbess/workspace/popequer/android_app/rust-adapter/glue.rs";
|
||||
rifgen::Generator::new(rifgen::TypeCases::CamelCase, rifgen::Language::Java,source_folder)
|
||||
.generate_interface(out_file);
|
||||
|
||||
let swig_gen = flapigen::Generator::new(flapigen::LanguageConfig::JavaConfig(
|
||||
flapigen::JavaConfig::new(
|
||||
Path::new("../app")
|
||||
.join("src")
|
||||
.join("main")
|
||||
.join("java")
|
||||
.join("net")
|
||||
.join("mbess")
|
||||
.join("popequer"),
|
||||
"net.mbess.popequer".into(),
|
||||
)
|
||||
.use_null_annotation_from_package("android.support.annotation".into()),
|
||||
))
|
||||
.rustfmt_bindings(true);
|
||||
|
||||
let out_dir = env::var("OUT_DIR").unwrap();
|
||||
let in_src = Path::new("./").join("glue.rs");
|
||||
let out_src = Path::new(&out_dir).join("java_glue.rs");
|
||||
swig_gen.expand("android bindings", &in_src, &out_src);
|
||||
}
|
||||
13
rust-adapter/glue.rs
Normal file
13
rust-adapter/glue.rs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
//Automatically generated by rifgen
|
||||
use crate::*;
|
||||
use jni_sys::*;
|
||||
foreign_class!(
|
||||
class Foo {
|
||||
self_type Foo;
|
||||
constructor Foo::new(val : i32)->Foo;
|
||||
fn Foo::f(& self , a : i32 , b : i32)->i32; alias f;
|
||||
fn Foo::get_data(& self)->i32; alias getData;
|
||||
# [doc = "Custom doc comment"]
|
||||
fn Foo::set_field(& mut self , v : i32); alias setField;
|
||||
}
|
||||
);
|
||||
1472
rust-adapter/java_glue.rs
Normal file
1472
rust-adapter/java_glue.rs
Normal file
File diff suppressed because it is too large
Load diff
59
rust-adapter/src/_old.rs
Normal file
59
rust-adapter/src/_old.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
#[no_mangle]
|
||||
pub extern "system" fn Java_net_mbess_popequer_Native_add(
|
||||
_env: JNIEnv,
|
||||
_class: JClass,
|
||||
a: i32,
|
||||
b: i32
|
||||
) -> i32 {
|
||||
a + b
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_net_mbess_popequer_Native_mul(
|
||||
_env: JNIEnv,
|
||||
_class: JClass,
|
||||
a: i32,
|
||||
b: i32
|
||||
) -> i32 {
|
||||
a * b
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_net_mbess_popequer_Native_fsInfo<'local>(
|
||||
mut env: JNIEnv<'local>,
|
||||
_class: JClass<'local>,
|
||||
jinput: JString
|
||||
) -> jstring {
|
||||
let input: String = env.get_string(&jinput).unwrap().into();
|
||||
let paths = std::fs::read_dir(format!("/{}", input));
|
||||
dbg!(&input);
|
||||
let mut out = String::from("");
|
||||
for path in paths.unwrap() {
|
||||
out.push_str(&format!("{:?}\n", path.unwrap().path()))
|
||||
}
|
||||
let output = env.new_string(format!("FS test {:?}", out))
|
||||
.expect("Couldn't create java string!");
|
||||
|
||||
output.into_raw()
|
||||
}
|
||||
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_net_mbess_popequer_Native_entryPoint<'local>(
|
||||
mut env: JNIEnv<'local>,
|
||||
_class: JClass<'local>,
|
||||
jinput: JString
|
||||
) -> jstring {
|
||||
let input: String = env.get_string(&jinput).unwrap().into();
|
||||
let paths = std::fs::read_dir(format!("/{}", input));
|
||||
dbg!(&input);
|
||||
let mut out = String::from("");
|
||||
for path in paths.unwrap() {
|
||||
out.push_str(&format!("{:?}\n", path.unwrap().path()))
|
||||
}
|
||||
let output = env.new_string(format!("FS test {:?}", out))
|
||||
.expect("Couldn't create java string!");
|
||||
|
||||
output.into_raw()
|
||||
}
|
||||
|
||||
15
rust-adapter/src/java_glue.rs
Normal file
15
rust-adapter/src/java_glue.rs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#![allow(
|
||||
clippy::enum_variant_names,
|
||||
clippy::unused_unit,
|
||||
clippy::let_and_return,
|
||||
clippy::not_unsafe_ptr_arg_deref,
|
||||
clippy::cast_lossless,
|
||||
clippy::blacklisted_name,
|
||||
clippy::too_many_arguments,
|
||||
clippy::trivially_copy_pass_by_ref,
|
||||
clippy::let_unit_value,
|
||||
clippy::clone_on_copy
|
||||
)]
|
||||
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/java_glue.rs"));
|
||||
33
rust-adapter/src/lib.rs
Normal file
33
rust-adapter/src/lib.rs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
mod java_glue;
|
||||
|
||||
pub use crate::java_glue::*;
|
||||
|
||||
use rifgen::rifgen_attr::*;
|
||||
|
||||
struct Foo {
|
||||
data: i32
|
||||
}
|
||||
|
||||
impl Foo {
|
||||
#[generate_interface(constructor)]
|
||||
fn new(val: i32) -> Foo {
|
||||
Foo{data: val}
|
||||
}
|
||||
|
||||
#[generate_interface]
|
||||
fn f(&self, a: i32, b: i32) -> i32 {
|
||||
self.data + a + b
|
||||
}
|
||||
|
||||
#[generate_interface]
|
||||
fn get_data(&self) -> i32 {
|
||||
self.data
|
||||
}
|
||||
|
||||
///Custom doc comment
|
||||
#[generate_interface]
|
||||
fn set_field(&mut self, v: i32) {
|
||||
self.data = v;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue