-
Notifications
You must be signed in to change notification settings - Fork 3
/
configure
executable file
·251 lines (209 loc) · 5.41 KB
/
configure
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
#!/bin/sh
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"; : "${_sanitizer:?}"
if echo 'int main(void) { return 0; }' |
compile "-fsanitize=${_sanitizer}"
then
echo "$@"
fi
}
cxx_compile() {
# shellcheck disable=SC2086
"${CXX}" ${CPPFLAGS} -Werror -o /dev/null -x c++ - "$@"
}
cxx_has_option() {
if echo 'int main(void) { return 0; }' | cxx_compile "$@"; then
echo "$@"
fi
}
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}"
}
makevar() {
# shellcheck disable=SC2016
var="$(printf 'all:\n\t@echo ${%s}\n' "$1" | make -sf -)"
if [ -n "${var}" ]; then
echo "${var}"
else
return 1
fi
}
# pedantic
#
# Pedantic flags supported by most compilers.
pedantic() {
cat <<-EOF | xargs
-O2
-Wall
-Werror
-Wextra
-Wmissing-prototypes
-Wpedantic
-Wshadow
-Wsign-conversion
-Wwrite-strings
EOF
}
# rmdup arg ...
#
# Remove duplicates from the given arguments while preserving the order.
rmdup() {
echo "$@" |
xargs printf '%s\n' |
awk '!x[$1]++' |
xargs
}
check_pledge() {
compile <<-EOF
#include <unistd.h>
int main(void) {
return !(pledge("stdio", NULL) == 0);
}
EOF
}
_benchmark=0
_fuzz=''
_pedantic=0
_sanitize=0
while [ $# -gt 0 ]; do
case "$1" in
--benchmark) _benchmark=1;;
--fuzz) shift; _fuzz="$1"; _sanitize=1;;
--pedantic) _pedantic=1;;
--sanitize) _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_PLEDGE=0
# Order is important, must come first if not defined.
DEBUG="$(makevar DEBUG || :)"
CC=$(makevar CC || fatal "CC: not defined")
CFLAGS="$(unset CFLAGS DEBUG; makevar CFLAGS || :) ${CFLAGS:-} ${DEBUG}"
CFLAGS="${CFLAGS} -MD -MP"
CXX=$(makevar CXX || fatal "CXX: not defined")
CXXFLAGS="$(unset CXXFLAGS DEBUG; makevar CXXFLAGS || :) ${CXXFLAGS:-} ${DEBUG}"
CXXFLAGS="${CFLAGS} -MD -MP"
CPPFLAGS="$(makevar CPPFLAGS || :)"
AFLAGS="$(makevar AFLAGS || :) -MD -MP"
ASFLAGS="$(makevar ASFLAGS || :) -MD -MP"
LDFLAGS="$(unset DEBUG; makevar LDFLAGS || :)"
PREFIX="$(makevar PREFIX || echo /usr/local)"
BINDIR="$(makevar BINDIR || echo "${PREFIX}/bin")"
MANDIR="$(makevar MANDIR || echo "${PREFIX}/man")"
INSTALL="$(makevar INSTALL || echo install)"
INSTALL_MAN="$(makevar INSTALL_MAN || echo "${INSTALL}")"
# Following chunks must happen after CC is defined.
if [ "${_pedantic}" -eq 1 ]; then
CFLAGS="$(cc_has_option -Wformat-signedness) ${CFLAGS}"
CFLAGS="$(cc_has_option -Wimplicit-fallthrough) ${CFLAGS}"
CFLAGS="$(cc_has_option -Wunreachable-code-aggressive) ${CFLAGS}"
CFLAGS="$(cc_has_option -Wused-but-marked-unused) ${CFLAGS}"
CFLAGS="-g $(pedantic) ${CFLAGS}"
DEBUG="-g ${DEBUG}"
LDFLAGS="$(cc_has_option -Wl,--fatal-warnings) ${LDFLAGS}"
fi
if [ "${_sanitize}" -eq 1 ]; then
{
cc_has_sanitizer address
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)"
CFLAGS="${_sanitize} ${CFLAGS}"
DEBUG="${_sanitize} ${DEBUG}"
fi
if [ "${_fuzz}" = "llvm" ]; then
NO_SANITIZE_FUZZER="$(cc_has_option -fno-sanitize=fuzzer)"
fi
fi
if [ "${_benchmark}" -eq 1 ]; then
CPPFLAGS="${CPPFLAGS} $(pkg-config --cflags-only-I benchmark)"
LDFLAGS_benchmark="${LDFLAGS} $(pkg-config --libs benchmark)"
fi
# Let CXX inherit applicable CFLAGS.
for _c in ${CFLAGS}; do
CXXFLAGS="${CXXFLAGS} $(cxx_has_option "${_c}")"
done
check_pledge && HAVE_PLEDGE=1
# Redirect stdout to config.h.
exec 1>config.h
printf '#ifndef CONFIG_H\n#define CONFIG_H\n'
# Headers needed for function prototypes.
{
:
} | sort | uniq | headers
[ "${HAVE_PLEDGE}" -eq 1 ] && printf '#define HAVE_PLEDGE\t1\n'
if [ -n "${_fuzz}" ]; then
printf '#define FUZZER_%s\t1\n' \
"$(echo "${_fuzz}" | tr '[:lower:]' '[:upper:]')"
fi
[ "${HAVE_PLEDGE}" -eq 0 ] &&
printf 'int pledge(const char *, const char *);\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})
CXX= $(echo ${CXX})
CXXFLAGS= $(rmdup ${CXXFLAGS})
CPPFLAGS= $(rmdup ${CPPFLAGS} -I\${.CURDIR})
DEBUG= $(rmdup ${DEBUG})
AFLAGS= $(rmdup ${AFLAGS})
ASFLAGS= $(rmdup ${ASFLAGS})
LDFLAGS= $(rmdup ${LDFLAGS})
LDFLAGS_benchmark= $(rmdup ${LDFLAGS_benchmark})
BINDIR?= $(echo ${BINDIR})
MANDIR?= $(echo ${MANDIR})
INSTALL?= $(echo ${INSTALL})
INSTALL_MAN?= $(echo ${INSTALL_MAN})
NO_SANITIZE_FUZZER= $(echo ${NO_SANITIZE_FUZZER:-})
.PATH: \${.CURDIR}/libks
VPATH= \${.CURDIR}/libks
EOF