Skip to content

Commit ba59c3f

Browse files
authored
Add GenerateEmbeddedJreTask to generate JRE (#740)
* Add GenerateEmbeddedJreTask to create custom JRE Introduced GenerateEmbeddedJreTask for generating a trimmed JRE using jlink. Added task registration in setupTasks to facilitate custom JRE creation during the build process. This helps minimize the bundled JRE size tailored to specific needs. * Move registration out of after evaluate * Improve task api and update docs * Update task to fix deprecations and address review comments * Ensure java home from task execution is used
1 parent 7188889 commit ba59c3f

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

docs/src/doc/user-guide/exporting.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ by using `jlink` with rosetta and an amd64 JDK on an arm64 MacOS.
5151
For desktop exports you need to make exports based on the platform you're on, as exporting will copy the generated jre folder to
5252
your export. An MacOS JRE will not work on Windows, so you'll need a Windows host to export for Windows.
5353
54+
Alternatively we provide a configurable gradle task which generates the JRE for your current host OS:
55+
`./gradlew generateEmbeddedJre`
56+
57+
The task can be configured like so:
58+
```kotlin
59+
tasks.withType<GenerateEmbeddedJreTask> {
60+
// the values in this example are the default values of the task
61+
this.javaHome = System.getProperty("java.home") // path to your java home dir
62+
this.arguments = arrayOf(
63+
"--strip-debug",
64+
"--no-header-files",
65+
"--no-man-pages",
66+
) // arguments to pass to the jlink command
67+
this.modules = arrayOf(
68+
"java.base",
69+
"java.logging",
70+
) // java module to include in the jre
71+
}
72+
```
73+
5474
## Specifics
5575
5676
`godot-bootstrap.jar` and `main.jar` are copied into `pck` during the export process.

kt/plugins/godot-gradle-plugin/src/main/kotlin/godot/gradle/projectExt/setupTasks.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package godot.gradle.projectExt
22

3+
import godot.gradle.tasks.GenerateEmbeddedJreTask
34
import godot.gradle.tasks.android.checkAndroidJarAccessibleTask
45
import godot.gradle.tasks.android.checkD8ToolAccessibleTask
56
import godot.gradle.tasks.android.createBootstrapDexJarTask
@@ -24,6 +25,11 @@ import godot.gradle.tasks.setupCleanTask
2425
import org.gradle.api.Project
2526

2627
fun Project.setupTasks() {
28+
tasks.register("generateEmbeddedJre", GenerateEmbeddedJreTask::class.java) { task ->
29+
task.group = "godot-kotlin-jvm"
30+
task.description = "Generates an embedded jre using jlink"
31+
}
32+
2733
afterEvaluate {
2834
with(it) {
2935
val packageBootstrapJarTask = packageBootstrapJarTask()
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package godot.gradle.tasks
2+
3+
import godot.tools.common.constants.Paths
4+
import org.gradle.api.DefaultTask
5+
import org.gradle.api.tasks.Input
6+
import org.gradle.api.tasks.TaskAction
7+
import org.gradle.process.ExecOperations
8+
import java.io.File
9+
import java.util.*
10+
import javax.inject.Inject
11+
12+
open class GenerateEmbeddedJreTask @Inject constructor(
13+
private val execOperations: ExecOperations,
14+
) : DefaultTask() {
15+
16+
@Input
17+
var modules: Array<String> = arrayOf(
18+
"java.base",
19+
"java.logging",
20+
)
21+
22+
@Input
23+
var outputDir: String = "${Paths.GODOT_KOTLIN_JVM_DIR}/jre-${getArch()}-${getOs()}"
24+
25+
@Input
26+
var arguments: Array<String> = arrayOf(
27+
"--strip-debug",
28+
"--no-header-files",
29+
"--no-man-pages"
30+
)
31+
32+
@Input
33+
var javaHome: String? = null
34+
35+
@TaskAction
36+
fun createJre() {
37+
File(outputDir).deleteRecursively()
38+
39+
val resolvedJavaHome = javaHome ?: System.getProperty("java.home")
40+
41+
execOperations.exec { spec ->
42+
spec.commandLine(
43+
"$resolvedJavaHome/bin/jlink",
44+
"--add-modules", modules.joinToString(","),
45+
"--output", outputDir,
46+
*arguments,
47+
)
48+
}
49+
50+
logger.lifecycle(
51+
"Custom JRE created in $outputDir using modules: '${modules.joinToString(",")}', arguments: '${
52+
arguments.joinToString(" ")
53+
}' and java home: $resolvedJavaHome"
54+
)
55+
}
56+
57+
private fun getArch(): String {
58+
return when (val arch = System.getProperty("os.arch")) {
59+
"aarch64", "arm64" -> "arm64"
60+
"x86_64", "amd64" -> "amd64"
61+
else -> throw IllegalArgumentException("Unsupported host architecture: $arch")
62+
}
63+
}
64+
65+
private fun getOs(): String {
66+
val os = System.getProperty("os.name").lowercase(Locale.US)
67+
return when {
68+
os.contains("mac", true) -> "macos"
69+
os.contains("nix", true) || os.contains("nux", true) || os.contains("aix", true) -> "linux"
70+
os.contains("win", true) -> "windows"
71+
else -> throw IllegalArgumentException("Unsupported host operating system: $os")
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)