Skip to content

Commit 7eb1776

Browse files
committed
feat!: add working implementation
1 parent e0ad0dc commit 7eb1776

15 files changed

Lines changed: 1109 additions & 1 deletion

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: GregoryConrad

.github/dependabot.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: 2
2+
enable-beta-ecosystems: true
3+
updates:
4+
- package-ecosystem: pub
5+
directories:
6+
- "**/*"
7+
schedule:
8+
interval: "weekly"
9+
groups:
10+
all:
11+
patterns:
12+
- "*"
13+
- package-ecosystem: cargo
14+
directories:
15+
- "**/*"
16+
schedule:
17+
interval: "weekly"
18+
groups:
19+
all:
20+
patterns:
21+
- "*"

.github/workflows/build.yml

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
name: Build & Test
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
schedule:
9+
- cron: "0 10 * * *" # run everyday at 10 AM
10+
11+
jobs:
12+
build_and_test:
13+
runs-on: ubuntu-latest
14+
steps:
15+
# Setup
16+
- uses: actions/checkout@v5
17+
- uses: cachix/install-nix-action@v31
18+
- uses: subosito/flutter-action@v2 # TODO remove this once flutter is in flake
19+
with:
20+
channel: beta
21+
- run: |
22+
# TODO remove this once patch lands
23+
flutter --version
24+
cd ${{ env.FLUTTER_ROOT }}
25+
curl https://patch-diff.githubusercontent.com/raw/flutter/flutter/pull/175323.patch | git apply
26+
git status
27+
rm bin/cache/flutter_tools.stamp bin/cache/flutter_tools.snapshot
28+
- name: Install direnv
29+
run: |
30+
curl -sfL https://direnv.net/install.sh | bash
31+
echo "$HOME/.local/bin" >> $GITHUB_PATH
32+
# - name: Load environment variables
33+
# run: |
34+
# direnv allow .
35+
# direnv export gha > "$GITHUB_ENV"
36+
37+
# # Codegen
38+
# - name: Run codegen
39+
# run: melos run codegen
40+
# - name: Ensure there are no diffs from codegen
41+
# run: git diff --exit-code
42+
43+
# # Rust
44+
# - name: Rust format
45+
# run: cargo fmt --all --check
46+
# - name: Rust lint
47+
# run: cargo clippy -- -D warnings
48+
# - name: Rust tests
49+
# run: cargo test
50+
51+
# # Dart/Flutter
52+
# - name: Dart format
53+
# run: dart format --set-exit-if-changed .
54+
# - name: Dart lint
55+
# run: flutter analyze --fatal-infos --fatal-warnings
56+
# - name: Dart tests
57+
# run: melos run test
58+
59+
# macos_integration_test:
60+
# runs-on: macos-latest
61+
# steps:
62+
# - uses: actions/checkout@v5
63+
# - uses: subosito/flutter-action@v2
64+
# with:
65+
# channel: beta # TODO remove
66+
# - name: Create macOS boilerplate
67+
# working-directory: packages/flutter_mimir/example
68+
# run: flutter create . --platforms=macos
69+
# - name: Disable macOS App Sandbox
70+
# working-directory: packages/flutter_mimir/example
71+
# run: /usr/libexec/PlistBuddy -c "Delete :com.apple.security.app-sandbox" macos/Runner/DebugProfile.entitlements
72+
# - name: Run Flutter integration tests
73+
# working-directory: packages/flutter_mimir/example
74+
# run: flutter test -d macos integration_test
75+
76+
# windows_integration_test:
77+
# runs-on: windows-latest
78+
# steps:
79+
# - uses: actions/checkout@v5
80+
# - uses: subosito/flutter-action@v2
81+
# with:
82+
# channel: beta # TODO remove
83+
# - name: Create Windows boilerplate
84+
# working-directory: packages/flutter_mimir/example
85+
# run: flutter create . --platforms=windows
86+
# - name: Run Flutter integration tests
87+
# working-directory: packages/flutter_mimir/example
88+
# run: flutter test -d windows integration_test
89+
90+
# linux_integration_test:
91+
# runs-on: ubuntu-latest
92+
# steps:
93+
# - uses: actions/checkout@v5
94+
# - uses: subosito/flutter-action@v2
95+
# with:
96+
# channel: beta # TODO remove
97+
# - uses: pyvista/setup-headless-display-action@v3
98+
# - name: Install dependencies for flutter integration test
99+
# run: sudo apt update && sudo apt-get install -y libglu1-mesa ninja-build clang cmake pkg-config libgtk-3-dev liblzma-dev
100+
# - name: Create Linux boilerplate
101+
# working-directory: packages/flutter_mimir/example
102+
# run: flutter create . --platforms=linux
103+
# - name: Run Flutter integration tests
104+
# working-directory: packages/flutter_mimir/example
105+
# run: flutter test -d linux integration_test
106+
107+
# ios_integration_test:
108+
# runs-on: macos-latest
109+
# steps:
110+
# - uses: actions/checkout@v5
111+
# - uses: subosito/flutter-action@v2
112+
# with:
113+
# channel: beta # TODO remove
114+
# - name: Start iOS Simulator
115+
# uses: futureware-tech/simulator-action@v4
116+
# id: start_simulator
117+
# with:
118+
# wait_for_boot: true
119+
# - name: Create iOS boilerplate
120+
# working-directory: packages/flutter_mimir/example
121+
# run: flutter create . --platforms=ios
122+
# - name: Run Flutter integration tests # NOTE: we often get timeouts otherwise
123+
# uses: Wandalen/wretry.action@v3
124+
# with:
125+
# attempt_limit: 5
126+
# current_path: packages/flutter_mimir/example
127+
# command: flutter test -d ${{ steps.start_simulator.outputs.udid }} integration_test
128+
129+
# android_integration_test:
130+
# runs-on: ubuntu-latest
131+
# steps:
132+
# - uses: actions/checkout@v5
133+
# - uses: subosito/flutter-action@v2
134+
# with:
135+
# channel: beta # TODO remove
136+
# - name: Create Android boilerplate
137+
# working-directory: packages/flutter_mimir/example
138+
# run: flutter create . --platforms=android
139+
# - name: Enable KVM for Android emulator
140+
# run: |
141+
# echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
142+
# sudo udevadm control --reload-rules
143+
# sudo udevadm trigger --name-match=kvm
144+
# - name: Run Flutter integration tests
145+
# uses: reactivecircus/android-emulator-runner@v2
146+
# with:
147+
# api-level: 35
148+
# target: default
149+
# arch: x86_64
150+
# ram-size: 1024M
151+
# disk-size: 2048M
152+
# working-directory: packages/flutter_mimir/example
153+
# script: flutter test -d `flutter devices | grep android | tr ' ' '\n' | grep emulator-` integration_test

