summaryrefslogtreecommitdiff
path: root/native-image/build.sh
blob: fa114d7a457a38f447a0e36b42b9de97094aad26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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"