Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/tools/build/depend-cleanup.sh
106763 views
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
71
set -e
72
set -u
73
74
warn()
75
{
76
echo "$(basename "$0"):" "$@" >&2
77
}
78
79
err()
80
{
81
warn "$@"
82
exit 1
83
}
84
85
usage()
86
{
87
echo "usage: $(basename $0) [-v] [-n] objtop srctop" >&2
88
}
89
90
VERBOSE=
91
PRETEND=
92
while 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
105
done
106
shift $((OPTIND-1))
107
108
if [ $# -ne 2 ]; then
109
usage
110
exit 1
111
fi
112
113
OBJTOP=$1
114
shift
115
SRCTOP=$1
116
shift
117
118
if [ ! -d "$OBJTOP" ]; then
119
err "$OBJTOP: Not a directory"
120
fi
121
122
if [ ! -d "$SRCTOP" -o ! -f "$SRCTOP/Makefile.inc1" ]; then
123
err "$SRCTOP: Not the root of a src tree"
124
fi
125
126
: ${CLEANMK=""}
127
if [ -z "${MAKE+set}" ]; then
128
err "MAKE not set"
129
fi
130
131
if [ -z "${MACHINE+set}" ]; then
132
err "MACHINE not set"
133
fi
134
135
if [ -z "${MACHINE_ARCH+set}" ]; then
136
err "MACHINE_ARCH not set"
137
fi
138
139
if [ -z "${ALL_libcompats+set}" ]; then
140
err "ALL_libcompats not set"
141
fi
142
143
run()
144
{
145
if [ "$VERBOSE" ]; then
146
echo "$@"
147
fi
148
if ! [ "$PRETEND" ]; then
149
"$@"
150
fi
151
}
152
153
# Clean the depend and object files for a given source file if the
154
# depend file matches a regex (which defaults to the source file
155
# name). This is typically used if a file was renamed, especially if
156
# only its extension was changed (e.g. from .c to .cc).
157
#
158
# $1 directory
159
# $2 source filename w/o extension
160
# $3 source extension
161
# $4 optional regex for egrep -w
162
clean_dep()
163
{
164
local dirprfx dir
165
for libcompat in "" $ALL_libcompats; do
166
dirprfx=${libcompat:+obj-lib${libcompat}}
167
dir="${OBJTOP%/}/${dirprfx}/$1"
168
if egrep -qw "${4:-$2\.$3}" "${dir}"/.depend.$2.*o 2>/dev/null; then
169
echo "Removing stale ${libcompat:+lib${libcompat} }dependencies and objects for $2.$3"
170
run rm -fv "${dir}"/.depend.$2.* "${dir}"/$2.*o
171
fi
172
done
173
}
174
175
# Clean the object file for a given source file if it exists and
176
# matches a regex. This is typically used if a a change in CFLAGS or
177
# similar caused a change in the generated code without a change in
178
# the sources.
179
#
180
# $1 directory
181
# $2 source filename w/o extension
182
# $3 source extension
183
# $4 regex for egrep -w
184
clean_obj()
185
{
186
local dirprfx dir
187
for libcompat in "" $ALL_libcompats; do
188
dirprfx=${libcompat:+obj-lib${libcompat}}
189
dir="${OBJTOP%/}/${dirprfx}/$1"
190
if strings "${dir}"/$2.*o 2>/dev/null | egrep -qw "${4}"; then
191
echo "Removing stale ${libcompat:+lib${libcompat} }objects for $2.$3"
192
run rm -fv "${dir}"/$2.*o
193
fi
194
done
195
}
196
197
extract_epoch()
198
{
199
[ -s "$1" ] || return 0
200
201
awk 'int($1) > 0 { epoch = $1 } END { print epoch }' "$1"
202
}
203
204
# Regular expression matching the names of src.conf(5) options which
205
# don't affect the build.
206
#
207
# This filter is applied to both the current options and the cached
208
# options so we don't force a rebuild just because the filter itself
209
# changed.
210
IGNORED_OPTS="CLEAN|DEPEND_CLEANUP|EXAMPLES|MAN|TESTS|WARNS|WERROR"
211
IGNORED_OPTS="${IGNORED_OPTS}|INSTALL.*|STAGING.*"
212
# Also ignore TOOLCHAIN and the options it forces if set. It is
213
# commonly used to speed up a build and is safe to toggle.
214
IGNORED_OPTS="${IGNORED_OPTS}|TOOLCHAIN|CLANG.*|LLDB?|LLVM_(BIN|COV).*"
215
216
extract_src_opts()
217
{
218
$MAKE -C "$SRCTOP" -f "$SRCTOP"/Makefile.inc1 \
219
-V $'SRC_OPT_LIST:O:ts\n' |
220
egrep -v "^WITH(OUT)?_(${IGNORED_OPTS})="
221
}
222
223
extract_obj_opts()
224
{
225
local fn
226
for fn; do
227
if [ -f "${fn}" ]; then
228
cat "${fn}"
229
else
230
echo "# ${fn}"
231
fi
232
done |
233
egrep -v "^WITH(OUT)?_(${IGNORED_OPTS})="
234
}
235
236
clean_world()
237
{
238
local buildepoch="$1"
239
local srcopts="$2"
240
241
# The caller may set CLEANMK in the environment to make target(s) that
242
# should be invoked instead of just destroying everything. This is
243
# generally used after legacy/bootstrap tools to avoid over-cleansing
244
# since we're generally in the temporary tree's ancestor.
245
if [ -n "$CLEANMK" ]; then
246
echo "Cleaning up the object tree"
247
run $MAKE -C "$SRCTOP" -f "$SRCTOP"/Makefile.inc1 $CLEANMK
248
else
249
echo "Cleaning up the temporary build tree"
250
run rm -rf "$OBJTOP"
251
fi
252
253
# We don't assume that all callers will have grabbed the build epoch, so
254
# we'll do it here as needed. This will be useful if we add other
255
# non-epoch reasons to force clean.
256
if [ -z "$buildepoch" ]; then
257
buildepoch=$(extract_epoch "$SRCTOP"/.clean_build_epoch)
258
fi
259
260
mkdir -p "$OBJTOP"
261
echo "$buildepoch" > "$OBJTOP"/.clean_build_epoch
262
echo "$srcopts" > "$OBJTOP"/.src_opts
263
264
exit 0
265
}
266
267
check_epoch_and_opts()
268
{
269
local srcepoch objepoch
270
local srcopts objopts
271
272
srcepoch=$(extract_epoch "$SRCTOP"/.clean_build_epoch)
273
if [ -z "$srcepoch" ]; then
274
err "Malformed .clean_build_epoch; please validate the last line"
275
fi
276
277
srcopts=$(extract_src_opts)
278
if [ -z "$srcopts" ]; then
279
err "Unable to extract source options"
280
fi
281
282
# We don't discriminate between the varying degrees of difference
283
# between epochs. If it went backwards we could be bisecting across
284
# epochs, in which case the original need to clean likely still stands.
285
objepoch=$(extract_epoch "$OBJTOP"/.clean_build_epoch)
286
if [ -z "$objepoch" ] || [ "$srcepoch" -ne "$objepoch" ]; then
287
echo "Cleaning - src epoch: $srcepoch, objdir epoch: ${objepoch:-unknown}"
288
clean_world "$srcepoch" "$srcopts"
289
# NORETURN
290
fi
291
292
objopts=$(extract_obj_opts "$OBJTOP"/.src_opts)
293
if [ "$srcopts" != "$objopts" ]; then
294
echo "Cleaning - build options have changed"
295
clean_world "$srcepoch" "$srcopts"
296
# NORETURN
297
fi
298
}
299
300
check_epoch_and_opts
301
302
#### Typical dependency cleanup begins here.
303
304
# Date Rev Description
305
306
# latest clean epoch (but not pushed until 20250814)
307
# 20250807 # All OpenSSL-using bits need rebuilt
308
309
# Examples from the past, not currently active
310
#
311
#Binary program replaced a shell script
312
# 20220524 68fe988a40ca kqueue_test binary replaced shell script
313
#if stat "$OBJTOP"/tests/sys/kqueue/libkqueue/*kqtest* \
314
# "$OBJTOP"/tests/sys/kqueue/libkqueue/.depend.kqtest* >/dev/null 2>&1; then
315
# echo "Removing old kqtest"
316
# run rm -fv "$OBJTOP"/tests/sys/kqueue/libkqueue/.depend.* \
317
# "$OBJTOP"/tests/sys/kqueue/libkqueue/*
318
#fi
319
320
# 20251219 # libkrb5profile is now internal
321
for libcompat in "" $ALL_libcompats; do
322
dirprfx=${libcompat:+obj-lib${libcompat}}
323
dir="${OBJTOP%/}/${dirprfx}"/krb5/util/profile
324
if [ -L "${dir}"/libkrb5profile.so ]; then
325
run rm -rfv "${dir}"
326
fi
327
done
328
329
# 20250904 aef807876c30 moused binary to directory
330
if [ -f "$OBJTOP"/usr.sbin/moused/moused ]; then
331
echo "Removing old moused binary"
332
run rm -fv "$OBJTOP"/usr.sbin/moused/moused
333
fi
334
335
if [ ${MACHINE} = riscv ]; then
336
# 20251031 df21a004be23 libc: scalar strrchr() in RISC-V assembly
337
clean_dep lib/libc strrchr c
338
339
# 20251031 563efdd3bd5d libc: scalar memchr() in RISC-V assembly
340
clean_dep lib/libc memchr c
341
342
# 20251031 40a958d5850d libc: scalar memset() in RISC-V assembly
343
clean_dep lib/libc memset c
344
345
# 20251031 e09c1583eddd libc: scalar strlen() in RISC-V assembly
346
clean_dep lib/libc strlen c
347
348
# 20251031 25fdd86a4c92 libc: scalar memcpy() in RISC-V assembly
349
clean_dep lib/libc memcpy c
350
351
# 20251031 5a52f0704435 libc: scalar strnlen() in RISC-V assembly
352
clean_dep lib/libc strnlen c
353
354
# 20251031 08af0bbc9c7d libc: scalar strchrnul() in RISC-V assembly
355
clean_dep lib/libc strchrnul c
356
357
# 20251031 b5dbf3de5611 libc/riscv64: implement bcopy() and bzero() through memcpy() and memset()
358
clean_dep lib/libc bcopy c "libc.string.bcopy.c"
359
clean_dep lib/libc bzero c "libc.string.bzero.c"
360
fi
361
362
if [ ${MACHINE_ARCH} = "aarch64" ]; then
363
# 20260113 41ccf82b29f3 libc/aarch64: Use MOPS implementations of memcpy/memmove/memset where availble
364
clean_dep lib/libc memset S "[^/]memset.S"
365
run rm -fv "$OBJTOP"/lib/libc/memset.S
366
fi
367
368