Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2014, 2015, 2016 Ingo Schwarze <schwarze@openbsd.org>
4 # Copyright (c) 2017, 2018 Kristaps Dzonsons <kristaps@bsd.lv>
5 # Copyright (c) 2022, 2023 Omar Polo <op@openbsd.org>
6 #
7 # Permission to use, copy, modify, and distribute this software for any
8 # purpose with or without fee is hereby granted, provided that the above
9 # copyright notice and this permission notice appear in all copies.
10 #
11 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 CDIAGFLAGS="-Wall -Wuninitialized -Wunused -Wmissing-prototypes"
20 CDIAGFLAGS="${CDIAGFLAGS} -Wstrict-prototypes -Wno-pointer-sign"
22 # don't ship releases with -Werror
23 test "$RELEASE" = no && CDIAGFLAGS="${CDIAGFLAGS} -Werror"
25 COMPATS=
27 push_cflags() {
28 if ! echo "$CFLAGS" | grep -q -e "$1"; then
29 CFLAGS="${CFLAGS+$CFLAGS } $1"
30 fi
31 }
33 push_ldflags() {
34 if ! echo "$LDFLAGS" | grep -q -e "$1"; then
35 LDFLAGS="${LDFLAGS+$LDFLAGS } $1"
36 fi
37 }
39 # singletest name var extra-cflags extra-libs msg
40 singletest() {
41 msg="$5"
42 if [ -z "$msg" ]; then
43 if [ -n "$3" ]; then
44 msg=" ($3)"
45 elif [ -n "$4" ]; then
46 msg=" ($4)"
47 fi
48 elif [ "$msg" = no ]; then
49 msg=""
50 fi
52 cat >&3 <<EOF
53 $1: testing...
54 $CC tests/$1.c $CFLAGS -Werror $3 -o test-$1 $LDFLAGS $4
55 EOF
56 if $CC tests/$1.c $CFLAGS -Werror $3 -o test-$1 $LDFLAGS $4 >&3 2>&3;
57 then
58 rm -f test-$1 test-$1.d
60 echo "$1: $CC$msg succeeded" >&3
61 echo "$1$msg: yes"
62 echo >&3
64 return 0
65 fi
67 echo "$1: $CC$msg failed with $?" >&3
68 echo "$1$msg: no"
69 echo >&3
71 rm -f test-$1 test-$1.d
73 return 1
74 }
76 # runtest name var extra-cflags extra-libs pkgconfig-name
77 runtest() {
78 if singletest "$1" "$2" "" ""; then
79 eval HAVE_$2=1
80 return 0
81 fi
83 if [ -n "$3" -o -n "$4" ]; then
84 echo "retrying with ${3+$3 }$4" >&3
85 if singletest "$1" "$2" "$3" "$4"; then
86 if [ -n "$3" ]; then
87 push_cflags "$3"
88 fi
89 if [ -n "$4" ]; then
90 push_ldflags "$4"
91 fi
92 eval HAVE_$2=1
93 return 0
94 fi
95 fi
97 if [ -n "$5" -a -n "$pkgconfig" ]; then
98 if $pkgconfig "$5"; then
99 cflags="$($pkgconfig --cflags "$5")"
100 ldflags="$($pkgconfig --libs "$5")"
101 echo "retrying with $pkgconfig" >&3
102 if singletest "$1" "$2" "$cflags" "$ldflags"; then
103 push_cflags "$cflags"
104 push_ldflags "$ldflags"
105 eval HAVE_$2=1
106 return 0
107 fi
108 fi
109 fi
111 if [ -f compat/$1.c ]; then
112 COMPATS="${COMPATS+$COMPATS }compat/$1.c"
113 fi
115 eval HAVE_$2=0
116 return 1
119 if singletest MMD _MMD -MMD >/dev/null; then
120 push_cflags -MMD
121 echo "adding -MMD to CFLAGS" >&2
122 echo "adding -MMD to CFLAGS" >&3
123 fi
125 if ! singletest WAIT_ANY WAIT_ANY; then
126 push_cflags -DWAIT_ANY=-1
127 fi
129 runtest __progname __PROGNAME || true
130 runtest err ERR || true
131 runtest freezero FREEZERO || true
132 runtest getdtablecount GETDTABLECOUNT || true
133 runtest getdtablesize GETDTABLESIZE || true
134 runtest getexecname GETEXECNAME || true
135 runtest getprogname GETPROGNAME || true
136 runtest libevent LIBEVENT "" -levent libevent_core || true
137 runtest pledge PLEDGE || true
138 runtest recallocarray RECALLOCARRAY -D_OPENBSD_SOURCE || true
139 runtest setgroups SETGROUPS -D_BSD_SOURCE || true
140 runtest setproctitle SETPROCTITLE || true
141 runtest setresgid SETRESGID -D_GNU_SOURCE || true
142 runtest setresuid SETRESUID -D_GNU_SOURCE || true
143 runtest sqlite3 SQLITE3 "" -lsqlite3 sqlite3 || true
144 runtest strlcat STRLCAT || true
145 runtest strlcpy STRLCPY || true
146 runtest strtonum STRTONUM || true
147 runtest sys_queue SYS_QUEUE || true
148 runtest sys_tree SYS_TREE || true
149 runtest unveil UNVEIL || true
150 runtest vasprintf VASPRINTF -D_GNU_SOURCE || true
152 if [ "$HAVE_SYS_QUEUE" -eq 0 -o "$HAVE_SYS_TREE" -eq 0 ]; then
153 CFLAGS="-I compat/sys $CFLAGS"
154 fi
156 if [ -n "$COMPATS" ]; then
157 CFLAGS="-I compat $CFLAGS"
158 fi
160 CFLAGS="$CFLAGS $CDIAGFLAGS"
162 exec > config.h
163 echo "config.h writing..." >&2
165 cat <<EOF
166 #ifndef CONFIG_H
167 #define CONFIG_H
169 #ifdef __cplusplus
170 # error "Do not use C++: this is a C application."
171 #endif
173 #define HAVE___PROGNAME ${HAVE___PROGNAME}
174 #define HAVE_ERR ${HAVE_ERR}
175 #define HAVE_FREEZERO ${HAVE_FREEZERO}
176 #define HAVE_GETDTABLECOUNT ${HAVE_GETDTABLECOUNT}
177 #define HAVE_GETDTABLESIZE ${HAVE_GETDTABLESIZE}
178 #define HAVE_GETEXECNAME ${HAVE_GETEXECNAME}
179 #define HAVE_GETPROGNAME ${HAVE_GETPROGNAME}
180 #define HAVE_SQLITE3 ${HAVE_SQLITE3}
181 #define HAVE_PLEDGE ${HAVE_PLEDGE}
182 #define HAVE_RECALLOCARRAY ${HAVE_RECALLOCARRAY}
183 #define HAVE_SETGROUPS ${HAVE_SETGROUPS}
184 #define HAVE_SETPROCTITLE ${HAVE_SETPROCTITLE}
185 #define HAVE_SETRESGID ${HAVE_SETRESGID}
186 #define HAVE_SETRESUID ${HAVE_SETRESUID}
187 #define HAVE_STRLCAT ${HAVE_STRLCAT}
188 #define HAVE_STRLCPY ${HAVE_STRLCPY}
189 #define HAVE_STRTONUM ${HAVE_STRTONUM}
190 #define HAVE_SYS_QUEUE ${HAVE_SYS_QUEUE}
191 #define HAVE_SYS_TREE ${HAVE_SYS_TREE}
192 #define HAVE_UNVEIL ${HAVE_UNVEIL}
193 #define HAVE_VASPRINTF ${HAVE_VASPRINTF}
195 #endif
196 EOF
198 exec > config.mk
199 echo "config.mk: writing..." >&2
201 cat <<EOF
202 include ../config.mk
204 CC = ${CC}
205 CFLAGS = ${CFLAGS}
206 LDFLAGS = ${LDFLAGS}
208 COMPATS = ${COMPATS}
209 EOF