]> The Tcpdump Group git mirrors - tcpdump/blob - build_common.sh
794ec2c6850a095c09ae4bdb1645931b447708f9
[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 # Try to make the current C compiler print its version information (usually
68 # multi-line) to stdout.
69 # shellcheck disable=SC2006
70 print_cc_version() {
71 case `basename "$CC"` in
72 gcc*|egcc*|clang*)
73 # GCC and Clang recognize --version, print to stdout and exit with 0.
74 "$CC" --version
75 ;;
76 xl*)
77 # XL C for AIX recognizes -qversion, prints to stdout and exits with 0,
78 # but on an unknown command-line flag displays its man page and waits.
79 "$CC" -qversion
80 ;;
81 sun*)
82 # Sun compilers recognize -V, print to stderr and exit with an error.
83 "$CC" -V 2>&1 || :
84 ;;
85 cc)
86 case `uname -s` in
87 SunOS)
88 # Most likely Sun C.
89 "$CC" -V 2>&1 || :
90 ;;
91 Darwin)
92 # Most likely Clang.
93 "$CC" --version
94 ;;
95 Linux|FreeBSD|NetBSD|OpenBSD)
96 # Most likely Clang or GCC.
97 "$CC" --version
98 ;;
99 esac
100 ;;
101 *)
102 "$CC" --version || "$CC" -V || :
103 ;;
104 esac
105 }
106
107 # For the current C compiler try to print a short and uniform identification
108 # string (such as "gcc-9.3.0") that is convenient to use in a case statement.
109 # shellcheck disable=SC2006
110 cc_id() {
111 cc_id_firstline=`print_cc_version | head -1`
112
113 cc_id_guessed=`echo "$cc_id_firstline" | sed 's/^.*clang version \([0-9\.]*\).*$/clang-\1/'`
114 if [ "$cc_id_firstline" != "$cc_id_guessed" ]; then
115 echo "$cc_id_guessed"
116 return
117 fi
118
119 cc_id_guessed=`echo "$cc_id_firstline" | sed 's/^IBM XL C.* for AIX, V\([0-9\.]*\).*$/xlc-\1/'`
120 if [ "$cc_id_firstline" != "$cc_id_guessed" ]; then
121 echo "$cc_id_guessed"
122 return
123 fi
124
125 cc_id_guessed=`echo "$cc_id_firstline" | sed 's/^.* Sun C \([0-9\.]*\) .*$/suncc-\1/'`
126 if [ "$cc_id_firstline" != "$cc_id_guessed" ]; then
127 echo "$cc_id_guessed"
128 return
129 fi
130
131 cc_id_guessed=`echo "$cc_id_firstline" | sed 's/^.* (.*) \([0-9\.]*\)$/gcc-\1/'`
132 if [ "$cc_id_firstline" != "$cc_id_guessed" ]; then
133 echo "$cc_id_guessed"
134 return
135 fi
136 }
137
138 # For the current C compiler try to print CFLAGS value that tells to treat
139 # warnings as errors.
140 # shellcheck disable=SC2006
141 cc_werr_cflags() {
142 case `cc_id` in
143 gcc-*|clang-*)
144 echo '-Werror'
145 ;;
146 xlc-*)
147 echo '-qhalt=w'
148 ;;
149 suncc-*)
150 echo '-errwarn=%all'
151 ;;
152 esac
153 }
154
155 increment() {
156 # No arithmetic expansion in Solaris /bin/sh before 11.
157 echo "${1:?} + 1" | bc
158 }
159
160 # Display text in magenta.
161 echo_magenta() {
162 # ANSI magenta, the imploded text, ANSI reset, newline.
163 printf '\033[35;1m%s\033[0m\n' "$*"
164 }
165
166 # Run a command after displaying it.
167 run_after_echo() {
168 : "${1:?}" # Require at least one argument.
169 printf '$ %s\n' "$*"
170 "$@"
171 }
172
173 print_so_deps() {
174 # shellcheck disable=SC2006
175 case `uname -s` in
176 Darwin)
177 run_after_echo otool -L "${1:?}"
178 ;;
179 *)
180 run_after_echo ldd "${1:?}"
181 ;;
182 esac
183 }
184
185 # Beware that setting MATRIX_DEBUG for tcpdump or tcpslice will produce A LOT
186 # of additional output there and in any nested libpcap builds. Multiplied by
187 # the matrix size, the full output log size might exceed limits of some CI
188 # systems (as it had previously happened with Travis CI). Use with caution on
189 # a reduced matrix.
190 handle_matrix_debug() {
191 [ "$MATRIX_DEBUG" != yes ] && return
192 echo '$ cat Makefile [...]'
193 sed '/^# DO NOT DELETE THIS LINE -- mkdep uses it.$/q' <Makefile
194 run_after_echo cat config.h
195 [ "$CMAKE" = yes ] || run_after_echo cat config.log
196 }
197
198 purge_directory() {
199 # shellcheck disable=SC2006
200 if [ "`uname -s`" = SunOS ] && [ "`uname -r`" = 5.11 ]; then
201 # In Solaris 11 /bin/sh the pathname expansion of "*" always includes
202 # "." and "..", so the straightforward rm would always fail.
203 (
204 cd "${1:?}"
205 for pd_each in *; do
206 if [ "$pd_each" != . ] && [ "$pd_each" != .. ]; then
207 rm -rf "$pd_each"
208 fi
209 done
210 )
211 else
212 rm -rf "${1:?}"/*
213 fi
214 }
215
216 # vi: set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab autoindent :