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 { 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("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("compileJNI-${name}") { group = "jni" workingDir(buildDir) executable("make") dependsOn(tasks["cmake-${name}"]) } tasks.named("compileJNI") { dependsOn(tasks["compileJNI-${name}"]) } tasks.register("cleanJNI-${name}") { group = "jni" delete(buildDir) } tasks.named("cleanJNI") { dependsOn(tasks["cleanJNI-${name}"]) } tasks.named("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("processResources") { dependsOn(tasks["compileJNI"]) } project.extensions.create("jni", project) } }