initial commit
This commit is contained in:
47 files changed
+1016
No files matched your search
@@ -0,0 +1,3 @@
|
||||
import java.io.File
|
||||
|
||||
data class GolangBuildOptions(val sourceDir: File, val outputDir: File, val platform: String)
|
||||
@@ -0,0 +1,85 @@
|
||||
import org.apache.tools.ant.taskdefs.condition.Os
|
||||
import org.gradle.api.GradleException
|
||||
import org.gradle.api.tasks.Exec
|
||||
import java.io.File
|
||||
import java.io.FileReader
|
||||
|
||||
import java.util.Properties
|
||||
|
||||
open class GolangBuildTask : Exec() {
|
||||
private lateinit var options: GolangBuildOptions
|
||||
private lateinit var abi: String
|
||||
|
||||
override fun exec() {
|
||||
val toolchainRoot = findAndroidNdkPath().resolve("toolchains/llvm/prebuilt/${detectOsType()}/bin")
|
||||
val linkerPrefix = toolChainPrefix()
|
||||
val compilerPrefix = linkerPrefix + options.platform
|
||||
|
||||
if ( !toolchainRoot.exists() )
|
||||
throw GradleException("Compiler not found")
|
||||
|
||||
workingDir = options.sourceDir
|
||||
|
||||
environment.put("GOARCH", golangArch())
|
||||
environment.put("GOOS", "android")
|
||||
environment.put("CGO_ENABLED", "1")
|
||||
environment.put("GOPATH", project.buildDir.resolve("intermediates/gopath").absolutePath)
|
||||
environment.put("CXX", toolchainRoot.resolve(compilerPrefix + "-clang++".exe()).absolutePath)
|
||||
environment.put("CC", toolchainRoot.resolve(compilerPrefix + "-clang".exe()).absolutePath)
|
||||
environment.put("LD", toolchainRoot.resolve(linkerPrefix + "ld".exe()).absolutePath)
|
||||
|
||||
commandLine = listOf("go".exe(), "build", "-buildmode", "c-shared", "-o", options.outputDir.resolve("$abi/libclash.so").absolutePath)
|
||||
|
||||
super.exec()
|
||||
}
|
||||
|
||||
fun setOptions(options: GolangBuildOptions, abi: String) {
|
||||
this.options = options
|
||||
this.abi = abi
|
||||
}
|
||||
|
||||
private fun findAndroidNdkPath(): File {
|
||||
val properties = FileReader(project.rootProject.file("local.properties")).use {
|
||||
Properties().apply { load(it) }
|
||||
}
|
||||
|
||||
return properties.getProperty("ndk.dir")?.let { File(it) }?.takeIf { it.exists() }
|
||||
?: throw GradleException("Android NDK not found.")
|
||||
}
|
||||
|
||||
private fun detectOsType(): String {
|
||||
return when {
|
||||
Os.isFamily(Os.FAMILY_WINDOWS) -> "linux-x86_64"
|
||||
Os.isFamily(Os.FAMILY_MAC) -> "darwin-x86_64"
|
||||
Os.isFamily(Os.FAMILY_UNIX) -> "linux-x86_64"
|
||||
else -> throw GradleException("Unsupported Build OS ${System.getenv("os.name")}")
|
||||
}
|
||||
}
|
||||
|
||||
private fun golangArch(): String {
|
||||
return when (abi) {
|
||||
"armeabi-v7a" -> "arm"
|
||||
"arm64-v8a" -> "arm64"
|
||||
"x86" -> "i686"
|
||||
"x86_64" -> "amd64"
|
||||
else -> throw GradleException("Unsupported arch $abi")
|
||||
}
|
||||
}
|
||||
|
||||
private fun toolChainPrefix(): String {
|
||||
return when (abi) {
|
||||
"armeabi-v7a" -> "arm-linux-androideabi"
|
||||
"arm64-v8a" -> "aarch64-linux-android"
|
||||
"x86" -> "i686-linux-android"
|
||||
"x86_64" -> "x86_64-linux-android"
|
||||
else -> throw GradleException("Unsupported arch $abi")
|
||||
}
|
||||
}
|
||||
|
||||
private fun String.exe(): String {
|
||||
return if ( Os.isFamily(Os.FAMILY_WINDOWS) )
|
||||
"$this.exe"
|
||||
else
|
||||
this
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user