Skip to content

Commit

Permalink
Add ndk_glue macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvc94ch committed Feb 19, 2020
1 parent a5c2576 commit 8249edd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
18 changes: 3 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,14 @@

```toml
[lib]
crate-type = ["cdylib"]
crate-type = ["lib", "cdylib"]
```

```rust
#[cfg(target_os = "android")]
#[no_mangle]
pub unsafe extern "C" fn ANativeActivity_onCreate(
activity: *mut std::os::raw::c_void,
saved_state: *mut std::os::raw::c_void,
saved_state_size: usize,
) {
ndk_glue::init(
activity as _,
saved_state as _,
saved_state_size as _,
app_main,
);
}
ndk_glue::ndk_glue!(main);

pub fn app_main() {
fn main() {
println!("hello world");
}
```
Expand Down
17 changes: 2 additions & 15 deletions ndk-examples/examples/hello_world.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
#[cfg(target_os = "android")]
#[no_mangle]
pub unsafe extern "C" fn ANativeActivity_onCreate(
activity: *mut std::os::raw::c_void,
saved_state: *mut std::os::raw::c_void,
saved_state_size: usize,
) {
std::env::set_var("RUST_BACKTRACE", "1");
ndk_glue::init(
activity as _,
saved_state as _,
saved_state_size as _,
app_main,
);
}
ndk_glue::ndk_glue!(main);

pub fn app_main() {
fn main() {
println!("hello world");
}
20 changes: 20 additions & 0 deletions ndk-glue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ use std::ptr::NonNull;
use std::sync::{RwLock, RwLockReadGuard};
use std::thread;

#[macro_export]
macro_rules! ndk_glue {
($main:ident) => {
#[no_mangle]
unsafe extern "C" fn ANativeActivity_onCreate(
activity: *mut std::os::raw::c_void,
saved_state: *mut std::os::raw::c_void,
saved_state_size: usize,
) {
std::env::set_var("RUST_BACKTRACE", "1");
$crate::init(
activity as _,
saved_state as _,
saved_state_size as _,
$main,
);
}
}
}

pub fn android_log(level: Level, tag: &CStr, msg: &CStr) {
use android_log_sys::LogPriority;
let prio = match level {
Expand Down

0 comments on commit 8249edd

Please sign in to comment.