dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.24k stars 1.57k forks source link

feature request: dart create template for FFI packages #48653

Closed maks closed 2 years ago

maks commented 2 years ago

I have found myself making FFI wrapper packages quite a lot lately.

So I have been some repetitive steps setting up each package with a few common steps to set up FFI on top of the package-simple template, so it would be nice to have this as a template as I suspect that this is becoming a more common use case for Dart packages as FFI has matured into a very useful feature. I think this would also be a good opportunity to provide developers with FFI "best practises" in the template.

Some of the things that I do pretty consistently for all new FFI package projects are:

Having a template to do all the above would be nice.

Above examples are from one of my recently started FFI binding packages but of course you can see my cargo-culting the above sort of recipe on several other FFI wrapper packages I have made.

vsmenon commented 2 years ago

@mit-mit - do we have a standard place where we track template work/requests?

mit-mit commented 2 years ago

Here's fine, cc @dcharkes

dcharkes commented 2 years ago

Yes we'd very much like to do this.

Even more so, we'd like to have a template that both works for Flutter and Dart. Instead of duplicating packages for the Dart and Flutter eco systems (e.g. https://pub.dev/packages/realm_dart https://pub.dev/packages/realm).

We will have a flutter create --template plugin_ffi for the Flutter side starting from the next version of Flutter.

In Flutter land we can always build the native code with the native compilers required by the Flutter toolchain.

@maks how do your packages work? Do you ship the native libraries with them? Do you expect the dll/so/dylib to be available on PATH?

nit:

analyzer:
  exclude:
    - "lib/src/libgit2_generated_bindings.dart"

I'd suggest generating lint ignores within the file instead using the preamble.

maks commented 2 years ago

That's awesome news @dcharkes ! 🎉

Currently I just document that users need to have the native library installed, as I remember seeing somewhere (on dart.dev ?) and I think in some issue comments here as well that it's recommended by the Dart team that dev's not ship binaries inside Dart packages on pub.dev. Is that still the case?

And thank you for that tip, I had completely missed the preamble config property, so I guess I want to use it to add // ignore_for_file: type=lint ?

dcharkes commented 2 years ago

And thank you for that tip, I had completely missed the preamble config property, so I guess I want to use it to add // ignore_for_file: type=lint ?

I'd list the specific lints, so that the rest is checked. For example:

Is that still the case?

At some point we need a solution for the binaries. Because when packages transitively start depending on each other, it becomes hard to figure which native libraries a user has to install. We might settle on a bin/setup.dart that would either build or download the native dependencies with a standardized command line interface so that we can run it for all packages which have native dependencies in a project.

Piero512 commented 2 years ago

And thank you for that tip, I had completely missed the preamble config property, so I guess I want to use it to add // ignore_for_file: type=lint ?

I'd list the specific lints, so that the rest is checked. For example:

Is that still the case?

At some point we need a solution for the binaries. Because when packages transitively start depending on each other, it becomes hard to figure which native libraries a user has to install. We might settle on a bin/setup.dart that would either build or download the native dependencies with a standardized command line interface so that we can run it for all packages which have native dependencies in a project.

Yes,this is great. It would be great for packages that do need to compile some extra code for the lack of asynchronous callbacks on Dart, and for the lack of file descriptors to RawSockets, et all.

maks commented 2 years ago

@dcharkes even though it was done for Flutter, I think https://github.com/flutter/flutter/pull/96225 pretty much addresses what I was asking for when I opened this issue, so I think it can be closed now that the plugin_ffi template is available in Flutter.