OILS / configure View on Github | oils.pub

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