#!/bin/sh1#2#3# Our current make(1)-based approach to dependency tracking cannot cope with4# certain source tree changes, including:5#6# - removing source files7# - replacing generated files with files committed to the tree8# - changing file extensions (e.g. a C source file rewritten in C++)9# - moving a file from one directory to another10#11# Note that changing extensions or moving files may occur in effect as a result12# of switching from a generic machine-independent (MI) implementation file to a13# machine-dependent (MD) one.14#15# We handle those cases here in an ad-hoc fashion by looking for the known-16# bad case in the main .depend file, and if found deleting all of the related17# .depend files (including for example the lib32 version).18#19# These tests increase the build time (albeit by a small amount), so they20# should be removed once enough time has passed and it is extremely unlikely21# anyone would try a NO_CLEAN build against an object tree from before the22# related change. One year should be sufficient.23#24# Groups of cleanup rules begin with a comment including the date and git hash25# of the affected commit, and a description. The clean_dep function (below)26# handles common dependency cleanup cases. See the comment above the function27# for its arguments.28#29# Examples of each of the special cases:30#31# - Removing a source file (including changing a file's extension). The path,32# file, and extension are passed to clean_dep.33#34# # 20231031 0527c9bdc718 Remove forward compat ino64 stuff35# clean_dep lib/libc fstat c36#37# # 20221115 42d10b1b56f2 move from rs.c to rs.cc38# clean_dep usr.bin/rs rs c39#40# - Moving a file from one directory to another. Note that a regex is passed to41# clean_dep, as the default regex is derived from the file name (strncat.c in42# this example) does not change. The regex matches the old location, does not43# match the new location, and does not match any dependency shared between44# them. The `/`s are replaced with `.` to avoid awkward escaping.45#46# # 20250110 3dc5429158cf add strncat SIMD implementation47# clean_dep lib/libc strncat c "libc.string.strncat.c"48#49# - Replacing generated files with files committed to the tree. This is special50# case of moving from one directory to another. The stale generated file also51# needs to be deleted, so that it isn't found in make's .PATH. Note the52# unconditional `rm -fv`: there's no need for an extra call to first check for53# the file's existence.54#55# # 20250110 3863fec1ce2d add strlen SIMD implementation56# clean_dep lib/libc strlen S arm-optimized-routines57# run rm -fv "$OBJTOP"/lib/libc/strlen.S58#59# A rule may be required for only one architecture:60#61# # 20220326 fbc002cb72d2 move from bcmp.c to bcmp.S62# if [ "$MACHINE_ARCH" = "amd64" ]; then63# clean_dep lib/libc bcmp c64# fi65#66# We also have a big hammer at the top of the tree, .clean_build_epoch, to be67# used in severe cases where we can't surgically remove just the parts that68# need rebuilt. This should be used sparingly.6970set -e71set -u7273warn()74{75echo "$(basename "$0"):" "$@" >&276}7778err()79{80warn "$@"81exit 182}8384usage()85{86echo "usage: $(basename $0) [-v] [-n] objtop srctop" >&287}8889VERBOSE=90PRETEND=91while getopts vn o; do92case "$o" in93v)94VERBOSE=195;;96n)97PRETEND=198;;99*)100usage101exit 1102;;103esac104done105shift $((OPTIND-1))106107if [ $# -ne 2 ]; then108usage109exit 1110fi111112OBJTOP=$1113shift114SRCTOP=$1115shift116117if [ ! -d "$OBJTOP" ]; then118err "$OBJTOP: Not a directory"119fi120121if [ ! -d "$SRCTOP" -o ! -f "$SRCTOP/Makefile.inc1" ]; then122err "$SRCTOP: Not the root of a src tree"123fi124125: ${CLEANMK=""}126if [ -z "${MAKE+set}" ]; then127err "MAKE not set"128fi129130if [ -z "${MACHINE+set}" ]; then131err "MACHINE not set"132fi133134if [ -z "${MACHINE_ARCH+set}" ]; then135err "MACHINE_ARCH not set"136fi137138if [ -z "${ALL_libcompats+set}" ]; then139err "ALL_libcompats not set"140fi141142run()143{144if [ "$VERBOSE" ]; then145echo "$@"146fi147if ! [ "$PRETEND" ]; then148"$@"149fi150}151152# Clean the depend and object files for a given source file if the153# depend file matches a regex (which defaults to the source file154# name). This is typically used if a file was renamed, especially if155# only its extension was changed (e.g. from .c to .cc).156#157# $1 directory158# $2 source filename w/o extension159# $3 source extension160# $4 optional regex for egrep -w161clean_dep()162{163local dirprfx dir164for libcompat in "" $ALL_libcompats; do165dirprfx=${libcompat:+obj-lib${libcompat}}166dir="${OBJTOP%/}/${dirprfx}/$1"167if egrep -qw "${4:-$2\.$3}" "${dir}"/.depend.$2.*o 2>/dev/null; then168echo "Removing stale ${libcompat:+lib${libcompat} }dependencies and objects for $2.$3"169run rm -fv "${dir}"/.depend.$2.* "${dir}"/$2.*o170fi171done172}173174# Clean the object file for a given source file if it exists and175# matches a regex. This is typically used if a a change in CFLAGS or176# similar caused a change in the generated code without a change in177# the sources.178#179# $1 directory180# $2 source filename w/o extension181# $3 source extension182# $4 regex for egrep -w183clean_obj()184{185local dirprfx dir186for libcompat in "" $ALL_libcompats; do187dirprfx=${libcompat:+obj-lib${libcompat}}188dir="${OBJTOP%/}/${dirprfx}/$1"189if strings "${dir}"/$2.*o 2>/dev/null | egrep -qw "${4}"; then190echo "Removing stale ${libcompat:+lib${libcompat} }objects for $2.$3"191run rm -fv "${dir}"/$2.*o192fi193done194}195196extract_epoch()197{198[ -s "$1" ] || return 0199200awk 'int($1) > 0 { epoch = $1 } END { print epoch }' "$1"201}202203# Regular expression matching the names of src.conf(5) options which204# don't affect the build.205#206# This filter is applied to both the current options and the cached207# options so we don't force a rebuild just because the filter itself208# changed.209IGNORED_OPTS="CLEAN|DEPEND_CLEANUP|EXAMPLES|MAN|TESTS|WARNS|WERROR"210IGNORED_OPTS="${IGNORED_OPTS}|INSTALL.*|STAGING.*"211# Also ignore TOOLCHAIN and the options it forces if set. It is212# commonly used to speed up a build and is safe to toggle.213IGNORED_OPTS="${IGNORED_OPTS}|TOOLCHAIN|CLANG.*|LLDB?|LLVM_(BIN|COV).*"214215extract_src_opts()216{217$MAKE -C "$SRCTOP" -f "$SRCTOP"/Makefile.inc1 \218-V $'SRC_OPT_LIST:O:ts\n' |219egrep -v "^WITH(OUT)?_(${IGNORED_OPTS})="220}221222extract_obj_opts()223{224local fn225for fn; do226if [ -f "${fn}" ]; then227cat "${fn}"228else229echo "# ${fn}"230fi231done |232egrep -v "^WITH(OUT)?_(${IGNORED_OPTS})="233}234235clean_world()236{237local buildepoch="$1"238local srcopts="$2"239240# The caller may set CLEANMK in the environment to make target(s) that241# should be invoked instead of just destroying everything. This is242# generally used after legacy/bootstrap tools to avoid over-cleansing243# since we're generally in the temporary tree's ancestor.244if [ -n "$CLEANMK" ]; then245echo "Cleaning up the object tree"246run $MAKE -C "$SRCTOP" -f "$SRCTOP"/Makefile.inc1 $CLEANMK247else248echo "Cleaning up the temporary build tree"249run rm -rf "$OBJTOP"250fi251252# We don't assume that all callers will have grabbed the build epoch, so253# we'll do it here as needed. This will be useful if we add other254# non-epoch reasons to force clean.255if [ -z "$buildepoch" ]; then256buildepoch=$(extract_epoch "$SRCTOP"/.clean_build_epoch)257fi258259mkdir -p "$OBJTOP"260echo "$buildepoch" > "$OBJTOP"/.clean_build_epoch261echo "$srcopts" > "$OBJTOP"/.src_opts262263exit 0264}265266check_epoch_and_opts()267{268local srcepoch objepoch269local srcopts objopts270271srcepoch=$(extract_epoch "$SRCTOP"/.clean_build_epoch)272if [ -z "$srcepoch" ]; then273err "Malformed .clean_build_epoch; please validate the last line"274fi275276srcopts=$(extract_src_opts)277if [ -z "$srcopts" ]; then278err "Unable to extract source options"279fi280281# We don't discriminate between the varying degrees of difference282# between epochs. If it went backwards we could be bisecting across283# epochs, in which case the original need to clean likely still stands.284objepoch=$(extract_epoch "$OBJTOP"/.clean_build_epoch)285if [ -z "$objepoch" ] || [ "$srcepoch" -ne "$objepoch" ]; then286echo "Cleaning - src epoch: $srcepoch, objdir epoch: ${objepoch:-unknown}"287clean_world "$srcepoch" "$srcopts"288# NORETURN289fi290291objopts=$(extract_obj_opts "$OBJTOP"/.src_opts)292if [ "$srcopts" != "$objopts" ]; then293echo "Cleaning - build options have changed"294clean_world "$srcepoch" "$srcopts"295# NORETURN296fi297}298299check_epoch_and_opts300301#### Typical dependency cleanup begins here.302303# Date Rev Description304305# latest clean epoch (but not pushed until 20250814)306# 20250807 # All OpenSSL-using bits need rebuilt307308# Examples from the past, not currently active309#310#Binary program replaced a shell script311# 20220524 68fe988a40ca kqueue_test binary replaced shell script312#if stat "$OBJTOP"/tests/sys/kqueue/libkqueue/*kqtest* \313# "$OBJTOP"/tests/sys/kqueue/libkqueue/.depend.kqtest* >/dev/null 2>&1; then314# echo "Removing old kqtest"315# run rm -fv "$OBJTOP"/tests/sys/kqueue/libkqueue/.depend.* \316# "$OBJTOP"/tests/sys/kqueue/libkqueue/*317#fi318319# 20251219 # libkrb5profile is now internal320for libcompat in "" $ALL_libcompats; do321dirprfx=${libcompat:+obj-lib${libcompat}}322dir="${OBJTOP%/}/${dirprfx}"/krb5/util/profile323if [ -L "${dir}"/libkrb5profile.so ]; then324run rm -rfv "${dir}"325fi326done327328# 20250904 aef807876c30 moused binary to directory329if [ -f "$OBJTOP"/usr.sbin/moused/moused ]; then330echo "Removing old moused binary"331run rm -fv "$OBJTOP"/usr.sbin/moused/moused332fi333334if [ ${MACHINE} = riscv ]; then335# 20251031 df21a004be23 libc: scalar strrchr() in RISC-V assembly336clean_dep lib/libc strrchr c337338# 20251031 563efdd3bd5d libc: scalar memchr() in RISC-V assembly339clean_dep lib/libc memchr c340341# 20251031 40a958d5850d libc: scalar memset() in RISC-V assembly342clean_dep lib/libc memset c343344# 20251031 e09c1583eddd libc: scalar strlen() in RISC-V assembly345clean_dep lib/libc strlen c346347# 20251031 25fdd86a4c92 libc: scalar memcpy() in RISC-V assembly348clean_dep lib/libc memcpy c349350# 20251031 5a52f0704435 libc: scalar strnlen() in RISC-V assembly351clean_dep lib/libc strnlen c352353# 20251031 08af0bbc9c7d libc: scalar strchrnul() in RISC-V assembly354clean_dep lib/libc strchrnul c355356# 20251031 b5dbf3de5611 libc/riscv64: implement bcopy() and bzero() through memcpy() and memset()357clean_dep lib/libc bcopy c "libc.string.bcopy.c"358clean_dep lib/libc bzero c "libc.string.bzero.c"359fi360361if [ ${MACHINE_ARCH} = "aarch64" ]; then362# 20260113 41ccf82b29f3 libc/aarch64: Use MOPS implementations of memcpy/memmove/memset where availble363clean_dep lib/libc memset S "[^/]memset.S"364run rm -fv "$OBJTOP"/lib/libc/memset.S365fi366367368