dart-lang / native

Dart packages related to FFI and native assets bundling.
BSD 3-Clause "New" or "Revised" License
117 stars 40 forks source link

[native_toolchain_rust] Introduce a package:native_toolchain_rust #883

Closed dcharkes closed 3 weeks ago

dcharkes commented 8 months ago

Edit: being developed on https://pub.dev/packages/native_toolchain_rust.

We should consider introducing a package to help calling the Rust compiler from Dart code.

Things this package could contain:

This package could be used in multiple ways:

  1. Running the Rust compiler from the user machine inside build.dart scripts.
  2. Running the Rust compiler from some tools/compile_all.dart script on the GitHub CI (after which the artifacts can be downloaded in a build.dart script).

cc @fzyzcjy @robertbastian Please post any wishes you have here.

fzyzcjy commented 8 months ago

Wow this is super great and helpful for flutter_rust_bridge, and looking forward to it! Option 1 is already very good, since it allows quite easy integration; and option 2 makes it even greater because we can now create a package with precompiled binaries easily, then users of packages do not need to spend time compiling. Some thoughts (I guess most are already in your mind, but anyway let me post it for completeness):

knopp commented 5 months ago

@fzyzcjy, I'm currently working on native_toolchain_rust (as a replacement for cargokit). Unlike cargokit however, it will not silently install thing during build. I think there is a cleaner approach to that: A separate tool - native_doctor that does all the installing and updates.

When native_toolchain_rust finds out that rustup is not installed, or some toolchains/targets are missing or outdated, it will fail the build with following (or similar) message:

Rustup not found.
Please run native_doctor in your project to fix the issue:

dart pub global activate native_doctor
dart pub global run native_doctor

When user runs native_doctor, it will check project dependencies to determine minimum required Rust and NDK version and outputs something like this:

Project: example (Flutter)
Buildable platforms: macos, ios, android

Native toolchain: NDK

  [✗] NDK installed, but too old
       ! Installed versions: 23.1.7779620
       ! Required minimum version: 25.1.8937393

Native toolchain: Rust

  [✗] Rustup not found
  [✗] Toolchain stable not installed
       ! Required minimum version: 1.77.2
       ! Missing targets: arm-linux-androideabi, aarch64-linux-android, i686-linux-android,
         x86_64-linux-android, aarch64-apple-ios, x86_64-apple-ios, aarch64-apple-ios-sim, 
         aarch64-apple-darwin, x86_64-apple-darwin

Proposed actions:

  • (NDK)  Install NDK 25.1.8937393 or newer
  • (Rust) Install rustup
  • (Rust) Install toolchain stable
  • (Rust) Install targets arm-linux-androideabi, aarch64-linux-android, i686-linux-android,
           x86_64-linux-android, aarch64-apple-ios, x86_64-apple-ios, aarch64-apple-ios-sim,
           aarch64-apple-darwin, x86_64-apple-darwin for toolchain stable

Do you want native doctor to perform proposed actions? (y/N)

When user confirms it, it will automatically install all required items.

@dcharkes, I am wondering if down the line, instead of separate tool, this could be somehow integrated with native assets.

dcharkes commented 5 months ago

@dcharkes, I am wondering if down the line, instead of separate tool, this could be somehow integrated with native assets.

That sounds amazing! Yes, maybe we can pull it in to Dart and Flutter.

We've been discussing:

  1. dart doctor in similar style to flutter doctor
  2. Adding the NDK to flutter doctor

Building this logic as a separate package native_doctor sounds great! For the time being users could do dart pub global run native_doctor. Oh you already did! 🎉

Once native_doctor is stable and working well, I can have a chat with some peeps on the team here whether it makes sense to have it in the SDKs. 👍 flutter native_tools? flutter doctor (but that doesn't install anything currently.)

Thanks for the great ideas and contributions @knopp and @fzyzcjy! 🚀

knopp commented 5 months ago

@dcharkes, I'd maybe like some feedback on native_toolchain_rust. I think it's in a decent shape for MVP, the build tests pass on all platforms (including android on all platforms). There are examples for both dart and flutter packages. It provides actionable message when the environment is not ready (and native_doctor tool to fix it). What would be required to publish it on pub? And somewhat related, is there even a very rough estimation for when native assets get enabled in Flutter main (and Dart dev) without the flag?

mosuem commented 5 months ago

Happy to take a look, what's the best course of action for that in Github? Creating a (draft) pull request for the native repository?

dcharkes commented 5 months ago

I'm happy to let the project be in its own repo. (Unless there are some benefits to having it in this mono repo.)

Maybe just a PR to another branch on the existing repo?

mosuem commented 5 months ago

Yes, the PR would be for reviewing only. Github doesn't allow adding comments on non-changed files in PRs, so I thought that might be the only way to review..

knopp commented 5 months ago

I don't see why it would need to live in the mono-repo.

I'm not sure if I understand things correctly, but here is a PR with code for all packages.

dcharkes commented 4 months ago

You need to be an admin in both publishers to do a transfer. They can invite you as admin, you do the transfer and then they can remove you from being admin. Alternatively you may create a third publishers where only you two are admin and it acts only as intermediary.

Can you invite dacoharkes@google.com to a publisher @knopp so I can transfer the package to you on pub.dev?

fzyzcjy commented 4 months ago

[...] Please post any wishes you have here.

Another wish may be hot restart / hot reload Rust (related e.g. https://github.com/fzyzcjy/flutter_rust_bridge/issues/1966). Here are my brainstorms:

sneurlax commented 3 weeks ago

@dcharkes is there any indication as to when native assets will be activated in a Flutter or Dart release without the flag?

edit: sorry, I just saw you were working on https://github.com/dart-lang/sdk/milestone/112 as of just last week; sorry to've pinged you, please launch on 🚀

dcharkes commented 3 weeks ago

@dcharkes is there any indication as to when native assets will be activated in a Flutter or Dart release without the flag?

edit: sorry, I just saw you were working on https://github.com/dart-lang/sdk/milestone/112 as of just last week; sorry to've pinged you, please launch on 🚀

We've got another milestone on this repo as well https://github.com/dart-lang/native/milestone/15. (And some Flutter issues that are not tracked on a milestone as it is in another org...)

Also, this issue itself is being worked on via https://pub.dev/packages/native_toolchain_rust, so I'll close this.