#!/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"