.github/workflows/flake-update.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Update flake.lock
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: '0 6 * * 0'
7+
8+
jobs:
9+
nix-flake-update:
10+
permissions:
11+
contents: write
12+
id-token: write
13+
issues: write
14+
pull-requests: write
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v5
18+
- uses: cachix/install-nix-action@v31
19+
- uses: DeterminateSystems/update-flake-lock@main
20+
with:
21+
pr-title: "chore(deps): update nix flake.lock"

.github/workflows/publish.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Publish Release(s)
2+
3+
on:
4+
push:
5+
tags:
6+
- "*"
7+
8+
jobs:
9+
publish_github_release:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
# NOTE: Required to create the release
13+
contents: write
14+
steps:
15+
- uses: actions/checkout@v5
16+
- uses: softprops/action-gh-release@v1
17+
18+
publish_pub_release:
19+
runs-on: ubuntu-latest
20+
permissions:
21+
# NOTE: Required to publish the packages
22+
contents: write
23+
id-token: write
24+
25+
steps:
26+
- uses: actions/checkout@v5
27+
- uses: subosito/flutter-action@v2
28+
- uses: bluefireteam/melos-action@v3
29+
with:
30+
publish: true

.gitignore

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.build/
9+
.buildlog/
10+
.history
11+
.svn/
12+
.swiftpm/
13+
migrate_working_dir/
14+
15+
# IntelliJ related
16+
*.iml
17+
*.ipr
18+
*.iws
19+
.idea/
20+
21+
# The .vscode folder contains launch configuration and tasks you configure in
22+
# VS Code which you may wish to be included in version control, so this line
23+
# is commented out by default.
24+
#.vscode/
25+
26+
# Flutter/Dart/Pub related
27+
pubspec.lock
28+
**/doc/api/
29+
.dart_tool/
30+
.packages
31+
/build/
32+
/coverage/
33+
.pub-cache/
34+
.pub/
35+
.flutter-plugins
36+
.flutter-plugins-dependencies
37+
38+
# Rust related
39+
/target/
40+
Cargo.lock
41+
42+
.direnv/

