Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GenerateEmbeddedJreTask to generate JRE #740

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/src/doc/user-guide/exporting.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ by using `jlink` with rosetta and an amd64 JDK on an arm64 MacOS.
For desktop exports you need to make exports based on the platform you're on, as exporting will copy the generated jre folder to
your export. An MacOS JRE will not work on Windows, so you'll need a Windows host to export for Windows.

Alternatively we provide a configurable gradle task which generates the JRE for your current host OS:
`./gradlew generateEmbeddedJre`

The task can be configured like so:
```kotlin
tasks.withType<GenerateEmbeddedJreTask> {
// the values in this example are the default values of the task
this.javaHome = System.getProperty("java.home") // path to your java home dir
this.arguments = arrayOf(
"--strip-debug",
"--no-header-files",
"--no-man-pages",
) // arguments to pass to the jlink command
this.modules = arrayOf(
"java.base",
"java.logging",
) // java module to include in the jre
}
```

## Specifics

`godot-bootstrap.jar` and `main.jar` are copied into `pck` during the export process.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package godot.gradle.projectExt

import godot.gradle.tasks.GenerateEmbeddedJreTask
import godot.gradle.tasks.android.checkAndroidJarAccessibleTask
import godot.gradle.tasks.android.checkD8ToolAccessibleTask
import godot.gradle.tasks.android.createMainDexFileTask
Expand All @@ -23,6 +24,11 @@ import godot.gradle.tasks.setupCleanTask
import org.gradle.api.Project

fun Project.setupTasks() {
tasks.register("generateEmbeddedJre", GenerateEmbeddedJreTask::class.java) { task ->
task.group = "godot-kotlin-jvm"
task.description = "Generates an embedded jre using jlink"
}

afterEvaluate {
with(it) {
val packageBootstrapJarTask = packageBootstrapJarTask()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package godot.gradle.tasks

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
import java.io.File
import java.util.*

open class GenerateEmbeddedJreTask : DefaultTask() {

@Input
var modules: Array<String> = arrayOf(
"java.base",
"java.logging",
)

@Input
var outputDir: String = "jvm/jre-${getArch()}-${getOs()}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder if we should declare the jvm directory path in the tool-common directory, to avoid hardcoding it here.


@Input
var arguments: Array<String> = arrayOf(
"--strip-debug",
"--no-header-files",
"--no-man-pages"
)

@Input
var javaHome: String = System.getProperty("java.home")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we potentially use the JVM running the gradle task if JAVA_HOME is not set ? Also can we make sure it's at least 11 ?


@TaskAction
fun createJre() {
File(outputDir).deleteRecursively()
project.exec {
it.commandLine(
"$javaHome/bin/jlink",
"--add-modules", modules.joinToString(","),
"--output", outputDir,
*arguments,
)
}

logger.lifecycle("Custom JRE created in $outputDir using modules: '${modules.joinToString(",")}', arguments: '${arguments.joinToString(" ")}' and java home: $javaHome")
}

private fun getArch(): String {
return when (val arch = System.getProperty("os.arch")) {
"aarch64", "arm64" -> "arm64"
"x86_64", "amd64" -> "amd64"
else -> throw IllegalArgumentException("Unsupported host architecture: $arch")
}
}

private fun getOs(): String {
val os = System.getProperty("os.name").lowercase(Locale.US)
return when {
os.contains("mac", true) -> "macos"
os.contains("nix", true) || os.contains("nux", true) || os.contains("aix", true) -> "linux"
os.contains("win", true) -> "windows"
else -> throw IllegalArgumentException("Unsupported host operating system: $os")
}
}
}
Loading