Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android glue #13

Merged
merged 17 commits into from
Apr 20, 2020
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 {
Copy link
Contributor

@katyo katyo Feb 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be it will the better to implement proc_macro instead.
So using attribute macros the usage may looks like the follow:

#[ndk::entry]
fn main() {
// ....
}

For example you can see cortex-m-rt entry macro implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that may be a good improvement, but I'm getting some PR fatigue, can we leave it for a follow up?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, don't worry :)

($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