summaryrefslogtreecommitdiff
path: root/native-image/build.sh
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@opera.com>2026-11-24 00:55:37 +0100
committerJoel Klinghed <the_jk@opera.com>2026-11-24 00:55:37 +0100
commit8c126e9a0370ace22b9293ea7616a59a25e5e89d (patch)
treeee465d78a123aa320c3befc06f133dd190ee78ca /native-image/build.sh
parent2871d7a506a12656adb36998be7684f664727503 (diff)
Document how to build the OFA Kotlin compiler native imageHEADmain
native-image/kotlin-2.4.21-native-image.patch is the change to the Kotlin repository at v2.4.21. It bundles parcelize and plugin jars from outside the repository, adds the constructor metadata that the plugin loader needs, and adds G1, PGO and GraalVM home options. It also has the reachability metadata collected from OFA modules. native-image/build.sh runs the plain, instrumented and PGO builds, and native-image/README.md describes the full procedure and the results. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'native-image/build.sh')
-rwxr-xr-xnative-image/build.sh69
1 files changed, 69 insertions, 0 deletions
diff --git a/native-image/build.sh b/native-image/build.sh
new file mode 100755
index 0000000..fa114d7
--- /dev/null
+++ b/native-image/build.sh
@@ -0,0 +1,69 @@
+#!/usr/bin/env bash
+# Builds the Kotlin compiler native image for OFA in a patched Kotlin checkout.
+# See native-image/README.md for the full procedure.
+#
+# Usage: build.sh plain|instrument|pgo <profile.iprof> [Kotlin checkout]
+# plain G1 and -O3, no PGO. Use it to collect reachability metadata.
+# instrument Image that writes PGO profiles (kotlin.proxy.pgoProfileDir).
+# pgo Final image, optimized with the merged profile.
+#
+# The result is in <Kotlin checkout>/prepare/compiler-native-image/build/dist.
+
+set -eu
+
+KOTLIN_VERSION=2.4.21
+GRAALVM_HOME=${GRAALVM_HOME:-/usr/lib/jvm/java-25-graalvm}
+# Builders need a portable target. -march=native gives an image that only runs on this CPU.
+MARCH=${MARCH:-x86-64-v3}
+EMPTY_TO_STRING_VERSION=2.4.0-0.1.5
+
+MODE=${1:-}
+case "$MODE" in
+plain)
+ OPTIMIZATION=(-Pkotlin.build.native-image.extra-args="-O3 -march=$MARCH")
+ shift
+ ;;
+instrument)
+ OPTIMIZATION=(-Pkotlin.build.native-image.pgo-instrument=true
+ -Pkotlin.build.native-image.extra-args="-march=$MARCH")
+ shift
+ ;;
+pgo)
+ if [ $# -lt 2 ]; then
+ echo "Usage: $0 pgo <profile.iprof> [Kotlin checkout]" >&2
+ exit 2
+ fi
+ # PGO enables -O3.
+ OPTIMIZATION=(-Pkotlin.build.native-image.pgo-profile="$(realpath "$2")"
+ -Pkotlin.build.native-image.extra-args="-march=$MARCH")
+ shift 2
+ ;;
+*)
+ echo "Usage: $0 plain|instrument|pgo <profile.iprof> [Kotlin checkout]" >&2
+ exit 2
+ ;;
+esac
+
+KOTLIN_DIR=${1:-$HOME/source/kotlin}
+
+EMPTY_TO_STRING_JAR=$(find ~/.gradle/caches/modules-2 \
+ -name "kotlin-empty-to-string-compiler-$EMPTY_TO_STRING_VERSION.jar" | head -1)
+if [ -z "$EMPTY_TO_STRING_JAR" ]; then
+ echo "No kotlin-empty-to-string-compiler-$EMPTY_TO_STRING_VERSION.jar in the Gradle cache." >&2
+ echo "Build any OFA Kotlin module once to download it." >&2
+ exit 1
+fi
+
+EXTRA_PLUGINS="com.opera.gradle.kotlin-empty-to-string-compiler"
+EXTRA_PLUGINS+=";com.opera.EmptyToStringCompilePluginRegistrar"
+EXTRA_PLUGINS+=";com.opera.EmptyToStringCommandLineProcessor"
+EXTRA_PLUGINS+=";kotlin-empty-to-string-compiler"
+EXTRA_PLUGINS+=";$EMPTY_TO_STRING_JAR"
+
+cd "$KOTLIN_DIR"
+./gradlew :kotlin-compiler-native-image:kotlincNativeImageDist \
+ -PdeployVersion="$KOTLIN_VERSION" -Pbuild.number="$KOTLIN_VERSION" \
+ -Pkotlin.build.native-image.graalvm-home="$GRAALVM_HOME" \
+ -Pkotlin.build.native-image.use-g1=true \
+ "${OPTIMIZATION[@]}" \
+ -Pkotlin.build.native-image.extra-plugins="$EXTRA_PLUGINS"