summaryrefslogtreecommitdiff
path: root/tools/ic_check.sh
blob: c584fad7f2ea405ee5d56cb69888580fa1e51489 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/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 <name> <ok|BAD> <detail>
    RESULTS+=("$(printf '%-28s %-4s %s' "$1" "$2" "$3")")
    if [ "$2" != ok ]; then
        FAILED=1
    fi
}

build() {
    # build <log name>. 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 <class file> <method> <value>: 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 <signature of icTestValue> <IC_TEST_CONST>
    cat >"$FILE_A" <<EOF
package com.opera.android.ictest

$1

const val IC_TEST_CONST = $2
EOF
}

write_x() {
    cat >"$FILE_X" <<EOF
package com.opera.android.annotations.ictest

const val IC_TEST_CROSS = $1
EOF
}

mkdir -p "$COREUTILS_DIR" "$ANNOTATIONS_DIR"
write_a "fun icTestValue(): Int = 1" 1
write_x 1
cat >"$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