Skip to content

Commit

Permalink
Split C interop slides into smaller slides.
Browse files Browse the repository at this point in the history
Part of #2445.
This splits up the slides in the interop with C section into smaller
slides. For calling C from Rust, instead of the long slide containing
the sample C lib, explaining bindgen, and how to run the Rust code,
these are now presented on separate slides. Similarly for calling Rust
from C, the simple Rust library is on its own slide, followed by a slide
on how to then call and run the Rust library from C.
  • Loading branch information
Eric Githinji committed Feb 14, 2025
1 parent 699c513 commit 599dbd0
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 78 deletions.
5 changes: 4 additions & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
52 changes: 1 addition & 51 deletions src/android/interoperability/with-c/bindgen.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,8 @@
# Using Bindgen

The [bindgen](https://rust-lang.github.io/rust-bindgen/introduction.html) tool
can auto-generate bindings from a C header file.
can auto-generate bindings from a C header file (in our case `libbirthday_wrapper.h`):

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):

_interoperability/bindgen/libbirthday_wrapper.h_:

```c
{{#include bindgen/libbirthday_wrapper.h:include}}
```
You can now auto-generate the bindings:

_interoperability/bindgen/Android.bp_:

Expand All @@ -56,24 +24,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}}
```

<details>

- The Android build rules will automatically call `bindgen` for you behind the
Expand Down
32 changes: 32 additions & 0 deletions src/android/interoperability/with-c/c-library.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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}}
```

Create a wrapper header file for the library (not strictly needed in this
example):

_interoperability/bindgen/libbirthday_wrapper.h_:

```c
{{#include bindgen/libbirthday_wrapper.h:include}}
```
19 changes: 19 additions & 0 deletions src/android/interoperability/with-c/run-our-binary.md
Original file line number Diff line number Diff line change
@@ -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}}
```
25 changes: 25 additions & 0 deletions src/android/interoperability/with-c/rust-library.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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}}
```

<details>

`#[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.

</details>


26 changes: 2 additions & 24 deletions src/android/interoperability/with-c/rust.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
# 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_

```c
{{#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
Expand All @@ -38,12 +24,4 @@ Build, push, and run the binary on your device:

```shell
{{#include ../../build_all.sh:analyze_numbers}}
```

<details>

`#[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.

</details>
```
4 changes: 2 additions & 2 deletions src/android/interoperability/with-c/rust/libanalyze/analyze.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 599dbd0

Please sign in to comment.