Skip to content

Commit 007b306

Browse files
fix: set more NDK env vars + update examples (#27)
Fixes #26
1 parent 55bcc6a commit 007b306

15 files changed

Lines changed: 69 additions & 39 deletions

File tree

examples/dart_only/pubspec.yaml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,5 @@ dependencies:
1010
native_toolchain_rs: ^0.1.2+2
1111

1212
dev_dependencies:
13-
# TODO use released version
14-
# ffigen: ^20.0.0-dev.0
15-
ffigen:
16-
git:
17-
url: https://github.com/dart-lang/native
18-
path: pkgs/ffigen
19-
ref: 85fa542
13+
ffigen: ^20.0.0-dev.1 # TODO switch to stable
2014
test: ^1.26.2

examples/dart_only/rust/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ name = "dart-only-example"
33
edition = "2024"
44

55
[lib]
6-
crate-type = ["staticlib", "cdylib"]
6+
# NOTE: also adding lib here so this can be consumed in the flutter example
7+
crate-type = ["staticlib", "cdylib", "lib"]
78

89
[build-dependencies]
10+
bindgen = "0.72.1"
911
cc = "1.2.39"
1012
cbindgen = "0.29.0"

examples/dart_only/rust/bindings.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ This file is automatically generated by build.rs; DO NOT MANUALLY EDIT!
33
This header is designed to be consumed by ffigen over on the Dart side.
44
*/
55

6+
#include <stdarg.h>
7+
#include <stdbool.h>
68
#include <stdint.h>
7-
8-
extern int32_t c_add(int32_t a, int32_t b);
9+
#include <stdlib.h>
910

1011
int32_t rust_add(int32_t a, int32_t b);

examples/dart_only/rust/build.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1-
use std::env;
2-
3-
use cbindgen::Language;
1+
use std::{env, path::PathBuf};
42

53
fn main() {
4+
create_c_to_rust_bindings();
5+
build_c_library();
6+
create_rust_to_dart_bindings();
7+
}
8+
9+
fn create_c_to_rust_bindings() {
10+
let bindings = bindgen::builder()
11+
.header("c/c_add.h")
12+
.generate()
13+
.expect("Unable to generate bindings");
14+
15+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
16+
bindings
17+
.write_to_file(out_path.join("bindings.rs"))
18+
.expect("Unable to write bindings");
19+
}
20+
21+
fn build_c_library() {
622
println!("cargo:rerun-if-changed=c/c_add.h");
723
println!("cargo:rerun-if-changed=c/c_add.c");
824

@@ -12,14 +28,14 @@ fn main() {
1228
.compile("c_add");
1329

1430
println!("cargo:rustc-link-lib=static=c_add");
31+
}
1532

33+
fn create_rust_to_dart_bindings() {
1634
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
1735

1836
cbindgen::Builder::new()
1937
.with_crate(crate_dir)
20-
.with_language(Language::C)
21-
.with_no_includes()
22-
.with_sys_include("stdint.h")
38+
.with_language(cbindgen::Language::C)
2339
.with_autogen_warning(
2440
"/*
2541
This file is automatically generated by build.rs; DO NOT MANUALLY EDIT!

examples/dart_only/rust/c/c_add.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#ifndef C_ADD_H
22
#define C_ADD_H
33

4+
#include <stdarg.h>
5+
#include <stdbool.h>
46
#include <stdint.h>
7+
#include <stdlib.h>
58

69
int32_t c_add(int32_t a, int32_t b);
710

examples/dart_only/rust/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
unsafe extern "C" {
2-
fn c_add(a: i32, b: i32) -> i32;
3-
}
1+
#![allow(warnings)]
2+
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
43

54
#[unsafe(no_mangle)]
65
pub extern "C" fn rust_add(a: i32, b: i32) -> i32 {

examples/dart_only/tool/ffigen.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ void main() {
88
headers: Headers(entryPoints: [packageRoot.resolve('rust/bindings.h')]),
99
output: Output(dartFile: packageRoot.resolve('lib/src/ffi.g.dart')),
1010
functions: Functions.includeSet({'rust_add'}),
11-
).generate(logger: null);
11+
).generate();
1212
}

examples/flutter/pubspec.yaml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@ dependencies:
1212
native_toolchain_rs: ^0.1.2+2
1313

1414
dev_dependencies:
15-
# TODO use released version
16-
# ffigen: ^20.0.0-dev.0
17-
ffigen:
18-
git:
19-
url: https://github.com/dart-lang/native
20-
path: pkgs/ffigen
21-
ref: 85fa542
15+
ffigen: ^20.0.0-dev.1 # TODO switch to stable
2216
flutter_test:
2317
sdk: flutter
2418
integration_test:

examples/flutter/rust/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,10 @@ edition = "2024"
55
[lib]
66
crate-type = ["staticlib", "cdylib"]
77

8+
[dependencies]
9+
# NOTE: this just verifies that cc/bindgen also work in flutter apps,
10+
# since dart-only-example uses cc/bindgen
11+
dart-only-example.path = "../../dart_only/rust"
12+
813
[build-dependencies]
914
cbindgen = "0.29.0"

examples/flutter/rust/bindings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ This file is automatically generated by build.rs; DO NOT MANUALLY EDIT!
33
This header is designed to be consumed by ffigen over on the Dart side.
44
*/
55

6+
#include <stdarg.h>
7+
#include <stdbool.h>
68
#include <stdint.h>
9+
#include <stdlib.h>
710

811
void reset_count(void);
912

0 commit comments

Comments
 (0)