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
54
app/src/main/java/net/mbess/popequer/Foo.java
Normal file
54
app/src/main/java/net/mbess/popequer/Foo.java
Normal file
|
@ -0,0 +1,54 @@
|
|||
// Automatically generated by flapigen
|
||||
package net.mbess.popequer;
|
||||
|
||||
|
||||
public final class Foo {
|
||||
|
||||
public Foo(int val) {
|
||||
mNativeObj = init(val);
|
||||
}
|
||||
private static native long init(int val);
|
||||
|
||||
public final int f(int a, int b) {
|
||||
int ret = do_f(mNativeObj, a, b);
|
||||
|
||||
return ret;
|
||||
}
|
||||
private static native int do_f(long self, int a, int b);
|
||||
|
||||
public final int getData() {
|
||||
int ret = do_getData(mNativeObj);
|
||||
|
||||
return ret;
|
||||
}
|
||||
private static native int do_getData(long self);
|
||||
/**
|
||||
* Custom doc comment
|
||||
*/
|
||||
public final void setField(int v) {
|
||||
do_setField(mNativeObj, v);
|
||||
}
|
||||
private static native void do_setField(long self, int v);
|
||||
|
||||
public synchronized void delete() {
|
||||
if (mNativeObj != 0) {
|
||||
do_delete(mNativeObj);
|
||||
mNativeObj = 0;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
try {
|
||||
delete();
|
||||
}
|
||||
finally {
|
||||
super.finalize();
|
||||
}
|
||||
}
|
||||
private static native void do_delete(long me);
|
||||
/*package*/ Foo(InternalPointerMarker marker, long ptr) {
|
||||
assert marker == InternalPointerMarker.RAW_PTR;
|
||||
this.mNativeObj = ptr;
|
||||
}
|
||||
/*package*/ long mNativeObj;
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
|
||||
// Automatically generated by flapigen
|
||||
package net.mbess.popequer;
|
||||
|
||||
/*package*/ enum InternalPointerMarker {
|
||||
RAW_PTR;
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
|
||||
// Automatically generated by flapigen
|
||||
package net.mbess.popequer;
|
||||
|
||||
/*package*/ final class JNIReachabilityFence {
|
||||
private JNIReachabilityFence() {}
|
||||
/*package*/ static native void reachabilityFence1(Object ref1);
|
||||
/*package*/ static native void reachabilityFence2(Object ref1, Object ref2);
|
||||
/*package*/ static native void reachabilityFence3(Object ref1, Object ref2, Object ref3);
|
||||
/*package*/ static native void reachabilityFence4(Object ref1, Object ref2, Object ref3, Object ref4);
|
||||
/*package*/ static native void reachabilityFence5(Object ref1, Object ref2, Object ref3, Object ref4, Object ref5);
|
||||
/*package*/ static native void reachabilityFence6(Object ref1, Object ref2, Object ref3, Object ref4, Object ref5, Object ref6);
|
||||
/*package*/ static native void reachabilityFence7(Object ref1, Object ref2, Object ref3, Object ref4, Object ref5, Object ref6, Object ref7);
|
||||
/*package*/ static native void reachabilityFence8(Object ref1, Object ref2, Object ref3, Object ref4, Object ref5, Object ref6, Object ref7, Object ref8);}
|
|
@ -1,8 +0,0 @@
|
|||
package net.mbess.popequer
|
||||
|
||||
object Native {
|
||||
init {
|
||||
System.loadLibrary("rustsandbox")
|
||||
}
|
||||
external fun add(a: Int, b: Int): Int
|
||||
}
|
|
@ -14,16 +14,21 @@ import kotlinx.coroutines.CoroutineScope
|
|||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import net.mbess.popequer.AppContext
|
||||
import net.mbess.popequer.Native
|
||||
import net.mbess.popequer.Foo
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
Native.add(143, 54).let {
|
||||
println("From foreign rust result: $it")
|
||||
}
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
val appContext = AppContext(this)
|
||||
//Native.fsInfo(appContext.gitActions.cloneFolder.absolutePath).let {
|
||||
// Log.d("MainActivity", "From foreign rust result fsInfo $it")
|
||||
//}
|
||||
System.loadLibrary("rustadapter")
|
||||
val foo_instance = Foo(10)
|
||||
Log.d("MainActivity", "From rust: " + foo_instance.f(32, 43))
|
||||
foo_instance.setField(154)
|
||||
Log.d("MainActivity", "${foo_instance.data}")
|
||||
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
Looper.prepare()
|
||||
|
|
|
@ -1,14 +1,21 @@
|
|||
[package]
|
||||
name = "rust-sandbox"
|
||||
name = "rust-adapter"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
name = "rustsandbox"
|
||||
name = "rustadapter"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
jni = "0.21.1"
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
use jni::{
|
||||
objects::{JClass, JObject},
|
||||
JNIEnv,
|
||||
};
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_net_mbess_popequer_Native_add(
|
||||
_env: JNIEnv,
|
||||
_class: JClass,
|
||||
a: i32,
|
||||
b: i32
|
||||
) -> i32 {
|
||||
a + b
|
||||
}
|
||||
|
Loading…
Reference in a new issue