-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure
executable file
·370 lines (307 loc) · 7.1 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
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
#!/bin/sh -e
# https://www.gnu.org/prep/standards/html_node/Configuration.html
__die() {
echo "Error: $*"
exit 1
}
VALUES="BINDIR CC CFLAGS CFLAGS_COV CFLAGS_SAN CXX LD LIBS PREFIX SRCDIR"
__defaults() {
# Variables may be specified in environment if not set via command-line.
for VALUE in ${VALUES}; do
case "${VALUE}" in
BINDIR) ;;
CC)
CC=${CC:-cc}
;;
CFLAGS)
CFLAGS=${CFLAGS:-}
;;
CFLAGS_COV)
CFLAGS_COV=${CFLAGS_COV:-}
;;
CFLAGS_SAN)
CFLAGS_SAN=${CFLAGS_SAN:-}
;;
CXX)
CXX=${CXX:-g++}
;;
LD)
LD=${LD:-ld}
;;
LIBS)
LIBS=${LIBS:-}
;;
PREFIX)
PREFIX=${PREFIX:-/usr/local}
;;
SRCDIR)
SRCDIR=${SRCDIR:-$(dirname "$0")}
;;
*)
__die "Internal error, missing default."
;;
esac
done
# Derived variables.
for VALUE in ${VALUES}; do
case "${VALUE}" in
BINDIR)
BINDIR=${BINDIR:-${PREFIX}/bin}
;;
esac
done
}
__synopsis() {
echo "usage: configure [OPTIONS...] [VAR=VALUE...]" >&2
}
__help() {
# Ensure that we show our defaults, not customized values.
# shellcheck disable=SC2086
unset ${VALUES}
__defaults
cat <<EOF >&2
configure [OPTIONS...] [VAR=VALUE...]
Customize a project using directives from file "configure.in", and optionally
via the environment or through OPTIONS, in order to generate a makefile (from
file "Makefile.in") and configuration summary in file "config.status".
Functions:
feature_test_macro COMPILER HEADER DEFINE API CODE
find_header COMPILER HEADER DEFINE
find_library NAME...
populate DIR
test_compiler_flags COMPILER VAR [OPTIONAL|REQUIRED] FLAGS...
Options:
-h, --help Show this help and exit.
--bindir=BINDIR Specify binary directory.
--prefix=PREFIX Specify installation prefix.
--srcdir=SRCDIR Specify source directory.
Defaults:
EOF
for VALUE in ${VALUES}; do
export LABEL="${VALUE}"
export "${VALUE?}"
sh -c 'echo " $LABEL=$'"$VALUE"'"' >&2
done
cat <<EOF >&2
Example:
CC=foo ./configure PREFIX=/bar --srcdir=/baz
EOF
}
# Show help, if requested, *before* processing anything else.
for OPT in "$@"; do
case "${OPT}" in
-h | --help)
__help
exit 0
;;
esac
done
for OPT in "$@"; do
# Variables may be set via a flag '--key=VALUE' or assignment "KEY=VALUE".
case "${OPT}" in
--bindir=*)
BINDIR=${OPT#*=}
;;
--prefix=*)
PREFIX=${OPT#*=}
;;
--srcdir=*)
SRCDIR=${OPT#*=}
;;
-*)
__synopsis
exit 1
;;
*=*)
KEY="${OPT%=*}"
VALUE="${OPT#*=}"
eval "$KEY=\"$VALUE\""
;;
*)
__synopsis
exit 1
;;
esac
done
__defaults
# Also look for sources in "..".
[ "${SRCDIR}" = "." ] && [ ! -f "${SRCDIR}/Makefile.in" ] && SRCDIR=".."
# SRCDIR must contain sources.
[ -f "${SRCDIR}/Makefile.in" ] || __die "Please set SRCDIR in environment or via '--srcdir' option."
__tidy() {
rm -rf "${WORKDIR}"
}
trap __tidy EXIT
# Source directory.
S="$(cd "${SRCDIR}" && pwd)"
# Build directory.
B="$(pwd)"
# Place to run probe commands.
WORKDIR="$(mktemp -d)"
# Remove left-over configuration (out-of-tree and in-tree).
rm -f Makefile "${S}/Makefile"
rm -f config.status "${S}/config.status"
# Populate out-of-tree sources since POSIX make does not support VPATH.
__populate() {
DIR="$1"
if [ ! "${S}" = "${B}" ]; then
DIR="$(cd "${DIR}" && pwd)"
SUB="${DIR#"${S}/"}"
[ -n "${SUB}" ] && [ ! "${S}" = "${SUB}" ] && echo "mkdir -p \"${SUB}\""
for ENTRY in "${DIR}"/*; do
if [ -f "${ENTRY}" ]; then
echo "ln -sf \"${ENTRY}\" \"${ENTRY#"${S}/"}\""
fi
done
fi
}
__cache_status() {
echo "#!/bin/sh"
echo "# Auto-generated by configure script."
echo
for VALUE in ${VALUES}; do
export LABEL="${VALUE}"
export "${VALUE?}"
# U-0027 APOSTROPHE Must be escaped in order to be preserved through the sub-shell.
# Use S and D to simplify the invocation.
sh -c 'S=$(printf "\x27"); D=$(printf "\x22"); echo $LABEL=$S$(echo $'"$VALUE"' |sed s/$S/$S$D$S$D$S/g)$S'
done
echo
# Make argument sed safe.
# - Escape backslash.
# - Escape ampersand.
echo "escape() {"
printf "\techo %s |sed -e s/'%s'/'%s'/g -e \"s/%s/%s/g\"\n" '"$*"' '\(\\\)' '\\\1' '\\&' '\\\\&'
echo "}"
echo
# Transform placeholders, using sed with SOH as a delimiter.
# shellcheck disable=SC2016
printf "SOH=%s\n" '"$(printf "\x01")"'
echo "sed \\"
for VALUE in ${VALUES}; do
# shellcheck disable=SC2016
printf "\t-e s%s@${VALUE}@%s\"%s${VALUE}%s\"%sg \\\\\n" '${SOH}' '${SOH}' '$(escape ${' '})' '${SOH}'
done
echo ' "'"${SRCDIR}/Makefile.in"'" > Makefile'
echo
for DIR in ${POPULATED}; do
__populate "${DIR}"
done
}
__pkg_config_variable_append() {
ARG="$1"
NAME="$2"
VAR="$3"
# Word splitting, preserving escapes.
eval "set -- $(pkg-config "$ARG" "$NAME" | sed 's/\\/\\\\\\/g')"
for WORD in "$@"; do
# Avoid duplicates.
echo "${VAR}" | grep -E -q -- "(^| )${WORD}( |$)" || VAR="${VAR} ${WORD}"
done
echo "${VAR}"
}
find_library() {
for NAME in "$@"; do
pkg-config "${NAME}" 2>/dev/null || __die "Cannot find package configuration for library: '${NAME}'."
CFLAGS="$(__pkg_config_variable_append --cflags "${NAME}" "${CFLAGS}")"
LIBS="$(__pkg_config_variable_append --libs "${NAME}" "${LIBS}")"
done
}
__filename_of() {
COMPILER="$1"
case "${COMPILER}" in
g++ | clang++)
echo "probe.cpp"
;;
*)
echo "probe.c"
;;
esac
}
find_header() {
COMPILER="$1"
NAME="$2"
DEFINE="$3"
cd "${WORKDIR}"
PROBE="$(__filename_of "${COMPILER}")"
cat <<EOF >"${PROBE}"
#include <${NAME}>
int main(void) {}
EOF
if "${COMPILER}" "${PROBE}" -o /dev/null 2>/dev/null; then
echo "Has $NAME"
CFLAGS="${CFLAGS} -D${DEFINE}"
else
echo "No $NAME"
fi
cd "${B}"
}
__flag_append() {
VAR="$1"
shift
FLAG="$*"
# U-0022 QUOTATION MARK must be escaped in order to be preserved through `eval'.
# Example: eval 'CFLAGS="$CFLAGS --dd \"\""'
D=$(printf "\x22")
eval "${VAR}"='"$'"$VAR $(echo "$*" | sed s/\\"$D"/\\\\"$D"/g)"'"'
}
test_compiler_flags() {
COMPILER="$1"
shift
VAR="$1"
shift
REQUIRED="$1"
shift
cd "${WORKDIR}"
PROBE="$(__filename_of "${COMPILER}")"
cat <<EOF >"${PROBE}"
int main(void){}
EOF
# Detect supported flags
for FLAG in "$@"; do
# shellcheck disable=SC2086
if ${COMPILER} ${FLAG} "${PROBE}" 2>/dev/null; then
echo "${COMPILER} supports ${FLAG}"
__flag_append "${VAR}" "${FLAG}"
elif [ "${REQUIRED}" = "REQUIRED" ]; then
__die "${COMPILER} does not support ${FLAG}"
else
echo "${COMPILER} does not support ${FLAG}"
fi
done
cd "${B}"
}
feature_test_macro() {
COMPILER="$1"
HEADER="$2"
DEFINE="$3"
API="$4"
CODE="$5"
cd "${WORKDIR}"
PROBE="$(__filename_of "${COMPILER}")"
cat <<EOF >"${PROBE}"
#undef ${DEFINE}
#include <${HEADER}>
int main(void) { ${CODE} }
EOF
if "${COMPILER}" -Werror "${PROBE}" -o /dev/null 2>/dev/null; then
echo "${COMPILER} has ${API}"
else
sed -i -e s/undef/define/ "${PROBE}"
if "${COMPILER}" -Werror "${PROBE}" -o /dev/null 2>/dev/null; then
echo "${COMPILER} has ${HEADER} ${DEFINE} ${API}"
CFLAGS="${CFLAGS} -DHAS_$(echo "${API}" | tr '[:lower:]' '[:upper:]')_GNU_SOURCE"
else
__die "${COMPILER} missing ${API}"
fi
fi
cd "${B}"
}
populate() {
POPULATED="${POPULATED} $*"
}
# shellcheck disable=SC1091
. "${S}/configure.in"
__cache_status >config.status
chmod +x config.status
./config.status