diff --git a/cmd/plt-build/build.gradle.kts b/cmd/plt-build/build.gradle.kts index 3302c26..5e107dd 100644 --- a/cmd/plt-build/build.gradle.kts +++ b/cmd/plt-build/build.gradle.kts @@ -29,6 +29,11 @@ dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:${kotlinCoroutinesVersion}") } +sourceSets.main { + resources { + srcDirs("src/main/resources", "build/natives") + } +} kotlin { jvmToolchain(24) } @@ -36,4 +41,12 @@ kotlin { tasks.test { useJUnitPlatform() } -project.layout.buildDirectory.set(file("../../build")) \ No newline at end of file +tasks.register("compileNativeLib") { + val java_home = System.getProperty("java.home") + environment("CGO_CFLAGS" to "-I$java_home/include -I$java_home/include/linux") + workingDir("src/main/go") + commandLine("go", "build", "-linkshared", "-buildmode=c-shared", "-o", layout.buildDirectory.get().dir("natives").file("pltbuild.so")) +} +tasks.processResources { + dependsOn += "compileNativeLib" +} \ No newline at end of file diff --git a/cmd/plt-build/src/main/go/pltbuild.go b/cmd/plt-build/src/main/go/main.go similarity index 88% rename from cmd/plt-build/src/main/go/pltbuild.go rename to cmd/plt-build/src/main/go/main.go index 6d91761..5887f35 100644 --- a/cmd/plt-build/src/main/go/pltbuild.go +++ b/cmd/plt-build/src/main/go/main.go @@ -1,6 +1,5 @@ -package _go +package main -//#cgo CFLAGS: -I "$JAVA_HOME/include" -I "$JAVA_HOME/include/linux" //#include "pltbuild.h" import "C" import ( diff --git a/cmd/plt-build/src/main/kotlin/moe/rosa/planterette/jni/GoFile.kt b/cmd/plt-build/src/main/kotlin/moe/rosa/planterette/jni/GoFile.kt index 0fc62f5..6a9f7ed 100644 --- a/cmd/plt-build/src/main/kotlin/moe/rosa/planterette/jni/GoFile.kt +++ b/cmd/plt-build/src/main/kotlin/moe/rosa/planterette/jni/GoFile.kt @@ -1,5 +1,19 @@ package moe.rosa.planterette.jni +import java.io.File +import java.nio.file.Files + object GoFile { external fun write(fd: Int, str: String) + init { + val libname = "pltbuild.so" + val url = GoFile::class.java.getResource("/$libname") + val tmpDir = Files.createTempDirectory("pltbuild").toFile() + tmpDir.deleteOnExit() + val nativeLibTmpFile = File(tmpDir, libname) + url?.openStream().use { + Files.copy(it!!, nativeLibTmpFile.toPath()) + } + System.load(nativeLibTmpFile.absolutePath) + } } \ No newline at end of file