#!/bin/bash set -e SRCDIR="$(cd "$(dirname "$0")" && pwd)" CONFIG="${1:-ASST1}" # Configure the build. cd "$SRCDIR" ./configure --ostree="$SRCDIR/root" # Clean userland objects so bear captures a full build. Without this, # an incremental build only regenerates .depend files, and bear records # -M commands instead of real compiles. clean also removes build/install; # the "includes" stage of bmake reinstalls the headers before compiling. bmake clean # Build userland (and capture compile commands for clangd). bear --output "$SRCDIR/compile_commands.json" -- bmake -j 10 bmake install # Configure a kernel. cd "$SRCDIR/kern/conf" ./config "$CONFIG" # Clean stale objects so bear captures a full build. # cleandir also removes includelinks, so run config again to restore them. cd "$SRCDIR/kern/compile/$CONFIG" bmake cleandir cd "$SRCDIR/kern/conf" ./config "$CONFIG" # Generate dependency files — outside bear, these are -M commands not -c. cd "$SRCDIR/kern/compile/$CONFIG" bmake depend -j 10 # Compile and install the kernel (append kernel compile commands). cd "$SRCDIR" bear --output "$SRCDIR/compile_commands.json" --append -- \ sh -c "cd kern/compile/$CONFIG && bmake -j 10" cd "$SRCDIR/kern/compile/$CONFIG" bmake install # Keep only real compile commands. bear also captures -M/-MM dependency # generation from "bmake depend"; clangd cannot use those, and a dep-gen # entry can shadow the real compile entry for the same file. python3 - "$SRCDIR/compile_commands.json" <<'EOF' import json, sys path = sys.argv[1] with open(path) as f: db = json.load(f) dep_flags = {"-M", "-MM", "-MD", "-MMD", "-MG", "-MP"} kept = [e for e in db if not dep_flags.intersection(e.get("arguments", []))] with open(path, "w") as f: json.dump(kept, f, indent=2) print(f"compile_commands.json: kept {len(kept)} of {len(db)} entries") EOF echo "Kernel + Userland baked fresh ! :)"