-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigure
More file actions
executable file
·444 lines (368 loc) · 8.92 KB
/
Copy pathconfigure
File metadata and controls
executable file
·444 lines (368 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/bin/sh
# shellcheck disable=SC2030,SC2031
set -e
atexit() {
local _err="$?"
# Dump contents of generated files to config.log.
exec 1>>config.log 2>&1
set -x
[ -e config.h ] && cat config.h
[ -e config.mk ] && cat config.mk
rm -rf "$@" || :
[ "${_err}" -ne 0 ] && fatal
exit 0
}
compile() {
# shellcheck disable=SC2086
"${CC}" ${CPPFLAGS} -Werror -o /dev/null -x c - "$@"
}
cc_has_option() {
if echo 'int main(void) { return 0; }' | compile "$@"; then
echo "$@"
fi
}
cc_has_sanitizer() {
local _sanitizer
_sanitizer="$1"; shift; : "${_sanitizer:?}"
if echo 'int main(void) { return 0; }' |
compile "-fsanitize=${_sanitizer}" "$@"
then
echo "${_sanitizer}"
fi
}
cc_sanitizer_runtime() {
case "$(uname -s)" in
OpenBSD) echo "-fsanitize-minimal-runtime";;
*) ;;
esac
}
fatal() {
[ $# -gt 0 ] && echo "fatal: ${*}"
exec 1>&3 2>&4
cat config.log
exit 1
}
headers() {
local _tmp="${WRKDIR}/headers"
cat >"${_tmp}"
[ -s "${_tmp}" ] || return 0
xargs printf '#include <%s>\n' <"${_tmp}"
}
is_openbsd() {
case "$(uname -s)" in
OpenBSD) return 0;;
*) return 1;;
esac
}
make_has_object_dir() (
cd "${WRKDIR}"
# shellcheck disable=SC2016
printf 'all:\n\t[ ${PWD} = ${.OBJDIR} ]\n' >Makefile
printf 'all:\n\tfalse\n' >GNUMakefile
mkdir -p obj
make >/dev/null 1>&2
)
make_variable() {
# shellcheck disable=SC2016
var="$(printf 'all:\n\t@echo ${%s}\n' "$1" | make -sf -)"
if [ -n "${var}" ]; then
echo "${var}"
else
return 1
fi
}
# rmdup arg ...
#
# Remove duplicates from the given arguments while preserving the order.
rmdup() {
echo "$@" |
xargs printf '%s\n' |
awk '!x[$1]++' |
xargs
}
check_arc4random() {
compile <<-EOF
#include <stdlib.h>
int main(void) {
return !(arc4random() <= 0xffffffff);
}
EOF
}
check_errc() {
compile <<-EOF
#include <err.h>
int main(void) {
errc(1, 0, "");
return 0;
}
EOF
}
# Check if strptime(3) is hidden behind _GNU_SOURCE.
check_gnu_source() {
local _tmp="${WRKDIR}/gnu"
cat <<-EOF >"${_tmp}"
#include <time.h>
int main(void) {
struct tm tm;
return !(strptime("0", "%s", &tm) != NULL);
}
EOF
compile <"${_tmp}" && return 1
{ echo "#define _GNU_SOURCE"; cat "${_tmp}"; } | compile
}
check_pledge() {
compile <<-EOF
#include <unistd.h>
int main(void) {
return !(pledge("stdio", NULL) == 0);
}
EOF
}
check_stat_tim() {
compile <<-EOF
#include <sys/stat.h>
int main(void) {
struct stat st;
if (stat("/var/empty", &st) == -1)
return 1;
return !(st.st_mtim.tv_sec > 0);
}
EOF
}
check_strlcpy() {
compile <<-EOF
#include <string.h>
int main(void) {
char buf[128];
return !(strlcpy(buf, "strlcpy", sizeof(buf)) < sizeof(buf));
}
EOF
}
check_warnc() {
compile <<-EOF
#include <err.h>
int main(void) {
warnc(1, "");
return 0;
}
EOF
}
# Quirk for GNU Bison 2.X present on macOS which declares YYSTYPE unless
# YYSTYPE_IS_DECLARED is defined.
check_yystype() {
local _c="${WRKDIR}/c"
local _yacc="${WRKDIR}/yacc"
cat <<-EOF >"${_c}"
%{
void yyerror(const char *, ...);
int yylex(void);
typedef double YYSTYPE;
%}
%%
grammar:
%%
int main(void) {
return 0;
}
void yyerror(const char *fmt, ...)
{
}
int yylex(void) {
return 0;
}
EOF
${YACC} -o "${_yacc}" "${_c}" || fatal "${YACC}: fatal error"
! compile <"${_yacc}"
}
_fuzz=''
_opt=2
_pedantic=0
_sanitize=''
while [ $# -gt 0 ]; do
case "$1" in
--fuzz) shift; _fuzz="$1"; _sanitize="address";;
--debug) _opt=0;;
--pedantic) _pedantic=1;;
--sanitize) shift; _sanitize="${1}";;
*) ;;
esac
shift
done
WRKDIR=$(mktemp -dt configure.XXXXXX)
trap 'atexit ${WRKDIR}' EXIT
exec 3>&1 4>&2
exec 1>config.log 2>&1
# At this point, all variables used must be defined.
set -u
# Enable tracing, will end up in config.log.
set -x
HAVE_ARC4RANDOM=0
HAVE_ERRC=0
HAVE_GNU_SOURCE=0
HAVE_PLEDGE=0
HAVE_STAT_TIM=0
HAVE_STRLCPY=0
HAVE_WARNC=0
HAVE_YYSTYPE=0
# Order is important, must come first if not defined.
DEBUG="$(make_variable DEBUG || :)"
CC=$(make_variable CC || fatal "CC: not defined")
CFLAGS="$(unset CFLAGS DEBUG; make_variable CFLAGS || :) ${CFLAGS:-} ${DEBUG}"
CFLAGS="${CFLAGS} -MD -MP"
CPPFLAGS="$(make_variable CPPFLAGS || :)"
LDFLAGS="$(unset DEBUG; make_variable LDFLAGS || :)"
YACC=$(make_variable YACC || fatal "YACC: not defined")
YFLAGS="$(make_variable YFLAGS || :)"
PREFIX="$(make_variable PREFIX || echo /usr/local)"
BINDIR="$(make_variable BINDIR || echo "${PREFIX}/bin")"
MANDIR="$(make_variable MANDIR || echo "${PREFIX}/man")"
INSTALL="$(make_variable INSTALL || echo install)"
INSTALL_MAN="$(make_variable INSTALL_MAN || echo "${INSTALL}")"
# Following chunks must happen after CC is defined.
if [ "${_opt}" -eq 0 ]; then
CFLAGS="${CFLAGS} -g -O0"
fi
if [ "${_pedantic}" -eq 1 ]; then
while read -r _o; do
CFLAGS="${CFLAGS} ${_o}"
done <<-CC-PEDANTIC
-g
-O${_opt}
-Wall
-Werror
-Wextra
-Wcast-qual
-Wmissing-prototypes
-Wpedantic
-Wshadow
-Wsign-conversion
-Wwrite-strings
CC-PEDANTIC
while read -r _o; do
CFLAGS="${CFLAGS} $(cc_has_option "${_o}")"
done <<-CC-PEDANTIC-OPTIONAL
-Waddress-of-packed-member
-Wcovered-switch-default
-Wextra-semi-stmt
-Wformat-signedness
-Wimplicit-fallthrough
-Wmissing-format-attribute
-Wmissing-variable-declarations
-Wtautological-unsigned-enum-zero-compare
-Wunreachable-code-aggressive
-Wused-but-marked-unused
-Wvla
-Wno-gnu-zero-variadic-macro-arguments
CC-PEDANTIC-OPTIONAL
while read -r _o; do
LDFLAGS="${LDFLAGS} $(cc_has_option "${_o}")"
done <<-LD-PEDANTIC-OPTIONAL
-Wl,--fatal-warnings
LD-PEDANTIC-OPTIONAL
DEBUG="-g ${DEBUG}"
fi
if [ -n "${_sanitize}" ]; then
(
if is_openbsd; then
# Abuse CPPFLAGS honored by compile.
CPPFLAGS="${CPPFLAGS} -fsanitize-minimal-runtime"
CPPFLAGS="${CPPFLAGS} -Wl,--no-execute-only"
fi
cc_has_sanitizer "${_sanitize}"
cc_has_sanitizer undefined
cc_has_sanitizer unsigned-integer-overflow
{ [ "${_fuzz}" = "llvm" ] && echo fuzzer; } || :
) >"${WRKDIR}/sanitize"
if ! cmp -s /dev/null "${WRKDIR}/sanitize"; then
_sanitize="-fsanitize=$(paste -sd , "${WRKDIR}/sanitize")"
_sanitize="${_sanitize} $(cc_has_option -fno-sanitize-recover=all)"
_sanitize="${_sanitize} $(cc_has_option -fno-omit-frame-pointer)"
if is_openbsd; then
_sanitize="${_sanitize} -fsanitize-minimal-runtime"
LDFLAGS="${LDFLAGS} -Wl,--no-execute-only"
fi
CFLAGS="${_sanitize} ${CFLAGS}"
DEBUG="${_sanitize} ${DEBUG}"
fi
fi
if make_has_object_dir; then
mkdir -p obj/{,libks}
fi
check_arc4random && HAVE_ARC4RANDOM=1
check_errc && HAVE_ERRC=1
check_gnu_source && HAVE_GNU_SOURCE=1
check_pledge && HAVE_PLEDGE=1
check_stat_tim && HAVE_STAT_TIM=1
check_strlcpy && HAVE_STRLCPY=1
check_warnc && HAVE_WARNC=1
check_yystype && HAVE_YYSTYPE=1
# Redirect stdout to config.h.
exec 1>config.h
printf '#ifndef CONFIG_H\n#define CONFIG_H\n'
# Order is important, must be present before any includes.
[ "${HAVE_GNU_SOURCE}" -eq 1 ] && printf '#define _GNU_SOURCE\n'
# Headers needed for function prototypes.
{
[ "${HAVE_ARC4RANDOM}" -eq 0 ] && echo stdint.h
[ "${HAVE_STRLCPY}" -eq 0 ] && echo stddef.h
} | sort | uniq | headers
[ "${HAVE_ARC4RANDOM}" -eq 1 ] && printf '#define HAVE_ARC4RANDOM\t1\n'
[ "${HAVE_ERRC}" -eq 1 ] && printf '#define HAVE_ERRC\t1\n'
[ "${HAVE_PLEDGE}" -eq 1 ] && printf '#define HAVE_PLEDGE\t1\n'
[ "${HAVE_STRLCPY}" -eq 1 ] && printf '#define HAVE_STRLCPY\t1\n'
[ "${HAVE_WARNC}" -eq 1 ] && printf '#define HAVE_WARNC\t1\n'
if [ -n "${_fuzz}" ]; then
printf '#define FUZZER_%s\t1\n' \
"$(echo "${_fuzz}" | tr '[:lower:]' '[:upper:]')"
fi
if [ "${HAVE_STAT_TIM}" -eq 0 ]; then
printf '#define st_atim\tst_atimespec\n'
printf '#define st_ctim\tst_ctimespec\n'
printf '#define st_mtim\tst_mtimespec\n'
fi
if [ "${HAVE_YYSTYPE}" -eq 1 ]; then
printf '#define YYSTYPE_IS_DECLARED\t1\n'
fi
[ "${HAVE_ARC4RANDOM}" -eq 0 ] &&
printf 'uint32_t arc4random(void);\n'
[ "${HAVE_ERRC}" -eq 0 ] && {
printf 'void errc(int, int, const char *, ...)\n';
printf '\t__attribute__((__noreturn__, __format__ (printf, 3, 4)));\n'; }
[ "${HAVE_PLEDGE}" -eq 0 ] &&
printf 'int pledge(const char *, const char *);\n'
[ "${HAVE_STRLCPY}" -eq 0 ] &&
printf 'size_t strlcpy(char *, const char *, size_t);\n'
[ "${HAVE_WARNC}" -eq 0 ] && {
printf 'void warnc(int, const char *, ...)\n'
printf '\t__attribute__((__format__ (printf, 2, 3)));\n'; }
printf '#endif\n'
# Redirect stdout to config.mk.
exec 1>config.mk
# Use echo to normalize whitespace.
# shellcheck disable=SC1083,SC2086,SC2116
cat <<EOF
CC= $(echo ${CC})
CFLAGS= $(rmdup ${CFLAGS})
CPPFLAGS= $(rmdup ${CPPFLAGS} -I\${.CURDIR})
DEBUG= $(rmdup ${DEBUG})
LDFLAGS= $(rmdup ${LDFLAGS})
YACC= $(echo ${YACC})
YFLAGS= $(rmdup ${YFLAGS})
BINDIR?= $(echo ${BINDIR})
MANDIR?= $(echo ${MANDIR})
INSTALL?= $(echo ${INSTALL})
INSTALL_MAN?= $(echo ${INSTALL_MAN})
EOF
if make_has_object_dir; then
cat <<EOF
.SUFFIXES: .c .o
.c.o:
\${Q}\${CC} \${CPPFLAGS} \${CFLAGS} \${DEBUG} -c -o \${<:\${.CURDIR}/%.c=%.o} \$<
EOF
else
cat <<EOF
.SUFFIXES: .c .o
.c.o:
\${Q}\${CC} \${CPPFLAGS} \${CFLAGS} \${DEBUG} -c -o \$@ \$<
EOF
fi