#!/bin/sh # # POSIX shell script to detect target system properties required by Oil. # Distributed with the source tarball. # # The only library Oil needs is readline. # # External utilities used: expr, cc # # TODO: Should be able to run this from another directory. # # Other settings: LTO, PGO? Consider moving prefix, LTO, PGO to build and # install steps. TMP=${TMP:-/tmp} # Assume that any system has $TMP set or /tmp exists readonly TMP # POSIX sh supports 'readonly' log() { echo "$0: $@" 1>&2 } die() { echo "$0 ERROR: $@" 1>&2 exit 1 } show_help() { cat < while true; do case "$1" in '') break ;; --help) # TODO: Fill out help show_help exit 0 ;; --with-readline) FLAG_with_readline=1 ;; --without-readline) FLAG_without_readline=1 ;; # TODO: Maybe prefix only needs to be part of the install step? I'm not # sure if we need it for building anything. --prefix=*) FLAG_prefix=$(expr "$1" : '--prefix=\(.*\)') ;; --prefix) if test $# -eq 1; then die "--prefix requires an argument" fi shift FLAG_prefix=$1 ;; *) die "Invalid argument '$1'" ;; esac shift done # No output file, no logging, no stderr. # TODO: Maybe send stdout/stderr to config.log? cc_quiet() { cc "$@" -o /dev/null >/dev/null 2>&1 } cc_or_die() { if ! cc "$@" >$TMP/cc.log 2>&1; then log "Error running 'cc $@':" cat $TMP/cc.log die "Fatal compile error running feature test" fi } # Check if a given program compiles cc_statement() { local pp_var="$1" local prog="$2" cat >$TMP/cc_statement.c <$TMP/detect_va_list.c < /* C89 */ int main() { va_list list1, list2; list1 = list2; } EOF if cc_quiet $TMP/detect_va_list.c; then echo '' # not an array else echo '#define VA_LIST_IS_ARRAY 1' fi } # Another way of working: set detected-config.mk ? # And set the default target as oil_readline, oil_no_readline, oil_lto, # oil_pgo, etc.? main() { if ! cc_quiet build/detect-cc.c; then die "Couldn't compile a basic C program (cc not installed?)" fi # The shell build actions will 'source _build/detected-config.sh'. And then # adjust flags to compiler (-D, -l, etc.) mkdir -p _build local sh_out=_build/detected-config.sh local c_out=_build/detected-config.h detect_and_echo_vars > $sh_out detect_c_language > $c_out log "Wrote $sh_out and $c_out" } unittest() { cc_print_expr 'sizeof(int)' local actual actual=$(cat $TMP/print_expr.out) test "$actual" = 4 || die "Expected 4, got $actual" check_sizeof SIZEOF_INT 'int' 4 || die "FAILED" # failing test #check_sizeof SIZEOF_INT 'int' 8 cc_statement HAVE_INT 'int x = (int)0;' || die "FAILED" cc_statement HAVE_FOO 'foo x = (foo)0;' && die "Expected to fail" #detect_c_language } main "$@" #unittest "$@"