Skip to content
This repository has been archived by the owner on Jul 1, 2023. It is now read-only.

Initial commit of package:flutter_wasm #55

Merged
merged 23 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions .github/workflows/test-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,57 @@ jobs:
- run: dart pub upgrade
- run: dart run wasm:setup
- run: dart test

flutter_analyze:
runs-on: ubuntu-latest
defaults:
run:
working-directory: flutter_wasm
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
with:
java-version: '12.x'
- uses: subosito/flutter-action@v1.5.3
with:
flutter-version: '2.5.0'
- id: install
run: dart pub upgrade
- run: dart format --output=none --set-exit-if-changed .
if: always() && steps.install.outcome == 'success'
- run: dart analyze --fatal-infos
kevmoo marked this conversation as resolved.
Show resolved Hide resolved
if: always() && steps.install.outcome == 'success'

flutter_test:
runs-on: ubuntu-latest
defaults:
run:
working-directory: flutter_wasm
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
with:
java-version: '12.x'
- uses: subosito/flutter-action@v1.5.3
with:
flutter-version: '2.5.0'
- run: flutter pub get
- run: flutter test

flutter_example_test:
runs-on: ubuntu-latest
defaults:
run:
working-directory: flutter_wasm/example
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
with:
java-version: '12.x'
- uses: subosito/flutter-action@v1.5.3
with:
flutter-version: '2.5.0'
- run: flutter pub get
- run: flutter test
- run: flutter build apk
- run: flutter build appbundle
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ These packages provide utilities for loading and running WASM modules.

Runs WASM modules in Dart native.

## flutter_wasm (coming soon)
## [flutter_wasm](https://github.com/dart-lang/wasm/blob/main/flutter_wasm/README.md)

Runs WASM modules in Flutter.
29 changes: 29 additions & 0 deletions flutter_wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
.packages
build/
10 changes: 10 additions & 0 deletions flutter_wasm/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: d5396898bed65dacfb355fb6e52ec7ccf76746d9
channel: unknown

project_type: plugin
3 changes: 3 additions & 0 deletions flutter_wasm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.1

- Initial version
22 changes: 22 additions & 0 deletions flutter_wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Provides utilities for loading and running WASM modules in Flutter apps.
Currently only Android is supported.

