From 8c126e9a0370ace22b9293ea7616a59a25e5e89d Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Tue, 24 Nov 2026 00:55:37 +0100 Subject: Document how to build the OFA Kotlin compiler native image 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 --- native-image/build.sh | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100755 native-image/build.sh (limited to 'native-image/build.sh') 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 [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 /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 [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 [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" -- cgit v1.3.1