|
| 1 | +// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +group 'dev.dart.flutter_wasm' |
| 6 | +version '1.0-SNAPSHOT' |
| 7 | + |
| 8 | +import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform |
| 9 | + |
| 10 | +buildscript { |
| 11 | + ext.kotlin_version = '1.3.50' |
| 12 | + repositories { |
| 13 | + google() |
| 14 | + mavenCentral() |
| 15 | + } |
| 16 | + |
| 17 | + dependencies { |
| 18 | + classpath 'com.android.tools.build:gradle:4.1.0' |
| 19 | + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +rootProject.allprojects { |
| 24 | + repositories { |
| 25 | + google() |
| 26 | + mavenCentral() |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +apply plugin: 'com.android.library' |
| 31 | +apply plugin: 'kotlin-android' |
| 32 | + |
| 33 | +android { |
| 34 | + compileSdkVersion 30 |
| 35 | + |
| 36 | + compileOptions { |
| 37 | + sourceCompatibility JavaVersion.VERSION_1_8 |
| 38 | + targetCompatibility JavaVersion.VERSION_1_8 |
| 39 | + } |
| 40 | + |
| 41 | + kotlinOptions { |
| 42 | + jvmTarget = '1.8' |
| 43 | + } |
| 44 | + |
| 45 | + sourceSets { |
| 46 | + main.java.srcDirs += 'src/main/kotlin' |
| 47 | + } |
| 48 | + |
| 49 | + defaultConfig { |
| 50 | + minSdkVersion 16 |
| 51 | + } |
| 52 | + |
| 53 | + def hostOs = DefaultNativePlatform.currentOperatingSystem.getName().toLowerCase() |
| 54 | + def hostArch = DefaultNativePlatform.currentArchitecture.getName().replaceAll('-', '_') |
| 55 | + def ndkBinDir = "${android.ndkDirectory}/toolchains/llvm/prebuilt/${hostOs}-${hostArch}/bin" |
| 56 | + def projectProperties = new Properties() |
| 57 | + projectProperties.load(project.rootProject.file('local.properties').newDataInputStream()) |
| 58 | + def flutterDir = projectProperties.getProperty('flutter.sdk') |
| 59 | + def platformVersion = -1 |
| 60 | + fileTree("${android.ndkDirectory}/platforms").visit { FileVisitDetails details -> |
| 61 | + if (details.isDirectory() && details.name.startsWith("android-")) { |
| 62 | + platformVersion = Math.max(platformVersion, details.name.substring(8) as int) |
| 63 | + } |
| 64 | + } |
| 65 | + if (platformVersion < 0) { |
| 66 | + throw new Exception("Can't find any valid platforms in ${android.ndkDirectory}/platforms") |
| 67 | + } |
| 68 | + |
| 69 | + tasks.register('wasm-pub-get') { |
| 70 | + doLast { |
| 71 | + exec { |
| 72 | + workingDir '..' |
| 73 | + commandLine "${flutterDir}/bin/flutter", 'pub', 'get' |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + def architectures = [ |
| 79 | + ['arm64-v8a', 'aarch64-linux-android', 'aarch64-linux-android', 'aarch64-linux-android', 'arch-arm64'], |
| 80 | + ['x86', 'i686-linux-android', 'i686-linux-android', 'i686-linux-android', 'arch-x86'], |
| 81 | + |
| 82 | + // TODO(#53,#54): Enable these when they're supported by Wasmer. |
| 83 | + // ['armeabi-v7a', 'armv7a-linux-androideabi', 'arm-linux-androideabi', 'armv7-linux-androideabi', 'arch-arm'], |
| 84 | + // ['x86_64', 'x86_64-linux-android', 'x86_64-linux-android', 'x86_64-linux-android', 'arch-x86_64'], |
| 85 | + ] |
| 86 | + for (arch in architectures) { |
| 87 | + def abi = arch[0] |
| 88 | + def clangPrefix = "${arch[1]}${platformVersion}" |
| 89 | + def arPrefix = arch[2] |
| 90 | + def rustTriple = arch[3] |
| 91 | + def sysroot = arch[4] |
| 92 | + def sysrootDir = "${android.ndkDirectory}/platforms/android-${platformVersion}/${sysroot}" |
| 93 | + def outDir = "${rootDir}/../build/app/intermediates/stripped_native_libs/debug/out/lib/${abi}/" |
| 94 | + |
| 95 | + tasks.register("wasm-lib-${abi}") { |
| 96 | + // Specify inputs and outputs so that incremental build works properly. |
| 97 | + inputs.property('platformVersion', platformVersion) |
| 98 | + inputs.property('abi', abi) |
| 99 | + outputs.file("${outDir}/libwasmer.so") |
| 100 | + |
| 101 | + dependsOn('wasm-pub-get') |
| 102 | + |
| 103 | + doLast { |
| 104 | + exec { |
| 105 | + workingDir '..' |
| 106 | + commandLine "${flutterDir}/bin/flutter", 'pub', 'run', 'wasm:setup', |
| 107 | + '--sysroot', sysrootDir, |
| 108 | + '--target', rustTriple, |
| 109 | + '--clang', "${ndkBinDir}/${clangPrefix}-clang", |
| 110 | + '--clangpp', "${ndkBinDir}/${clangPrefix}-clang++", |
| 111 | + '--ar', "${ndkBinDir}/${arPrefix}-ar", |
| 112 | + '-o', outDir |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + tasks.withType(JavaCompile) { compileTask -> |
| 119 | + for (arch in architectures) { |
| 120 | + def abi = arch[0] |
| 121 | + compileTask.dependsOn("wasm-lib-${abi}") |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +dependencies { |
| 127 | + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" |
| 128 | +} |
0 commit comments