OILS / configure View on Github | oils.pub

536 lines, 320 significant
1#!/bin/sh
2#
3# POSIX shell script to detect system properties required by Oils. Distributed
4# with the source tarball.
5#
6# For usage, run:
7#
8# ./configure --help
9#
10# External utilities used: cc
11# Optional dependency: GNU readline
12#
13# TODO:
14# - Should be able to run this from another directory.
15# - Other settings: LTO, PGO? Consider moving prefix, LTO, PGO to build and
16# install steps.
17
18TMP=${TMPDIR:-/tmp} # Assume that any system has $TMPDIR set or /tmp exists
19readonly TMP # POSIX sh supports 'readonly'
20
21log() {
22 echo "$0: $@" 1>&2
23}
24
25info() {
26 echo "$0 INFO: $@" 1>&2
27}
28
29die() {
30 echo "$0 ERROR: $@" 1>&2
31 exit 1
32}
33
34show_help() {
35 cat <<'EOF'
36Detect system settings before a build of oils-for-unix.
37
38Usage:
39 ./configure FLAG*
40 ./configure --help
41
42Installation directories:
43 --prefix=PREFIX Prefix for the bin/ directory [/usr/local]
44 --datarootdir=DATAROOTDIR Prefix for data files, including man page [PREFIX/share]
45
46Optional features:
47 --with-readline Fail unless readline is available.
48 --without-readline Don't compile with readline, even if it's available.
49 The shell won't have any interactive features.
50 --readline=DIR An alternative readline installation to link against
51 --with-systemtap-sdt Fail unless systemtap-sdt is available.
52 --without-systemtap-sdt Don't compile with systemtap-sdt, even if it's available.
53
54EOF
55}
56
57# This script roughly follows the GNU Standards
58# https://www.gnu.org/prep/standards/html_node/Configuration.html
59# https://www.gnu.org/prep/standards/html_node/Directory-Variables.html
60#
61# Default installation is /usr/local/bin/oil, but this can be changed with
62# --prefix.
63#
64# While this script only uses a handful of the standard directory variables
65# listed on the above documents, it accepts most of them in the --arg=value
66# form as noops. This helps automated build-systems passing preconfigured
67# sets of arguments to configure oil.
68FLAG_prefix='/usr/local'
69FLAG_datarootdir='' # default initialized after processing flags
70FLAG_with_readline='' # Fail if it's not available.
71FLAG_without_readline='' # Don't even check if it's available
72FLAG_readline=''
73FLAG_without_systemtap_sdt='' # Don't even check if it's available
74FLAG_without_libc_features=''
75
76# These variables are set by detect_readline and used by echo_cpp and
77# echo_shell_vars
78detected_deps=''
79
80have_readline=''
81readline_dir=''
82
83have_systemtap_sdt=''
84
85# libc
86have_fnm_extmatch=''
87have_glob_period=''
88have_pwent=''
89
90parse_flags() {
91 while true; do
92 case "$1" in
93 '')
94 break
95 ;;
96 --help)
97 show_help
98 exit 0
99 ;;
100
101 --with-readline)
102 FLAG_with_readline=1
103 ;;
104
105 --without-readline)
106 FLAG_without_readline=1
107 ;;
108
109 --readline=*)
110 FLAG_readline="${1#*=}"
111 ;;
112 --readline)
113 if test $# -eq 1; then
114 die "--readline requires an argument"
115 fi
116 shift
117 FLAG_readline=$1
118 ;;
119
120 --without-systemtap-sdt)
121 FLAG_without_systemtap_sdt=1
122 ;;
123
124 --without-libc-features)
125 FLAG_without_libc_features=1
126 ;;
127
128 # TODO: Maybe prefix only needs to be part of the install step? I'm not
129 # sure if we need it for building anything.
130 --prefix=*)
131 FLAG_prefix="${1#*=}"
132 ;;
133 --prefix)
134 if test $# -eq 1; then
135 die "--prefix requires an argument"
136 fi
137 shift
138 FLAG_prefix=$1
139 ;;
140
141 # Following autoconf's spelling of --mandir
142 --datarootdir=*)
143 FLAG_datarootdir="${1#*=}"
144 ;;
145 --datarootdir)
146 if test $# -eq 1; then
147 die "--datarootdir requires an argument"
148 fi
149 shift
150 FLAG_datarootdir=$1
151 ;;
152
153 --with-*|--enable-*)
154 info "Argument '$1' not used by this configure script"
155 ;;
156
157 --build=*|--host=*)
158 info "Argument '$1' not used by this configure script"
159 ;;
160
161 --exec-prefix=*|--bindir=*|--sbindir=*|--libexecdir=*|--sysconfdir=*)
162 info "Argument '$1' not used by this configure script"
163 ;;
164 --sharedstatedir=*|--localstatedir=*|--runstatedir=*)
165 info "Argument '$1' not used by this configure script"
166 ;;
167 --libdir=*|--includedir=*|--oldincludedir=*)
168 info "Argument '$1' not used by this configure script"
169 ;;
170 --datadir=*|--infodir=*|--localedir=*|--mandir=*|--docdir=*)
171 info "Argument '$1' not used by this configure script"
172 ;;
173 --htmldir=*|--dvidir=*|--pdfdir=*|--psdir=*)
174 info "Argument '$1' not used by this configure script"
175 ;;
176
177 *)
178 die "Invalid argument '$1'"
179 ;;
180 esac
181 shift
182 done
183
184 # If not set, fallback to --prefix
185 FLAG_datarootdir=${FLAG_datarootdir:-$FLAG_prefix/share}
186}
187
188# No output file, no logging, no stderr.
189# TODO: Maybe send stdout/stderr to config.log?
190cc_quiet() {
191 cc "$@" -o /dev/null >/dev/null 2>&1
192}
193
194cc_or_die() {
195 if ! cc "$@" >$TMP/cc.log 2>&1; then
196 log "Error running 'cc $@':"
197 cat $TMP/cc.log
198 die "Fatal compile error running feature test"
199 fi
200}
201
202# Check if a given program compiles
203cc_statement() {
204 local pp_var="$1"
205 local prog="$2"
206 local includes="$3"
207
208 cat >$TMP/cc_statement.c <<EOF
209$includes
210int main() {
211 $prog
212}
213EOF
214 # Return exit code of compiler
215 if cc_quiet $TMP/cc_statement.c; then
216 echo "#define $pp_var 1"
217 return 0
218 else
219 return 1
220 fi
221}
222
223# Check if a given library is installed via compilation
224cc_header_file() {
225 local pp_var="$1"
226 local c_lib="$2"
227
228 cc_statement "$pp_var" 'return 0;' "#include <$c_lib>"
229}
230
231detect_readline() {
232 detected_deps=1 # for assertions in echo_shell_vars and echo_cpp
233
234 # User disabled readline
235 if test -n "$FLAG_without_readline"; then
236 # have_readline remains false
237 return
238 fi
239
240 # User requested specific location
241 if test -n "$FLAG_readline"; then
242 if cc_quiet build/detect-readline.c \
243 -L "$FLAG_readline/lib" \
244 -I "$FLAG_readline/include" \
245 -l readline; then
246
247 readline_dir="$FLAG_readline"
248 have_readline=1
249 fi
250 return
251 fi
252
253 # Detect in default location
254 if cc_quiet build/detect-readline.c -l readline; then
255 have_readline=1
256 return
257 fi
258
259 # User requested that it be found
260 if test "$FLAG_with_readline" = 1 && test "$have_readline" != 1; then
261 die 'readline was not detected on the system (--with-readline passed).'
262 fi
263}
264
265detect_systemtap_sdt() {
266 detected_deps=1 # for assertions in echo_shell_vars and echo_cpp
267
268 if test -n "$FLAG_without_systemtap_sdt"; then
269 return
270 fi
271
272 if cc_quiet build/detect-systemtap-sdt.c; then
273 have_systemtap_sdt=1
274 return
275 fi
276}
277
278detect_libc() {
279 if test -n "$FLAG_without_libc_features"; then
280 return
281 fi
282
283 # Check if non-POSIX FNM_EXTMATCH is available
284 if cc_quiet build/detect-fnm-extmatch.c; then
285 have_fnm_extmatch=1
286 fi
287
288 # Check if non-POSIX GLOB_PERIOD is available
289 if cc_quiet build/detect-glob-period.c; then
290 have_glob_period=1
291 fi
292
293 # Check if pwent is callable. E.g. bionic libc (Android) doesn't have it
294 if cc_quiet build/detect-pwent.c; then
295 have_pwent=1
296 fi
297}
298
299echo_shell_vars() {
300 if test "$detected_deps" != 1; then
301 die 'called echo_shell_vars before detecting readline.'
302 fi
303
304 # Present a consistent interface to build/ninja-rules-cpp.sh
305 if test "$have_readline" = 1; then
306 echo 'HAVE_READLINE=1'
307 echo "READLINE_DIR=$readline_dir"
308 else
309 echo 'HAVE_READLINE='
310 echo 'READLINE_DIR='
311 fi
312
313 echo "PREFIX=$FLAG_prefix"
314 echo "DATAROOTDIR=$FLAG_datarootdir"
315
316 if cc_quiet build/detect-cc.c -Wl,--gc-sections; then
317 echo 'STRIP_FLAGS=--gc-sections'
318 elif cc_quiet build/detect-cc.c -Wl,-dead_strip; then
319 echo 'STRIP_FLAGS=-dead_strip'
320 fi
321}
322
323# c.m4 AC_LANG_INT_SAVE
324cc_print_expr() {
325 local c_expr="$1"
326 cat >$TMP/print_expr.c <<EOF
327#include <stdio.h>
328#include <sys/types.h> /* size_t, pid_t */
329
330int main() {
331 printf("%lu", $c_expr);
332}
333EOF
334 cc_or_die -o $TMP/print_expr $TMP/print_expr.c
335 $TMP/print_expr > $TMP/print_expr.out
336}
337
338# Shell note:
339# - local is not POSIX, but most shells have it.
340# C note:
341# - autoconf uses ac_fn_compute_int (in sh) aka AC_COMPUTE_INT (in m4).
342# - it uses different tests when cross compiling.
343# - cross-compiling does binary search?
344# - other one does AC_LANG_INT_SAVE
345# - generates a C program that outputs to conftest.val!
346# - well why not use exit code?
347# - QEMU configure doesn't do any tests
348
349# Hm, don't bother with cross compiling case for now.
350
351# Check if the size of a type is greater than a certain integer.
352check_sizeof() {
353 local pp_var="$1"
354 local c_type="$2"
355 local min_bytes="$3"
356
357 cc_print_expr "sizeof($c_type)"
358
359 local actual_bytes
360 actual_bytes=$(cat $TMP/print_expr.out)
361
362 if test -n "$min_bytes" && test "$actual_bytes" -lt "$min_bytes"; then
363 die "sizeof($c_type) should be at least $min_bytes; got $actual_bytes"
364 fi
365
366 # Echo to stdout!
367 echo "#define $pp_var $actual_bytes"
368}
369
370detect_c_language() {
371 # This is the equivalent of AC_CHECK_SIZEOF(int, 4)
372 check_sizeof SIZEOF_INT 'int' 4
373 check_sizeof SIZEOF_LONG 'long' 4
374 check_sizeof SIZEOF_VOID_P 'void *' 4
375 check_sizeof SIZEOF_SHORT 'short' 2
376 check_sizeof SIZEOF_FLOAT 'float' 4
377 check_sizeof SIZEOF_DOUBLE 'double' 8
378
379 check_sizeof SIZEOF_SIZE_T 'size_t' 4
380
381 # NOTE: This might only be relevant for large file support, which we don't
382 # have.
383 check_sizeof SIZEOF_FPOS_T 'fpos_t' 4
384 check_sizeof SIZEOF_PID_T 'pid_t' 4
385
386 check_sizeof SIZEOF_OFF_T 'off_t' ''
387 # autoconf checks if we have time.h, but the check isn't used. We just
388 # assume it's there.
389 check_sizeof SIZEOF_TIME_T 'time_t' ''
390
391 if cc_statement HAVE_LONG_LONG 'long long x; x = (long long)0;'
392 then
393 check_sizeof SIZEOF_LONG_LONG 'long long' 8
394 fi
395 if cc_statement HAVE_LONG_DOUBLE 'long double x; x = (long double)0;'
396 then
397 check_sizeof SIZEOF_LONG_DOUBLE 'long double' 8
398 fi
399
400 if cc_statement HAVE_C99_BOOL '_Bool x; x = (_Bool)0;'
401 then
402 # NOTE: this is mainly used in ctypes.h, which we might not need.
403 check_sizeof SIZEOF__BOOL '_Bool' 1
404 fi
405 # NOTE: Python also has a check for C99 uintptr_t. Just assume we don't
406 # have it?
407
408 #if cc_statement HAVE_C99_BOOL 'wchar_t x; x = (wchar_t)0;'
409 #then
410 # check_sizeof SIZEOF_WCHAR_T 'wchar_t' 4
411 #fi
412
413 # TODO: Detect header and size.
414 echo '#define HAVE_WCHAR_H 1'
415 echo '#define SIZEOF_WCHAR_T 4'
416
417 cat >$TMP/detect_va_list.c <<EOF
418#include <stdarg.h> /* C89 */
419int main() {
420 va_list list1, list2;
421 list1 = list2;
422}
423EOF
424 if cc_quiet $TMP/detect_va_list.c; then
425 echo '' # not an array
426 else
427 echo '#define VA_LIST_IS_ARRAY 1'
428 fi
429
430 # TODO: are these feature checks really necessary, or can we
431 # strip these out of posixmodule.c entirely?
432 cc_header_file HAVE_PTY_H 'pty.h'
433 cc_header_file HAVE_LIBUTIL_H 'libutil.h'
434 cc_header_file HAVE_UTIL_H 'util.h'
435
436 # TODO: are these feature checks really necessary?
437 cc_statement HAVE_STAT_TV_NSEC \
438 'struct stat st; st.st_mtim.tv_nsec = 1; return 0;' \
439 '#include <sys/stat.h>'
440 cc_statement HAVE_STAT_TV_NSEC2 \
441 'struct stat st; st.st_mtimespec.tv_nsec = 1; return 0;' \
442 '#include <sys/stat.h>'
443}
444
445echo_cpp() {
446 if test "$detected_deps" != 1; then
447 die 'called echo_cpp before detecting readline.'
448 fi
449 # Dev builds can use non-portable clock_gettime()
450 if test -n "$_OIL_DEV"; then
451 echo '#define GC_TIMING 1'
452 log 'Turned on -D GC_TIMING because $_OIL_DEV is set'
453 fi
454
455 if test "$have_readline" = 1; then
456 echo '#define HAVE_READLINE 1'
457 else
458 echo '/* #undef HAVE_READLINE */'
459 fi
460
461 # Used by mycpp/probes.h
462 if test "$have_systemtap_sdt" = 1; then
463 echo '#define HAVE_SYSTEMTAP_SDT 1'
464 else
465 echo '/* #undef HAVE_SYSTEMTAP_SDT */'
466 fi
467
468 if test "$have_fnm_extmatch" = 1; then
469 echo '/* libc defines FNM_EXTMATCH */'
470 else
471 # INVALID value that we can detect in code
472 # e.g. musl libc does not support this, but glibc does
473 echo '#define FNM_EXTMATCH 0'
474 fi
475
476 if test "$have_glob_period" = 1; then
477 echo '/* libc defines GLOB_PERIOD */'
478 else
479 # INVALID value that we can detect in code
480 # e.g. Android does not support this, but glibc and musl libc do
481 echo '#define GLOB_PERIOD 0'
482 fi
483
484 # Used by cpp/core.cc
485 if test "$have_pwent" = 1; then
486 echo '#define HAVE_PWENT 1'
487 else
488 echo '/* #undef HAVE_PWENT */'
489 fi
490}
491
492# Another way of working: set detected-config.mk ?
493# And set the default target as oil_readline, oil_no_readline, oil_lto,
494# oil_pgo, etc.?
495main() {
496 parse_flags "$@" # sets FLAG_*
497
498 mkdir -p _build
499
500 if ! cc_quiet build/detect-cc.c; then
501 die "Couldn't compile a basic C program (cc not installed?)"
502 fi
503
504 # Sets globals $have_readline and $readline_dir
505 detect_readline
506
507 detect_libc
508
509 detect_systemtap_sdt
510
511 # Generate configuration for oils-for-unix
512 local cpp_out=_build/detected-cpp-config.h
513 echo_cpp > $cpp_out
514 log "Wrote $cpp_out"
515
516 # Legacy OVM build: shell build actions will 'source
517 # _build/detected-config.sh'. And then adjust flags to compiler (-D, -l,
518 # etc.)
519 local sh_out=_build/detected-config.sh
520
521 echo_shell_vars > $sh_out
522 log "Wrote $sh_out"
523
524 # Fast mode
525 if test -n "$_OIL_DEV"; then
526 return
527 fi
528
529 local c_out=_build/detected-config.h
530 detect_c_language > $c_out
531 log "Wrote $c_out"
532}
533
534if test -z "$_OIL_CONFIGURE_TEST"; then
535 main "$@"
536fi