OILS / configure View on Github | oils.pub

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