#!/usr/bin/env bash # Checks that incremental Kotlin compilation stays correct in the OFA build. # # The stub runs the native image compiler in a subprocess. The Kotlin daemon # then gets no lookup or output data from the compiler. Incremental # compilation uses that data to find the files that a change makes dirty. # If it is missing, a build can succeed with stale class files. # # Usage: ic_check.sh proxy|jvm [OFA mobile/mobile dir] # proxy uses the kotlinProxy* properties from gradle-local.properties. # jvm sets -PkotlinProxyStubJar= to use the JVM compiler. Run it first. # It must pass, else the test itself is wrong. # # The script adds test files to components/annotations and # components/coreutils, and removes them on exit. set -u MODE=${1:-} OFA=${2:-/home/the_jk/opera/head-1/mobile/mobile} JAVAP=/usr/lib/jvm/java-25-graalvm/bin/javap case "$MODE" in proxy) EXTRA_ARGS=() ;; jvm) EXTRA_ARGS=(-PkotlinProxyStubJar=) ;; *) echo "Usage: $0 proxy|jvm [OFA mobile/mobile dir]" >&2 exit 2 ;; esac if [ ! -x "$OFA/gradlew" ]; then echo "No gradlew in $OFA" >&2 exit 2 fi ANNOTATIONS_DIR="$OFA/components/annotations/src/main/java/com/opera/android/annotations/ictest" COREUTILS_DIR="$OFA/components/coreutils/src/main/java/com/opera/android/ictest" CLASSES="$OFA/bin/gradle/components/coreutils/intermediates/built_in_kotlinc/debug/compileDebugKotlin/classes" FILE_A="$COREUTILS_DIR/IcTestA.kt" FILE_B="$COREUTILS_DIR/IcTestB.kt" FILE_C="$COREUTILS_DIR/IcTestC.kt" FILE_X="$ANNOTATIONS_DIR/IcTestCross.kt" CLASS_B="$CLASSES/com/opera/android/ictest/IcTestBKt.class" CLASS_C="$CLASSES/com/opera/android/ictest/IcTestCKt.class" if [ -e "$ANNOTATIONS_DIR" ] || [ -e "$COREUTILS_DIR" ]; then echo "Test files exist from an earlier run. Remove them first:" >&2 echo " $ANNOTATIONS_DIR" >&2 echo " $COREUTILS_DIR" >&2 exit 2 fi LOG_DIR=$(mktemp -d /tmp/ic-check-"$MODE".XXXXXX) cleanup() { rm -f "$FILE_A" "$FILE_B" "$FILE_C" "$FILE_X" rmdir "$COREUTILS_DIR" "$ANNOTATIONS_DIR" 2>/dev/null } trap cleanup EXIT RESULTS=() FAILED=0 record() { # record RESULTS+=("$(printf '%-28s %-4s %s' "$1" "$2" "$3")") if [ "$2" != ok ]; then FAILED=1 fi } build() { # build . Returns the Gradle exit code. local log="$LOG_DIR/$1.log" (cd "$OFA" && ./gradlew --console=plain --no-build-cache "${EXTRA_ARGS[@]}" \ :components:coreutils:compileDebugKotlin >"$log" 2>&1) } # has_const : true if the method loads the constant. has_const() { "$JAVAP" -c -p "$1" 2>/dev/null | awk -v m="$2" -v want="iconst_$3" ' index($0, " " m "(") { inside = 1; next } inside && /^ [a-z].*\(/ { inside = 0 } inside && index($0, want) { found = 1 } END { exit found ? 0 : 1 }' } write_a() { # write_a cat >"$FILE_A" <"$FILE_X" <"$FILE_B" <<'EOF' package com.opera.android.ictest fun icTestUse(): Int = icTestValue() fun icTestConst(): Int = IC_TEST_CONST EOF cat >"$FILE_C" <<'EOF' package com.opera.android.ictest import com.opera.android.annotations.ictest.IC_TEST_CROSS fun icTestCross(): Int = IC_TEST_CROSS EOF echo "Mode: $MODE. Logs: $LOG_DIR" echo "0/4 setup build" if ! build 0-setup; then echo "Setup build failed, see $LOG_DIR/0-setup.log" >&2 exit 1 fi # 1. A constant is inlined into its users. Only IcTestA.kt changes, so # IcTestB.kt is recompiled only if the build finds the dependency. echo "1/4 constant in the same module" write_a "fun icTestValue(): Int = 1" 2 if ! build 1-const; then record "1 constant, same module" BAD "build failed, see 1-const.log" elif has_const "$CLASS_B" icTestConst 2; then record "1 constant, same module" ok "IcTestB recompiled" else record "1 constant, same module" BAD "IcTestBKt.class still has the old constant" fi # 2. The same, but the constant is in components/annotations. echo "2/4 constant in a dependency module" write_x 2 if ! build 2-cross; then record "2 constant, other module" BAD "build failed, see 2-cross.log" elif has_const "$CLASS_C" icTestCross 2; then record "2 constant, other module" ok "IcTestC recompiled" else record "2 constant, other module" BAD "IcTestCKt.class still has the old constant" fi # 3. A signature change must give a compile error in the unchanged caller. echo "3/4 signature change" write_a "fun icTestValue(x: Int): Int = x" 2 if build 3-signature; then record "3 signature change" BAD "build succeeded, caller in IcTestB.kt not recompiled" elif grep -q "IcTestB.kt" "$LOG_DIR/3-signature.log"; then record "3 signature change" ok "error reported in IcTestB.kt" else record "3 signature change" BAD "build failed without an error in IcTestB.kt, see 3-signature.log" fi write_a "fun icTestValue(): Int = 1" 2 if ! build 3-restore; then echo "Restore build failed, see $LOG_DIR/3-restore.log" >&2 exit 1 fi # 4. A deleted source file must also remove its class file. echo "4/4 deleted file" rm -f "$FILE_B" if ! build 4-delete; then record "4 deleted file" BAD "build failed, see 4-delete.log" elif [ -e "$CLASS_B" ]; then record "4 deleted file" BAD "IcTestBKt.class still exists" else record "4 deleted file" ok "IcTestBKt.class removed" fi echo echo "Results ($MODE):" printf '%s\n' "${RESULTS[@]}" exit $FAILED