ffi_tool 0.2.5

Dart native
Flutter Android iOS

A tool for generating 'dart:ffi' bindings for C and Objective-C libraries.

Overview #

Pub Package Github Actions CI Build Status

This is a simple package for generating dart:ffi bindings.

You can contribute at github.com/dart-interop/ffi_tool.

The advantages over handwritten dart:ffi code are:

  • Less boilerplate
    • You don't have to define multiple types for each C function.
    • You can require the generated code to use cupertino_ffi reference counting methods (arcPush, arcPop, arcReturn).
  • Source code readability
    • You can use the original identifiers (such as *size_t instead of Pointer<IntPtr>).
    • You can define aliases, e.g. const len_t = 'int32';.

Getting started #

1.Add dependency #

In pubspec.yaml:

dev_dependencies:
  ffi_tool: ^0.2.5

Run pub get.

2.Write a script #

Create tool/generate_example.dart:

import 'package:ffi_tool/c.dart';
import 'dart:io';

void main() {
  // Generates source code and runs 'dartfmt'
  generateFile(File('lib/src/generated.dart'), library);
}

final library = const Library(
  // Where the library is found?
  dynamicLibraryPath: 'path/to/library',

  // Optional imports
  importedUris: {
    'package:example/library.dart',
  },

  /// List of generated functions, structs, and global variables
  elements: <Element>[
    // A definition for a function in C
    Func(
      name: 'Example',
      parameterTypes: ['int32', 'float32', '*void'],
      returnType: 'void',
    ),

    // A definition for a struct in C
    Struct(
      name: 'ExampleStruct',
      fields: [
        Field(
          name: 'length',
          type: 'size_t',
        ),
      ],
    ),

    // A definition for a global variable in C
    Global(
      name: 'ExampleGlobal',
      type: 'Int32',
    ),
  ],
);

3.Run the script #

If you use Dart SDK, run:

pub run tool/generate_example.dart

If you use Flutter SDK, run:

flutter pub run tool/generate_example.dart
7
likes
100
pub points
35%
popularity

A tool for generating 'dart:ffi' bindings for C and Objective-C libraries.

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

terrier989@gmail.com

License

MIT (LICENSE)

Dependencies

meta

More

Packages that depend on ffi_tool