ffi_tool 0.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • new58

Overview #

GitHub Actions status

This library helps developers to generate dart:ffi bindings. You can contribute at github.com/dart-interop/ffi_tool.

Getting started #

In "pubspec.yaml", add:

dev_dependencies:
  ffi_tool: ^0.1.0

Run pub get.

You have to write a generator script (see examples below) in some file. For example, "tool/generate_example.dart".

Run the script with pub run tool/generate_example.dart.

Generator scripts #

C library #

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

void main() {
  generateFile(File("lib/src/generated.dart"), library);
}

final library = Library(
  dynamicLibraryIdentifier: "dlForExampleLibrary",
  dynamicLibraryPath: "path/to/library",
  importedUris: [
    "package:ffi/ffi.dart",
  ],
  elements: <Element>[
    // C function
    Func(
      name: "Example",
      parameterTypes: ["Int32", "Floa64", "*void", "void"],
      returnType: "ReturnType",
    ),

    // C global variable
    Global(
      name: "ExampleGlobal",
      type: "Int32",
    ),
  ],
);

Objective-C library #

Objective-C supports reflection so we are able to generate APIs automatically. The generated libraries use cupertino_ffi for reference counting. Please make sure you understand the reference counting patterns that you need to follow when you use the generated library.

import 'package:ffi_tool/objective_c.dart';

void main() {
  generateAll([
    ObjcLibrary(
      productName: "Example",
      uri: "https://github.com/example/project",
      libraryName: "example",
      libraryPath: "/System/Library/Frameworks/CoreML.framework/Versions/Current/CoreML",
      generatedPath: "example.dart",
    ),
  ]);
}

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(
  dynamicLibraryIdentifier: "dlForExampleLibrary",
  dynamicLibraryPath: "path/to/library",
  importedUris: [
    "package:ffi/ffi.dart",
  ],
  elements: [
    // A function
    Func(
      name: "Example",
      parameterTypes: ["Int32", "Floa64", "*void", "void"],
      returnType: "ReturnType",
    ),

    // 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.1.1

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';
import 'package:ffi_tool/objc.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
17
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]
58
Learn more about scoring.

We analyzed this package on Oct 10, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, other

Platform components identified in package: io.

Health suggestions

Fix lib/src/c/library.dart. (-0.50 points)

Analysis of lib/src/c/library.dart reported 1 hint:

line 21 col 8: Unused import: 'package:ffi_tool/c.dart'.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.5.0 <3.0.0
cupertino_ffi ^0.1.5 0.1.5
meta ^1.1.0 1.1.7
Dev dependencies
ffi ^0.1.1 0.1.2 0.1.3-dev.1
pedantic ^1.8.0
test ^1.6.0