OILS / configure View on Github | oils.pub

543 lines, 325 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 # TODO: I don't think we need this in shell
314 if test "$have_systemtap_sdt" = 1; then
315 echo 'HAVE_SYSTEMTAP_SDT=1'
316 else
317 echo 'HAVE_SYSTEMTAP_SDT='
318 fi
319
320 echo "PREFIX=$FLAG_prefix"
321 echo "DATAROOTDIR=$FLAG_datarootdir"
322
323 if cc_quiet build/detect-cc.c -Wl,--gc-sections; then
324 echo 'STRIP_FLAGS=--gc-sections'
325 elif cc_quiet build/detect-cc.c -Wl,-dead_strip; then
326 echo 'STRIP_FLAGS=-dead_strip'
327 fi
328}
329
330# c.m4 AC_LANG_INT_SAVE
331cc_print_expr() {
332 local c_expr="$1"
333 cat >$TMP/print_expr.c <<EOF
334#include <stdio.h>
335#include <sys/types.h> /* size_t, pid_t */
336
337int main() {
338 printf("%lu", $c_expr);
339}
340EOF
341 cc_or_die -o $TMP/print_expr $TMP/print_expr.c
342 $TMP/print_expr > $TMP/print_expr.out
343}
344
345# Shell note:
346# - local is not POSIX, but most shells have it.
347# C note:
348# - autoconf uses ac_fn_compute_int (in sh) aka AC_COMPUTE_INT (in m4).
349# - it uses different tests when cross compiling.
350# - cross-compiling does binary search?
351# - other one does AC_LANG_INT_SAVE
352# - generates a C program that outputs to conftest.val!
353# - well why not use exit code?
354# - QEMU configure doesn't do any tests
355
356# Hm, don't bother with cross compiling case for now.
357
358# Check if the size of a type is greater than a certain integer.
359check_sizeof() {
360 local pp_var="$1"
361 local c_type="$2"
362 local min_bytes="$3"
363
364 cc_print_expr "sizeof($c_type)"
365
366 local actual_bytes
367 actual_bytes=$(cat $TMP/print_expr.out)
368
369 if test -n "$min_bytes" && test "$actual_bytes" -lt "$min_bytes"; then
370 die "sizeof($c_type) should be at least $min_bytes; got $actual_bytes"
371 fi
372
373 # Echo to stdout!
374 echo "#define $pp_var $actual_bytes"
375}
376
377detect_c_language() {
378 # This is the equivalent of AC_CHECK_SIZEOF(int, 4)
379 check_sizeof SIZEOF_INT 'int' 4
380 check_sizeof SIZEOF_LONG 'long' 4
381 check_sizeof SIZEOF_VOID_P 'void *' 4
382 check_sizeof SIZEOF_SHORT 'short' 2
383 check_sizeof SIZEOF_FLOAT 'float' 4
384 check_sizeof SIZEOF_DOUBLE 'double' 8
385
386 check_sizeof SIZEOF_SIZE_T 'size_t' 4
387
388 # NOTE: This might only be relevant for large file support, which we don't
389 # have.
390 check_sizeof SIZEOF_FPOS_T 'fpos_t' 4
391 check_sizeof SIZEOF_PID_T 'pid_t' 4
392
393 check_sizeof SIZEOF_OFF_T 'off_t' ''
394 # autoconf checks if we have time.h, but the check isn't used. We just
395 # assume it's there.
396 check_sizeof SIZEOF_TIME_T 'time_t' ''
397
398 if cc_statement HAVE_LONG_LONG 'long long x; x = (long long)0;'
399 then
400 check_sizeof SIZEOF_LONG_LONG 'long long' 8
401 fi
402 if cc_statement HAVE_LONG_DOUBLE 'long double x; x = (long double)0;'
403 then
404 check_sizeof SIZEOF_LONG_DOUBLE 'long double' 8
405 fi
406
407 if cc_statement HAVE_C99_BOOL '_Bool x; x = (_Bool)0;'
408 then
409 # NOTE: this is mainly used in ctypes.h, which we might not need.
410 check_sizeof SIZEOF__BOOL '_Bool' 1
411 fi
412 # NOTE: Python also has a check for C99 uintptr_t. Just assume we don't
413 # have it?
414
415 #if cc_statement HAVE_C99_BOOL 'wchar_t x; x = (wchar_t)0;'
416 #then
417 # check_sizeof SIZEOF_WCHAR_T 'wchar_t' 4
418 #fi
419
420 # TODO: Detect header and size.
421 echo '#define HAVE_WCHAR_H 1'
422 echo '#define SIZEOF_WCHAR_T 4'
423
424 cat >$TMP/detect_va_list.c <<EOF
425#include <stdarg.h> /* C89 */
426int main() {
427 va_list list1, list2;
428 list1 = list2;
429}
430EOF
431 if cc_quiet $TMP/detect_va_list.c; then
432 echo '' # not an array
433 else
434 echo '#define VA_LIST_IS_ARRAY 1'
435 fi
436
437 # TODO: are these feature checks really necessary, or can we
438 # strip these out of posixmodule.c entirely?
439 cc_header_file HAVE_PTY_H 'pty.h'
440 cc_header_file HAVE_LIBUTIL_H 'libutil.h'
441 cc_header_file HAVE_UTIL_H 'util.h'
442
443 # TODO: are these feature checks really necessary?
444 cc_statement HAVE_STAT_TV_NSEC \
445 'struct stat st; st.st_mtim.tv_nsec = 1; return 0;' \
446 '#include <sys/stat.h>'
447 cc_statement HAVE_STAT_TV_NSEC2 \
448 'struct stat st; st.st_mtimespec.tv_nsec = 1; return 0;' \
449 '#include <sys/stat.h>'
450}
451
452echo_cpp() {
453 if test "$detected_deps" != 1; then
454 die 'called echo_cpp before detecting readline.'
455 fi
456 # Dev builds can use non-portable clock_gettime()
457 if test -n "$_OIL_DEV"; then
458 echo '#define GC_TIMING 1'
459 log 'Turned on -D GC_TIMING because $_OIL_DEV is set'
460 fi
461
462 if test "$have_readline" = 1; then
463 echo '#define HAVE_READLINE 1'
464 else
465 echo '/* #undef HAVE_READLINE */'
466 fi
467
468 # Used by mycpp/probes.h
469 if test "$have_systemtap_sdt" = 1; then
470 echo '#define HAVE_SYSTEMTAP_SDT 1'
471 else
472 echo '/* #undef HAVE_SYSTEMTAP_SDT */'
473 fi
474
475 if test "$have_fnm_extmatch" = 1; then
476 echo '/* libc defines FNM_EXTMATCH */'
477 else
478 # INVALID value that we can detect in code
479 # e.g. musl libc does not support this, but glibc does
480 echo '#define FNM_EXTMATCH 0'
481 fi
482
483 if test "$have_glob_period" = 1; then
484 echo '/* libc defines GLOB_PERIOD */'
485 else
486 # INVALID value that we can detect in code
487 # e.g. Android does not support this, but glibc and musl libc do
488 echo '#define GLOB_PERIOD 0'
489 fi
490
491 # Used by cpp/core.cc
492 if test "$have_pwent" = 1; then
493 echo '#define HAVE_PWENT 1'
494 else
495 echo '/* #undef HAVE_PWENT */'
496 fi
497}
498
499# Another way of working: set detected-config.mk ?
500# And set the default target as oil_readline, oil_no_readline, oil_lto,
501# oil_pgo, etc.?
502main() {
503 parse_flags "$@" # sets FLAG_*
504
505 mkdir -p _build
506
507 if ! cc_quiet build/detect-cc.c; then
508 die "Couldn't compile a basic C program (cc not installed?)"
509 fi
510
511 # Sets globals $have_readline and $readline_dir
512 detect_readline
513
514 detect_libc
515
516 detect_systemtap_sdt
517
518 # Generate configuration for oils-for-unix
519 local cpp_out=_build/detected-cpp-config.h
520 echo_cpp > $cpp_out
521 log "Wrote $cpp_out"
522
523 # Legacy OVM build: shell build actions will 'source
524 # _build/detected-config.sh'. And then adjust flags to compiler (-D, -l,
525 # etc.)
526 local sh_out=_build/detected-config.sh
527
528 echo_shell_vars > $sh_out
529 log "Wrote $sh_out"
530
531 # Fast mode
532 if test -n "$_OIL_DEV"; then
533 return
534 fi
535
536 local c_out=_build/detected-config.h
537 detect_c_language > $c_out
538 log "Wrote $c_out"
539}
540
541if test -z "$_OIL_CONFIGURE_TEST"; then
542 main "$@"
543fi