dart-use-ffigen
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGenerating FFI Bindings using package:ffigen
使用package:ffigen生成FFI绑定
Contents
目录
Introduction
简介
Automate and standardize the generation of FFI bindings using (). Writing FFI bindings by hand is error-prone, brittle, and highly discouraged.
package:ffigenFfiGenerator使用(即)自动化并标准化FFI绑定的生成。手动编写FFI绑定容易出错、稳定性差,强烈不建议这么做。
package:ffigenFfiGeneratorConstraints
约束条件
- No Hand-Written FFI Bindings: If native headers (files) exist or are generated by a build step, never write manual
.h,DynamicLibrary.lookupexternal functions, or raw struct classes. Always use@Nativeto generate them.FfiGenerator - Generator Location: The generator script should be located at within the target package root.
tool/ffigen.dart - Header Location: If the native header files are third-party, they should be located in within the target package (otherwise placing them in a
third_party/directory at the package root is also acceptable). If the headers are not in one of these standard locations, notify the user that it would be cleaner to move the header files to the standard location (e.g.,src/).third_party/ - Targeted Inclusion Filters: Avoid importing an entire native library unless specifically needed. Always apply precise inclusion lists using positive matches to minimize the size and cognitive load of the generated code (e.g., using or filtering matches in
Functions.includeSetclosures).include - Output Setup: If the generated FFI bindings interface with a third-party library (or reference third-party headers), the generated files must always be placed under . The primary generated FFI bindings file must strictly use the
lib/src/third_party/extension (e.g..g.dart).sqlite3.g.dart - Preamble & License Headers: Always supply a premium in the
preambleclass to specify the license. This must match the native third-party library's license, explicitly include the copyright header of the target native header file, and contain an automatic generation warning (e.g.Output).// Generated by package:ffigen. Do not edit manually. - No Unnecessary Commits of Stale Bindings: Ensure you run the generator script and check if the generated files have changed before finishing your task. Always verify the package by running .
dart analyze - Record Usage and Tree Shaking: If the package is integrated into standard runtime execution or compiles native assets via native hooks:
- Enable recorded usage on all functions by setting under
recordUse: (_) => true.Functions - Specify the target in
recordUseMapping(which must strictly be aOutputfile under.g.dart, e.g.lib/src/third_party/) to register bindings for symbol tree shaking.lib/src/third_party/sqlite3.record_use_mapping.g.dart
- Enable recorded usage on all functions by setting
- 禁止手动编写FFI绑定:如果存在原生头文件(文件)或通过构建步骤生成头文件,绝不要手动编写
.h、DynamicLibrary.lookup外部函数或原始结构体类。务必使用@Native生成这些内容。FfiGenerator - 生成器脚本位置:生成器脚本应位于目标包根目录下的。
tool/ffigen.dart - 头文件位置:如果原生头文件是第三方的,应放在目标包内的目录下(否则放在包根目录的
third_party/目录也可)。如果头文件不在这些标准位置,需告知用户将其移至标准位置会更规范(例如src/)。third_party/ - 精准的包含过滤:除非特别需要,否则避免导入整个原生库。务必使用精确的包含列表(正向匹配),以最小化生成代码的体积和认知负担(例如使用或在
Functions.includeSet闭包中过滤匹配项)。include - 输出设置:如果生成的FFI绑定与第三方库交互(或引用第三方头文件),生成的文件必须放在下。主生成的FFI绑定文件必须严格使用
lib/src/third_party/扩展名(例如.g.dart)。sqlite3.g.dart - 序言与许可证头:务必在类中提供规范的
Output以指定许可证。其必须与原生第三方库的许可证匹配,明确包含目标原生头文件的版权声明,并包含自动生成警告(例如preamble)。// Generated by package:ffigen. Do not edit manually. - 禁止提交过时的绑定文件:在完成任务前,确保运行生成器脚本并检查生成文件是否有变更。务必通过运行验证包。
dart analyze - 记录使用情况与摇树优化:如果包集成到标准运行时执行或通过原生钩子编译原生资源:
- 在下设置
Functions,为所有函数启用使用记录。recordUse: (_) => true - 在中指定
Output目标(必须是recordUseMapping下的lib/src/third_party/文件,例如.g.dart),以注册绑定用于符号摇树优化。lib/src/third_party/sqlite3.record_use_mapping.g.dart
- 在
FFIgen Overview
FFIgen概述
To construct the programmatic generator, use the core configuration objects imported from :
package:ffigen/ffigen.dart要构建程序化生成器,请使用从导入的核心配置对象:
package:ffigen/ffigen.dart1. FfiGenerator
FfiGenerator1. FfiGenerator
FfiGeneratorThe parent class that orchestrates the configuration, parsing, and code generation.
dart
FfiGenerator({
Headers headers = const Headers(),
Enums enums = Enums.excludeAll,
Functions functions = Functions.excludeAll,
Globals globals = Globals.excludeAll,
Integers integers = const Integers(),
Macros macros = Macros.excludeAll,
Structs structs = Structs.excludeAll,
Typedefs typedefs = Typedefs.excludeAll,
Unions unions = Unions.excludeAll,
UnnamedEnums unnamedEnums = UnnamedEnums.excludeAll,
ObjectiveC? objectiveC,
required Output output,
}).generate();协调配置、解析和代码生成的父类。
dart
FfiGenerator({
Headers headers = const Headers(),
Enums enums = Enums.excludeAll,
Functions functions = Functions.excludeAll,
Globals globals = Globals.excludeAll,
Integers integers = const Integers(),
Macros macros = Macros.excludeAll,
Structs structs = Structs.excludeAll,
Typedefs typedefs = Typedefs.excludeAll,
Unions unions = Unions.excludeAll,
UnnamedEnums unnamedEnums = UnnamedEnums.excludeAll,
ObjectiveC? objectiveC,
required Output output,
}).generate();2. Headers
Headers2. Headers
HeadersConfigures Clang header parsing targets and compiler flags.
- : A list of target header
entryPointsinputs.Uri - : A filter function
includethat handles transitive header imports.bool Function(Uri header) - : Custom preprocessor/include compiler flags to pass directly to libclang.
compilerOptions - : Set to
ignoreSourceErrorsto silence errors occurring inside third-party headers during parsing.true
配置Clang头文件解析目标和编译器标志。
- :目标头文件
entryPoints输入列表。Uri - :处理传递性头文件导入的过滤函数
include。bool Function(Uri header) - :直接传递给libclang的自定义预处理器/包含编译器标志。
compilerOptions - :设置为
ignoreSourceErrors以静默解析第三方头文件时出现的错误。true
3. Functions
Functions3. Functions
FunctionsSpecifies which native C/C++ functions to expose in Dart.
- : A matcher function (e.g.
includeor(decl) => {'my_func'}.contains(decl.originalName)).Functions.includeSet({'my_func'}) - : Declares functions as leaf functions (
isLeaf) if they do not call back into Dart or block thread execution.(decl) => true - : Enables metadata generation for native asset tree shaking (essential in
recordUse). Set todart-lang/native.(_) => true
指定要在Dart中暴露的原生C/C++函数。
- :匹配函数(例如
include或(decl) => {'my_func'}.contains(decl.originalName))。Functions.includeSet({'my_func'}) - :如果函数不会回调到Dart或阻塞线程执行,则将其声明为叶子函数(
isLeaf)。(decl) => true - :启用原生资源摇树优化的元数据生成(在
recordUse中至关重要)。设置为dart-lang/native。(_) => true
4. Output
Output4. Output
OutputConfigures target generated files.
- : Target
dartFilewhere the primary FFI bindings will be written.Uri - : Target
recordUseMappingfor recorded usage metadata maps (crucial for linking-time tree shaking).Uri - : Text inserted at the top of the generated file (licensing, annotations).
preamble - : Set to
formatto run the Dart formatter automatically.true
配置目标生成文件。
- :主FFI绑定将写入的目标
dartFile。Uri - :记录使用情况元数据映射的目标
recordUseMapping(对链接时摇树优化至关重要)。Uri - :插入到生成文件顶部的文本(许可证、注释)。
preamble - :设置为
format以自动运行Dart格式化工具。true
Step-by-Step Workflow
分步工作流程
Step 1: Check/Add Dependencies
步骤1:检查/添加依赖
Open the package's and verify the contains . Use the Dart MCP server or look up the latest version on pub.dev (e.g., ).
pubspec.yamldev_dependenciesffigen^20.1.1You can add it automatically using the CLI:
bash
dart pub add dev:ffigen你可以通过CLI自动添加:
bash
dart pub add dev:ffigenStep 2: Formulate Paths Dynamically
步骤2:动态制定路径
Create a programmatic generator script under the package's directory (e.g., ).
Resolve paths relative to to make sure it runs successfully from any working directory:
tool/tool/ffigen.dartPlatform.scriptdart
final packageRoot = Platform.script.resolve('../');
final headerFile = packageRoot.resolve('third_party/library.h');
final targetBindings = packageRoot.resolve('lib/src/third_party/bindings.g.dart');在包的目录下创建程序化生成器脚本(例如)。
相对于解析路径,确保脚本可从任何工作目录成功运行:
tool/tool/ffigen.dartPlatform.scriptdart
final packageRoot = Platform.script.resolve('../');
final headerFile = packageRoot.resolve('third_party/library.h');
final targetBindings = packageRoot.resolve('lib/src/third_party/bindings.g.dart');Step 3: Write the Script (tool/ffigen.dart
)
tool/ffigen.dart步骤3:编写脚本(tool/ffigen.dart
)
tool/ffigen.dartDefine and run with dynamic options (see complete example below).
void main()FfiGenerator定义并使用动态选项运行(见下方完整示例)。
void main()FfiGeneratorStep 4: Run Code Generation
步骤4:运行代码生成
Execute the script from the terminal inside the target package folder:
bash
dart run tool/ffigen.dart在目标包文件夹内的终端中执行脚本:
bash
dart run tool/ffigen.dartStep 5: Static Analysis
步骤5:静态分析
Verify that the generated bindings are correct and resolve any analysis issues. FFIgen automatically runs the Dart formatter on the output file (via configuration), so manual formatting is not required.
format: true- Run the static analyzer inside the target package:
bash
dart analyze - Addressing Warnings/Lints: If reports style or lint warnings inside the generated file, append the corresponding warning codes to the
dart analyzelist in your generator script'signore_for_file:configuration (e.g., addingpreamble,camel_case_types, etc.). Do not modify the package's global rules.non_constant_identifier_names - Addressing Compilation Errors: If reports actual compiler or analysis errors (not warnings) inside the generated file, do not attempt to edit the generated file manually. Report these error details directly to the user so they can file an issue on the repository at github.com/dart-lang/native.
dart analyze
验证生成的绑定是否正确,并解决任何分析问题。FFIgen会自动对输出文件运行Dart格式化工具(通过配置),因此无需手动格式化。
format: true- 在目标包内运行静态分析器:
bash
undefined
dart analyze
2. **处理警告/代码规范问题**:如果`dart analyze`在生成文件中报告样式或代码规范警告,将相应的警告代码添加到生成器脚本`preamble`配置的`ignore_for_file:`列表中(例如添加`camel_case_types`、`non_constant_identifier_names`等)。不要修改包的全局规则。
3. **处理编译错误**:如果`dart analyze`在生成文件中报告实际的编译器或分析错误(而非警告),不要尝试手动编辑生成文件。直接向用户报告这些错误详情,以便他们在[github.com/dart-lang/native](https://github.com/dart-lang/native)仓库提交问题。Concrete Example: Binding a C Library
具体示例:绑定C库
Let's assume we are working with the SQLite package under , which embeds SQLite C library sources inside and accesses it via FFI.
pkgs/code_assets/example/sqlitethird_party/sqlite/假设我们正在处理下的SQLite包,该包将SQLite C库源码嵌入在中,并通过FFI访问它。
pkgs/code_assets/example/sqlitethird_party/sqlite/The C Header File (third_party/sqlite/sqlite3.h
)
third_party/sqlite/sqlite3.hC头文件(third_party/sqlite/sqlite3.h
)
third_party/sqlite/sqlite3.hc
// The author disclaims copyright to this source code. In place of
// a legal notice, here is a blessing:
//
// May you do good and not evil.
// May you find forgiveness for yourself and forgive others.
// May you share freely, never taking more than you give.
#ifndef SQLITE3_H_
#define SQLITE3_H_
const char *sqlite3_libversion(void);
#endif // SQLITE3_H_c
// The author disclaims copyright to this source code. In place of
// a legal notice, here is a blessing:
//
// May you do good and not evil.
// May you find forgiveness for yourself and forgive others.
// May you share freely, never taking more than you give.
#ifndef SQLITE3_H_
#define SQLITE3_H_
const char *sqlite3_libversion(void);
#endif // SQLITE3_H_BEFORE: Manual FFI Binding (The Anti-Pattern)
之前:手动FFI绑定(反模式)
A developer might attempt to handcraft this integration. It is fragile, blocks tree-shaking metadata, and is highly prone to ABI and structural mapping issues:
dart
// lib/src/sqlite3_manual.dart
import 'dart:ffi' as ffi;
import 'package:ffi/ffi.dart';
// Flaw 1: Hardcoded DynamicLibrary lookup blocks integration with modern native asset compilation.
final ffi.DynamicLibrary _dylib = ffi.DynamicLibrary.open('libsqlite3.so');
// Flaw 2: Manual function type matching requires writing redundant dynamic lookup boilerplate and lacks tree-shaking metadata.
typedef _sqlite3_libversion_C = ffi.Pointer<ffi.Char> Function();
typedef _sqlite3_libversion_Dart = ffi.Pointer<ffi.Char> Function();
final _sqlite3_libversion_Dart sqlite3LibVersion = _dylib
.lookup<ffi.NativeFunction<_sqlite3_libversion_C>>('sqlite3_libversion')
.asFunction();开发者可能会尝试手工编写此集成。这种方式稳定性差,会阻碍摇树优化元数据,且极易出现ABI和结构映射问题:
dart
// lib/src/sqlite3_manual.dart
import 'dart:ffi' as ffi;
import 'package:ffi/ffi.dart';
// 缺陷1:硬编码的DynamicLibrary查找会阻碍与现代原生资产编译的集成。
final ffi.DynamicLibrary _dylib = ffi.DynamicLibrary.open('libsqlite3.so');
// 缺陷2:手动函数类型匹配需要编写冗余的动态查找样板代码,且缺少摇树优化元数据。
typedef _sqlite3_libversion_C = ffi.Pointer<ffi.Char> Function();
typedef _sqlite3_libversion_Dart = ffi.Pointer<ffi.Char> Function();
final _sqlite3_libversion_Dart sqlite3LibVersion = _dylib
.lookup<ffi.NativeFunction<_sqlite3_libversion_C>>('sqlite3_libversion')
.asFunction();AFTER: Generating via FFIgen (The Correct Pattern)
之后:通过FFIgen生成(正确模式)
Create a programmatic script at :
tool/ffigen.dartdart
// 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() {
// Resolve paths dynamically relative to Platform.script
final packageRoot = Platform.script.resolve('../');
final entryHeader = packageRoot.resolve('third_party/sqlite/sqlite3.h');
final bindingsOutput = packageRoot.resolve('lib/src/third_party/sqlite3.g.dart');
final treeShakeMapping = packageRoot.resolve('lib/src/third_party/sqlite3.record_use_mapping.g.dart');
FfiGenerator(
headers: Headers(
entryPoints: [entryHeader],
),
functions: Functions(
include: (decl) => {'sqlite3_libversion'}.contains(decl.originalName),
// Essential for package optimization and tree-shaking
recordUse: (_) => true,
),
output: Output(
dartFile: bindingsOutput,
recordUseMapping: treeShakeMapping,
format: true,
preamble: '''
// The author disclaims copyright to this source code. In place of
// a legal notice, here is a blessing:
//
// May you do good and not evil.
// May you find forgiveness for yourself and forgive others.
// May you share freely, never taking more than you give.
// AUTO-GENERATED FILE - DO NOT MODIFY.
// Generated via ffigen.
// To regenerate: dart run tool/ffigen.dart
// ignore_for_file: type=lint, unused_import, unused_element, deprecated_member_use_from_same_package, experimental_member_use
''',
),
).generate();
print('Successfully generated sqlite3 FFI bindings.');
}在创建程序化脚本:
tool/ffigen.dartdart
// 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() {
// 相对于Platform.script动态解析路径
final packageRoot = Platform.script.resolve('../');
final entryHeader = packageRoot.resolve('third_party/sqlite/sqlite3.h');
final bindingsOutput = packageRoot.resolve('lib/src/third_party/sqlite3.g.dart');
final treeShakeMapping = packageRoot.resolve('lib/src/third_party/sqlite3.record_use_mapping.g.dart');
FfiGenerator(
headers: Headers(
entryPoints: [entryHeader],
),
functions: Functions(
include: (decl) => {'sqlite3_libversion'}.contains(decl.originalName),
// 对包优化和摇树优化至关重要
recordUse: (_) => true,
),
output: Output(
dartFile: bindingsOutput,
recordUseMapping: treeShakeMapping,
format: true,
preamble: '''
// The author disclaims copyright to this source code. In place of
// a legal notice, here is a blessing:
//
// May you do good and not evil.
// May you find forgiveness for yourself and forgive others.
// May you share freely, never taking more than you give.
// AUTO-GENERATED FILE - DO NOT MODIFY.
// Generated via ffigen.
// To regenerate: dart run tool/ffigen.dart
// ignore_for_file: type=lint, unused_import, unused_element, deprecated_member_use_from_same_package, experimental_member_use
''',
),
).generate();
print('Successfully generated sqlite3 FFI bindings.');
}Running the Generator script
运行生成器脚本
Run this in the package root directory:
bash
dart run tool/ffigen.dartThis will automatically create:
lib/src/third_party/sqlite3.g.dartlib/src/third_party/sqlite3.record_use_mapping.g.dart
在包根目录运行:
bash
dart run tool/ffigen.dart这将自动创建:
lib/src/third_party/sqlite3.g.dartlib/src/third_party/sqlite3.record_use_mapping.g.dart
Verification Checklist
验证清单
Always perform the following verification before completing a binding generation task:
- Correct Setup: Verify the target generated files are inside (required for third-party licensed code) and the primary FFI bindings file strictly uses the
lib/src/third_party/extension..g.dart - Static Analysis: Run and ensure there are zero compiler/analyzer errors or warnings in the package.
dart analyze- If static warnings are reported in the generated bindings, suppress them by adding rules to the generator's
ignore_for_fileconfiguration (do not modify global package rules).preamble - If actual compiler or analyzer errors are reported in the generated bindings, do not edit the generated file manually. Report the details to the user and direct them to file an issue at github.com/dart-lang/native.
- If static warnings are reported in the generated bindings, suppress them by adding
在完成绑定生成任务前,务必执行以下验证:
- 正确设置:验证目标生成文件是否在内(第三方许可代码的要求),且主FFI绑定文件严格使用
lib/src/third_party/扩展名。.g.dart - 静态分析:运行,确保包中没有编译器/分析器错误或警告。
dart analyze- 如果生成的绑定中出现静态警告,通过在生成器的配置中添加
preamble规则来抑制(不要修改包的全局规则)。ignore_for_file - 如果生成的绑定中出现实际的编译器或分析器错误,不要手动编辑生成文件。向用户报告详情,并引导他们在github.com/dart-lang/native提交问题。
- 如果生成的绑定中出现静态警告,通过在生成器的