Skip to content

Commit

Permalink
Set up GraalVM Native Image
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 1, 2024
1 parent 093d315 commit e671630
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 3 deletions.
29 changes: 28 additions & 1 deletion .github/workflows/kt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@ jobs:
with:
distribution: temurin
java-version: 21
- uses: graalvm/setup-graalvm@v1
with:
java-version: 21
github-token: ${{ secrets.GITHUB_TOKEN }}
set-java-home: false
components: native-image
- uses: gradle/actions/setup-gradle@v4
- run: ./gradlew build distZip
- run: ./gradlew build nativeCompile distZip
working-directory: kt
- uses: actions/upload-artifact@v4
with:
name: aoc2024-exe
path: kt/aoc2024-exe/build/distributions/*.zip
- uses: actions/upload-artifact@v4
with:
name: aoc2024-native
path: kt/graalvm/build/native/nativeCompile/*
- uses: actions/upload-artifact@v4
with:
name: aoc2024-kexe
Expand Down Expand Up @@ -64,6 +74,23 @@ jobs:
env:
AOC2024_DATADIR: inputs

run-graalvm:
needs: [ get-inputs, build ]
runs-on: ubuntu-latest

steps:
- uses: actions/download-artifact@v4
with:
name: inputs
path: inputs
- uses: actions/download-artifact@v4
with:
name: aoc2024-native
- run: chmod +x aoc2024-native
- run: ./aoc2024-native
env:
AOC2024_DATADIR: inputs

run-native:
needs: [ get-inputs, build ]
runs-on: ubuntu-latest
Expand Down
23 changes: 23 additions & 0 deletions kt/aoc2024-exe/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,26 @@ benchmark {
}
}
}

afterEvaluate {
val jvmBenchBenchmarkJar by tasks.existing
configurations.consumable("jvmBenchmark") {
attributes {
attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.LIBRARY))
attribute(
TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE,
objects.named(TargetJvmEnvironment.STANDARD_JVM),
)
attribute(
LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
objects.named(LibraryElements.JAR),
)
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME))
attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.EMBEDDED))
}
outgoing {
capability("com.github.ephemient.aoc2024:aoc2024-bench:1.0")
artifact(jvmBenchBenchmarkJar)
}
}
}
60 changes: 60 additions & 0 deletions kt/aoc2024-lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,66 @@ kotlin {
}
}

val jvmTestCompilation = kotlin.jvm().compilations.getByName("test")
val jvmTestJar by tasks.registering(Jar::class) {
group = BasePlugin.BUILD_GROUP
description = "Assembles an archive containing the test classes."
archiveClassifier = "test"
from(jvmTestCompilation.output.allOutputs)
}
for ((name, base, usage) in listOf(
Triple("jvmTestApiElements", "jvmTestApi", Usage.JAVA_API),
Triple("jvmTestRuntimeElements", "jvmTestRuntimeClasspath", Usage.JAVA_RUNTIME),
)) {
configurations.consumable(name) {
extendsFrom(configurations.getByName(base))
attributes {
attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.LIBRARY))
attribute(
TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE,
objects.named(TargetJvmEnvironment.STANDARD_JVM),
)
attribute(
LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
objects.named(LibraryElements.JAR),
)
attribute(Usage.USAGE_ATTRIBUTE, objects.named(usage))
}
outgoing {
capability("com.github.ephemient.aoc2024:aoc2024-test:1.0")
artifact(jvmTestJar) {
type = ArtifactTypeDefinition.JAR_TYPE
}
variants.create("classes") {
attributes {
attribute(
LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
objects.named(LibraryElements.CLASSES),
)
}
for (classesDir in jvmTestCompilation.output.classesDirs) {
artifact(classesDir) {
type = ArtifactTypeDefinition.JVM_CLASS_DIRECTORY
builtBy(jvmTestCompilation.compileTaskProvider)
}
}
}
variants.create("resources") {
attributes {
attribute(
LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
objects.named(LibraryElements.RESOURCES),
)
}
artifact(jvmTestCompilation.output.resourcesDir) {
type = ArtifactTypeDefinition.JVM_RESOURCES_DIRECTORY
builtBy(jvmTestCompilation.compileTaskProvider)
}
}
}
}
}

dependencies {
detektPlugins(libs.bundles.detekt.plugins)
}
Expand Down
56 changes: 56 additions & 0 deletions kt/graalvm/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
plugins {
application
alias(libs.plugins.native.image)
}

application {
mainClass.set("com.github.ephemient.aoc2024.exe.Main")
}

val benchmark by sourceSets.creating
val externalTestClasses = configurations.dependencyScope("externalTestClasses")
val externalTestClasspath = configurations.resolvable("externalTestClasspath") {
extendsFrom(externalTestClasses.get())
isTransitive = false
attributes {
attribute(
LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
objects.named(LibraryElements.CLASSES),
)
}
}
configurations.testImplementation {
extendsFrom(externalTestClasses.get())
}

dependencies {
implementation(projects.aoc2024Exe)
testImplementation(libs.junit.jupiter.api)
externalTestClasses(projects.aoc2024Lib) {
capabilities {
requireCapability("com.github.ephemient.aoc2024:aoc2024-test:1.0")
}
}
benchmark.implementationConfigurationName(projects.aoc2024Exe) {
capabilities {
requireCapability("com.github.ephemient.aoc2024:aoc2024-bench:1.0")
}
}
}

graalvmNative {
binaries {
getByName("main") {
imageName = "aoc2024-native"
}
create("benchmark") {
mainClass = "org.openjdk.jmh.Main"
classpath(benchmark.runtimeClasspath)
}
}
}

tasks.withType<Test>().configureEach {
testClassesDirs = files(testClassesDirs, externalTestClasspath.get())
useJUnitPlatform()
}
2 changes: 1 addition & 1 deletion kt/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kotlin.code.style=official
org.gradle.caching=true
org.gradle.configuration-cache=true
#org.gradle.configuration-cache=true
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m -XX:+UseParallelGC -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.parallel=true
2 changes: 2 additions & 0 deletions kt/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ junit-jupiter = "5.11.3"
kotlin = "2.1.0"
kotlinx-benchmark = "0.4.13"
kotlinx-coroutines = "1.9.0"
native-image = "0.10.3"
okio = "3.9.1"

[plugins]
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
kotlin-plugin-allopen = { id = "org.jetbrains.kotlin.plugin.allopen", version.ref = "kotlin" }
kotlinx-benchmark = { id = "org.jetbrains.kotlinx.benchmark", version.ref = "kotlinx-benchmark" }
native-image = { id = "org.graalvm.buildtools.native", version.ref = "native-image" }

[libraries]
detekt-formatting = { module = "io.gitlab.arturbosch.detekt:detekt-formatting", version.ref = "detekt" }
Expand Down
2 changes: 1 addition & 1 deletion kt/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ gradle.afterProject {
}

rootProject.name = "aoc2024"
include("aoc2024-exe", "aoc2024-lib")
include("aoc2024-exe", "aoc2024-lib", "graalvm")

0 comments on commit e671630

Please sign in to comment.