-
Notifications
You must be signed in to change notification settings - Fork 11
/
configure
executable file
·374 lines (323 loc) · 8.03 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
371
372
373
374
#!/bin/sh
#
# SPDX-License-Identifier: GPL-2.0-only
#
# Copyright (C) 2023 Ammar Faizi <ammarfaizi2@gnuweeb.org>
#
# Inspired by liburing's configure script.
#
set -e;
cc=${CC:-gcc};
cxx=${CXX:-g++};
ld=${LD:-$cc};
build_dir=${O:-$(dirname $(realpath $0))};
USER_CFLAGS="${CFLAGS}";
USER_CXXFLAGS="${CXXFLAGS}";
USER_LDFLAGS="${LDFLAGS}";
USER_LIB_LDFLAGS="${LIB_LDFLAGS}";
for opt do
optarg=$(expr "x$opt" : 'x[^=]*=\(.*\)' || true);
case "$opt" in
--help|-h)
show_help=yes;
;;
--build-dir=*)
build_dir="$optarg";
;;
--prefix=*)
prefix="$optarg";
;;
--includedir=*)
includedir="$optarg";
;;
--libdir=*)
libdir="$optarg";
;;
--libdevdir=*)
libdevdir="$optarg";
;;
--mandir=*)
mandir="$optarg";
;;
--datadir=*)
datadir="$optarg";
;;
--cc=??*)
cc="$optarg";
;;
--cxx=*)
cxx="$optarg";
;;
--gui)
use_gui="yes";
;;
--debug)
use_debug="yes";
;;
*)
echo "ERROR: unknown option $opt";
echo "Try '$0 --help' for more information";
exit 1;
;;
esac;
done
if test -z "$prefix"; then
prefix=/usr;
fi
if test -z "$includedir"; then
includedir="$prefix/include";
fi
if test -z "$libdir"; then
libdir="$prefix/lib";
fi
if test -z "$libdevdir"; then
libdevdir="$prefix/lib";
fi
if test -z "$mandir"; then
mandir="$prefix/man";
fi
if test -z "$datadir"; then
datadir="$prefix/share";
fi
if test "$show_help" = "yes"; then
cat <<EOF
Usage: ./configure [options]
Options: [defaults in brackets after descriptions]
--help Print this message
--build-dir=PATH Set build output in PATH [$build_dir]
--prefix=PATH Install in PATH [$prefix]
--includedir=PATH Install headers in PATH [$includedir]
--libdir=PATH Install runtime libraries in PATH [$libdir]
--libdevdir=PATH Install development libraries in PATH [$libdevdir]
--mandir=PATH Install man pages in PATH [$mandir]
--datadir=PATH Install shared data in PATH [$datadir]
--cc=CMD Use CMD as the C compiler
--cxx=CMD Use CMD as the C++ compiler
--gui Add the GUI support
--debug Build with debug enabled
EOF
exit 0;
fi
mkdir -pv "${build_dir}";
build_dir="$(realpath "${build_dir}")";
tmp_dir="${build_dir}/.tmp";
tmp_o="${tmp_dir}/tmp.o";
tmp_c="${tmp_dir}/tmp.c";
tmp_cpp="${tmp_dir}/tmp.cpp";
tmp_exe="${tmp_dir}/tmp.exe";
mkdir -pv "${tmp_dir}";
rm -f "${build_dir}/config.log";
config_make="${build_dir}/config.make";
config_h="${build_dir}/config.h";
config_log="${build_dir}/config.log";
# NB: do not call "exit" in the trap handler; this is buggy with some shells;
# see <1285349658-3122-1-git-send-email-loic.minier@linaro.org>
trap "rm -rf \"${tmp_dir}\"" EXIT INT HUP QUIT TERM;
fatal()
{
echo $@;
echo "Configure failed, check config.log and/or the above output";
rm -f "${config_make}";
rm -f "${config_h}";
exit 1;
}
# Print result for each configuration test
print_config()
{
printf " %-55s%s\n" "$1" "$2";
}
# Default flags.
CFLAGS="-O2 -D_GNU_SOURCE -include ${build_dir}/config.h";
CXXFLAGS="-O2 -D_GNU_SOURCE -include ${build_dir}/config.h";
LDFLAGS="-O2";
LIB_LDFLAGS="-lpthread";
# Print configure header at the top of $config_h.
printf "/*\n" > $config_h;
printf " * Automatically generated by configure - do not modify!\n" >> $config_h;
printf " *\n" >> $config_h;
printf " * Configured with:" >> $config_h;
printf " * '%s'" "$0" "$@" >> $config_h;
printf "\n" >> $config_h;
printf " */\n" >> $config_h;
printf "# Automatically generated by configure - do not modify!\n" > $config_make;
printf "# Configured with:" >> $config_make;
printf " '%s'" "$0" "$@" >> $config_make;
printf "\n" >> $config_make;
add_config_make()
{
printf "%s\n" "$1=$2" >> $config_make;
}
add_config_h()
{
printf "%s\n" "#define $1" >> $config_h;
}
add_config()
{
add_config_make $1 "y";
add_config_h $1;
print_config "Adding $1=y";
}
add_make_var()
{
printf "%s\n" "$1=$2" >> $config_make;
}
do_cc()
{
# Run the compiler, capturing its output to the log.
echo "${cc}" "$@" >> $config_log;
"${cc}" "$@" >> $config_log 2>&1 || return $?;
return 0;
}
do_cxx()
{
# Run the compiler, capturing its output to the log.
echo "${cxx}" "$@" >> $config_log;
"${cxx}" "$@" >> $config_log 2>&1 || return $?;
return 0;
}
compile_cc()
{
local_cflags="$1";
local_ldflags="$2 $LIBS";
printf "\n\nCompiling test case %s:\n" "$3" >> $config_log;
do_cc $CFLAGS $local_cflags -o "${tmp_exe}" "${tmp_c}" \
${LDFLAGS} ${local_ldflags};
}
compile_cxx()
{
local_cxxflags="$1";
local_ldflags="$2 $LIBS";
printf "\n\nCompiling test case %s:" "$3" >> $config_log;
do_cxx $CXXFLAGS $local_cxxflags -o "${tmp_exe}" "${tmp_cpp}" \
${LDFLAGS} ${local_ldflags};
}
__has_cflags()
{
cat > $tmp_c << EOF
int main(void)
{
return 0;
}
EOF
compile_cc "-Werror $1" "" "$1" || return $?;
return 0;
}
__has_cxxflags()
{
cat > $tmp_cpp << EOF
int main(void)
{
return 0;
}
EOF
compile_cxx "-Werror $1" "" "$1" || return $?;
return 0;
}
__has_c_and_cxxflags()
{
__has_cflags $1 || return $?;
__has_cxxflags $1 || return $?;
return 0;
}
add_c_flag()
{
local_append_flag=$1;
if test ! -z "$2"; then
local_append_flag="$2";
fi;
if __has_cflags $1; then
ret=0;
support="yes";
CFLAGS="$1 ${CFLAGS}";
else
ret=1;
support="no";
fi;
print_config "Does CC support ${1}?" "${support}";
return $ret;
}
add_cxx_flag()
{
local_append_flag=$1;
if test ! -z "$2"; then
local_append_flag="$2";
fi;
if __has_cxxflags $1; then
support="yes";
CXXFLAGS="$1 ${CXXFLAGS}";
else
support="no";
fi;
ret=$?;
print_config "Does CXX support ${1}?" "${support}";
return $ret;
}
add_c_and_cxx_flag()
{
add_c_flag "$@";
add_cxx_flag "$@";
}
printf "\n%s:\n" "Compilers";
print_config "CC" "${cc}";
print_config "CXX" "${cxx}";
# Don't exit if add_* functions return non-zero.
set +e;
printf "\n%s:\n" "-W flags";
add_c_and_cxx_flag "-Wall";
add_c_and_cxx_flag "-Wextra";
add_c_and_cxx_flag "-Wsequence-point";
add_c_and_cxx_flag "-Wunreachable-code";
add_c_and_cxx_flag "-Wunreachable-code-loop-increment";
add_c_and_cxx_flag "-Wformat-signedness";
add_c_and_cxx_flag "-Wformat-security";
add_c_and_cxx_flag "-Wformat";
add_c_and_cxx_flag "-Wstack-usage=4096";
add_c_and_cxx_flag "-Wmissing-prototypes";
add_c_and_cxx_flag "-Wstrict-prototypes";
add_c_and_cxx_flag "-Wmissing-variable-declarations";
add_c_and_cxx_flag "-Wstrict-aliasing=3";
add_c_and_cxx_flag "-Wshorten-64-to-32";
add_c_and_cxx_flag "-Wunsafe-loop-optimizations";
printf "\n%s:\n" "-f flags";
add_c_and_cxx_flag "-fno-stack-protector";
add_c_and_cxx_flag "-fdata-sections";
add_c_and_cxx_flag "-ffunction-sections";
add_c_and_cxx_flag "-fno-strict-aliasing";
add_c_and_cxx_flag "-fvisibility=hidden";
add_c_and_cxx_flag "-flto" && LDFLAGS="-flto ${LDFLAGS}";
add_c_and_cxx_flag "-fpie -fPIE" && LDFLAGS="-fpie -fPIE ${LDFLAGS}";
printf "\n%s:\n" "Misc flags";
LDFLAGS="-rdynamic ${LDFLAGS}";
add_c_and_cxx_flag "-ggdb3" && LDFLAGS="-ggdb3 ${LDFLAGS}";
# Bring it back!
set -e;
printf "\n%s:\n" "CONFIG";
add_config "CONFIG_POLL";
add_config "CONFIG_EPOLL";
add_config "CONFIG_IO_URING";
add_config "CONFIG_TEAVPN_SERVER";
add_config "CONFIG_TEAVPN_CLIENT";
add_config "CONFIG_LINUX";
if test "${use_gui}" = "yes"; then
add_config "CONFIG_GUI";
gtk3_cflags=`pkg-config --cflags gtk+-3.0`;
gtk3_lib_ldflags=`pkg-config --libs gtk+-3.0`;
CFLAGS="${CFLAGS} ${gtk3_cflags}";
CXXFLAGS="${CXXFLAGS} ${gtk3_cflags}";
LIB_LDFLAGS="${LD_FLAGS} ${gtk3_lib_ldflags}";
fi;
if test "${use_debug}" = "yes"; then
add_config "CONFIG_DEBUG";
else
CFLAGS="${CFLAGS} -DNDEBUG";
CXXFLAGS="${CXXFLAGS} -DNDEBUG";
fi;
add_make_var "CC" "${cc}";
add_make_var "CXX" "${cxx}";
add_make_var "CFLAGS" "$(echo "${CFLAGS} ${USER_CFLAGS}" | awk '{$1=$1};1')";
add_make_var "CXXFLAGS" "$(echo "${CXXFLAGS} ${USER_CXXFLAGS}" | awk '{$1=$1};1')";
add_make_var "LDFLAGS" "$(echo "${LDFLAGS} ${USER_LIB_LDFLAGS}" | awk '{$1=$1};1')";
add_make_var "LIB_LDFLAGS" "$(echo "${LIB_LDFLAGS} ${USER_LIB_LDFLAGS}" | awk '{$1=$1};1')";
printf "override O=%s\n" "${build_dir}" >> $config_make;
ln -s "${config_make}" >> /dev/null 2>&1 || true;
printf "\n";