xref: /freebsd/tools/build/stale-symlink-buildworld.sh (revision 53bb5613a8a15363718b6e6de8d965bf9a2c5469)
1#!/bin/sh
2# Copyright (c) Feb 2024 Wolfram Schneider <wosch@FreeBSD.org>
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# stale-symlink-buildworld.sh - check for stale symlinks on a FreeBSD system
6#
7# The purpose of this script is to detect stale symlinks, report them to
8# stderr and exit with a non-zero status. All other cases are ignored,
9# such as no symlinks, missing directories, permission problems, etc.
10#
11# You can run the script before or after `make installworld', or any other
12# make targets thats installs files.
13#
14# You can also check your local ports with:
15#
16#   env STALE_SYMLINK_BUILDWORLD_DIRS=/usr/local ./stale-symlink-buildworld.sh
17
18
19PATH="/bin:/usr/bin"; export PATH
20
21: ${ncpu=$(nproc)}
22
23obj_dir_prefix=${MAKEOBJDIRPREFIX:="/usr/obj"}
24
25# check other directories as well
26: ${STALE_SYMLINK_BUILDWORLD_DIRS=$obj_dir_prefix}
27
28trap 'rm -f $script' 0
29script=$(mktemp -t stale-symlink)
30chmod u+x $script
31
32# create a temporary shell script to check for stale symbolic links
33cat << 'EOF' > $script
34file="$1"
35
36if [ ! -e "$file" ]; then
37  echo "stale symlink detected: $(ls -ld $file)" >&2
38  exit 1
39else
40  exit 0
41fi
42EOF
43
44find -s -H \
45  /bin \
46  /boot \
47  /etc \
48  /lib \
49  /libexec \
50  /sbin \
51  /usr/bin \
52  /usr/include \
53  /usr/lib \
54  /usr/lib32 \
55  /usr/libdata \
56  /usr/libexec \
57  /usr/sbin \
58  /usr/src \
59  /usr/share \
60  $STALE_SYMLINK_BUILDWORLD_DIRS \
61  -type l \
62  -print0 | xargs -n1 -0 -P${ncpu} $script
63
64#EOF
65