From cab6f7e5b46f04a63b9ab1323f62856fa298ccaa Mon Sep 17 00:00:00 2001 From: DongHyun Kim Date: Sat, 17 Feb 2024 12:51:51 +0900 Subject: [PATCH 1/3] Merge Cargokit branch --- .../cargokit/build_tool/lib/src/android_environment.dart | 4 ++-- flutter_ffi_plugin/cargokit/gradle/plugin.gradle | 2 +- flutter_ffi_plugin/cargokit/run_build_tool.sh | 8 +++++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/flutter_ffi_plugin/cargokit/build_tool/lib/src/android_environment.dart b/flutter_ffi_plugin/cargokit/build_tool/lib/src/android_environment.dart index f22db46e..9342964b 100755 --- a/flutter_ffi_plugin/cargokit/build_tool/lib/src/android_environment.dart +++ b/flutter_ffi_plugin/cargokit/build_tool/lib/src/android_environment.dart @@ -115,7 +115,7 @@ class AndroidEnvironment { final cxxKey = 'CXX_${target.rust}'; final cxxValue = path.join(toolchainPath, 'clang++$exe'); - final cxxfFlagsKey = 'CXXFLAGS_${target.rust}'; + final cxxFlagsKey = 'CXXFLAGS_${target.rust}'; final cxxFlagsValue = targetArg; final linkerKey = @@ -152,7 +152,7 @@ class AndroidEnvironment { ccKey: ccValue, cfFlagsKey: cFlagsValue, cxxKey: cxxValue, - cxxfFlagsKey: cxxFlagsValue, + cxxFlagsKey: cxxFlagsValue, ranlibKey: ranlibValue, rustFlagsKey: rustFlagsValue, linkerKey: selfPath, diff --git a/flutter_ffi_plugin/cargokit/gradle/plugin.gradle b/flutter_ffi_plugin/cargokit/gradle/plugin.gradle index 5127f606..c30ffac4 100755 --- a/flutter_ffi_plugin/cargokit/gradle/plugin.gradle +++ b/flutter_ffi_plugin/cargokit/gradle/plugin.gradle @@ -141,7 +141,7 @@ class CargoKitPlugin implements Plugin { } def task = project.tasks.create(taskName, CargoKitBuildTask.class) { - buildMode = variant.name + buildMode = variant.buildType.name buildDir = cargoBuildDir outputDir = cargoOutputDir ndkVersion = plugin.project.android.ndkVersion diff --git a/flutter_ffi_plugin/cargokit/run_build_tool.sh b/flutter_ffi_plugin/cargokit/run_build_tool.sh index 0e2c9735..6e594a23 100755 --- a/flutter_ffi_plugin/cargokit/run_build_tool.sh +++ b/flutter_ffi_plugin/cargokit/run_build_tool.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -e @@ -42,6 +42,12 @@ void main(List args) { } EOF +# Create alias for `shasum` if it does not exist and `sha1sum` exists +if ! [ -x "$(command -v shasum)" ] && [ -x "$(command -v sha1sum)" ]; then + shopt -s expand_aliases + alias shasum="sha1sum" +fi + # Dart run will not cache any package that has a path dependency, which # is the case for our build_tool_runner. So instead we precompile the package # ourselves. From 04d7fa47c6c69080904a07d4b196c044bc037caf Mon Sep 17 00:00:00 2001 From: DongHyun Kim Date: Sat, 17 Feb 2024 13:55:03 +0900 Subject: [PATCH 2/3] Make Dart's `lint:core` happy --- flutter_ffi_plugin/analysis_options.yaml | 1 + flutter_ffi_plugin/bin/rinf.dart | 2 +- flutter_ffi_plugin/bin/src/message.dart | 6 +++--- flutter_ffi_plugin/lib/rinf.dart | 1 + flutter_ffi_plugin/pubspec.yaml | 3 +++ 5 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 flutter_ffi_plugin/analysis_options.yaml diff --git a/flutter_ffi_plugin/analysis_options.yaml b/flutter_ffi_plugin/analysis_options.yaml new file mode 100644 index 00000000..c36c2c5d --- /dev/null +++ b/flutter_ffi_plugin/analysis_options.yaml @@ -0,0 +1 @@ +include: package:lints/core.yaml diff --git a/flutter_ffi_plugin/bin/rinf.dart b/flutter_ffi_plugin/bin/rinf.dart index f74b8c65..458321b7 100755 --- a/flutter_ffi_plugin/bin/rinf.dart +++ b/flutter_ffi_plugin/bin/rinf.dart @@ -3,7 +3,7 @@ import 'src/helpers.dart'; import 'src/message.dart'; Future main(List args) async { - if (args.length == 0) { + if (args.isEmpty) { print("No operation is provided."); print("Use `rinf --help` to see all available operations."); return; diff --git a/flutter_ffi_plugin/bin/src/message.dart b/flutter_ffi_plugin/bin/src/message.dart index b9bee3ee..b408e14e 100644 --- a/flutter_ffi_plugin/bin/src/message.dart +++ b/flutter_ffi_plugin/bin/src/message.dart @@ -88,7 +88,7 @@ Future generateMessageCode({ final subPath = entry.key; final resourceNames = entry.value; await Directory('$rustOutputPath$subPath').create(recursive: true); - if (resourceNames.length == 0) { + if (resourceNames.isEmpty) { continue; } final protoPaths = []; @@ -148,7 +148,7 @@ Future generateMessageCode({ final subPath = entry.key; final resourceNames = entry.value; await Directory('$dartOutputPath$subPath').create(recursive: true); - if (resourceNames.length == 0) { + if (resourceNames.isEmpty) { continue; } final protoPaths = []; @@ -180,7 +180,7 @@ Future generateMessageCode({ final subPath = entry.key; final filesAndMarks = entry.value; for (final entry in filesAndMarks.entries) { - if (entry.value.length == 0) { + if (entry.value.isEmpty) { continue; } final filename = entry.key; diff --git a/flutter_ffi_plugin/lib/rinf.dart b/flutter_ffi_plugin/lib/rinf.dart index 16617c56..77d812c5 100755 --- a/flutter_ffi_plugin/lib/rinf.dart +++ b/flutter_ffi_plugin/lib/rinf.dart @@ -1,4 +1,5 @@ /// This module supports communication with Rust. +library; import 'dart:typed_data'; import 'src/exports.dart'; diff --git a/flutter_ffi_plugin/pubspec.yaml b/flutter_ffi_plugin/pubspec.yaml index 5b99f5e0..ae6fed94 100644 --- a/flutter_ffi_plugin/pubspec.yaml +++ b/flutter_ffi_plugin/pubspec.yaml @@ -16,6 +16,9 @@ dependencies: ffi: ^2.1.0 yaml: ^3.1.2 +dev_dependencies: + lints: ^3.0.0 + # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec From 1872afb8e2706c82fa6d11bac564bfa57e660097 Mon Sep 17 00:00:00 2001 From: DongHyun Kim Date: Sat, 17 Feb 2024 14:24:22 +0900 Subject: [PATCH 3/3] Fix the problem on the web --- documentation/docs/running-and-building.md | 4 ++-- flutter_ffi_plugin/bin/src/helpers.dart | 12 ++++++++++-- flutter_ffi_plugin/bin/src/message.dart | 6 +++--- flutter_ffi_plugin/example/native/hub/Cargo.toml | 4 ++-- rust_crate/Cargo.toml | 2 +- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/documentation/docs/running-and-building.md b/documentation/docs/running-and-building.md index 929355bc..b9c7df89 100644 --- a/documentation/docs/running-and-building.md +++ b/documentation/docs/running-and-building.md @@ -44,7 +44,7 @@ flutter build web When deploying your web app, ensure that your web server is configured to include cross-origin-related HTTP headers in its responses. These headers enable web browsers using your website to gain access to `SharedArrayBuffer` web API, which is something similar to shared memory on the web. -- [`cross-origin-opener-policy`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Opener-Policy): `same-origin` -- [`cross-origin-embedder-policy`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Embedder-Policy): `require-corp`. +- [`Cross-Origin-Opener-Policy`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Opener-Policy): `same-origin` +- [`Cross-Origin-Embedder-Policy`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Embedder-Policy): `require-corp`. Additionally, don't forget to specify the MIME type `application/wasm` for `.wasm` files within the server configuration to ensure optimal performance. diff --git a/flutter_ffi_plugin/bin/src/helpers.dart b/flutter_ffi_plugin/bin/src/helpers.dart index e1597bd6..011e66a9 100644 --- a/flutter_ffi_plugin/bin/src/helpers.dart +++ b/flutter_ffi_plugin/bin/src/helpers.dart @@ -231,8 +231,16 @@ Future buildWebassembly({bool isReleaseMode = false}) async { await Process.run("cargo", ["install", "wasm-bindgen-cli"]); // Verify Flutter SDK web server's response headers. - print("Patching Flutter SDK's web server with CORS HTTP headers."); - await patchServerHeaders(); + try { + await patchServerHeaders(); + print("Patched Flutter SDK's web server with CORS HTTP headers."); + } catch (error) { + print("Failed patching Flutter's web server with CORS HTTP headers."); + print("Try using the command below."); + print('flutter run' + + ' --web-header=Cross-Origin-Opener-Policy=same-origin' + + ' --web-header=Cross-Origin-Embedder-Policy=require-corp'); + } // Prepare the webassembly output path. final flutterProjectPath = Directory.current; diff --git a/flutter_ffi_plugin/bin/src/message.dart b/flutter_ffi_plugin/bin/src/message.dart index b408e14e..8eda4984 100644 --- a/flutter_ffi_plugin/bin/src/message.dart +++ b/flutter_ffi_plugin/bin/src/message.dart @@ -464,7 +464,7 @@ Future patchServerHeaders() async { var serverFileContent = await serverFile.readAsString(); // Check if the server already includes cross-origin HTTP headers. - if (serverFileContent.contains('cross-origin-opener-policy')) { + if (serverFileContent.contains('Cross-Origin-Opener-Policy')) { return; } @@ -475,11 +475,11 @@ Future patchServerHeaders() async { ); lines.insert(serverDeclaredIndex + 1, """ httpServer.defaultResponseHeaders.add( - 'cross-origin-opener-policy', + 'Cross-Origin-Opener-Policy', 'same-origin', ); httpServer.defaultResponseHeaders.add( - 'cross-origin-embedder-policy', + 'Cross-Origin-Embedder-Policy', 'require-corp', );"""); serverFileContent = lines.join("\n"); diff --git a/flutter_ffi_plugin/example/native/hub/Cargo.toml b/flutter_ffi_plugin/example/native/hub/Cargo.toml index d806446b..7425b737 100755 --- a/flutter_ffi_plugin/example/native/hub/Cargo.toml +++ b/flutter_ffi_plugin/example/native/hub/Cargo.toml @@ -14,6 +14,6 @@ crate-type = ["lib", "cdylib", "staticlib"] [dependencies] rinf = "6.6.0" prost = "0.12.3" -wasm-bindgen = "0.2.90" -tokio_with_wasm = "0.4.0" +wasm-bindgen = "0.2.91" +tokio_with_wasm = "0.4.1" sample_crate = { path = "../sample_crate" } diff --git a/rust_crate/Cargo.toml b/rust_crate/Cargo.toml index dbceb6bd..05fc3550 100644 --- a/rust_crate/Cargo.toml +++ b/rust_crate/Cargo.toml @@ -18,4 +18,4 @@ allo-isolate = "0.1.24" [target.'cfg(target_family = "wasm")'.dependencies] js-sys = "0.3.67" -wasm-bindgen = "0.2.90" +wasm-bindgen = "0.2.91"