README.md

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,116 @@
1+
<p align="center">
2+
<a href="https://github.com/GregoryConrad/native_toolchain_rs/actions"><img src="https://github.com/GregoryConrad/native_toolchain_rs/actions/workflows/build.yml/badge.svg" alt="Build Status"></a>
3+
<a href="https://github.com/GregoryConrad/native_toolchain_rs"><img src="https://img.shields.io/github/stars/GregoryConrad/native_toolchain_rs.svg?style=flat&logo=github&colorB=deeppink&label=stars" alt="Github Stars"></a>
4+
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/license-MIT-purple.svg" alt="MIT License"></a>
5+
</p>
6+
17
# native_toolchain_rs
2-
Rust support for Dart’s Native Assets
8+
Rust support for Dart's Native Assets.
9+
10+
---
11+
12+
## Why native_toolchain_rs?
13+
1. It's opinionated.
14+
That might sound bad, but it's opinionated in the way that _keeps you from shooting yourself in the foot_.
15+
2. Does more with less.
16+
The API is incredibly easy to use: the only thing you _need_ to provide is `assetName`,
17+
and the rest is auto-magically figured out.
18+
(But you can still tweak the functionality as much as you need to!)
19+
20+
21+
## Getting Started
22+
1. Install [rustup](https://rustup.rs), for Rust, on your development computer
23+
(if you are a library author, consumers of your package will have to do the same)
24+
2. Run `flutter pub add native_toolchain_rs` for Flutter or `dart pub add native_toolchain_rs` for Dart-only
25+
3. (Until [Native Assets](https://github.com/dart-lang/sdk/issues/50565) is fully released) Switch to Flutter's beta channel: `flutter channel beta`
26+
4. See [Code Setup](#code-setup)
27+
28+
29+
## Code Setup
30+
`native_toolchain_rs` will look (by default) for `native/` or `rust/` (customizable)
31+
in your Dart package's root.
32+
Create a `Cargo.toml` and `rust-toolchain.toml` in your chosen Rust sources directory;
33+
keep reading for what these two files must contain
34+
(but don't worry if you forget, you'll get a helpful error message).
35+
36+
### Quick n' dirty ("it just works") setup
37+
```dart
38+
// hook/build.dart:
39+
import 'package:hooks/hooks.dart';
40+
import 'package:native_toolchain_rs/native_toolchain_rs.dart';
41+
42+
void main(List<String> args) async {
43+
await build(args, (input, output) async {
44+
await RustBuilder(
45+
assetName: 'src/my_ffi_bindings.g.dart',
46+
).run(input: input, output: output);
47+
});
48+
}
49+
```
50+
51+
### Recommended setup (so you get logs)
52+
```dart
53+
// hook/build.dart:
54+
import 'package:hooks/hooks.dart';
55+
import 'package:logging/logging.dart';
56+
import 'package:native_toolchain_rs/native_toolchain_rs.dart';
57+
58+
void main(List<String> args) async {
59+
Logger.root.level = Level.CONFIG; // or whatever level you prefer
60+
61+
await build(args, (input, output) async {
62+
final logger = Logger('my_package_build')..onRecord.listen(print);
63+
64+
await RustBuilder(
65+
assetName: 'src/my_ffi_bindings.g.dart',
66+
// ...maybe enable some Cargo features or something in here too
67+
).run(
68+
input: input,
69+
output: output,
70+
logger: logger,
71+
);
72+
});
73+
}
74+
```
75+
76+
### `Cargo.toml`
77+
Keep at least the following in your `Cargo.toml`.
78+
```toml
79+
[package]
80+
name = "my-crate-name" # name it whatever you want, doesn't matter
81+
82+
[lib]
83+
crate-type = ["staticlib", "cdylib"] # THESE ARE IMPORTANT!
84+
```
85+
86+
### `rust-toolchain.toml`
87+
Keep at least the following in your `rust-toolchain.toml`.
88+
```toml
89+
[toolchain]
90+
channel = "1.90.0" # or newer, but *do not* use stable, beta, or nightly alone (without a version number)!
91+
92+
# The targets you want to support; these are the default:
93+
targets = [
94+
# Android
95+
"armv7-linux-androideabi",
96+
"aarch64-linux-android",
97+
"x86_64-linux-android",
98+
99+
# iOS (device + simulator)
100+
"aarch64-apple-ios",
101+
"aarch64-apple-ios-sim",
102+
"x86_64-apple-ios",
103+
104+
# Windows
105+
"aarch64-pc-windows-msvc",
106+
"x86_64-pc-windows-msvc",
107+
108+
# Linux
109+
"aarch64-unknown-linux-gnu",
110+
"x86_64-unknown-linux-gnu",
111+
112+
# macOS
113+
"aarch64-apple-darwin",
114+
"x86_64-apple-darwin",
115+
]
116+
```

analysis_options.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include: package:very_good_analysis/analysis_options.yaml
2+
3+
analyzer:
4+
errors:
5+
close_sinks: warning

0 commit comments

Comments
 (0)