From d7fff0533eba145744e7e1db5fb6e919b3ca0b39 Mon Sep 17 00:00:00 2001 From: zjs81 Date: Fri, 20 Nov 2026 16:04:49 -0700 Subject: [PATCH] Allow building without ONNX Runtime flutter_onnxruntime downloads ORT natively at build time, and the Linux and Windows downloads from github.com/microsoft/onnxruntime are unavailable. scripts/onnx.sh off swaps in a pure-Dart stub via pubspec_overrides.yaml, so no platform pulls ORT; image messages then show as unavailable, as on web. Linux CI builds use this mode for now. --- .github/workflows/build.yml | 10 +++- .github/workflows/flutter_dart.yml | 6 +++ .gitignore | 3 ++ README.md | 20 +++++++ documentation/image-messages.md | 6 ++- lib/services/image_codec_backend.dart | 29 ++++++++-- lib/services/image_codec_service.dart | 12 +++-- scripts/appimage/test_runtime.sh | 7 ++- scripts/onnx.sh | 54 +++++++++++++++++++ test/services/image_codec_runtime_test.dart | 35 ++++++++++++ .../lib/flutter_onnxruntime.dart | 37 +++++++++++++ .../flutter_onnxruntime_stub/pubspec.yaml | 9 ++++ 12 files changed, 215 insertions(+), 13 deletions(-) create mode 100755 scripts/onnx.sh create mode 100644 test/services/image_codec_runtime_test.dart create mode 100644 third_party/flutter_onnxruntime_stub/lib/flutter_onnxruntime.dart create mode 100644 third_party/flutter_onnxruntime_stub/pubspec.yaml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6c9db6f6..022e1a4f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -12,6 +12,12 @@ on: - 'v*' pull_request: +env: + # ONNX Runtime for the Linux jobs. Its Linux downloads from + # github.com/microsoft/onnxruntime currently 404, so tagged Linux AppImages + # ship without image messages until this is set back to "on". + MESHCORE_LINUX_ONNX: "off" + jobs: android: runs-on: ubuntu-latest @@ -71,7 +77,7 @@ jobs: run: | shellcheck scripts/build_appimage.sh scripts/appimage/*.sh scripts/appimage/AppRun bash scripts/appimage/test_packaging.sh - - run: flutter pub get + - run: scripts/onnx.sh "$MESHCORE_LINUX_ONNX" - run: flutter build linux --release --no-pub - uses: docker/setup-buildx-action@v3 - name: Read AppImage base image @@ -141,7 +147,7 @@ jobs: run: | shellcheck scripts/build_appimage.sh scripts/appimage/*.sh scripts/appimage/AppRun bash scripts/appimage/test_packaging.sh - - run: flutter pub get + - run: scripts/onnx.sh "$MESHCORE_LINUX_ONNX" - run: flutter test --reporter expanded - name: Build ARM64 Linux release run: | diff --git a/.github/workflows/flutter_dart.yml b/.github/workflows/flutter_dart.yml index 93fdd230..3ac94bcc 100644 --- a/.github/workflows/flutter_dart.yml +++ b/.github/workflows/flutter_dart.yml @@ -38,3 +38,9 @@ jobs: - name: Run tests run: flutter test -r github + + - name: Analyze and test without ONNX Runtime + run: | + scripts/onnx.sh off + flutter analyze --fatal-infos --fatal-warnings + flutter test -r github diff --git a/.gitignore b/.gitignore index 6e4fb526..43dcc50b 100644 --- a/.gitignore +++ b/.gitignore @@ -94,3 +94,6 @@ keystore.properties # Claude Code local working dir (worktrees, jobs, settings) .claude/ + +# Written by scripts/onnx.sh off (ONNX-free build) +/pubspec_overrides.yaml diff --git a/README.md b/README.md index 958589d7..b05e7449 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,26 @@ The matrix describes implemented functionality, not a guarantee that every featu flutter run ``` +### Building without ONNX Runtime + +ONNX Runtime (ORT) runs only the [image message](documentation/image-messages.md) codec, and every platform build fetches it: Linux and Windows from GitHub releases, Android from Maven Central, iOS and macOS from CocoaPods/SwiftPM. If those downloads fail, build without it: + +```bash +scripts/onnx.sh off # stub flutter_onnxruntime, then flutter pub get +flutter build apk --release # or linux, windows, ios, macos +scripts/onnx.sh on # back to the default build +``` + +`off` writes a git-ignored `pubspec_overrides.yaml` that points `flutter_onnxruntime` at the pure-Dart stub in `third_party/flutter_onnxruntime_stub`, so no native plugin or ORT library is built, downloaded or bundled. On Windows without bash, create that file by hand: + +```yaml +dependency_overrides: + flutter_onnxruntime: + path: third_party/flutter_onnxruntime_stub +``` + +Image messages then behave as on web: the app cannot send them or reconstruct received ones, and Settings says the build has no ONNX Runtime. Everything else is unchanged. While the override is active, `flutter pub get` also rewrites the committed plugin registrants (`linux/`, `windows/`, `macos/Flutter/`); switch back before committing. Linux builds in CI (`.github/workflows/build.yml`) currently use this mode because the ORT Linux release downloads are unavailable. + ### Linux AppImage `flutter build linux --release` produces the regular Flutter bundle and an diff --git a/documentation/image-messages.md b/documentation/image-messages.md index 935dd398..1881ba0a 100644 --- a/documentation/image-messages.md +++ b/documentation/image-messages.md @@ -5,12 +5,16 @@ MeshCore Open can send a highly compressed image as channel GRP_DATA packets. Un ## Requirements and setup - Companion firmware version code **11 or later** (identified in the connector as v1.15.0) for the current channel-data layout. -- A native runtime with a working ONNX backend. The web build has no image codec backend. +- A native runtime with a working ONNX backend. The web build has no image codec backend, and neither does a native build made without ONNX Runtime (see below). - The image model bundle installed through App Settings → Image model, and Enable image messages turned on. - Enough storage for the roughly 1 GB bundle and enough available memory for reconstruction, which can peak above 2 GB. Actual latency and memory usage depend on hardware. Downloads use the configured Hugging Face model bundle, support resume, and verify file checksums. Download the model while internet is available. Once installed, encoding and reconstruction run locally; photos are not uploaded for inference. Compatible recipients need the same supported codec/model format. +### Builds without ONNX Runtime + +`scripts/onnx.sh off` builds the app with a pure-Dart stub in place of `flutter_onnxruntime`, so no platform downloads or bundles ORT ([how to build](../README.md#building-without-onnx-runtime)). In that build image messages are unavailable, exactly as on web: the image button and Settings explain that the build has no ONNX Runtime, the model cannot be downloaded, and received images cannot be reconstructed. `scripts/onnx.sh on` restores the default build. The app detects the stub because its `OnnxRuntime()` constructor throws (`imageCodecRuntimeAvailable` in [image_codec_backend.dart](../lib/services/image_codec_backend.dart)). + ## Send an image 1. Open channel chat and tap the image button beside the composer. diff --git a/lib/services/image_codec_backend.dart b/lib/services/image_codec_backend.dart index e453f565..8335acde 100644 --- a/lib/services/image_codec_backend.dart +++ b/lib/services/image_codec_backend.dart @@ -871,14 +871,33 @@ class ImageCodecCancelled implements Exception { String toString() => 'Image processing stopped.'; } +/// Whether this build can run the image codec at all. +/// +/// False on web and in a build made with `scripts/onnx.sh off`, where +/// `flutter_onnxruntime` is the pure-Dart stub whose [OnnxRuntime] constructor +/// throws [UnsupportedError]. The real constructor has no side effects. +final bool imageCodecRuntimeAvailable = _probeOnnxRuntime(); + +bool _probeOnnxRuntime() { + if (kIsWeb) { + return false; + } + try { + OnnxRuntime(); + return true; + } on UnsupportedError { + return false; + } +} + /// Factory used by the codec worker isolate. /// -/// Returns `null` on web, where there is no local model file and no isolate. -/// Everywhere else it returns the real [OnnxImageCodecBackend]; the session -/// turns a `null` here into an [ImageCodecUnimplemented], which the service -/// surfaces as `lastError` and maps to `ImageCodecAvailability.unavailable`. +/// Returns `null` when [imageCodecRuntimeAvailable] is false. Everywhere else +/// it returns the real [OnnxImageCodecBackend]; the session turns a `null` +/// here into an [ImageCodecUnimplemented], which the service surfaces as +/// `lastError` and maps to `ImageCodecAvailability.unavailable`. ImageCodecBackend? createImageCodecBackend() { - if (kIsWeb) { + if (!imageCodecRuntimeAvailable) { return null; } return OnnxImageCodecBackend(); diff --git a/lib/services/image_codec_service.dart b/lib/services/image_codec_service.dart index 932d90d7..2c5443a4 100644 --- a/lib/services/image_codec_service.dart +++ b/lib/services/image_codec_service.dart @@ -8,7 +8,8 @@ import '../models/image_codec_support.dart'; import '../utils/app_logger.dart'; import '../widgets/image_send_codec_binding.dart'; import 'app_settings_service.dart'; -import 'image_codec_backend.dart' show kImageCodecBitstreamPathAvailable; +import 'image_codec_backend.dart' + show imageCodecRuntimeAvailable, kImageCodecBitstreamPathAvailable; import 'image_codec_file_store.dart'; import 'image_codec_session.dart'; import 'image_codec_settings_store.dart'; @@ -177,9 +178,10 @@ class ImageCodecService extends ChangeNotifier implements ImageSendCodec { /// Why [availability] is `unavailable`, or null when it is not. /// - /// EXACTLY THREE CAUSES, all of them permanent properties of the build or the + /// EXACTLY FOUR CAUSES, all of them permanent properties of the build or the /// platform, none of them fixable by the user: /// * web, which has no native inference runtime; + /// * a build made without ONNX Runtime ([imageCodecRuntimeAvailable]); /// * a build compiled without the entropy path /// ([kImageCodecBitstreamPathAvailable]); /// * a backend that failed to load. @@ -194,6 +196,10 @@ class ImageCodecService extends ChangeNotifier implements ImageSendCodec { return 'The image codec needs a native inference runtime, which the web ' 'build does not have.'; } + if (!imageCodecRuntimeAvailable) { + return 'This build was made without ONNX Runtime, so it cannot send or ' + 'show image messages.'; + } if (!kImageCodecBitstreamPathAvailable) { return 'This build ships the image decoder only. Encoding and decoding a ' 'bitstream also needs the entropy-side graph and the rANS coder, ' @@ -510,7 +516,7 @@ class ImageCodecService extends ChangeNotifier implements ImageSendCodec { /// works. [canEncode]/[canDecode] additionally require the entropy path. bool get canRunInference { final model = selectedModel; - return !kIsWeb && + return imageCodecRuntimeAvailable && !_backendMissing && model != null && model.localPath.isNotEmpty; diff --git a/scripts/appimage/test_runtime.sh b/scripts/appimage/test_runtime.sh index c09e60f1..2bcf6ec5 100644 --- a/scripts/appimage/test_runtime.sh +++ b/scripts/appimage/test_runtime.sh @@ -72,8 +72,11 @@ appdir="$workspace/squashfs-root" log 'Checking extracted AppImage structure' [[ -x "$appdir/AppRun" && -f "$appdir/usr/bin/lib/libapp.so" && \ -f "$appdir/usr/bin/data/icudtl.dat" && -d "$appdir/usr/bin/data/flutter_assets" ]] -[[ -f "$appdir/usr/bin/lib/libonnxruntime.so.1" && \ - -f "$appdir/usr/share/glib-2.0/schemas/gschemas.compiled" ]] +[[ -f "$appdir/usr/share/glib-2.0/schemas/gschemas.compiled" ]] +# ONNX-free builds (scripts/onnx.sh off) have neither the plugin nor ORT. +if [[ -f "$appdir/usr/bin/lib/libflutter_onnxruntime_plugin.so" ]]; then + [[ -f "$appdir/usr/bin/lib/libonnxruntime.so.1" ]] +fi log 'Starting bundled ELF architecture audit' elf_count=0 diff --git a/scripts/onnx.sh b/scripts/onnx.sh new file mode 100755 index 00000000..5f3b9cdd --- /dev/null +++ b/scripts/onnx.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +# Switch ONNX Runtime (the image codec's inference engine) on or off. +# +# scripts/onnx.sh off build without ORT: no native plugin, no downloads +# scripts/onnx.sh on default build with ORT +# scripts/onnx.sh status print the current mode +# +# "off" writes pubspec_overrides.yaml, pointing flutter_onnxruntime at the +# pure-Dart stub in third_party/flutter_onnxruntime_stub; "on" removes it. +# Both run `flutter pub get` (set FLUTTER to use another flutter binary). +set -euo pipefail + +root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +overrides="$root/pubspec_overrides.yaml" +marker='# Written by scripts/onnx.sh' +flutter_bin="${FLUTTER:-flutter}" + +is_ours() { + [[ -f "$overrides" ]] && grep -Fq "$marker" "$overrides" +} + +case "${1:-}" in + off) + if [[ -f "$overrides" ]] && ! is_ours; then + echo "pubspec_overrides.yaml exists and was not written by this script; edit it by hand." >&2 + exit 1 + fi + cat > "$overrides" <&2 + exit 1 + fi + rm -f "$overrides" + ;; + status) + if is_ours; then echo off; else echo on; fi + exit 0 + ;; + *) + echo "usage: $0 off|on|status" >&2 + exit 2 + ;; +esac + +cd "$root" +"$flutter_bin" pub get +echo "ONNX Runtime: $1" diff --git a/test/services/image_codec_runtime_test.dart b/test/services/image_codec_runtime_test.dart new file mode 100644 index 00000000..e175903f --- /dev/null +++ b/test/services/image_codec_runtime_test.dart @@ -0,0 +1,35 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:meshcore_open/models/image_codec_support.dart'; +import 'package:meshcore_open/services/app_settings_service.dart'; +import 'package:meshcore_open/services/image_codec_backend.dart'; +import 'package:meshcore_open/services/image_codec_service.dart'; +import 'package:meshcore_open/services/image_codec_settings_store.dart'; +import 'package:meshcore_open/widgets/image_send_codec_binding.dart'; + +/// Runs in both build modes; each half skips in the other (`scripts/onnx.sh`). +void main() { + ImageCodecService newService() { + final service = ImageCodecService( + AppSettingsService(), + settingsStore: InMemoryImageCodecSettingsStore( + const ImageCodecPreferences(enabled: true), + ), + ); + addTearDown(service.dispose); + return service; + } + + test('ONNX-free build reports the image codec unavailable', () { + final service = newService(); + expect(createImageCodecBackend(), isNull); + expect(service.availability, ImageCodecAvailability.unavailable); + expect(service.unavailableReason, contains('without ONNX Runtime')); + expect(service.canRunInference, isFalse); + }, skip: imageCodecRuntimeAvailable ? 'ONNX Runtime is compiled in' : false); + + test('ONNX build has a codec backend', () { + final service = newService(); + expect(createImageCodecBackend(), isA()); + expect(service.unavailableReason, isNull); + }, skip: imageCodecRuntimeAvailable ? false : 'built without ONNX Runtime'); +} diff --git a/third_party/flutter_onnxruntime_stub/lib/flutter_onnxruntime.dart b/third_party/flutter_onnxruntime_stub/lib/flutter_onnxruntime.dart new file mode 100644 index 00000000..64bb6461 --- /dev/null +++ b/third_party/flutter_onnxruntime_stub/lib/flutter_onnxruntime.dart @@ -0,0 +1,37 @@ +/// Stand-in for `flutter_onnxruntime` in builds made without ONNX Runtime. +/// +/// Mirrors only the API MeshCore Open uses. [OnnxRuntime] cannot be +/// constructed, which is how the app detects this build. +library; + +UnsupportedError _unsupported() => + UnsupportedError('This build was made without ONNX Runtime.'); + +class OnnxRuntime { + OnnxRuntime() { + throw _unsupported(); + } + + Future createSession(String modelPath) => throw _unsupported(); +} + +abstract class OrtSession { + List get inputNames; + + List get outputNames; + + Future> run(Map inputs); + + Future close(); +} + +abstract class OrtValue { + List get shape; + + static Future fromList(dynamic data, List shape) => + throw _unsupported(); + + Future> asFlattenedList(); + + Future dispose(); +} diff --git a/third_party/flutter_onnxruntime_stub/pubspec.yaml b/third_party/flutter_onnxruntime_stub/pubspec.yaml new file mode 100644 index 00000000..4027cb5a --- /dev/null +++ b/third_party/flutter_onnxruntime_stub/pubspec.yaml @@ -0,0 +1,9 @@ +name: flutter_onnxruntime +description: >- + Pure-Dart stand-in for flutter_onnxruntime, used by `scripts/onnx.sh off` to + build MeshCore Open without ONNX Runtime. No native plugin, no downloads. +version: 1.9.0 +publish_to: none + +environment: + sdk: ^3.7.0