Skip to content

Commit f3d890c

Browse files
authored
[ffigen] add example from readme as real example for reference (#2697)
1 parent c2cad10 commit f3d890c

File tree

14 files changed

+130
-7
lines changed

14 files changed

+130
-7
lines changed

.github/workflows/ffigen.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
channel: 'stable'
3838
- id: install
3939
name: Install dependencies
40-
run: flutter pub get && flutter pub get --directory="example/shared_bindings" && flutter pub get --directory="../objective_c"
40+
run: flutter pub get && flutter pub get --directory="example/shared_bindings" && flutter pub get --directory="../objective_c" && flutter pub get --directory="example/add"
4141
- name: Check formatting
4242
run: dart format --output=none --set-exit-if-changed .
4343
if: always() && steps.install.outcome == 'success'

pkgs/ffigen/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ app has been created via `dart create ffigen_example`.
117117

118118
That's it! Run your app with `dart run` to see it in action!
119119

120+
The complete and runnable example can be found in [example/add](example/add).
121+
120122
## More Examples
121123

122124
The `code_asset` package contains [comprehensive examples](../code_assets/example)

pkgs/ffigen/example/README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
## Examples
22

3-
- [Simple](https://github.com/dart-lang/native/tree/main/pkgs/ffigen/example/simple)
4-
- [cJSON](https://github.com/dart-lang/native/tree/main/pkgs/ffigen/example/c_json)
5-
- [LibClang](https://github.com/dart-lang/native/tree/main/pkgs/ffigen/example/libclang-example)
6-
- [ObjectiveC](https://github.com/dart-lang/native/tree/main/pkgs/ffigen/example/objective_c)
7-
- [Swift](https://github.com/dart-lang/native/tree/main/pkgs/ffigen/example/swift)
3+
- [Add](add): Simple end-to-end example from FFIgen's [README.md](../README.md)
4+
that shows how FFIgen can be used to call a custom C API from a Dart app.
5+
- [package:code_assets examples](../../code_assets/example): Various end-to-end
6+
examples showcasing how FFIgen can be used in real world scenarios.
7+
- [Simple](simple): Very simple example that generates bindigns for a very small
8+
header file.
9+
- [cJSON](c_json): Demonstrates generation of bindings for a C library
10+
([cJson](https://github.com/DaveGamble/cJSON)).
11+
- [LibClang](libclang-example): Demonstrates generating bindings for
12+
[Libclang](https://clang.llvm.org/doxygen/group__CINDEX.html).
13+
- [ObjectiveC](objective_c): Showcases how to generate bindings for an
14+
Objective-C library.
15+
- [Swift](swift): Demonstrates how to use FFIgen to interact with Swift
16+
libraries.
17+
- [FFINative](ffinative): Example for generating `Native` bindings for a very
18+
small header file.
19+
- [SharedBindings](shared_bindings): Showcases how bindings can share types with
20+
other bindings.

pkgs/ffigen/example/add/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
A simple end-to-end example using FFIgen.
2+
3+
FFIgen bindings are generated by the script in `tool/ffigen.dart`. Execute
4+
`dart run tool/ffigen.dart` to regenerate the bindings.
5+
6+
The C source files for which bindings are generated are located in `src/`.
7+
8+
The bindings are imported and used by `lib/add.dart`.
9+
10+
To run the app and see it in action execute `dart run`.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:add/add.dart' as add;
6+
7+
void main(List<String> arguments) {
8+
add.answerToLife();
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'package:code_assets/code_assets.dart';
2+
import 'package:hooks/hooks.dart';
3+
import 'package:native_toolchain_c/native_toolchain_c.dart';
4+
5+
void main(List<String> args) async {
6+
await build(args, (input, output) async {
7+
if (input.config.buildCodeAssets) {
8+
final builder = CBuilder.library(
9+
name: 'add',
10+
assetName: 'add.g.dart',
11+
sources: ['src/add.c'],
12+
);
13+
await builder.run(input: input, output: output);
14+
}
15+
});
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'add.g.dart';
6+
7+
void answerToLife() {
8+
print('The answer to the Ultimate Question is ${add(40, 2)}!');
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// AUTO GENERATED FILE, DO NOT EDIT.
2+
//
3+
// Generated by `package:ffigen`.
4+
// ignore_for_file: type=lint
5+
import 'dart:ffi' as ffi;
6+
7+
@ffi.Native<ffi.Int Function(ffi.Int, ffi.Int)>()
8+
external int add(int a, int b);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: add
2+
description: A simple end-to-end example showcasing FFIgen.
3+
publish_to: none
4+
5+
environment:
6+
sdk: '>=3.8.0 <4.0.0'
7+
8+
dependencies:
9+
code_assets: any
10+
ffi: ^2.1.4
11+
hooks: any
12+
native_toolchain_c: any
13+
14+
dev_dependencies:
15+
ffigen:
16+
path: '../../'
17+
test: ^1.25.15

pkgs/ffigen/example/add/src/add.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
int add(int a, int b) {
6+
return a + b;
7+
}

0 commit comments

Comments
 (0)