|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Build and run the Java binding's AST dumper. |
| 3 | +# |
| 4 | +# hack/dump/java.sh <golden.json> <out.json> |
| 5 | +# |
| 6 | +# `mvn` is not used. The `compile` phase in java/pom.xml is bound to |
| 7 | +# `python3 scripts/build.py`, which builds the Rust JNI shim, and the AST |
| 8 | +# dumper needs none of that: `com.kcl.ast` is plain Java over Jackson, and the |
| 9 | +# whole point of the exercise is to check the decoder, not the transport. So |
| 10 | +# this compiles the AST sources plus the dumper against the Jackson jars |
| 11 | +# directly. |
| 12 | +# |
| 13 | +# It compiles the *sources* rather than reusing `target/classes`, for the same |
| 14 | +# reason `hack/dump/swift.sh` compiles the Swift sources and |
| 15 | +# `hack/dump/wasm.sh` compiles the TypeScript: a build artifact may be older |
| 16 | +# than the code, and a harness that silently checked yesterday's decoder would |
| 17 | +# report agreement for a decoder that no longer exists. |
| 18 | +# |
| 19 | +# The classpath is resolved with `mvn dependency:build-classpath` rather than |
| 20 | +# hand-listed, so the dumper cannot drift onto a different Jackson than the one |
| 21 | +# the binding's pom declares. |
| 22 | +set -euo pipefail |
| 23 | + |
| 24 | +if [ "$#" -ne 2 ]; then |
| 25 | + echo "usage: hack/dump/java.sh <golden.json> <out.json>" >&2 |
| 26 | + exit 2 |
| 27 | +fi |
| 28 | + |
| 29 | +golden="$1" |
| 30 | +out="$2" |
| 31 | +root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" |
| 32 | +cd "$root" |
| 33 | + |
| 34 | +# `java` is a stub on a machine with no JRE, so ask for the compiler by name |
| 35 | +# and let the shell report it missing rather than running a broken launcher. |
| 36 | +javac="$(command -v javac || true)" |
| 37 | +java_bin="$(command -v java || true)" |
| 38 | +if [ -z "$javac" ] || [ -z "$java_bin" ]; then |
| 39 | + echo "no JDK on PATH (both javac and java are needed: javac to build the dumper, java to run it)" >&2 |
| 40 | + exit 3 |
| 41 | +fi |
| 42 | +if ! "$java_bin" -version >/dev/null 2>&1; then |
| 43 | + echo "\`java -version\` fails: there is a javac on PATH but no runtime behind it" >&2 |
| 44 | + "$java_bin" -version >&2 || true |
| 45 | + exit 3 |
| 46 | +fi |
| 47 | + |
| 48 | +build="$(mktemp -d "${TMPDIR:-/tmp}/kcl-ast-dump-java.XXXXXX")" |
| 49 | +trap 'rm -rf "$build"' EXIT |
| 50 | + |
| 51 | +# Online, deliberately. `dependency:build-classpath` is the only way to learn |
| 52 | +# the Jackson version the pom actually declares without duplicating it here, and |
| 53 | +# a cold CI runner has an empty local maven repository — asking maven to resolve |
| 54 | +# that classpath offline would fail before the dumper ever ran, and it would |
| 55 | +# fail as a *classpath* error rather than as a decoder error. The CI job sets |
| 56 | +# `cache: 'maven'`, so the warm path is the normal one and this is the cold |
| 57 | +# fallback. |
| 58 | +mvn -q -f java/pom.xml \ |
| 59 | + dependency:build-classpath -Dmdep.outputFile="$build/cp.txt" >/dev/null |
| 60 | + |
| 61 | +# `com.kcl.ast`, the `com.kcl.loader.*` types `JsonUtil` names, and |
| 62 | +# `JsonUtil` itself — the binding's real entry point for turning `ast_json` |
| 63 | +# into a tree, which is the code path a Java caller actually goes through. |
| 64 | +# |
| 65 | +# `com.kcl.util.SematicUtil` is deliberately left out: it is symbol/scope |
| 66 | +# resolution over `LoadPackageResult`, which drags in the generated |
| 67 | +# `com.kcl.api.Spec` (six figures of protoc output) and has nothing to do with |
| 68 | +# decoding an AST. The dumper needs no part of it, and compiling it would put |
| 69 | +# the generated protobuf on the harness's critical path for no gain. |
| 70 | +find java/src/main/java/com/kcl/ast java/src/main/java/com/kcl/loader \ |
| 71 | + hack/dump/java -name '*.java' > "$build/sources.txt" |
| 72 | +echo java/src/main/java/com/kcl/util/JsonUtil.java >> "$build/sources.txt" |
| 73 | + |
| 74 | +# `--release 8` is the language level java/pom.xml declares, so a Java 9+ API |
| 75 | +# in the dumper or in `com.kcl.ast` is a compile error here rather than |
| 76 | +# something that passes on a new JDK and fails on the zulu 8 the build matrix |
| 77 | +# uses. It is also a slightly stricter check than the default: compiling at the |
| 78 | +# running JDK's level would happily accept `List.of` or `var`. |
| 79 | +javac -nowarn --release 8 -encoding UTF-8 \ |
| 80 | + -cp "$(cat "$build/cp.txt")" \ |
| 81 | + -d "$build/classes" \ |
| 82 | + @"$build/sources.txt" |
| 83 | + |
| 84 | +"$java_bin" -cp "$build/classes:$(cat "$build/cp.txt")" astdump.Dump "$golden" "$out" |
0 commit comments