This is a wrapper around [package:wasm](https://github.com/dart-lang/wasm/blob/main/wasm/README.md).
See that package for more information and documentation. The basic
usage is mostly the same as in that package. The main thing this plugin does is
run `wasm:setup` for your target device during app compilation.

## Usage

1. Add a dependency to *both* package `wasm` and `flutter_wasm` in
`pubspec.yaml` and run `flutter pub get`. [#52](https://github.com/dart-lang/wasm/issues/52)

1. Next run `flutter run wasm:setup` to build the Wasmer runtime for your host
machine. This does not build the runtime for your target device. It will take a
few minutes.

1. Load your wasm code in your app. See the [example app](https://github.com/dart-lang/wasm/blob/main/flutter_wasm/example/lib/main.dart).

1. Run your app using `flutter run`. If you see an error at runtime saying
"libwasmer.so not found", just try rebuiling. The first build sometimes fails.
[#51](https://github.com/dart-lang/wasm/issues/51)
8 changes: 8 additions & 0 deletions flutter_wasm/android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
128 changes: 128 additions & 0 deletions flutter_wasm/android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

group 'dev.dart.flutter_wasm'
version '1.0-SNAPSHOT'

import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform

buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

rootProject.allprojects {
repositories {
google()
mavenCentral()
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 30

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = '1.8'
}

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}

defaultConfig {
minSdkVersion 16
}

def hostOs = DefaultNativePlatform.currentOperatingSystem.getName().toLowerCase()
def hostArch = DefaultNativePlatform.currentArchitecture.getName().replaceAll('-', '_')
def ndkBinDir = "${android.ndkDirectory}/toolchains/llvm/prebuilt/${hostOs}-${hostArch}/bin"
def projectProperties = new Properties()
projectProperties.load(project.rootProject.file('local.properties').newDataInputStream())
def flutterDir = projectProperties.getProperty('flutter.sdk')
def platformVersion = -1
fileTree("${android.ndkDirectory}/platforms").visit { FileVisitDetails details ->
if (details.isDirectory() && details.name.startsWith("android-")) {
platformVersion = Math.max(platformVersion, details.name.substring(8) as int)
}
}
if (platformVersion < 0) {
throw new Exception("Can't find any valid platforms in ${android.ndkDirectory}/platforms")
}

tasks.register('wasm-pub-get') {
doLast {
exec {
workingDir '..'
commandLine "${flutterDir}/bin/flutter", 'pub', 'get'
}
}
}

def architectures = [
['arm64-v8a', 'aarch64-linux-android', 'aarch64-linux-android', 'aarch64-linux-android', 'arch-arm64'],
['x86', 'i686-linux-android', 'i686-linux-android', 'i686-linux-android', 'arch-x86'],

// TODO(#53,#54): Enable these when they're supported by Wasmer.
// ['armeabi-v7a', 'armv7a-linux-androideabi', 'arm-linux-androideabi', 'armv7-linux-androideabi', 'arch-arm'],
// ['x86_64', 'x86_64-linux-android', 'x86_64-linux-android', 'x86_64-linux-android', 'arch-x86_64'],
]
for (arch in architectures) {
def abi = arch[0]
def clangPrefix = "${arch[1]}${platformVersion}"
def arPrefix = arch[2]
def rustTriple = arch[3]
def sysroot = arch[4]
def sysrootDir = "${android.ndkDirectory}/platforms/android-${platformVersion}/${sysroot}"
def outDir = "${rootDir}/../build/app/intermediates/stripped_native_libs/debug/out/lib/${abi}/"

tasks.register("wasm-lib-${abi}") {
// Specify inputs and outputs so that incremental build works properly.
inputs.property('platformVersion', platformVersion)
inputs.property('abi', abi)
outputs.file("${outDir}/libwasmer.so")

dependsOn('wasm-pub-get')

doLast {
exec {
workingDir '..'
commandLine "${flutterDir}/bin/flutter", 'pub', 'run', 'wasm:setup',
'--sysroot', sysrootDir,
'--target', rustTriple,
'--clang', "${ndkBinDir}/${clangPrefix}-clang",
'--clangpp', "${ndkBinDir}/${clangPrefix}-clang++",
'--ar', "${ndkBinDir}/${arPrefix}-ar",
'-o', outDir
}
}
}
}

tasks.withType(JavaCompile) { compileTask ->
for (arch in architectures) {
def abi = arch[0]
compileTask.dependsOn("wasm-lib-${abi}")
}
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
1 change: 1 addition & 0 deletions flutter_wasm/android/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'flutter_wasm'
3 changes: 3 additions & 0 deletions flutter_wasm/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.dart.flutter_wasm">
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

package dev.dart.flutter_wasm

import androidx.annotation.NonNull

import io.flutter.embedding.engine.plugins.FlutterPlugin

/** FlutterWasmPlugin */
class FlutterWasmPlugin: FlutterPlugin {
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {}
}
46 changes: 46 additions & 0 deletions flutter_wasm/example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
/build/

# Web related
lib/generated_plugin_registrant.dart

# Symbolication related
app.*.symbols

# Obfuscation related
app.*.map.json

# Android Studio will place build artifacts here
/android/app/debug
/android/app/profile
/android/app/release
10 changes: 10 additions & 0 deletions flutter_wasm/example/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: d5396898bed65dacfb355fb6e52ec7ccf76746d9
channel: unknown

project_type: app
16 changes: 16 additions & 0 deletions flutter_wasm/example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# flutter_wasm_example

Demonstrates how to use the flutter_wasm plugin.

## Getting Started

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook)

For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
13 changes: 13 additions & 0 deletions flutter_wasm/example/android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
gradle-wrapper.jar
/.gradle
/captures/
/gradlew
/gradlew.bat
/local.properties
GeneratedPluginRegistrant.java

# Remember to never publicly share your keystore.
# See https://flutter.dev/docs/deployment/android#reference-the-keystore-from-the-app
key.properties
**/*.keystore
**/*.jks
Loading