]> The Tcpdump Group git mirrors - tcpdump/blob - build_common.sh
Makefile: Run shellcheck for build_common.sh too. [skip ci]
[tcpdump] / build_common.sh
1 #!/bin/sh -e
2
3 # The only purpose of the above shebang is to orient shellcheck right.
4 # To make CI scripts maintenance simpler, copies of this file in the
5 # libpcap, tcpdump and tcpslice git repositories should be identical.
6 # Please mind that Solaris /bin/sh before 11 does not support the $()
7 # command substitution syntax, hence the SC2006 directives.
8
9 # A poor man's mktemp(1) for OSes that don't have one (e.g. AIX 7, Solaris 9).
10 mktempdir_diy() {
11 while true; do
12 # /bin/sh implements $RANDOM in AIX 7, but not in Solaris before 11,
13 # thus use dd and od instead.
14 # shellcheck disable=SC2006
15 mktempdir_diy_suffix=`dd if=/dev/urandom bs=1 count=4 2>/dev/null | od -t x -A n | head -1 | tr -d '\t '`
16 [ -z "$mktempdir_diy_suffix" ] && return 1
17 mktempdir_diy_path="${TMPDIR:-/tmp}/${1:?}.${mktempdir_diy_suffix}"
18 # "test -e" would be more appropriate, but it is not available in
19 # Solaris /bin/sh before 11.
20 if [ ! -d "$mktempdir_diy_path" ]; then
21 mkdir "$mktempdir_diy_path"
22 chmod go= "$mktempdir_diy_path"
23 echo "$mktempdir_diy_path"
24 break
25 fi
26 # Try again (AIX /dev/urandom returns zeroes quite often).
27 done
28 }
29
30 mktempdir() {
31 mktempdir_prefix=${1:-tmp}
32 # shellcheck disable=SC2006
33 case `uname -s` in
34 Darwin|FreeBSD|NetBSD)
35 # In these operating systems mktemp(1) always appends an implicit
36 # ".XXXXXXXX" suffix to the requested template when creating a
37 # temporary directory.
38 mktemp -d -t "$mktempdir_prefix"
39 ;;
40 AIX)
41 mktempdir_diy "$mktempdir_prefix"
42 ;;
43 SunOS)
44 # shellcheck disable=SC2006
45 case `uname -r` in
46 5.10|5.11)
47 mktemp -d -t "${mktempdir_prefix}.XXXXXXXX"
48 ;;
49 *)
50 mktempdir_diy "$mktempdir_prefix"
51 ;;
52 esac
53 ;;
54 *)
55 # At least Linux and OpenBSD implementations require explicit trailing
56 # X'es in the template, so make it the same suffix as above.
57 mktemp -d -t "${mktempdir_prefix}.XXXXXXXX"
58 ;;
59 esac
60 }
61
62 print_sysinfo() {
63 uname -a
64 date
65 }
66
67 print_cc_version() {
68 # shellcheck disable=SC2006
69 case `basename "$CC"` in
70 gcc*|clang*)
71 # GCC and Clang recognize --version, print to stdout and exit with 0.
72 "$CC" --version
73 ;;
74 xl*)
75 # XL C for AIX recognizes -qversion, prints to stdout and exits with 0,
76 # but on an unknown command-line flag displays its man page and waits.
77 "$CC" -qversion
78 ;;
79 sun*)
80 # Sun compilers recognize -V, print to stderr and exit with an error.
81 "$CC" -V 2>&1 || :
82 ;;
83 *)
84 "$CC" --version || "$CC" -V || :
85 ;;
86 esac
87 }
88
89 increment() {
90 # No arithmetic expansion in Solaris /bin/sh before 11.
91 echo "${1:?} + 1" | bc
92 }
93
94 # Display text in magenta.
95 echo_magenta() {
96 # ANSI magenta, the imploded text, ANSI reset, newline.
97 printf '\033[35;1m%s\033[0m\n' "$*"
98 }
99
100 # Run a command after displaying it.
101 run_after_echo() {
102 : "${1:?}" # Require at least one argument.
103 printf '$ %s\n' "$*"
104 "$@"
105 }
106
107 print_so_deps() {
108 # shellcheck disable=SC2006
109 case `uname -s` in
110 Darwin)
111 run_after_echo otool -L "${1:?}"
112 ;;
113 *)
114 run_after_echo ldd "${1:?}"
115 ;;
116 esac
117 }
118
119 # Beware that setting MATRIX_DEBUG for tcpdump or tcpslice will produce A LOT
120 # of additional output there and in any nested libpcap builds. Multiplied by
121 # the matrix size, the full output log size might exceed limits of some CI
122 # systems (as it had previously happened with Travis CI). Use with caution on
123 # a reduced matrix.
124 handle_matrix_debug() {
125 [ "$MATRIX_DEBUG" != yes ] && return
126 echo '$ cat Makefile [...]'
127 sed '/^# DO NOT DELETE THIS LINE -- mkdep uses it.$/q' <Makefile
128 run_after_echo cat config.h
129 [ "$CMAKE" = yes ] || run_after_echo cat config.log
130 }
131
132 purge_directory() {
133 # shellcheck disable=SC2006
134 if [ "`uname -s`" = SunOS ] && [ "`uname -r`" = 5.11 ]; then
135 # In Solaris 11 /bin/sh the pathname expansion of "*" always includes
136 # "." and "..", so the straightforward rm would always fail.
137 (
138 cd "${1:?}"
139 for pd_each in *; do
140 if [ "$pd_each" != . ] && [ "$pd_each" != .. ]; then
141 rm -rf "$pd_each"
142 fi
143 done
144 )
145 else
146 rm -rf "${1:?}"/*
147 fi
148 }
149
150 # vi: set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab autoindent :