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