Package the Rust mobile core and generated Kotlin API for Android (#92)
Dependency security audit / rustsec (push) Failing after 24s
Dependency security audit / rustsec (push) Failing after 24s
This commit is contained in:
1 parent
e079b82b15
commit
fcb36a0684
42 files changed
+1586
-172
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import java.io.File
|
||||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||
|
||||
plugins {
|
||||
@@ -10,6 +11,8 @@ android {
|
||||
ndkVersion = libs.versions.ndk.get()
|
||||
defaultConfig {
|
||||
minSdk = libs.versions.minSdk.get().toInt()
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
ndk { abiFilters += listOf("arm64-v8a", "armeabi-v7a", "x86_64") }
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
@@ -28,5 +31,134 @@ kotlin {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("${libs.jna.get().module}:${libs.versions.jna.get()}@aar")
|
||||
implementation(libs.annotation)
|
||||
testImplementation(libs.junit)
|
||||
androidTestImplementation(libs.android.test.runner)
|
||||
}
|
||||
|
||||
val workspace = rootProject.layout.projectDirectory.dir("..").asFile
|
||||
val cargoExecutable = providers.environmentVariable("CARGO").orElse(
|
||||
providers.environmentVariable("PATH").map { path ->
|
||||
path.split(File.pathSeparatorChar).map { File(it, "cargo") }
|
||||
.firstOrNull { it.isFile && it.canExecute() }?.absolutePath
|
||||
?: error("Cargo must be installed and in PATH")
|
||||
}
|
||||
)
|
||||
val rustVersion = libs.versions.rust.get()
|
||||
val ndkDirectory = androidComponents.sdkComponents.ndkDirectory
|
||||
val sdkDirectory = androidComponents.sdkComponents.sdkDirectory
|
||||
val rustInputs = files(
|
||||
workspace.resolve("Cargo.toml"),
|
||||
workspace.resolve("Cargo.lock"),
|
||||
rootProject.file("rust-toolchain.toml"),
|
||||
fileTree(workspace.resolve("crates")) { include("**/*.rs", "**/Cargo.toml", "**/uniffi.toml") }
|
||||
)
|
||||
val checkRustToolchain = tasks.register<Exec>("checkRustToolchain") {
|
||||
workingDir = workspace
|
||||
commandLine(
|
||||
cargoExecutable.get(), "+$rustVersion", "run", "--locked",
|
||||
"--package", "ironstorage-android-build", "--", "check-toolchain",
|
||||
cargoExecutable.get(), rustVersion, ndkDirectory.get().asFile.absolutePath,
|
||||
libs.versions.ndk.get(), libs.versions.cargoNdk.get()
|
||||
)
|
||||
}
|
||||
androidComponents.onVariants { variant ->
|
||||
val name = variant.name
|
||||
val title = name.replaceFirstChar { it.uppercaseChar() }
|
||||
val rawNative = layout.buildDirectory.dir("generated/native-raw/$name/jniLibs")
|
||||
val nativeOutput = layout.buildDirectory.dir("generated/native/$name/jniLibs")
|
||||
val rawBindings = layout.buildDirectory.dir("generated/bindings-raw/$name")
|
||||
val bindings = layout.buildDirectory.dir("generated/bindings/$name")
|
||||
val native = tasks.register<Exec>("build${title}Rust") {
|
||||
dependsOn(checkRustToolchain)
|
||||
workingDir = workspace
|
||||
inputs.files(rustInputs)
|
||||
inputs.property("rustVersion", rustVersion)
|
||||
inputs.property("cargoNdk", libs.versions.cargoNdk.get())
|
||||
inputs.property("ndkVersion", libs.versions.ndk.get())
|
||||
inputs.property("minSdk", libs.versions.minSdk.get())
|
||||
outputs.dir(rawNative)
|
||||
environment("ANDROID_NDK_HOME", ndkDirectory.get().asFile.absolutePath)
|
||||
environment("ANDROID_HOME", sdkDirectory.get().asFile.absolutePath)
|
||||
environment(
|
||||
"RUSTFLAGS",
|
||||
"-D warnings -C link-arg=-Wl,-z,max-page-size=16384 -C link-arg=-Wl,-z,common-page-size=16384"
|
||||
)
|
||||
environment("CARGO_PROFILE_RELEASE_DEBUG", "2")
|
||||
commandLine(
|
||||
listOf(
|
||||
cargoExecutable.get(), "+$rustVersion", "ndk",
|
||||
"--target", "arm64-v8a",
|
||||
"--target", "armeabi-v7a",
|
||||
"--target", "x86_64",
|
||||
"--platform", libs.versions.minSdk.get(),
|
||||
"--output-dir", rawNative.get().asFile.absolutePath,
|
||||
"build", "--locked", "--package", "ironstorage-android", "--lib"
|
||||
) +
|
||||
if (variant.buildType == "release") listOf("--release") else emptyList()
|
||||
)
|
||||
}
|
||||
val audit = tasks.register<Exec>("package${title}Rust") {
|
||||
dependsOn(native)
|
||||
workingDir = workspace
|
||||
inputs.dir(rawNative)
|
||||
inputs.files(
|
||||
fileTree(workspace.resolve("tools/android-build")) {
|
||||
include("**/*.rs", "Cargo.toml")
|
||||
}
|
||||
)
|
||||
outputs.dir(nativeOutput)
|
||||
commandLine(
|
||||
cargoExecutable.get(), "+$rustVersion", "run", "--locked",
|
||||
"--package", "ironstorage-android-build", "--", "package-native",
|
||||
rawNative.get().asFile.absolutePath, nativeOutput.get().asFile.absolutePath
|
||||
)
|
||||
}
|
||||
val generate = tasks.register<Exec>("generate${title}RustBindings") {
|
||||
dependsOn(audit)
|
||||
workingDir = workspace
|
||||
inputs.dir(nativeOutput)
|
||||
inputs.file(rootProject.file("uniffi.toml"))
|
||||
inputs.files(
|
||||
fileTree(workspace.resolve("tools/android-build")) {
|
||||
include("**/*.rs", "Cargo.toml")
|
||||
}
|
||||
)
|
||||
outputs.dir(rawBindings)
|
||||
commandLine(
|
||||
cargoExecutable.get(), "+$rustVersion", "run", "--locked",
|
||||
"--package", "ironstorage-android-build", "--bin", "ironstorage-uniffi-bindgen",
|
||||
"--", "generate", "--library",
|
||||
nativeOutput.get().file(
|
||||
"arm64-v8a/libironstorage_android.so"
|
||||
).asFile.absolutePath,
|
||||
"--language", "kotlin", "--config",
|
||||
rootProject.file(
|
||||
"uniffi.toml"
|
||||
).absolutePath,
|
||||
"--out-dir", rawBindings.get().asFile.absolutePath, "--no-format"
|
||||
)
|
||||
}
|
||||
val prepare = tasks.register<Exec>("prepare${title}RustBindings") {
|
||||
dependsOn(generate)
|
||||
workingDir = workspace
|
||||
inputs.dir(rawBindings)
|
||||
inputs.files(
|
||||
fileTree(workspace.resolve("tools/android-build")) {
|
||||
include("**/*.rs", "Cargo.toml")
|
||||
}
|
||||
)
|
||||
outputs.dir(bindings)
|
||||
commandLine(
|
||||
cargoExecutable.get(), "+$rustVersion", "run", "--locked",
|
||||
"--package", "ironstorage-android-build", "--", "prepare-bindings",
|
||||
rawBindings.get().asFile.absolutePath, bindings.get().asFile.absolutePath
|
||||
)
|
||||
}
|
||||
variant.sources.kotlin!!.addStaticSourceDirectory(bindings.get().asFile.absolutePath)
|
||||
variant.sources.jniLibs!!.addStaticSourceDirectory(nativeOutput.get().asFile.absolutePath)
|
||||
tasks.matching {
|
||||
it.name == "pre${title}Build" || it.name == "compile${title}Kotlin"
|
||||
}.configureEach { dependsOn(prepare) }
|
||||
}
|
||||
Reference in new issue
Block a user