#!/usr/bin/env bash
# The runtime archives a release build links (ADR 0031, ADR 0032), next to
# `louise` in target/
/, where `louise build` looks for them:
#
# liblouise_rt.a checked: what `cargo build -p louise_rt` makes, with
# the leak oracle; debug builds, tests, LOUISE_LEAKS
# liblouise_rt-fast.a fast: the `runtime` profile (whole-program LTO, one
# codegen unit, panic = abort), no leak oracle;
# release builds
# liblouise_rt-lto.a lto: fast, as LLVM bitcode, for cross-language
# ThinLTO with the program's C; release builds when
# clang and ld.lld can read it. Its LLVM version is
# in liblouise_rt-lto.llvm. Built only when clang,
# ld.lld and llvm-ar are on PATH.
#
# scripts/runtime.sh [DIR] # DIR: target/DIR (default: $LOUISE_PROFILE,
# # else release; `debug` for a dev build)
#
# The fast and lto archives are built with Rust's std rebuilt without its
# `backtrace` feature (ADR 0042): Rust panics in the runtime keep their
# message and lose the backtrace, and programs are about 40% smaller. The
# same compiler rebuilds it, so the lto archive's LLVM stays the system
# lld's; `-Zbuild-std` is unstable, hence RUSTC_BOOTSTRAP=1 for that build
# only. Without rust-src (rust-toolchain.toml lists it) the archives use
# the prebuilt std, as before; so does LOUISE_SMALL_STD=0.
set -euo pipefail
cd "$(dirname "$0")/.."
dir="target/${1:-${LOUISE_PROFILE:-release}}"
mkdir -p "$dir"
# Copied only when it changed: its modification time is part of what a
# skipped build checks (ADR 0025 Revision 1).
install_if_changed() {
if ! cmp -s "$1" "$2"; then
cp "$1" "$2"
fi
}
host="$(rustc -vV | sed -n 's/^host: //p')"
small=()
cargo_small=(cargo)
sysroot="$(rustc --print sysroot)"
if [ "${LOUISE_SMALL_STD:-1}" != 0 ] && [ -d "$sysroot/lib/rustlib/src/rust/library" ]; then
cargo_small=(env RUSTC_BOOTSTRAP=1 cargo)
# shellcheck disable=SC2054 # one flag, whose value lists crates
small=(--target "$host" -Zbuild-std=std,panic_abort -Zbuild-std-features=)
fi
"${cargo_small[@]}" build --quiet --profile runtime -p louise_rt --no-default-features --features mimalloc,tls,crypto \
--target-dir target/fast "${small[@]}"
if [ ${#small[@]} -gt 0 ]; then
install_if_changed "target/fast/$host/runtime/liblouise_rt.a" "$dir/liblouise_rt-fast.a"
else
install_if_changed target/fast/runtime/liblouise_rt.a "$dir/liblouise_rt-fast.a"
fi
if ! command -v clang >/dev/null || ! command -v ld.lld >/dev/null || ! command -v llvm-ar >/dev/null; then
echo "runtime.sh: no clang, ld.lld or llvm-ar: not building liblouise_rt-lto.a" >&2
rm -f "$dir/liblouise_rt-lto.a" "$dir/liblouise_rt-lto.llvm"
exit 0
fi
# The newest LLVM in the archive: rustc's, or clang's for mimalloc. A
# linker reads bitcode of its own version or older: an older one links the
# fast archive instead (ADR 0032).
rust_llvm="$(rustc -vV | sed -n 's/^LLVM version: \([0-9]*\).*/\1/p')"
clang_llvm="$(clang --version | sed -n 's/.*clang version \([0-9]*\).*/\1/p' | head -1)"
llvm=$(( rust_llvm > clang_llvm ? rust_llvm : clang_llvm ))
var="${host//-/_}"
# Bitcode from rustc (-Clinker-plugin-lto) and from clang for mimalloc's C.
# `--target` keeps the flags off build scripts, which are linked as usual;
# its own target directory keeps the other builds' artifacts.
env RUSTFLAGS="-Clinker-plugin-lto" \
"CC_$var=clang" "CFLAGS_$var=-flto=thin" "AR_$var=llvm-ar" \
"${cargo_small[@]}" build --quiet --profile runtime-lto --target "$host" --target-dir target/lto \
-p louise_rt --no-default-features --features mimalloc,tls,crypto "${small[@]:2}"
install_if_changed "target/lto/$host/runtime-lto/liblouise_rt.a" "$dir/liblouise_rt-lto.a"
echo "$llvm" > "target/lto/llvm"
install_if_changed target/lto/llvm "$dir/liblouise_rt-lto.llvm"