Add custom Gradle plugin to build bdk-android library
This commit is contained in:
parent
758608419b
commit
51f978e78e
@ -1,5 +1,18 @@
|
||||
package org.bitcoindevkit.plugins
|
||||
|
||||
|
||||
val operatingSystem: OS = when {
|
||||
System.getProperty("os.name").contains("mac", ignoreCase = true) -> OS.MAC
|
||||
System.getProperty("os.name").contains("linux", ignoreCase = true) -> OS.LINUX
|
||||
else -> OS.OTHER
|
||||
}
|
||||
|
||||
val architecture: Arch = when (System.getProperty("os.arch")) {
|
||||
"x86_64" -> Arch.X86_64
|
||||
"aarch64" -> Arch.AARCH64
|
||||
else -> Arch.OTHER
|
||||
}
|
||||
|
||||
enum class Arch {
|
||||
AARCH64,
|
||||
X86_64,
|
||||
|
@ -2,90 +2,117 @@ package org.bitcoindevkit.plugins
|
||||
|
||||
import org.gradle.kotlin.dsl.register
|
||||
|
||||
val operatingSystem: OS = when {
|
||||
System.getProperty("os.name").contains("mac", ignoreCase = true) -> OS.MAC
|
||||
System.getProperty("os.name").contains("linux", ignoreCase = true) -> OS.LINUX
|
||||
else -> OS.OTHER
|
||||
}
|
||||
val architecture: Arch = when (System.getProperty("os.arch")) {
|
||||
"x86_64" -> Arch.X86_64
|
||||
"aarch64" -> Arch.AARCH64
|
||||
else -> Arch.OTHER
|
||||
val llvmArchPath = when (operatingSystem) {
|
||||
OS.MAC -> "darwin-x86_64"
|
||||
OS.LINUX -> "linux-x86_64"
|
||||
OS.OTHER -> throw Error("Cannot build Android library from current architecture")
|
||||
}
|
||||
|
||||
// register a task of type Exec called buildJvmBinary
|
||||
// which will run something like
|
||||
// cargo build --release --target aarch64-apple-darwin
|
||||
val buildJvmBinary by tasks.register<Exec>("buildJvmBinary") {
|
||||
// group = "Bitcoindevkit"
|
||||
// description = "Build the JVM binaries for the bitcoindevkit"
|
||||
// arm64-v8a is the most popular hardware architecture for Android
|
||||
val buildAndroidAarch64Binary by tasks.register<Exec>("buildAndroidAarch64Binary") {
|
||||
|
||||
workingDir("${project.projectDir}/../bdk-ffi")
|
||||
val cargoArgs: MutableList<String> = mutableListOf("build", "--release", "--target")
|
||||
|
||||
if (operatingSystem == OS.MAC && architecture == Arch.X86_64) {
|
||||
cargoArgs.add("x86_64-apple-darwin")
|
||||
} else if (operatingSystem == OS.MAC && architecture == Arch.AARCH64) {
|
||||
cargoArgs.add("aarch64-apple-darwin")
|
||||
} else if (operatingSystem == OS.LINUX) {
|
||||
cargoArgs.add("x86_64-unknown-linux-gnu")
|
||||
}
|
||||
val cargoArgs: MutableList<String> = mutableListOf("build", "--release", "--target", "aarch64-linux-android")
|
||||
|
||||
executable("cargo")
|
||||
args(cargoArgs)
|
||||
|
||||
// if ANDROID_NDK_ROOT is not set then set it to github actions default
|
||||
if (System.getenv("ANDROID_NDK_ROOT") == null) {
|
||||
environment(
|
||||
Pair("ANDROID_NDK_ROOT", "${System.getenv("ANDROID_SDK_ROOT")}/ndk-bundle")
|
||||
)
|
||||
}
|
||||
|
||||
environment(
|
||||
// add build toolchain to PATH
|
||||
Pair("PATH", "${System.getenv("PATH")}:${System.getenv("ANDROID_NDK_ROOT")}/toolchains/llvm/prebuilt/$llvmArchPath/bin"),
|
||||
|
||||
Pair("CFLAGS", "-D__ANDROID_API__=21"),
|
||||
Pair("CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER", "aarch64-linux-android21-clang"),
|
||||
Pair("CC", "aarch64-linux-android21-clang")
|
||||
)
|
||||
|
||||
doLast {
|
||||
println("Native library for bdk-jvm on ${cargoArgs.last()} successfully built")
|
||||
println("Native library for bdk-android on aarch64 built successfully")
|
||||
}
|
||||
}
|
||||
//
|
||||
// // move the native libs build by cargo from bdk-ffi/target/.../release/
|
||||
// // to their place in the bdk-jvm library
|
||||
// val moveNativeJvmLib by tasks.register<Copy>("moveNativeJvmLib") {
|
||||
// // group = "Bitcoindevkit"
|
||||
// // description = "Move the native libraries to the bdk-jvm project"
|
||||
// dependsOn(buildJvmBinary)
|
||||
//
|
||||
// var targetDir = ""
|
||||
// var resDir = ""
|
||||
// if (operatingSystem == OS.MAC && architecture == Arch.X86_64) {
|
||||
// targetDir = "x86_64-apple-darwin"
|
||||
// resDir = "darwin-x86-64"
|
||||
// } else if (operatingSystem == OS.MAC && architecture == Arch.AARCH64) {
|
||||
// targetDir = "aarch64-apple-darwin"
|
||||
// resDir = "darwin-aarch64"
|
||||
// } else if (operatingSystem == OS.LINUX) {
|
||||
// targetDir = "x86_64-unknown-linux-gnu"
|
||||
// resDir = "linux-x86-64"
|
||||
// }
|
||||
//
|
||||
// from("${project.projectDir}/../bdk-ffi/target/$targetDir/release/libbdkffi.dylib")
|
||||
// into("${project.projectDir}/../jvm/src/main/resources/$resDir/")
|
||||
//
|
||||
// doLast {
|
||||
// println("$targetDir native binaries for JVM moved to ./jvm/src/main/resources/$resDir/")
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // generate the bindings using the bdk-ffi-bindgen tool
|
||||
// // created in the bdk-ffi submodule
|
||||
// val generateJvmBindings by tasks.register<Exec>("generateJvmBindings") {
|
||||
// // group = "Bitcoindevkit"
|
||||
// // description = "Building the bindings file for the bitcoindevkit"
|
||||
// dependsOn(moveNativeJvmLib)
|
||||
//
|
||||
// workingDir("${project.projectDir}/../bdk-ffi")
|
||||
// executable("cargo")
|
||||
// args("run", "--package", "bdk-ffi-bindgen", "--", "--language", "kotlin", "--out-dir", "../jvm/src/main/kotlin")
|
||||
//
|
||||
// doLast {
|
||||
// println("JVM bindings file successfully created")
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// tasks.register("buildAndroidLib") {
|
||||
// group = "Bitcoindevkit"
|
||||
// description = "Aggregate task to build Android library"
|
||||
// dependsOn(buildJvmBinary, moveNativeJvmLib, generateJvmBindings)
|
||||
// }
|
||||
|
||||
// the x86_64 version of the library is mostly used by emulators
|
||||
val buildAndroidX86_64Binary by tasks.register<Exec>("buildAndroidX86_64Binary") {
|
||||
|
||||
workingDir("${project.projectDir}/../bdk-ffi")
|
||||
val cargoArgs: MutableList<String> = mutableListOf("build", "--release", "--target", "x86_64-linux-android")
|
||||
|
||||
executable("cargo")
|
||||
args(cargoArgs)
|
||||
|
||||
// if ANDROID_NDK_ROOT is not set then set it to github actions default
|
||||
if (System.getenv("ANDROID_NDK_ROOT") == null) {
|
||||
environment(
|
||||
Pair("ANDROID_NDK_ROOT", "${System.getenv("ANDROID_SDK_ROOT")}/ndk-bundle")
|
||||
)
|
||||
}
|
||||
|
||||
environment(
|
||||
// add build toolchain to PATH
|
||||
Pair("PATH", "${System.getenv("PATH")}:${System.getenv("ANDROID_NDK_ROOT")}/toolchains/llvm/prebuilt/$llvmArchPath/bin"),
|
||||
|
||||
Pair("CFLAGS", "-D__ANDROID_API__=21"),
|
||||
Pair("CARGO_TARGET_X86_64_LINUX_ANDROID_LINKER", "x86_64-linux-android21-clang"),
|
||||
Pair("CC", "x86_64-linux-android21-clang")
|
||||
)
|
||||
|
||||
doLast {
|
||||
println("Native library for bdk-android on x86_64 built successfully")
|
||||
}
|
||||
}
|
||||
|
||||
// move the native libs build by cargo from bdk-ffi/target/<architecture>/release/
|
||||
// to their place in the bdk-android library
|
||||
// the task only copies the available binaries built using the buildAndroid<architecture>Binary tasks
|
||||
val moveNativeAndroidLibs by tasks.register<Copy>("moveNativeAndroidLibs") {
|
||||
|
||||
dependsOn(buildAndroidAarch64Binary)
|
||||
|
||||
into("${project.projectDir}/../android/src/main/jniLibs/")
|
||||
|
||||
into("arm64-v8a") {
|
||||
from("${project.projectDir}/../bdk-ffi/target/aarch64-linux-android/release/libbdkffi.so")
|
||||
}
|
||||
|
||||
into("x86_64") {
|
||||
from("${project.projectDir}/../bdk-ffi/target/x86_64-linux-android/release/libbdkffi.so")
|
||||
}
|
||||
|
||||
doLast {
|
||||
println("Native binaries for Android moved to ./android/src/main/jniLibs/")
|
||||
}
|
||||
}
|
||||
|
||||
// generate the bindings using the bdk-ffi-bindgen tool located in the bdk-ffi submodule
|
||||
val generateAndroidBindings by tasks.register<Exec>("generateAndroidBindings") {
|
||||
dependsOn(moveNativeAndroidLibs)
|
||||
|
||||
workingDir("${project.projectDir}/../bdk-ffi")
|
||||
executable("cargo")
|
||||
args("run", "--package", "bdk-ffi-bindgen", "--", "--language", "kotlin", "--out-dir", "../android/src/main/kotlin")
|
||||
|
||||
doLast {
|
||||
println("Android bindings file successfully created")
|
||||
}
|
||||
}
|
||||
|
||||
// create an aggregate task which will run the required tasks to build the Android libs in order
|
||||
// the task will also appear in the printout of the ./gradlew tasks task with group and description
|
||||
tasks.register("buildAndroidLib") {
|
||||
group = "Bitcoindevkit"
|
||||
description = "Aggregate task to build Android library"
|
||||
|
||||
dependsOn(
|
||||
buildAndroidAarch64Binary,
|
||||
buildAndroidX86_64Binary,
|
||||
moveNativeAndroidLibs,
|
||||
generateAndroidBindings
|
||||
)
|
||||
}
|
||||
|
@ -1,22 +1,9 @@
|
||||
package org.bitcoindevkit.plugins
|
||||
|
||||
val operatingSystem: OS = when {
|
||||
System.getProperty("os.name").contains("mac", ignoreCase = true) -> OS.MAC
|
||||
System.getProperty("os.name").contains("linux", ignoreCase = true) -> OS.LINUX
|
||||
else -> OS.OTHER
|
||||
}
|
||||
val architecture: Arch = when (System.getProperty("os.arch")) {
|
||||
"x86_64" -> Arch.X86_64
|
||||
"aarch64" -> Arch.AARCH64
|
||||
else -> Arch.OTHER
|
||||
}
|
||||
|
||||
// register a task of type Exec called buildJvmBinary
|
||||
// which will run something like
|
||||
// cargo build --release --target aarch64-apple-darwin
|
||||
val buildJvmBinary by tasks.register<Exec>("buildJvmBinary") {
|
||||
// group = "Bitcoindevkit"
|
||||
// description = "Build the JVM binaries for the bitcoindevkit"
|
||||
|
||||
workingDir("${project.projectDir}/../bdk-ffi")
|
||||
val cargoArgs: MutableList<String> = mutableListOf("build", "--release", "--target")
|
||||
@ -40,8 +27,7 @@ val buildJvmBinary by tasks.register<Exec>("buildJvmBinary") {
|
||||
// move the native libs build by cargo from bdk-ffi/target/.../release/
|
||||
// to their place in the bdk-jvm library
|
||||
val moveNativeJvmLib by tasks.register<Copy>("moveNativeJvmLib") {
|
||||
// group = "Bitcoindevkit"
|
||||
// description = "Move the native libraries to the bdk-jvm project"
|
||||
|
||||
dependsOn(buildJvmBinary)
|
||||
|
||||
var targetDir = ""
|
||||
@ -68,8 +54,7 @@ val moveNativeJvmLib by tasks.register<Copy>("moveNativeJvmLib") {
|
||||
// generate the bindings using the bdk-ffi-bindgen tool
|
||||
// created in the bdk-ffi submodule
|
||||
val generateJvmBindings by tasks.register<Exec>("generateJvmBindings") {
|
||||
// group = "Bitcoindevkit"
|
||||
// description = "Building the bindings file for the bitcoindevkit"
|
||||
|
||||
dependsOn(moveNativeJvmLib)
|
||||
|
||||
workingDir("${project.projectDir}/../bdk-ffi")
|
||||
@ -81,8 +66,15 @@ val generateJvmBindings by tasks.register<Exec>("generateJvmBindings") {
|
||||
}
|
||||
}
|
||||
|
||||
// create an aggregate task which will run the 3 required tasks to build the JVM libs in order
|
||||
// the task will also appear in the printout of the ./gradlew tasks task with group and description
|
||||
tasks.register("buildJvmLib") {
|
||||
group = "Bitcoindevkit"
|
||||
description = "Aggregate task to build JVM library"
|
||||
dependsOn(buildJvmBinary, moveNativeJvmLib, generateJvmBindings)
|
||||
|
||||
dependsOn(
|
||||
buildJvmBinary,
|
||||
moveNativeJvmLib,
|
||||
generateJvmBindings
|
||||
)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user