xref: /freebsd/tools/build/depend-cleanup.sh (revision 9978553d0199e7ec0bdd1c44fc7f6c7b0c11e43b)
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# latest clean epoch (but not pushed until 20250814)
264# 20250807	# All OpenSSL-using bits need rebuilt
265
266# Examples from the past, not currently active
267#
268#Binary program replaced a shell script
269# 20220524  68fe988a40ca    kqueue_test binary replaced shell script
270#if stat "$OBJTOP"/tests/sys/kqueue/libkqueue/*kqtest* \
271#    "$OBJTOP"/tests/sys/kqueue/libkqueue/.depend.kqtest* >/dev/null 2>&1; then
272#       echo "Removing old kqtest"
273#       run rm -fv "$OBJTOP"/tests/sys/kqueue/libkqueue/.depend.* \
274#          "$OBJTOP"/tests/sys/kqueue/libkqueue/*
275#fi
276
277# 20250904  aef807876c30    moused binary to directory
278if [ -f "$OBJTOP"/usr.sbin/moused/moused ]; then
279	echo "Removing old moused binary"
280        run rm -fv "$OBJTOP"/usr.sbin/moused/moused
281fi
282
283if [ ${MACHINE} = riscv ]; then
284	# 20251031  df21a004be23  libc: scalar strrchr() in RISC-V assembly
285	clean_dep   lib/libc strrchr c
286
287	# 20251031  563efdd3bd5d  libc: scalar memchr() in RISC-V assembly
288	clean_dep   lib/libc memchr c
289
290	# 20251031  40a958d5850d  libc: scalar memset() in RISC-V assembly
291	clean_dep   lib/libc memset c
292
293	# 20251031  e09c1583eddd  libc: scalar strlen() in RISC-V assembly
294	clean_dep   lib/libc strlen c
295
296	# 20251031  25fdd86a4c92  libc: scalar memcpy() in RISC-V assembly
297	clean_dep   lib/libc memcpy c
298
299	# 20251031  5a52f0704435  libc: scalar strnlen() in RISC-V assembly
300	clean_dep   lib/libc strnlen c
301
302	# 20251031  08af0bbc9c7d  libc: scalar strchrnul() in RISC-V assembly
303	clean_dep   lib/libc strchrnul c
304
305	# 20251031  b5dbf3de5611  libc/riscv64: implement bcopy() and bzero() through memcpy() and memset()
306	clean_dep   lib/libc bcopy c "libc.string.bcopy.c"
307	clean_dep   lib/libc bzero c "libc.string.bzero.c"
308fi
309