1#!/bin/sh 2# 3# 4# Our current make(1)-based approach to dependency tracking cannot cope with 5# certain source tree changes, including: 6# 7# - removing source files 8# - replacing generated files with files committed to the tree 9# - changing file extensions (e.g. a C source file rewritten in C++) 10# - moving a file from one directory to another 11# 12# Note that changing extensions or moving files may occur in effect as a result 13# of switching from a generic machine-independent (MI) implementation file to a 14# machine-dependent (MD) one. 15# 16# We handle those cases here in an ad-hoc fashion by looking for the known- 17# bad case in the main .depend file, and if found deleting all of the related 18# .depend files (including for example the lib32 version). 19# 20# These tests increase the build time (albeit by a small amount), so they 21# should be removed once enough time has passed and it is extremely unlikely 22# anyone would try a NO_CLEAN build against an object tree from before the 23# related change. One year should be sufficient. 24# 25# Groups of cleanup rules begin with a comment including the date and git hash 26# of the affected commit, and a description. The clean_dep function (below) 27# handles common dependency cleanup cases. See the comment above the function 28# for its arguments. 29# 30# Examples of each of the special cases: 31# 32# - Removing a source file (including changing a file's extension). The path, 33# file, and extension are passed to clean_dep. 34# 35# # 20231031 0527c9bdc718 Remove forward compat ino64 stuff 36# clean_dep lib/libc fstat c 37# 38# # 20221115 42d10b1b56f2 move from rs.c to rs.cc 39# clean_dep usr.bin/rs rs c 40# 41# - Moving a file from one directory to another. Note that a regex is passed to 42# clean_dep, as the default regex is derived from the file name (strncat.c in 43# this example) does not change. The regex matches the old location, does not 44# match the new location, and does not match any dependency shared between 45# them. The `/`s are replaced with `.` to avoid awkward escaping. 46# 47# # 20250110 3dc5429158cf add strncat SIMD implementation 48# clean_dep lib/libc strncat c "libc.string.strncat.c" 49# 50# - Replacing generated files with files committed to the tree. This is special 51# case of moving from one directory to another. The stale generated file also 52# needs to be deleted, so that it isn't found in make's .PATH. Note the 53# unconditional `rm -fv`: there's no need for an extra call to first check for 54# the file's existence. 55# 56# # 20250110 3863fec1ce2d add strlen SIMD implementation 57# clean_dep lib/libc strlen S arm-optimized-routines 58# run rm -fv "$OBJTOP"/lib/libc/strlen.S 59# 60# A rule may be required for only one architecture: 61# 62# # 20220326 fbc002cb72d2 move from bcmp.c to bcmp.S 63# if [ "$MACHINE_ARCH" = "amd64" ]; then 64# clean_dep lib/libc bcmp c 65# fi 66# 67# We also have a big hammer at the top of the tree, .clean_build_epoch, to be 68# used in severe cases where we can't surgically remove just the parts that 69# need rebuilt. This should be used sparingly. 70 71set -e 72set -u 73 74warn() 75{ 76 echo "$(basename "$0"):" "$@" >&2 77} 78 79err() 80{ 81 warn "$@" 82 exit 1 83} 84 85usage() 86{ 87 echo "usage: $(basename $0) [-v] [-n] objtop srctop" >&2 88} 89 90VERBOSE= 91PRETEND= 92while getopts vn o; do 93 case "$o" in 94 v) 95 VERBOSE=1 96 ;; 97 n) 98 PRETEND=1 99 ;; 100 *) 101 usage 102 exit 1 103 ;; 104 esac 105done 106shift $((OPTIND-1)) 107 108if [ $# -ne 2 ]; then 109 usage 110 exit 1 111fi 112 113OBJTOP=$1 114shift 115SRCTOP=$1 116shift 117 118if [ ! -d "$OBJTOP" ]; then 119 err "$OBJTOP: Not a directory" 120fi 121 122if [ ! -d "$SRCTOP" -o ! -f "$SRCTOP/Makefile.inc1" ]; then 123 err "$SRCTOP: Not the root of a src tree" 124fi 125 126: ${CLEANMK=""} 127if [ -n "$CLEANMK" ]; then 128 if [ -z "${MAKE+set}" ]; then 129 err "MAKE not set" 130 fi 131fi 132 133if [ -z "${MACHINE+set}" ]; then 134 err "MACHINE not set" 135fi 136 137if [ -z "${MACHINE_ARCH+set}" ]; then 138 err "MACHINE_ARCH not set" 139fi 140 141if [ -z "${ALL_libcompats+set}" ]; then 142 err "ALL_libcompats not set" 143fi 144 145run() 146{ 147 if [ "$VERBOSE" ]; then 148 echo "$@" 149 fi 150 if ! [ "$PRETEND" ]; then 151 "$@" 152 fi 153} 154 155# Clean the depend and object files for a given source file if the 156# depend file matches a regex (which defaults to the source file 157# name). This is typically used if a file was renamed, especially if 158# only its extension was changed (e.g. from .c to .cc). 159# 160# $1 directory 161# $2 source filename w/o extension 162# $3 source extension 163# $4 optional regex for egrep -w 164clean_dep() 165{ 166 for libcompat in "" $ALL_libcompats; do 167 dirprfx=${libcompat:+obj-lib${libcompat}/} 168 if egrep -qw "${4:-$2\.$3}" "$OBJTOP"/$dirprfx$1/.depend.$2.*o 2>/dev/null; then 169 echo "Removing stale ${libcompat:+lib${libcompat} }dependencies and objects for $2.$3" 170 run rm -fv \ 171 "$OBJTOP"/$dirprfx$1/.depend.$2.* \ 172 "$OBJTOP"/$dirprfx$1/$2.*o 173 fi 174 done 175} 176 177# Clean the object file for a given source file if it exists and 178# matches a regex. This is typically used if a a change in CFLAGS or 179# similar caused a change in the generated code without a change in 180# the sources. 181# 182# $1 directory 183# $2 source filename w/o extension 184# $3 source extension 185# $4 regex for egrep -w 186clean_obj() 187{ 188 for libcompat in "" $ALL_libcompats; do 189 dirprfx=${libcompat:+obj-lib${libcompat}/} 190 if strings "$OBJTOP"/$dirprfx$1/$2.*o 2>/dev/null | egrep -qw "${4}"; then 191 echo "Removing stale ${libcompat:+lib${libcompat} }objects for $2.$3" 192 run rm -fv \ 193 "$OBJTOP"/$dirprfx$1/$2.*o 194 fi 195 done 196} 197 198extract_epoch() 199{ 200 [ -s "$1" ] || return 0 201 202 awk 'int($1) > 0 { epoch = $1 } END { print epoch }' "$1" 203} 204 205clean_world() 206{ 207 local buildepoch="$1" 208 209 # The caller may set CLEANMK in the environment to make target(s) that 210 # should be invoked instead of just destroying everything. This is 211 # generally used after legacy/bootstrap tools to avoid over-cleansing 212 # since we're generally in the temporary tree's ancestor. 213 if [ -n "$CLEANMK" ]; then 214 echo "Cleaning up the object tree" 215 run $MAKE -C "$SRCTOP" -f "$SRCTOP"/Makefile.inc1 $CLEANMK 216 else 217 echo "Cleaning up the temporary build tree" 218 run rm -rf "$OBJTOP" 219 fi 220 221 # We don't assume that all callers will have grabbed the build epoch, so 222 # we'll do it here as needed. This will be useful if we add other 223 # non-epoch reasons to force clean. 224 if [ -z "$buildepoch" ]; then 225 buildepoch=$(extract_epoch "$SRCTOP"/.clean_build_epoch) 226 fi 227 228 mkdir -p "$OBJTOP" 229 echo "$buildepoch" > "$OBJTOP"/.clean_build_epoch 230 231 exit 0 232} 233 234check_epoch() 235{ 236 local srcepoch objepoch 237 238 srcepoch=$(extract_epoch "$SRCTOP"/.clean_build_epoch) 239 if [ -z "$srcepoch" ]; then 240 err "Malformed .clean_build_epoch; please validate the last line" 241 fi 242 243 # We don't discriminate between the varying degrees of difference 244 # between epochs. If it went backwards we could be bisecting across 245 # epochs, in which case the original need to clean likely still stands. 246 objepoch=$(extract_epoch "$OBJTOP"/.clean_build_epoch) 247 if [ -z "$objepoch" ] || [ "$srcepoch" -ne "$objepoch" ]; then 248 if [ "$VERBOSE" ]; then 249 echo "Cleaning - src epoch: $srcepoch, objdir epoch: ${objepoch:-unknown}" 250 fi 251 252 clean_world "$srcepoch" 253 # NORETURN 254 fi 255} 256 257check_epoch 258 259#### Typical dependency cleanup begins here. 260 261# Date Rev Description 262 263# 20220326 fbc002cb72d2 move from bcmp.c to bcmp.S 264if [ "$MACHINE_ARCH" = "amd64" ]; then 265 clean_dep lib/libc bcmp c 266fi 267 268# 20220524 68fe988a40ca kqueue_test binary replaced shell script 269if stat "$OBJTOP"/tests/sys/kqueue/libkqueue/*kqtest* \ 270 "$OBJTOP"/tests/sys/kqueue/libkqueue/.depend.kqtest* >/dev/null 2>&1; then 271 echo "Removing old kqtest" 272 run rm -fv "$OBJTOP"/tests/sys/kqueue/libkqueue/.depend.* \ 273 "$OBJTOP"/tests/sys/kqueue/libkqueue/* 274fi 275 276# 20221115 42d10b1b56f2 move from rs.c to rs.cc 277clean_dep usr.bin/rs rs c 278 279# 20230110 bc42155199b5 usr.sbin/zic/zic -> usr.sbin/zic 280if [ -d "$OBJTOP"/usr.sbin/zic/zic ] ; then 281 echo "Removing old zic directory" 282 run rm -rf "$OBJTOP"/usr.sbin/zic/zic 283fi 284 285# 20230208 29c5f8bf9a01 move from mkmakefile.c to mkmakefile.cc 286clean_dep usr.sbin/config mkmakefile c 287# 20230209 83d7ed8af3d9 convert to main.cc and mkoptions.cc 288clean_dep usr.sbin/config main c 289clean_dep usr.sbin/config mkoptions c 290 291# 20230401 54579376c05e kqueue1 from syscall to C wrapper 292clean_dep lib/libc kqueue1 S 293 294# 20230623 b077aed33b7b OpenSSL 3.0 update 295if [ -f "$OBJTOP"/secure/lib/libcrypto/aria.o ]; then 296 echo "Removing old OpenSSL 1.1.1 tree" 297 for libcompat in "" $ALL_libcompats; do 298 dirprfx=${libcompat:+obj-lib${libcompat}/} 299 run rm -rf "$OBJTOP"/${dirprfx}secure/lib/libcrypto \ 300 "$OBJTOP"/${dirprfx}secure/lib/libssl 301 done 302fi 303 304# 20230714 ee8b0c436d72 replace ffs/fls implementations with clang builtins 305clean_dep lib/libc ffs S 306clean_dep lib/libc ffsl S 307clean_dep lib/libc ffsll S 308clean_dep lib/libc fls S 309clean_dep lib/libc flsl S 310clean_dep lib/libc flsll S 311 312# 20230815 28f6c2f29280 GoogleTest update 313if [ -e "$OBJTOP"/tests/sys/fs/fusefs/mockfs.o ] && \ 314 grep -q '_ZN7testing8internal18g_linked_ptr_mutexE' "$OBJTOP"/tests/sys/fs/fusefs/mockfs.o; then 315 echo "Removing stale fusefs GoogleTest objects" 316 run rm -rf "$OBJTOP"/tests/sys/fs/fusefs 317fi 318 319# 20231031 0527c9bdc718 Remove forward compat ino64 stuff 320clean_dep lib/libc fstat c 321clean_dep lib/libc fstatat c 322clean_dep lib/libc fstatfs c 323clean_dep lib/libc getdirentries c 324clean_dep lib/libc getfsstat c 325clean_dep lib/libc statfs c 326 327# 20240308 e6ffc7669a56 Remove pointless MD syscall(2) 328# 20240308 0ee0ae237324 Remove pointless MD syscall(2) 329# 20240308 7b3836c28188 Remove pointless MD syscall(2) 330if [ ${MACHINE} != i386 ]; then 331 libcompats= 332 for libcompat in $ALL_libcompats; do 333 if [ $MACHINE = amd64 ] && [ $libcompat = 32 ]; then 334 continue 335 fi 336 libcompats="${libcompats+$libcompats }$libcompat" 337 done 338 ALL_libcompats="$libcompats" clean_dep lib/libsys syscall S ".*/syscall\.S" 339 ALL_libcompats="$libcompats" clean_dep lib/libc syscall S ".*/syscall\.S" 340fi 341 342# 20240416 2fda3ab0ac19 WITH_NVME: Remove from broken 343if [ -f "$OBJTOP"/rescue/rescue/rescue.mk ] && \ 344 ! grep -q 'nvme_util.o' "$OBJTOP"/rescue/rescue/rescue.mk; then 345 echo "removing rescue.mk without nvme_util.o" 346 run rm -fv "$OBJTOP"/rescue/rescue/rescue.mk 347fi 348 349# 20240910 e2df9bb44109 350clean_dep cddl/lib/libzpool abd_os c "linux/zfs/abd_os\.c" 351 352# 20241007 353clean_dep cddl/lib/libzpool zfs_debug c "linux/zfs/zfs_debug\.c" 354 355# 20241011 356clean_dep cddl/lib/libzpool arc_os c "linux/zfs/arc_os\.c" 357 358# 20241018 1363acbf25de libc/csu: Support IFUNCs on riscv 359if [ ${MACHINE} = riscv ]; then 360 for f in "$OBJTOP"/lib/libc/.depend.libc_start1.*o; do 361 if [ ! -f "$f" ]; then 362 continue 363 fi 364 if ! grep -q 'lib/libc/csu/riscv/reloc\.c' "$f"; then 365 echo "Removing stale dependencies and objects for libc_start1.c" 366 run rm -fv \ 367 "$OBJTOP"/lib/libc/.depend.libc_start1.* \ 368 "$OBJTOP"/lib/libc/libc_start1.*o 369 break 370 fi 371 done 372fi 373 374# 20241018 5deeebd8c6ca Merge llvm-project release/19.x llvmorg-19.1.2-0-g7ba7d8e2f7b6 375p="$OBJTOP"/lib/clang/libclang/clang/Basic 376f="$p"/arm_mve_builtin_sema.inc 377if [ -e "$f" ]; then 378 if grep -q SemaBuiltinConstantArgRange "$f"; then 379 echo "Removing pre-llvm19 clang-tblgen output" 380 run rm -fv "$p"/*.inc 381 fi 382fi 383 384# 20241025 cb5e41b16083 Unbundle hash functions fom lib/libcrypt 385clean_obj lib/libcrypt crypt-md5 c __MD5Init 386clean_obj lib/libcrypt crypt-nthash c __MD4Init 387clean_obj lib/libcrypt crypt-sha256 c __SHA256Init 388clean_obj lib/libcrypt crypt-sha512 c __SHA512Init 389 390# 20241213 b55f5e1c4ae3 jemalloc: Move generated jemalloc.3 into lib/libc tree 391if [ -h "$OBJTOP"/lib/libc/jemalloc.3 ]; then 392 # Have to cleanup the jemalloc.3 in the obj tree since make gets 393 # confused and won't use the one in lib/libc/malloc/jemalloc/jemalloc.3 394 echo "Removing stale jemalloc.3 object" 395 run rm -fv "$OBJTOP"/lib/libc/jemalloc.3 396fi 397 398if [ $MACHINE_ARCH = aarch64 ]; then 399 # 20250110 5e7d93a60440 add strcmp SIMD implementation 400 ALL_libcompats= clean_dep lib/libc strcmp S arm-optimized-routines 401 run rm -fv "$OBJTOP"/lib/libc/strcmp.S 402 403 # 20250110 b91003acffe7 add strspn optimized implementation 404 ALL_libcompats= clean_dep lib/libc strspn c 405 406 # 20250110 f2bd390a54f1 add strcspn optimized implementation 407 ALL_libcompats= clean_dep lib/libc strcspn c 408 409 # 20250110 89b3872376cb add optimized strpbrk & strsep implementations 410 ALL_libcompats= clean_dep lib/libc strpbrk c "libc.string.strpbrk.c" 411 412 # 20250110 79287d783c72 strcat enable use of SIMD 413 ALL_libcompats= clean_dep lib/libc strcat c "libc.string.strcat.c" 414 415 # 20250110 756b7fc80837 add strlcpy SIMD implementation 416 ALL_libcompats= clean_dep lib/libc strlcpy c 417 418 # 20250110 25c485e14769 add strncmp SIMD implementation 419 ALL_libcompats= clean_dep lib/libc strncmp S arm-optimized-routines 420 run rm -fv "$OBJTOP"/lib/libc/strncmp.S 421 422 # 20250110 bad17991c06d add memccpy SIMD implementation 423 ALL_libcompats= clean_dep lib/libc memccpy c 424 425 # 20250110 3dc5429158cf add strncat SIMD implementation 426 ALL_libcompats= clean_dep lib/libc strncat c "libc.string.strncat.c" 427 428 # 20250110 bea89d038ac5 add strlcat SIMD implementation, and move memchr 429 ALL_libcompats= clean_dep lib/libc strlcat c "libc.string.strlcat.c" 430 ALL_libcompats= clean_dep lib/libc memchr S "[[:space:]]memchr.S" 431 run rm -fv "$OBJTOP"/lib/libc/memchr.S 432 433 # 20250110 3863fec1ce2d add strlen SIMD implementation 434 ALL_libcompats= clean_dep lib/libc strlen S arm-optimized-routines 435 run rm -fv "$OBJTOP"/lib/libc/strlen.S 436 437 # 20250110 79e01e7e643c add bcopy & bzero wrapper 438 ALL_libcompats= clean_dep lib/libc bcopy c "libc.string.bcopy.c" 439 ALL_libcompats= clean_dep lib/libc bzero c "libc.string.bzero.c" 440 441 # 20250110 f2c98669fc1b add ASIMD-enhanced timingsafe_bcmp implementation 442 ALL_libcompats= clean_dep lib/libc timingsafe_bcmp c 443 444 # 20250110 3f224333af16 add timingsafe_memcmp() assembly implementation 445 ALL_libcompats= clean_dep lib/libc timingsafe_memcmp c 446fi 447 448# 20250402 839d0755fea8 ctld converted to C++ 449clean_dep usr.sbin/ctld ctld c 450clean_dep usr.sbin/ctld conf c 451clean_dep usr.sbin/ctld discovery c 452clean_dep usr.sbin/ctld isns c 453clean_dep usr.sbin/ctld kernel c 454clean_dep usr.sbin/ctld login c 455clean_dep usr.sbin/ctld uclparse c 456 457# 20250425 2e47f35be5dc libllvm, libclang and liblldb became shared libraries 458if [ -f "$OBJTOP"/lib/clang/libllvm/libllvm.a ]; then 459 echo "Removing old static libllvm library" 460 run rm -fv "$OBJTOP"/lib/clang/libllvm/libllvm.a 461fi 462if [ -f "$OBJTOP"/lib/clang/libclang/libclang.a ]; then 463 echo "Removing old static libclang library" 464 run rm -fv "$OBJTOP"/lib/clang/libclang/libclang.a 465fi 466if [ -f "$OBJTOP"/lib/clang/liblldb/liblldb.a ]; then 467 echo "Removing old static liblldb library" 468 run rm -fv "$OBJTOP"/lib/clang/liblldb/liblldb.a 469fi 470 471# 20250813 4f766afc1ca0 tcopy converted to C++ 472clean_dep usr.bin/tcopy tcopy c 473 474# 20250904 aef807876c30 moused binary to directory 475if [ -f "$OBJTOP"/usr.sbin/moused/moused ]; then 476 echo "Removing old moused binary" 477 run rm -fv "$OBJTOP"/usr.sbin/moused/moused 478fi 479