#!/usr/bin/env bash # Does a refactor change what the compiler produces? Builds `louise` at a # base revision and from the working tree, and compares, for every program # in tests/run, tests/stress, tests/bench and the examples: # - `louise check` output (diagnostics); # - `louise core` and `louise core --no-opt` (the Core IR); # - the generated C of `louise build`, in one C unit and in three # (LOUISE_C_UNITS), without #line directives so paths don't matter. # Prints each difference and exits 1 if there is one (issue gabla108/louise#226; the # procedure docs/code-health.md asked of behaviour-preserving PRs). # # scripts/same-output.sh [BASE] # BASE: a git revision (default: HEAD) # # The base is built in a temporary worktree with its own target directory # (target/same-output/base, kept between runs to build incrementally). # Outputs go to target/same-output/{base,head}. set -euo pipefail cd "$(dirname "$0")/.." # mold links, and the shared-host heavy lock for the two compiler builds. source scripts/lib.sh root=$PWD base_rev=$(git rev-parse --verify "${1:-HEAD}^{commit}") out="$root/target/same-output" mkdir -p "$out" # Builds the compiler and its debug runtime archive, next to each other # (where `louise build` looks for it). $1: source tree, $2: target dir. build_louise() { (cd "$1" && CARGO_TARGET_DIR="$2" heavy cargo build --quiet -p louise -p louise_rt) } worktree="$out/base-src" git worktree remove --force "$worktree" 2>/dev/null || true git worktree add --quiet --detach "$worktree" "$base_rev" trap 'git worktree remove --force "$worktree"' EXIT echo "same-output: building the base ($(git rev-parse --short "$base_rev")) and the working tree" build_louise "$worktree" "$out/base-target" build_louise "$root" "$root/target" programs=() for f in tests/run/*.lse tests/stress/*.lse tests/bench/*.lse; do programs+=("$f"); done packages=(examples/http_demo examples/qsort) # Writes the outputs of the compiler $1 for the sources $3... under $2. produce() { local louise=$1 dest=$2 name src d units shift 2 mkdir -p "$dest" for src in "$@"; do name=${src//\//_} d="$dest/$name" mkdir -p "$d" # Packages are named relative to their directory, files absolutely; # the paths in diagnostics then match between the two trees. if [ -d "$src" ]; then (cd "$src" && "$louise" check . >"$d/check" 2>&1 || true) (cd "$src" && "$louise" core . >"$d/core" 2>&1 || true) (cd "$src" && "$louise" core --no-opt . >"$d/core-no-opt" 2>&1 || true) else (cd "$root" && "$louise" check "$src" >"$d/check" 2>&1 || true) (cd "$root" && "$louise" core "$src" >"$d/core" 2>&1 || true) (cd "$root" && "$louise" core --no-opt "$src" >"$d/core-no-opt" 2>&1 || true) fi for units in 1 3; do local b="$d/c$units" mkdir -p "$b" if [ -d "$src" ]; then # The package's build/ holds other builds' C too: only `prog`'s, # written afresh, is compared. rm -f "$src"/build/prog.c "$src"/build/prog.*.c (cd "$src" && LOUISE_C_UNITS=$units LOUISE_NO_LINE_DIRECTIVES=1 \ "$louise" build -o "$b/prog" . >"$b/log" 2>&1 || true) cp "$src"/build/prog.c "$src"/build/prog.*.c "$b/" 2>/dev/null || true else (cd "$root" && LOUISE_C_UNITS=$units LOUISE_NO_LINE_DIRECTIVES=1 \ "$louise" build -o "$b/prog" "$src" >"$b/log" 2>&1 || true) cp "$b"/build/*.c "$b/" 2>/dev/null || true fi # Keep the C and the log only: executables and caches are large. rm -rf "$b/build" "$b/prog" done done } rm -rf "$out/base" "$out/head" # Single files build into their own directories, so both sides run at # once; a package builds in its own build/, so those take turns. produce "$out/base-target/debug/louise" "$out/base" "${programs[@]}" & produce "$root/target/debug/louise" "$out/head" "${programs[@]}" & wait produce "$out/base-target/debug/louise" "$out/base" "${packages[@]}" produce "$root/target/debug/louise" "$out/head" "${packages[@]}" if diff -r "$out/base" "$out/head" >"$out/diff" 2>&1; then echo "same-output: no differences (${#programs[@]} programs, ${#packages[@]} packages)" else echo "same-output: differences ($out/diff):" diff -rq "$out/base" "$out/head" | sed "s|$out/||g" exit 1 fi