ffi_tool 0.2.5

  • Readme
  • Changelog
  • Example
  • Installing
  • 68

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

0.2.5 #

  • Fixes issues caused by a breaking change after a pull request didn't go through enough code review.

0.2.3 #

  • Adds support for disabling dartfmt.

0.2.2 #

  • Bug fixes

0.2.1 #

  • Bug fixes
  • Support for generating 'library', 'part of', and 'parts' directives.

0.2.0 #

  • Support for pedantic 1.9 linter warning.
  • Added struct support.
  • Refactored the API.

0.1.2 #

  • The package now works in the latest Dart SDK.
  • Removed cupertino_ffi stuff.

0.1.1 #

  • Added Objective-C support.

0.1.0 #

  • Initial release

example/example.dart

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

void main() {
  generateFile(File('generated.dart'), library);
}

final library = Library(
  dynamicLibraryPath: 'path/to/library',
  elements: [
    // A function
    Func(
      name: 'Example',
      parameterTypes: ['int32', 'float64', '*void'],
      returnType: 'void',
    ),

    // A global
    Global(
      name: 'ExampleGlobal',
      type: 'int32',
    ),
  ],
);

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  ffi_tool: ^0.2.5

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:ffi_tool/c.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
35
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
68
Learn more about scoring.

We analyzed this package on Jul 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13

Analysis suggestions

Package not compatible with runtime flutter-web on web

Because of the import of dart:io via the import chain package:ffi_tool/c.dartpackage:ffi_tool/src/c/generate_file.dartdart:io

Package not compatible with runtime web

Because of the import of dart:io via the import chain package:ffi_tool/c.dartpackage:ffi_tool/src/c/generate_file.dartdart:io

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
meta ^1.1.0 1.2.1
Dev dependencies
ffi ^0.1.3
pedantic ^1.9.0
test ^1.6.0