diff --git a/src/SUMMARY.md b/src/SUMMARY.md index f19fec02..8725bef2 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -260,7 +260,10 @@ - [Logging](android/logging.md) - [Interoperability](android/interoperability.md) - [With C](android/interoperability/with-c.md) - - [Calling C with Bindgen](android/interoperability/with-c/bindgen.md) + - [A Simple C Library](android/interoperability/with-c/c-library.md) + - [Bindgen](android/interoperability/with-c/bindgen.md) + - [Running Our Binary](android/interoperability/with-c/run-our-binary.md) + - [A Simple Rust Library](android/interoperability/with-c/rust-library.md) - [Calling Rust from C](android/interoperability/with-c/rust.md) - [With C++](android/interoperability/cpp.md) - [The Bridge Module](android/interoperability/cpp/bridge.md) diff --git a/src/android/interoperability/with-c/bindgen.md b/src/android/interoperability/with-c/bindgen.md index 3470b6eb..f817f213 100644 --- a/src/android/interoperability/with-c/bindgen.md +++ b/src/android/interoperability/with-c/bindgen.md @@ -3,28 +3,6 @@ The [bindgen](https://rust-lang.github.io/rust-bindgen/introduction.html) tool can auto-generate bindings from a C header file. -First create a small C library: - -_interoperability/bindgen/libbirthday.h_: - -```c -{{#include bindgen/libbirthday.h:card}} -``` - -_interoperability/bindgen/libbirthday.c_: - -```c -{{#include bindgen/libbirthday.c:print_card}} -``` - -Add this to your `Android.bp` file: - -_interoperability/bindgen/Android.bp_: - -```javascript -{{#include bindgen/Android.bp:libbirthday}} -``` - Create a wrapper header file for the library (not strictly needed in this example): @@ -34,8 +12,6 @@ _interoperability/bindgen/libbirthday_wrapper.h_: {{#include bindgen/libbirthday_wrapper.h:include}} ``` -You can now auto-generate the bindings: - _interoperability/bindgen/Android.bp_: ```javascript @@ -56,24 +32,6 @@ _interoperability/bindgen/main.rs_: {{#include bindgen/main.rs:main}} ``` -Build, push, and run the binary on your device: - -```shell -{{#include ../../build_all.sh:print_birthday_card}} -``` - -Finally, we can run auto-generated tests to ensure the bindings work: - -_interoperability/bindgen/Android.bp_: - -```javascript -{{#include bindgen/Android.bp:libbirthday_bindgen_test}} -``` - -```shell -{{#include ../../build_all.sh:libbirthday_bindgen_test}} -``` -
- The Android build rules will automatically call `bindgen` for you behind the diff --git a/src/android/interoperability/with-c/c-library.md b/src/android/interoperability/with-c/c-library.md new file mode 100644 index 00000000..d93042a8 --- /dev/null +++ b/src/android/interoperability/with-c/c-library.md @@ -0,0 +1,23 @@ +# A Simple C Library + +Let's first create a small C library: + +_interoperability/bindgen/libbirthday.h_: + +```c +{{#include bindgen/libbirthday.h:card}} +``` + +_interoperability/bindgen/libbirthday.c_: + +```c +{{#include bindgen/libbirthday.c:print_card}} +``` + +Add this to your `Android.bp` file: + +_interoperability/bindgen/Android.bp_: + +```javascript +{{#include bindgen/Android.bp:libbirthday}} +``` diff --git a/src/android/interoperability/with-c/run-our-binary.md b/src/android/interoperability/with-c/run-our-binary.md new file mode 100644 index 00000000..5ae102f1 --- /dev/null +++ b/src/android/interoperability/with-c/run-our-binary.md @@ -0,0 +1,19 @@ +# Running Our Binary + +Build, push, and run the binary on your device: + +```shell +{{#include ../../build_all.sh:print_birthday_card}} +``` + +Finally, we can run auto-generated tests to ensure the bindings work: + +_interoperability/bindgen/Android.bp_: + +```javascript +{{#include bindgen/Android.bp:libbirthday_bindgen_test}} +``` + +```shell +{{#include ../../build_all.sh:libbirthday_bindgen_test}} +``` diff --git a/src/android/interoperability/with-c/rust-library.md b/src/android/interoperability/with-c/rust-library.md new file mode 100644 index 00000000..7733180f --- /dev/null +++ b/src/android/interoperability/with-c/rust-library.md @@ -0,0 +1,23 @@ +# A Simple Rust Library + +Exporting Rust functions and types to C is easy. Here's a simple Rust library: + +_interoperability/rust/libanalyze/analyze.rs_ + +```rust,editable +{{#include rust/libanalyze/analyze.rs:analyze_numbers}} +``` + +_interoperability/rust/libanalyze/Android.bp_ + +```javascript +{{#include rust/libanalyze/Android.bp}} +``` + +
+ +`#[unsafe(no_mangle)]` disables Rust's usual name mangling, so the exported +symbol will just be the name of the function. You can also use +`#[unsafe(export_name = "some_name")]` to specify whatever name you want. + +
diff --git a/src/android/interoperability/with-c/rust.md b/src/android/interoperability/with-c/rust.md index f4323686..b7b260b1 100644 --- a/src/android/interoperability/with-c/rust.md +++ b/src/android/interoperability/with-c/rust.md @@ -1,12 +1,6 @@ # Calling Rust -Exporting Rust functions and types to C is easy: - -_interoperability/rust/libanalyze/analyze.rs_ - -```rust,editable -{{#include rust/libanalyze/analyze.rs:analyze_numbers}} -``` +We can now call this from a C binary: _interoperability/rust/libanalyze/analyze.h_ @@ -14,14 +8,6 @@ _interoperability/rust/libanalyze/analyze.h_ {{#include rust/libanalyze/analyze.h:analyze_numbers}} ``` -_interoperability/rust/libanalyze/Android.bp_ - -```javascript -{{#include rust/libanalyze/Android.bp}} -``` - -We can now call this from a C binary: - _interoperability/rust/analyze/main.c_ ```c @@ -39,11 +25,3 @@ Build, push, and run the binary on your device: ```shell {{#include ../../build_all.sh:analyze_numbers}} ``` - -
- -`#[unsafe(no_mangle)]` disables Rust's usual name mangling, so the exported -symbol will just be the name of the function. You can also use -`#[unsafe(export_name = "some_name")]` to specify whatever name you want. - -
diff --git a/src/android/interoperability/with-c/rust/libanalyze/analyze.h b/src/android/interoperability/with-c/rust/libanalyze/analyze.h index a8035be5..409ca03f 100644 --- a/src/android/interoperability/with-c/rust/libanalyze/analyze.h +++ b/src/android/interoperability/with-c/rust/libanalyze/analyze.h @@ -15,8 +15,8 @@ */ // ANCHOR: analyze_numbers -#ifndef ANALYSE_H -#define ANALYSE_H +#ifndef ANALYZE_H +#define ANALYZE_H void analyze_numbers(int x, int y);