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