Code Monkey home page Code Monkey logo

ffi_tool's Introduction

Overview

Pub Package Github Actions CI Build Status

This library helps developers to generate 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).
  • Possibly better 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';.
    • You can configure how a shared library is loaded at runtime.

Getting started

1.Add dependency

In 'pubspec.yaml':

dev_dependencies:
  ffi_tool: ^0.3.0

Run pub get.

2.Write a script

Create 'tool/generate_example.dart'.

Example

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.platformAware(
  // Configure, how the dynamic library should be loaded depending on the platform
  dynamicLibraryConfig: DynamicLibraryConfig(
      windows: DynamicLibraryPlatformConfig.open('path/to/library.dll'),
      android: DynamicLibraryPlatformConfig.open('path/to/library.so'),
      iOS: DynamicLibraryPlatformConfig.process()),

  // Optional library preamble
  preamble = '// Licensed under MIT license\r\n// AUTOMATICALLY GENERATED. DO NOT EDIT.'
  
  // Optional imports
  importedUris: {
    ImportedUri('package:example/library.dart'),
  },

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

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

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

3.Run the script

With Dart SDK:

pub run tool/generate_example.dart

With Flutter SDK:

flutter pub run tool/generate_example.dart

ffi_tool's People

Contributors

epnw-eric avatar sachaarbonel avatar terrier989 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.