#!/bin/sh # Build the Lean image of the `build` check, on the machine whose runner will use it. Three steps, each of which can # be run again: # # sh docker/build.sh [base|compile|commit|all] # # base docker/lean.Dockerfile, with the checkout's tree as the context: the toolchain, the dependencies with # Mathlib's cache, Comparator and its tools, the tree in /opt/proof, as $TAG-base. Minutes. # compile `lake build` of every library lakefile.toml declares (the project's own 33,429 files) in a container # named $NAME, with no network; the build # then becomes nobody's (the check builds a commit on top of it without copying it: checks/build.py). # Hours. If it stops (a reboot, a full disk, a kill), run this step again: the container is still there # and Lake goes on from the modules it has. # commit the finished container as $TAG, then a check that the image holds a complete build # (`lake build --no-build` of the same libraries). # all the three in turn (the default). # # TAG=… the image (shoal-hodge-lean:3344da59) # NAME=… the compile's container (hodge-lean-build) # MIRRORS=cn take apt, the toolchain, Go and Rust from mirrors a host in Beijing can reach (lean.Dockerfile's ARGs) # CPUSET=96-127 the cores to use (Docker's classic builder honours it); all of them if unset. Lake starts one job # per core it sees. # MEMORY=300g a limit for the compile's container; none if unset # # Run it under nohup and read its log. To watch the compile: # docker exec $NAME sh -c 'find .lake/build/lib -name "*.olean" | wc -l' (of 33,429) set -eu main=$(cd "${1:?a checkout of the project main}" && pwd) step=${2:-all} TAG=${TAG:-shoal-hodge-lean:3344da59} NAME=${NAME:-hodge-lean-build} cpus=${CPUSET:+--cpuset-cpus $CPUSET} # every library lakefile.toml declares, whatever its defaultTargets say: the check builds each of them on a commit # (checks/build.py), so the image must hold each of them built LIBS="python3 -c 'import tomllib; print(*[l[\"name\"] for l in tomllib.load(open(\"lakefile.toml\", \"rb\")).get(\"lean_lib\", [])])'" mirrors= if [ "${MIRRORS:-}" = cn ]; then mirrors="--build-arg APT_MIRROR=http://mirrors.aliyun.com/ubuntu --build-arg LEAN_DL=https://mirror.sjtu.edu.cn/elan/leanprover/lean4/releases/download --build-arg GO_DL=https://mirrors.aliyun.com/golang --build-arg GOPROXY=https://goproxy.cn,direct --build-arg RUST_DIST=https://rsproxy.cn --build-arg CARGO_MIRROR=sparse+https://rsproxy.cn/index/ --build-arg GITHUB_MIRROR=https://gh-proxy.com/https://github.com" fi base() { # the tree as git holds it (no .git, nothing ignored); the classic builder, which honours --cpuset-cpus; the host's # network, since through Docker's bridge downloads crawl git -C "$main" archive HEAD | DOCKER_BUILDKIT=0 docker build --network host $cpus $mirrors -f docker/lean.Dockerfile -t "$TAG-base" - } compile() { since=$(date +%s) if docker container inspect "$NAME" >/dev/null 2>&1; then docker start "$NAME" >/dev/null else # Lake writes a .setup.json for each module it compiles (every transitive import's path, about 1.5 MB) and # never reads it again: most of a build's bytes (lean-ci-notes.md). A loop deletes those older than 5 minutes # while the build runs, and the rest go before the chmod. Lake starts a `lean` per core of the host, whatever the # cpuset (124 at once on 32 cores, 12-02): LEAN_NUM_THREADS holds it to the cores the container has. docker run -d --name "$NAME" --network none $cpus ${MEMORY:+--memory $MEMORY} --log-opt max-size=200m --log-opt max-file=5 "$TAG-base" bash -c \ "export LEAN_NUM_THREADS=\$(nproc); date +'lake build starts %F %T'; (while sleep 240; do find .lake/build -name '*.setup.json' -mmin +5 -delete 2>/dev/null; done) & trim=\$!; lake build \$($LIBS); code=\$?; kill \$trim; [ \$code = 0 ] && find .lake/build -name '*.setup.json' -delete && chmod -R a+rX .lake/build && chown -R nobody:nogroup .lake/build && date +'lake build ends %F %T'" >/dev/null fi docker logs -f --since "$since" "$NAME" 2>&1 || true code=$(docker wait "$NAME") echo "compile: exit $code" [ "$code" = 0 ] } commit() { [ "$(docker inspect -f '{{.State.Status}} {{.State.ExitCode}}' "$NAME")" = "exited 0" ] || { echo "$NAME has not finished its build"; exit 1; } docker commit -c 'CMD ["/bin/bash"]' "$NAME" "$TAG" docker run --rm --network none $cpus "$TAG" bash -c "lake build --no-build \$($LIBS)" docker image inspect --format "$TAG {{.Id}} {{.Size}} bytes" "$TAG" echo "The container $NAME and the image $TAG-base can go: docker rm $NAME; docker rmi $TAG-base" } case "$step" in base) base ;; compile) compile ;; commit) commit ;; all) base; compile; commit ;; *) echo "base, compile, commit or all"; exit 2 ;; esac