Skip to content

Add a generator script to regenerate native bindings #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 1 addition & 4 deletions .github/workflows/io_file.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ jobs:
with:
sdk: dev
- run: dart pub get
- run: dart run ffigen --config ffigen.yaml
- run: dart run ffigen --config constants-ffigen.yaml
- run: dart --enable-experiment=native-assets run tool/build_constants.dart
- run: dart format lib/src/*.dart
- run: dart --enable-experiment=native-assets run tool/generate.dart
- run: git diff --exit-code

desktop-vm-test:
Expand Down
2 changes: 1 addition & 1 deletion pkgs/io_file/constants-ffigen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: NativeAddBindings
description: |
Bindings for `constants.g.h`.

Regenerate bindings with `dart run ffigen --config constants-ffigen.yaml`.
Regenerate bindings with `dart run tool/generate.dart`.
output: 'lib/src/constant_bindings.g.dart'
headers:
entry-points:
Expand Down
2 changes: 1 addition & 1 deletion pkgs/io_file/ffigen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: NativeAddBindings
description: |
Bindings for `src/libc_shim.h`.

Regenerate bindings with `dart run ffigen --config ffigen.yaml`.
Regenerate bindings with `dart run tool/generate.dart`.
output: 'lib/src/libc_bindings.g.dart'
headers:
entry-points:
Expand Down
1 change: 1 addition & 0 deletions pkgs/io_file/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dev_dependencies:
args: ^2.7.0
benchmark_harness: ^2.3.1
dart_flutter_team_lints: ^3.4.0
dart_style: ^3.1.0
errno: ^1.4.1
ffigen: ^19.0.0
stdlibc:
Expand Down
37 changes: 37 additions & 0 deletions pkgs/io_file/tool/generate.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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.

// Run all code generation and formatting steps.

import 'dart:io';

import 'package:dart_style/dart_style.dart';

import 'package:ffigen/src/executables/ffigen.dart' as ffigen;
import 'build_constants.dart' as build_constants;

void _formatFile(String path) {
final formatter = DartFormatter(
languageVersion: DartFormatter.latestLanguageVersion,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: You might want to do Process.run(Platform.executable, ['format']) now that the formatter has subtle changes per language version.

);
final file = File(path);
file.writeAsStringSync(formatter.format(file.readAsStringSync(), uri: path));
}

void main() async {
build_constants.main();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose all generators throw on failure? Or do you need to check a return value?


await ffigen.main(['--no-format', '-v', 'severe', '--config', 'ffigen.yaml']);
await ffigen.main([
'--no-format',
'-v',
'severe',
'--config',
'constants-ffigen.yaml',
]);

_formatFile('lib/src/constant_bindings.g.dart');
_formatFile('lib/src/constants.g.dart');
_formatFile('lib/src/libc_bindings.g.dart');
}