Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ffigen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
channel: 'stable'
- id: install
name: Install dependencies
run: flutter pub get && flutter pub get --directory="example/shared_bindings" && flutter pub get --directory="../objective_c"
run: flutter pub get && flutter pub get --directory="example/shared_bindings" && flutter pub get --directory="../objective_c" && flutter pub get --directory="example/add"
- name: Check formatting
run: dart format --output=none --set-exit-if-changed .
if: always() && steps.install.outcome == 'success'
Expand Down
2 changes: 2 additions & 0 deletions pkgs/ffigen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ app has been created via `dart create ffigen_example`.

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

The complete and runnable example can be found in [example/add](example/add).

## More Examples

The `code_asset` package contains [comprehensive examples](../code_assets/example)
Expand Down
23 changes: 18 additions & 5 deletions pkgs/ffigen/example/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
## Examples

- [Simple](https://github.com/dart-lang/native/tree/main/pkgs/ffigen/example/simple)
- [cJSON](https://github.com/dart-lang/native/tree/main/pkgs/ffigen/example/c_json)
- [LibClang](https://github.com/dart-lang/native/tree/main/pkgs/ffigen/example/libclang-example)
- [ObjectiveC](https://github.com/dart-lang/native/tree/main/pkgs/ffigen/example/objective_c)
- [Swift](https://github.com/dart-lang/native/tree/main/pkgs/ffigen/example/swift)
- [Add](add): Simple end-to-end example from FFIgen's [README.md](../README.md)
that shows how FFIgen can be used to call a custom C API from a Dart app.
- [package:code_assets examples](../../code_assets/example): Various end-to-end
examples showcasing how FFIgen can be used in real world scenarios.
- [Simple](simple): Very simple example that generates bindigns for a very small
header file.
- [cJSON](c_json): Demonstrates generation of bindings for a C library
([cJson](https://github.com/DaveGamble/cJSON)).
- [LibClang](libclang-example): Demonstrates generating bindings for
[Libclang](https://clang.llvm.org/doxygen/group__CINDEX.html).
- [ObjectiveC](objective_c): Showcases how to generate bindings for an
Objective-C library.
- [Swift](swift): Demonstrates how to use FFIgen to interact with Swift
libraries.
- [FFINative](ffinative): Example for generating `Native` bindings for a very
small header file.
- [SharedBindings](shared_bindings): Showcases how bindings can share types with
other bindings.
10 changes: 10 additions & 0 deletions pkgs/ffigen/example/add/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
A simple end-to-end example using FFIgen.

FFIgen bindings are generated by the script in `tool/ffigen.dart`. Execute
`dart run tool/ffigen.dart` to regenerate the bindings.

The C source files for which bindings are generated are located in `src/`.

The bindings are imported and used by `lib/add.dart`.

To run the app and see it in action execute `dart run`.
9 changes: 9 additions & 0 deletions pkgs/ffigen/example/add/bin/add.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:add/add.dart' as add;

void main(List<String> arguments) {
add.answerToLife();
}
16 changes: 16 additions & 0 deletions pkgs/ffigen/example/add/hook/build.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:code_assets/code_assets.dart';
import 'package:hooks/hooks.dart';
import 'package:native_toolchain_c/native_toolchain_c.dart';

void main(List<String> args) async {
await build(args, (input, output) async {
if (input.config.buildCodeAssets) {
final builder = CBuilder.library(
name: 'add',
assetName: 'add.g.dart',
sources: ['src/add.c'],
);
await builder.run(input: input, output: output);
}
});
}
9 changes: 9 additions & 0 deletions pkgs/ffigen/example/add/lib/add.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'add.g.dart';

void answerToLife() {
print('The answer to the Ultimate Question is ${add(40, 2)}!');
}
8 changes: 8 additions & 0 deletions pkgs/ffigen/example/add/lib/add.g.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// AUTO GENERATED FILE, DO NOT EDIT.
//
// Generated by `package:ffigen`.
// ignore_for_file: type=lint
import 'dart:ffi' as ffi;

@ffi.Native<ffi.Int Function(ffi.Int, ffi.Int)>()
external int add(int a, int b);
17 changes: 17 additions & 0 deletions pkgs/ffigen/example/add/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: add
description: A simple end-to-end example showcasing FFIgen.
publish_to: none

environment:
sdk: '>=3.8.0 <4.0.0'

dependencies:
code_assets: any
ffi: ^2.1.4
hooks: any
native_toolchain_c: any

dev_dependencies:
ffigen:
path: '../../'
test: ^1.25.15
7 changes: 7 additions & 0 deletions pkgs/ffigen/example/add/src/add.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

int add(int a, int b) {
return a + b;
}
5 changes: 5 additions & 0 deletions pkgs/ffigen/example/add/src/add.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

int add(int a, int b);
12 changes: 12 additions & 0 deletions pkgs/ffigen/example/add/test/add_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:add/add.g.dart';
import 'package:test/test.dart';

void main() {
test('add', () {
expect(add(40, 2), 42);
});
}
15 changes: 15 additions & 0 deletions pkgs/ffigen/example/add/tool/ffigen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';

import 'package:ffigen/ffigen.dart';

void main() {
final packageRoot = Platform.script.resolve('../');
FfiGenerator(
output: Output(dartFile: packageRoot.resolve('lib/add.g.dart')),
headers: Headers(entryPoints: [packageRoot.resolve('src/add.h')]),
functions: Functions.includeSet({'add'}),
).generate();
}
2 changes: 1 addition & 1 deletion pkgs/jnigen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ That's it! Run your app with `flutter run` on an Android device to see it in
action.

The complete example can be found in
[jnigen/example/in_app_java](example/in_app_java), which adds a few more classes
[example/in_app_java](example/in_app_java), which adds a few more classes
to demonstrate using classes from Gradle JAR and source dependencies.

## More Examples
Expand Down
Loading