-
Notifications
You must be signed in to change notification settings - Fork 42
/
configure
executable file
·223 lines (182 loc) · 4.48 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
#!/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 -f "$@"
[ "$_err" -ne 0 ] && fatal
exit 0
}
compile() {
$CC -Werror -o /dev/null -x c - $@
}
fatal() {
[ $# -gt 0 ] && echo "fatal: ${*}"
exec 1>&3 2>&4
cat config.log
return 1
}
headers() {
cat >"$TMP1"
[ -s "$TMP1" ] || return 0
xargs printf '#include <%s>\n' <"$TMP1"
}
makevar() {
var="$(printf 'all:\n\t@echo ${%s}\n' "$1" | make -sf -)"
if [ -n "$var" ]; then
echo "$var"
else
return 1
fi
}
check_curses() {
compile $@ <<-EOF
#include <curses.h>
#include <term.h>
int main(void) {
return !(setupterm(NULL, 1, NULL) != ERR);
}
EOF
}
check_dead() {
compile <<-EOF
#include <stdlib.h>
${1} int dead(void);
int main(void) {
return 0;
}
EOF
}
# Check if strptime(3) is hidden behind _GNU_SOURCE.
check_gnu_source() {
cat <<-EOF >"$TMP1"
#include <time.h>
int main(void) {
struct tm tm;
return !(strptime("0", "%s", &tm) != NULL);
}
EOF
compile <"$TMP1" && return 1
{ echo "#define _GNU_SOURCE"; cat "$TMP1"; } | compile
}
check_pledge() {
compile <<-EOF
#include <unistd.h>
int main(void) {
return !(pledge("stdio", NULL) == 0);
}
EOF
}
check_reallocarray() {
compile <<-EOF
#include <stdlib.h>
int main(void) {
return !(reallocarray(NULL, 1, 1) != NULL);
}
EOF
}
check_strtonum() {
compile <<-EOF
#include <stdlib.h>
int main(void) {
return !(strtonum("1", 1, 2, NULL) != 0);
}
EOF
}
TMP1=$(mktemp -t configure.XXXXXX)
trap "atexit $TMP1" 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_CURSES=0
HAVE_DEAD2=0
HAVE_DEAD=0
HAVE_GNU_SOURCE=0
HAVE_NCURSESW=0
HAVE_NORETURN=0
HAVE_PLEDGE=0
HAVE_REALLOCARRAY=0
HAVE_STRTONUM=0
# Order is important, must come first if not defined.
DEBUG="$(makevar DEBUG || :)"
CC=$(makevar CC || fatal "CC: not defined")
CFLAGS=$(DEBUG= makevar CFLAGS || :)
CFLAGS="${CFLAGS} ${DEBUG} -Wall -Wextra -MD -MP"
CPPFLAGS="$(makevar CPPFLAGS || :)"
CPPFLAGS="${CPPFLAGS} -I\${.CURDIR}"
LDFLAGS="$(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)"
if check_curses -lcurses; then
HAVE_CURSES=1
LDFLAGS="${LDFLAGS} -lcurses"
elif check_curses -lncursesw; then
HAVE_NCURSESW=1
LDFLAGS="${LDFLAGS} -lncursesw"
elif check_curses -ltinfow; then
HAVE_NCURSESW=1
LDFLAGS="${LDFLAGS} -ltinfow"
else
fatal "curses library not found"
fi
check_dead __dead && HAVE_DEAD=1
check_dead __dead2 && HAVE_DEAD2=1
check_dead '__attribute__((__noreturn__))' && HAVE_NORETURN=1
check_gnu_source && HAVE_GNU_SOURCE=1
check_pledge && HAVE_PLEDGE=1
check_reallocarray && HAVE_REALLOCARRAY=1
check_strtonum && HAVE_STRTONUM=1
# Redirect stdout to config.h.
exec 1>config.h
# 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_CURSES -eq 1 ] && echo curses.h term.h
[ $HAVE_NCURSESW -eq 1 ] && echo ncursesw/curses.h ncursesw/term.h
[ $HAVE_PLEDGE -eq 0 ] && echo stdlib.h
[ $HAVE_REALLOCARRAY -eq 0 ] && echo stdlib.h
[ $HAVE_STRTONUM -eq 0 ] && echo stdlib.h
} | sort | uniq | headers
[ $HAVE_PLEDGE -eq 1 ] && printf '#define HAVE_PLEDGE\t1\n'
[ $HAVE_REALLOCARRAY -eq 1 ] && printf '#define HAVE_REALLOCARRAY\t1\n'
[ $HAVE_STRTONUM -eq 1 ] && printf '#define HAVE_STRTONUM\t1\n'
if [ $HAVE_DEAD -eq 1 ]; then
:
elif [ $HAVE_DEAD2 -eq 1 ]; then
printf '#define __dead __dead2\n'
elif [ $HAVE_NORETURN -eq 1 ]; then
printf '#define __dead __attribute__((__noreturn__))\n'
else
printf '#define __dead\n'
fi
[ $HAVE_PLEDGE -eq 0 ] && \
printf 'int pledge(const char *, const char *);\n'
[ $HAVE_REALLOCARRAY -eq 0 ] && \
printf 'void *reallocarray(void *, size_t, size_t);\n'
[ $HAVE_STRTONUM -eq 0 ] && \
printf 'long long strtonum(const char *, long long, long long, const char **);\n'
# Redirect stdout to config.mk.
exec 1>config.mk
# Use echo to normalize whitespace.
cat <<EOF
CC= $(echo $CC)
CFLAGS= $(echo $CFLAGS)
CPPFLAGS= $(echo $CPPFLAGS)
DEBUG= $(echo $DEBUG)
LDFLAGS= $(echo $LDFLAGS)
BINDIR?= $(echo $BINDIR)
MANDIR?= $(echo $MANDIR)
INSTALL?= $(echo $INSTALL)
INSTALL_MAN?= \${INSTALL}
EOF