|
| 1 | +import java.util.Locale |
| 2 | + |
| 3 | +plugins { |
| 4 | + id("application") |
| 5 | +} |
| 6 | + |
| 7 | +val mainClassName = "Main" |
| 8 | +val nativeImageName = "basic-desktop-graalvm" |
| 9 | +val assetsDir = file("../../../assets") |
| 10 | +val nativeOutputDir = layout.buildDirectory.dir("native/nativeCompile") |
| 11 | +val isWindows = System.getProperty("os.name").startsWith("Windows", ignoreCase = true) |
| 12 | +val nativeExecutableName = if(isWindows) "$nativeImageName.exe" else nativeImageName |
| 13 | +val nativeExecutable = nativeOutputDir.map { it.file(nativeExecutableName) } |
| 14 | +val nativeImageOutputBase = nativeOutputDir.map { it.file(nativeImageName) } |
| 15 | +val nativeImageArgsFile = layout.buildDirectory.file("tmp/nativeCompile/native-image.args") |
| 16 | +val exampleExitAfterSeconds = providers.gradleProperty("exampleExitAfterSeconds") |
| 17 | +val nativeImageThreads = providers.gradleProperty("nativeImageThreads").orElse("2") |
| 18 | +val nativeImageBuilderMaxHeap = providers.gradleProperty("nativeImageBuilderMaxHeap").orElse("4g") |
| 19 | + |
| 20 | +fun lwjglNativesClassifier(): String { |
| 21 | + val osName = System.getProperty("os.name").lowercase(Locale.ROOT) |
| 22 | + val architecture = System.getProperty("os.arch").lowercase(Locale.ROOT) |
| 23 | + val isX64 = architecture == "amd64" || architecture == "x86_64" |
| 24 | + val isX86 = architecture == "x86" || architecture == "i386" |
| 25 | + val isArm64 = architecture == "aarch64" || architecture == "arm64" |
| 26 | + val isArm32 = architecture.startsWith("arm") && !isArm64 |
| 27 | + |
| 28 | + return when { |
| 29 | + osName.startsWith("windows") && isX64 -> "natives-windows" |
| 30 | + osName.startsWith("windows") && isX86 -> "natives-windows-x86" |
| 31 | + osName.startsWith("mac") && isX64 -> "natives-macos" |
| 32 | + osName.startsWith("mac") && isArm64 -> "natives-macos-arm64" |
| 33 | + osName.startsWith("linux") && isX64 -> "natives-linux" |
| 34 | + osName.startsWith("linux") && isArm64 -> "natives-linux-arm64" |
| 35 | + osName.startsWith("linux") && isArm32 -> "natives-linux-arm32" |
| 36 | + else -> throw GradleException( |
| 37 | + "Unsupported LWJGL native platform: ${System.getProperty("os.name")} $architecture" |
| 38 | + ) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +val lwjglNatives = lwjglNativesClassifier() |
| 43 | + |
| 44 | +application { |
| 45 | + mainClass.set(mainClassName) |
| 46 | +} |
| 47 | + |
| 48 | +dependencies { |
| 49 | + implementation(variantOf(libs.gdxPlatform) { classifier("natives-desktop") }) |
| 50 | + implementation(libs.gdxBackendLwjgl3) |
| 51 | + implementation(variantOf(libs.gdxBox2dPlatform) { classifier("natives-desktop") }) |
| 52 | + implementation(variantOf(libs.gdxFreetypePlatform) { classifier("natives-desktop") }) |
| 53 | + implementation(project(":examples:basic:core")) |
| 54 | + |
| 55 | + runtimeOnly(variantOf(libs.lwjglCore) { classifier(lwjglNatives) }) |
| 56 | + runtimeOnly(variantOf(libs.lwjglGlfw) { classifier(lwjglNatives) }) |
| 57 | + runtimeOnly(variantOf(libs.lwjglJemalloc) { classifier(lwjglNatives) }) |
| 58 | + runtimeOnly(variantOf(libs.lwjglOpenal) { classifier(lwjglNatives) }) |
| 59 | + runtimeOnly(variantOf(libs.lwjglOpengl) { classifier(lwjglNatives) }) |
| 60 | + runtimeOnly(variantOf(libs.lwjglStb) { classifier(lwjglNatives) }) |
| 61 | +} |
| 62 | + |
| 63 | +tasks.register<JavaExec>("basic_desktop_graalvm_jvm_run") { |
| 64 | + dependsOn("classes") |
| 65 | + group = "example-desktop-graalvm" |
| 66 | + description = "Run the GraalVM module's SpriteBatchTest launcher on the current JVM" |
| 67 | + mainClass.set(mainClassName) |
| 68 | + classpath = sourceSets["main"].runtimeClasspath |
| 69 | + workingDir = assetsDir |
| 70 | + jvmArgs("--enable-native-access=ALL-UNNAMED") |
| 71 | + |
| 72 | + if(System.getProperty("os.name").startsWith("Mac", ignoreCase = true)) { |
| 73 | + jvmArgs("-XstartOnFirstThread") |
| 74 | + } |
| 75 | + doFirst { |
| 76 | + if(exampleExitAfterSeconds.isPresent) { |
| 77 | + args("--exit-after-seconds=${exampleExitAfterSeconds.get()}") |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +tasks.named<JavaExec>("run") { |
| 83 | + workingDir = assetsDir |
| 84 | + jvmArgs("--enable-native-access=ALL-UNNAMED") |
| 85 | + if(System.getProperty("os.name").startsWith("Mac", ignoreCase = true)) { |
| 86 | + jvmArgs("-XstartOnFirstThread") |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +val nativeCompile = tasks.register<Exec>("nativeCompile") { |
| 91 | + dependsOn("classes") |
| 92 | + group = "build" |
| 93 | + description = "Compile the GraalVM Native Image SpriteBatchTest executable" |
| 94 | + inputs.files(sourceSets["main"].runtimeClasspath) |
| 95 | + inputs.property("nativeImageThreads", nativeImageThreads) |
| 96 | + inputs.property("nativeImageBuilderMaxHeap", nativeImageBuilderMaxHeap) |
| 97 | + outputs.file(nativeExecutable) |
| 98 | + |
| 99 | + doFirst { |
| 100 | + val graalvmHome = providers.environmentVariable("GRAALVM_HOME") |
| 101 | + .orElse(providers.environmentVariable("JAVA_HOME")) |
| 102 | + .orElse(providers.systemProperty("java.home")) |
| 103 | + .get() |
| 104 | + val nativeImageCommand = file("$graalvmHome/bin/${if(isWindows) "native-image.cmd" else "native-image"}") |
| 105 | + if(!nativeImageCommand.isFile) { |
| 106 | + throw GradleException( |
| 107 | + "Native Image was not found at ${nativeImageCommand.absolutePath}. " + |
| 108 | + "Set GRAALVM_HOME or JAVA_HOME to a GraalVM JDK that includes Native Image." |
| 109 | + ) |
| 110 | + } |
| 111 | + |
| 112 | + nativeOutputDir.get().asFile.mkdirs() |
| 113 | + val nativeImageArgs = listOf( |
| 114 | + "--no-fallback", |
| 115 | + "--parallelism=${nativeImageThreads.get()}", |
| 116 | + "-J-Xmx${nativeImageBuilderMaxHeap.get()}", |
| 117 | + "-O3", |
| 118 | + "-march=native", |
| 119 | + "--enable-native-access=ALL-UNNAMED", |
| 120 | + "-cp", |
| 121 | + sourceSets["main"].runtimeClasspath.asPath, |
| 122 | + "-o", |
| 123 | + nativeImageOutputBase.get().asFile.absolutePath, |
| 124 | + mainClassName |
| 125 | + ) |
| 126 | + val argsFile = nativeImageArgsFile.get().asFile |
| 127 | + argsFile.parentFile.mkdirs() |
| 128 | + argsFile.writeText(nativeImageArgs.joinToString(System.lineSeparator()) { argument -> |
| 129 | + "\"${argument.replace("\\", "\\\\").replace("\"", "\\\"")}\"" |
| 130 | + }) |
| 131 | + commandLine(nativeImageCommand.absolutePath, "@${argsFile.absolutePath}") |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +val copyBasicAssetsToGraalVM = tasks.register<Copy>("copyBasicAssetsToGraalVM") { |
| 136 | + dependsOn(nativeCompile) |
| 137 | + from(assetsDir) |
| 138 | + into(nativeOutputDir) |
| 139 | +} |
| 140 | + |
| 141 | +tasks.register("basic_desktop_graalvm_build") { |
| 142 | + dependsOn(copyBasicAssetsToGraalVM) |
| 143 | + group = "example-desktop-graalvm" |
| 144 | + description = "Build the optimized GraalVM Native Image SpriteBatchTest executable and copy its assets" |
| 145 | +} |
| 146 | + |
| 147 | +tasks.register<Exec>("basic_desktop_graalvm_run") { |
| 148 | + dependsOn(copyBasicAssetsToGraalVM) |
| 149 | + group = "example-desktop-graalvm" |
| 150 | + description = "Build and run SpriteBatchTest as an optimized GraalVM native executable" |
| 151 | + executable = nativeExecutable.get().asFile.absolutePath |
| 152 | + workingDir = nativeOutputDir.get().asFile |
| 153 | + |
| 154 | + doFirst { |
| 155 | + if(exampleExitAfterSeconds.isPresent) { |
| 156 | + args("--exit-after-seconds=${exampleExitAfterSeconds.get()}") |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments