1+ import java.nio.file.Files
2+
3+ import org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask
4+
15plugins {
26 id(" stan-kotlin-conventions" )
37
48 application
9+
10+ alias(libs.plugins.graalvm)
511}
612
713application {
@@ -19,3 +25,55 @@ dependencies {
1925 implementation(libs.koinCore)
2026 implementation(libs.kotlinReflect)
2127}
28+
29+ graalvmNative {
30+ toolchainDetection = System .getenv(" GRAALVM_HOME" ) == null
31+
32+ metadataRepository {
33+ enabled = true
34+ }
35+
36+ // For options see https://graalvm.github.io/native-build-tools/latest/gradle-plugin.html.
37+ binaries {
38+ named(" main" ) {
39+ imageName = provider { application.applicationName }
40+
41+ buildArgs.addAll(
42+ " --report-unsupported-elements-at-runtime" ,
43+ " --parallelism=8" ,
44+ " -J-Xmx16g"
45+ )
46+ }
47+ }
48+ }
49+
50+ tasks.named<BuildNativeImageTask >(" nativeCompile" ) {
51+ // Gradle's "Copy" task cannot handle symbolic links, see https://github.com/gradle/gradle/issues/3982. That is why
52+ // links contained in the GraalVM distribution archive get broken during provisioning and are replaced by empty
53+ // files. Address this by recreating the links in the toolchain directory.
54+ val toolchainDir = System .getenv(" GRAALVM_HOME" )?.let { File (it) }
55+ ? : options.get().javaLauncher.get().executablePath.asFile.parentFile.run {
56+ if (name == " bin" ) parentFile else this
57+ }
58+
59+ val toolchainFiles = toolchainDir.walkTopDown().filter { it.isFile }
60+ val emptyFiles = toolchainFiles.filter { it.length() == 0L }
61+
62+ // Find empty toolchain files that are named like other toolchain files and assume these should have been links.
63+ val links = toolchainFiles.mapNotNull { file ->
64+ emptyFiles.singleOrNull { it != file && it.name == file.name }?.let {
65+ file to it
66+ }
67+ }
68+
69+ // Fix up symbolic links.
70+ links.forEach { (target, link) ->
71+ logger.quiet(" Fixing up '$link ' to link to '$target '." )
72+
73+ if (link.delete()) {
74+ Files .createSymbolicLink(link.toPath(), target.toPath())
75+ } else {
76+ logger.warn(" Unable to delete '$link '." )
77+ }
78+ }
79+ }
0 commit comments