This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
ScriptCraft/scriptcraft/buildSrc/src/main/kotlin/JNIPlugin.kt

81 lines
2.3 KiB
Kotlin
Raw Normal View History

2020-05-26 19:48:44 +00:00
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.Delete
import org.gradle.api.tasks.Exec
import org.gradle.kotlin.dsl.*
import java.io.File
2020-05-31 02:15:53 +00:00
import java.util.concurrent.Callable
2020-05-26 19:48:44 +00:00
class JNIPlugin : Plugin<Project> {
2020-06-01 21:32:20 +00:00
open class Config(private val project: Project) {
2020-05-31 02:15:53 +00:00
var root: File = File(project.rootDir, "src/main/c")
2020-05-26 19:48:44 +00:00
2020-05-31 02:15:53 +00:00
fun addPlatform(name: String, libExtension: String) = project.run {
val buildDir = Callable {
File(root, "build-${name}")
}
2020-05-26 19:48:44 +00:00
tasks.register<Exec>("cmake-${name}") {
doFirst {
2020-05-31 02:15:53 +00:00
val file = buildDir.call()
if (!file.exists()) {
file.mkdir()
2020-05-26 19:48:44 +00:00
}
}
group = "jni"
workingDir(buildDir)
executable("cmake")
args("-DCMAKE_TOOLCHAIN_FILE=${projectDir.absolutePath}/cmake/${name}-toolchain.cmake", "..")
}
tasks.register<Exec>("compileJNI-${name}") {
group = "jni"
workingDir(buildDir)
executable("make")
dependsOn(tasks["cmake-${name}"])
}
tasks.named("compileJNI") {
dependsOn(tasks["compileJNI-${name}"])
}
tasks.register<Delete>("cleanJNI-${name}") {
group = "jni"
delete(buildDir)
}
tasks.named("cleanJNI") {
dependsOn(tasks["cleanJNI-${name}"])
}
tasks.named<Copy>("processResources") {
from(buildDir) {
include("*${libExtension}")
into(File("natives", name).path)
}
}
}
}
override fun apply(project: Project): Unit = project.run {
tasks.register("cleanJNI") {
group = "jni"
}
tasks.named("clean") {
dependsOn(tasks["cleanJNI"])
}
tasks.register("compileJNI") {
group = "jni"
}
tasks.named<Copy>("processResources") {
dependsOn(tasks["compileJNI"])
}
2020-06-01 21:32:20 +00:00
project.extensions.create<Config>("jni", project)
2020-05-26 19:48:44 +00:00
}
}