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
TheBrokenRail e80e229efb
Some checks failed
ScriptCraft/pipeline/head There was a failure building this commit
Update Docs and Gradle
2020-06-01 17:32:20 -04:00

81 lines
2.3 KiB
Kotlin

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
import java.util.concurrent.Callable
class JNIPlugin : Plugin<Project> {
open class Config(private val project: Project) {
var root: File = File(project.rootDir, "src/main/c")
fun addPlatform(name: String, libExtension: String) = project.run {
val buildDir = Callable {
File(root, "build-${name}")
}
tasks.register<Exec>("cmake-${name}") {
doFirst {
val file = buildDir.call()
if (!file.exists()) {
file.mkdir()
}
}
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"])
}
project.extensions.create<Config>("jni", project)
}
}