#! /bin/sh

# configure
# Copyright 1984-2017 Cisco Systems, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

srcdir=`dirname "$0"`

machs=""; last=""; sep0=""; sep1=""; sep2=""; sep3=""; sep4=" and ";
for fn in "$srcdir"/boot/*/scheme.boot ; do
    next=`echo $fn | sed -e 's/.*\/boot\/\(.*\)\/scheme.boot/\1/'`
    if [ "$next" != '*' ] ; then
        machs=$machs$sep0$last
        last=$next
        sep0=$sep1; sep1=", "; sep2=$sep3; sep3=$sep4; sep4=", and "
    fi
done
if [ "$srcdir" != "." ]; then
    for fn in boot/*/scheme.boot ; do
        next=`echo $fn | sed -e 's/boot\/\(.*\)\/scheme.boot/\1/'`
        if [ "$next" != '*' ] ; then
            machs=$machs$sep0$last
            last=$next
            sep0=$sep1; sep1=", "; sep2=$sep3; sep3=$sep4; sep4=", and "
        fi
    done
fi
machs=$machs$sep2$last

m=""
w=""
pb=no
pbarch=no
threads=yes
nothreads=no
libffi=no
temproot=""
help=no
forceworkarea=no
gzipmanpages=yes
installowner=""
installgroup=""
installbin=""
installlib=""
installman=""
installdoc=""
installcsug=""
installreleasenotes=""
installschemename="scheme"
installpetitename="petite"
installscriptname="scheme-script"
cflagsset=no
disablex11=no
disablecurses=no
disableiconv=no
addflags=yes
addwarningflags=no
default_warning_flags="-Wpointer-arith -Wall -Wextra -Wno-implicit-fallthrough"
: ${CC:="gcc"}
: ${CPPFLAGS:=""}
: ${CFLAGS:=""}
: ${LD:="ld"}
: ${LDFLAGS:=""}
: ${LIBS:=""}
: ${AR:="ar"}
: ${ARFLAGS:="rc"}
: ${RANLIB:="ranlib"}
: ${WINDRES:="windres"}
zlibLib=
LZ4Lib=
Kernel=KernelLib
buildKernelOnly=no
pbendian=l
emscripten=no
empetite=no
moreBootFiles=
preloadBootFiles=
alwaysUseBootFile=

# On WSL, set OS to "Windows_NT" (as already set in MSYS2) to create a
# Windows build instead of a Linux (on Windows) build:
if [ "$OS" = "Windows_NT" ] ; then
    CONFIG_UNAME="CYGWIN_NT-"
else
    CONFIG_UNAME=`uname`
fi

case "${CONFIG_UNAME}" in
  Linux)
    unixsuffix=le
    installprefix=/usr
    installmansuffix=share/man
    ;;
  QNX)
    if uname -a | egrep 'x86' > /dev/null 2>&1 ; then
      m32=i3qnx
      tm32=ti3qnx
    fi
    installprefix=/usr/local
    installmansuffix=man
    ;;
  FreeBSD)
    unixsuffix=fb
    installprefix=/usr/local
    installmansuffix=man
    ;;
  OpenBSD)
    unixsuffix=ob
    installprefix=/usr/local
    installmansuffix=man
    ;;
  NetBSD)
    unixsuffix=nb
    installprefix=/usr
    installmansuffix=share/man
    gzipmanpages=no
    ;;
  Darwin)
    if uname -a | egrep 'i386|i686|amd64|athlon|x86_64' > /dev/null 2>&1 ; then
      m32=i3osx
      m64=a6osx
      tm32=ti3osx
      tm64=ta6osx
    elif uname -a | egrep 'arm|aarch' > /dev/null 2>&1 ; then
      m64=arm64osx
      tm64=tarm64osx
    elif uname -a | egrep 'Power' > /dev/null 2>&1 ; then
      m64=ppc32osx
      tm64=tppc32osx
      pbendian=b
      default_warning_flags=""
    fi
    installprefix=/usr/local
    installmansuffix=share/man
    ;;
  SunOS)
    if uname -a | egrep 'i386|i686|amd64|athlon|x86_64' > /dev/null 2>&1 ; then
      m32=i3s2
      m64=a6s2
      tm32=ti3s2
      tm64=ta6s2
      installprefix=/usr
      installmansuffix=share/man
      gzipmanpages=no
    fi
    ;;
  CYGWIN_NT-*)
    if uname -a | egrep 'i386|i686|amd64|athlon|x86_64' > /dev/null 2>&1 ; then
      m32=i3nt
      m64=a6nt
      tm32=ti3nt
      tm64=ta6nt
      installprefix=/usr/local
      installmansuffix=share/man
    fi
    ;;
esac

if [ "$unixsuffix" != "" ] ; then
    if uname -a | egrep 'i386|i686|amd64|athlon|x86_64' > /dev/null 2>&1 ; then
        m32=i3${unixsuffix}
        m64=a6${unixsuffix}
        tm32=ti3${unixsuffix}
        tm64=ta6${unixsuffix}
    elif uname -a | egrep 'power|ppc' > /dev/null 2>&1 ; then
        m32=ppc32${unixsuffix}
        tm32=tppc32${unixsuffix}
        pbendian=b
    elif uname -a | egrep 'armv|aarch64' > /dev/null 2>&1 ; then
        m32=arm32${unixsuffix}
        m64=arm64${unixsuffix}
        tm32=tarm32${unixsuffix}
        tm64=tarm64${unixsuffix}
    fi
fi

threads=""
bits=""

while [ $# != 0 ] ; do
  case $1 in
    -m=*)
      m=`echo $1 | sed -e 's/^-m=//'`
      ;;
    --machine=*)
      m=`echo $1 | sed -e 's/^--machine=//'`
      ;;
    --boot=*)
      mboot=`echo $1 | sed -e 's/^--boot=//'`
      ;;
    --threads)
      threads=yes
      ;;
    --nothreads)
      threads=no
      ;;
    --64)
      bits=64
      ;;
    --32)
      bits=32
      ;;
    --pb)
      pb=yes
      ;;
    --pbarch)
      pbarch=yes
      ;;
    --emscripten)
      emscripten=yes
      CC="emcc"
      LD="emld"
      AR="emar"
      RANLIB="emranlib"
      ;;
    --force)
      forceworkarea=yes
      ;;
    --installprefix=*)
      installprefix=`echo $1 | sed -e 's/^--installprefix=//'`
      ;;
    --installlib=*)
      installlib=`echo $1 | sed -e 's/^--installlib=//'`
      ;;
    --installbin=*)
      installbin=`echo $1 | sed -e 's/^--installbin=//'`
      ;;
    --installman=*)
      installman=`echo $1 | sed -e 's/^--installman=//'`
      ;;
    --installdoc=*)
      installdoc=`echo $1 | sed -e 's/^--installdoc=//'`
      ;;
    --installcsug=*)
      installcsug=`echo $1 | sed -e 's/^--installcsug=//'`
      ;;
    --installreleasenotes=*)
      installreleasenotes=`echo $1 | sed -e 's/^--installreleasenotes=//'`
      ;;
    --installowner=*)
      installowner=`echo $1 | sed -e 's/^--installowner=//'`
      ;;
    --installgroup=*)
      installgroup=`echo $1 | sed -e 's/^--installgroup=//'`
      ;;
    --installschemename=*)
      installschemename=`echo $1 | sed -e 's/^--installschemename=//'`
      ;;
    --installpetitename=*)
      installpetitename=`echo $1 | sed -e 's/^--installpetitename=//'`
      ;;
    --installscriptname=*)
      installscriptname=`echo $1 | sed -e 's/^--installscriptname=//'`
      ;;
    --toolprefix=*)
      toolprefix=`echo $1 | sed -e 's/^--toolprefix=//'`
      CC="${toolprefix}${CC}"
      LD="${toolprefix}${LD}"
      AR="${toolprefix}${AR}"
      RANLIB="${toolprefix}${RANLIB}"
      WINDRES="${toolprefix}${WINDRES}"
      ;;
    --gzip-man-pages)
      gzipmanpages=yes
      ;;
    --nogzip-man-pages)
      gzipmanpages=no
      ;;
    --temproot=*)
      temproot=`echo $1 | sed -e 's/^--temproot=//'`
      ;;
    --workarea=*)
      w=`echo $1 | sed -e 's/^--workarea=//'`
      ;;
    --help)
      help=yes
      ;;
    --disable-x11)
      disablex11=yes
      ;;
    --disable-curses)
      disablecurses=yes
      ;;
    --disable-iconv)
      disableiconv=yes
      ;;
    --enable-libffi)
      libffi=yes
      ;;
    --disable-auto-flags)
      addflags=no
      ;;
    --enable-warning-flags)
      addwarningflags=yes
      ;;
    --libkernel)
      Kernel=KernelLib
      ;;
    --kernelobj)
      Kernel=KernelO
      ;;
    --emboot=*)
      bootfilenames=`echo $1 | sed -e 's/^--emboot=//'`
      moreBootFiles="${moreBootFiles} ${bootfilenames}"
      ;;
    --empetite)
      empetite=yes
      ;;
    --start=*)
      alwaysUseBootFile=`echo $1 | sed -e 's/^--start=//'`
      ;;
    CC=*)
      CC=`echo $1 | sed -e 's/^CC=//'`
      ;;
    CPPFLAGS=*)
      CPPFLAGS=`echo $1 | sed -e 's/^CPPFLAGS=//'`
      ;;
    CFLAGS=*)
      CFLAGS=`echo $1 | sed -e 's/^CFLAGS=//'`
      cflagsset=yes
      ;;
    CC_FOR_BUILD=*)
      CC_FOR_BUILD=`echo $1 | sed -e 's/^CC_FOR_BUILD=//'`
      ;;
    LD=*)
      LD=`echo $1 | sed -e 's/^LD=//'`
      ;;
    LDFLAGS=*)
      LDFLAGS=`echo $1 | sed -e 's/^LDFLAGS=//'`
      ;;
    LIBS=*)
      LIBS=`echo $1 | sed -e 's/^LIBS=//'`
      ;;
    AR=*)
      AR=`echo $1 | sed -e 's/^AR=//'`
      ;;
    ARFLAGS=*)
      ARFLAGS=`echo $1 | sed -e 's/^ARFLAGS=//'`
      ;;
    RANLIB=*)
      RANLIB=`echo $1 | sed -e 's/^RANLIB=//'`
      ;;
    WINDRES=*)
      WINDRES=`echo $1 | sed -e 's/^WINDRES=//'`
      ;;
    ZLIB=*)
      zlibLib=`echo $1 | sed -e 's/^ZLIB=//'`
      ;;
    LZ4=*)
      LZ4Lib=`echo $1 | sed -e 's/^LZ4=//'`
      ;;
    *)
      echo "option '$1' unrecognized or missing an argument; try $0 --help"
      exit 1
      ;;
  esac
  shift
done

if [ $pbarch = yes ] ; then
    pb=yes
fi

if [ $emscripten = yes ] ; then
    pb=yes
    bits=32
    if [ "$m" != "" ] ; then
        echo "Don't combine --emscripten with -m or --machine"
        exit 1
    fi
    m32=em
fi

case "$m" in
    pb)
        echo "Don't select pb using -m or --machine, because pb prefers to use the"
        echo " machine as the kernel host machine. Instead, use --pb or --pbarch"
        echo " to select a pb (portable bytecode) build."
        exit 1
        ;;
esac

if [ "$bits" = "" ] ; then
  if uname -a | egrep 'amd64|x86_64|aarch64|arm64|ppc64|powerpc64' > /dev/null 2>&1 ; then
    bits=64
  else
    bits=32
  fi
fi

if [ "$threads" = "" ] ; then
    defaultthreads=yes
else
    defaultthreads=$threads
fi

if [ "$threads" = "" ] ; then
    if [ "$pb" = "yes" ] ; then
        if [ "$pbarch" = "yes" ] ; then
            threads=yes
        else
            threads=no
        fi
    else
        threads=yes
    fi
fi

if [ $bits = 64 ] ; then
  if [ $defaultthreads = yes ] ; then defaultm=$tm64 ; else defaultm=$m64 ; fi
else
  if [ $defaultthreads = yes ] ; then defaultm=$tm32 ; else defaultm=$m32 ; fi
fi

if [ "$m" = "" ] ; then
  machine_supplied=no
  if [ $pb = yes ] ; then
     m=pb
     if [ $threads = yes ] ; then m=t$m ; fi
     if [ $bits = 64 ] ; then mpbhost=$m64 ; else mpbhost=$m32 ; fi
     flagsm=$mpbhost
     if [ "$mpbhost" = "" ] ; then
         flagsm=unknown
     fi
  else
    m=$defaultm
    flagsm=$m
  fi
elif [ $pb = yes ] ; then
  mpbhost=$m
  flagsm=$m
  m=pb
else
  flagsm=$m
fi

if [ $pbarch = yes ] ; then
    m=pb$bits$pbendian
    if [ $threads = yes ] ; then
        m=t$m
    fi
fi

if [ "$mboot" = "" ] ; then
    mboot="$m"
else
    magain=`echo $mboot | sed -e 's/-.*//'`
    if [ "$m" != "$magain" ]; then
        echo "Machine $m is not consistent with boot directory $magain"
        exit 1
    fi
    buildKernelOnly=yes
fi

if [ "$w" = "" ] ; then
    if [ $emscripten = yes ] ; then
        w=em-$mboot
    else
        w=$mboot
    fi
fi

if [ "$installbin" = "" ] ; then
  installbin=$installprefix/bin
fi

if [ "$installlib" = "" ] ; then
  installlib=$installprefix/lib
fi

if [ "$installman" = "" ] ; then
  installman=$installprefix/$installmansuffix
fi

if [ "$installdoc" = "" ] ; then
  installdoc=$installprefix/share/doc
fi

if [ "$installcsug" = "" ] ; then
  installcsug=$installdoc/csug9.5
fi

if [ "$installreleasenotes" = "" ] ; then
  installreleasenotes=$installdoc/csv9
fi

if [ "$help" = "yes" ]; then
  echo "Purpose:"
  echo "  $0 determines the machine type and constructs a custom Makefile"
  echo "  and Mf-install, taking into account the options below."
  echo ""
  echo "Options (defaults shown in parens):"
  echo "  --machine=<machine type>          explicitly specify machine type ($m)"
  echo "  -m=<machine type>                 same as --machine=<machine type> ($m)"
  echo "  --threads                         specify threaded version ($threads)"
  echo "  --nothreads                       specify non-threaded version ($nothreads)"
  echo "  --32|--64                         specify 32/64-bit version ($bits)"
  echo "  --pb                              specify pb (portable bytecode) version"
  echo "  --pbarch                          specify pb with inferred word and endianness"
  echo "  --emscripten                      build via emscripten (\"em\" tool prefix)"
  echo "  --disable-x11                     disable X11 support"
  echo "  --disable-curses                  disable [n]curses support"
  echo "  --disable-iconv                   disable iconv support"
  echo "  --enable-libffi                   enable libffi support for pb"
  echo "  --disable-auto-flags              no auto additions to CFLAGS/LDFLAGS/LIBS"
  echo "  --enable-warning-flags            add GCC warning flags to CFLAGS"
  echo "  --libkernel                       build libkernel.a (the default)"
  echo "  --kernelobj                       build kernel.o instead of libkernel.a"
  echo "  --installprefix=<pathname>        final installation root ($installprefix)"
  echo "  --installbin=<pathname>           bin directory ($installbin)"
  echo "  --installlib=<pathname>           lib directory ($installlib)"
  echo "  --installman=<pathname>           manpage directory ($installman)"
  echo "  --installdoc=<pathname>           documentation root ($installdoc)"
  echo "  --installcsug=<pathname>          guide directory ($installcsug)"
  # abbreviate "release notes" to fit default help in 80 cols:
  echo "  --installreleasenotes=<pathname>  notes directory ($installreleasenotes)"
  echo "  --temproot=<pathname>             staging root ($temproot)"
  echo "  --installowner=<ownername>        install with owner ($installowner)"
  echo "  --installgroup=<groupname>        install with group ($installgroup)"
  echo "  --installschemename=<schemename>  install scheme as ($installschemename)"
  echo "  --installpetitename=<petitename>  install petite as ($installpetitename)"
  echo "  --installscriptname=<scriptname>  install scheme-script as ($installscriptname)"
  echo "  --toolprefix=<prefix>             prefix tool (compiler, linker, ...) names"
  echo "  --boot=<machine type>-<tag>       build from prepared variant (e.g., pbchunk)"
  echo "  --emboot=\"<file> ...\"             additional boot <file>s with emscripten"
  echo "  --empetite                        omit \"scheme.boot\" with emscripten"
  echo "  --start=<name>                    load <boot>.boot instead of <exe>.boot"
  echo "  --[no]gzip-man-pages              compress manual pages ($gzipmanpages)"
  echo "  --workarea=<pathname>             build directory ($w)"
  echo "  --force                           configure even without boot files"
  echo "  CC=<C compiler>                   C compiler"
  echo "  CPPFLAGS=<C preprocessor flags>   C preprocessor flags"
  echo "  CFLAGS=<C compiler flags>         C compiler flags"
  echo "  CC_FOR_BUILD=<C compiler>         C compiler and flags for build machine"
  echo "  LD=<linker>                       linker"
  echo "  LDFLAGS=<linker flags>            additional linker flags"
  echo "  LIBS=<libraries>                  additional libraries"
  echo "  AR=<archiver>                     archiver"
  echo "  ARFLAGS=<archiver flgs>           archiver flags"
  echo "  RANLIB=<archive indexer>          archive indexer"
  echo "  WINDRES=<resource compiler>       resource compiler"
  echo "  ZLIB=<lib>                        link to <lib> instead of own zlib"
  echo "  LZ4=<lib>                         link to <lib> instead of own LZ4"
  echo ""
  echo "Available machine types: $machs"
  echo ""
  echo "Examples:"
  echo "  $0 --machine=i3le"
  echo ""
  echo "  set machine-type to i3le rather than to determined type"
  echo ""
  echo "  $0 --threads --installprefix=/usr/local"
  echo ""
  echo "  specify threaded version and set installation directory to /usr/local."
  echo ""
  echo "  $0 --installprefix=/usr/local --temproot=/tmp"
  echo ""
  echo "  declare the final destination to be /usr/local but staging area"
  echo "  to be /tmp/usr/local.  Make will record the final destination in the"
  echo "  installed manual pages but actually install the system and manual"
  echo "  pages in the staging area."
  echo ""
  exit 0
fi

optFlags=-O2

if [ "$emscripten" = "yes" ]; then
    flagsm=em
    buildKernelOnly=yes
    disableiconv=yes
    disablecurses=yes
fi

if [ "$cflagsset" = "no" -o "$addwarningflags" = "yes" ] ; then
    warningFlags="$default_warning_flags"
else
    warningFlags=""
fi

# Infer flags needed for threads:
case "${flagsm}" in
  *le|*fb|*ob|*nb)
      threadFlags="-D_REENTRANT -pthread"
      threadLibs="-lpthread"
      ;;
  *s2)
      threadFlags="-pthread"
      threadLibs="-lpthread"
      ;;
  em)
      threadFlags="-pthread"
      threadLibs=""
      ;;
  *)
      threadFlags=""
      threadLibs=""
      ;;
esac

flagsmuni=`echo $flagsm | sed -e 's/^t//'`
muni=`echo $m | sed -e 's/^t//'`

# Set default CFLAGS if not provided at all. Assuming that the
# compiler is for the right platform, compilation should generally
# succeed if no flags are set; anything required should be propagated
# a different way
if [ "$cflagsset" = "no" ] ; then
  case "${flagsmuni}" in
    a6le)
        CFLAGS="-m64 -msse2 ${optFlags}"
        ;;
    a6nt)
        CFLAGS="${optFlags}"
        ;;
    a6*)
        CFLAGS="-m64 ${optFlags}"
        ;;
    i3le)
        CFLAGS="-m32 -msse2 -mfpmath=sse ${optFlags}"
        ;;
    i3nt)
        CFLAGS="${optFlags}"
        ;;
    i3qnx)
        CC=qcc
        CFLAGS="-m32 -N2048K ${optFlags}"
        ;;
    i3*)
        CFLAGS="-m32 ${optFlags}"
        ;;
    arm32*)
        CFLAGS="${optFlags}"
        ;;
    arm64osx)
        CFLAGS="-arch arm64 ${optFlags}"
        ;;
    ppc32osx)
        CFLAGS="${optFlags}"
        ;;
    ppc32*)
        CFLAGS="-m32 ${optFlags}"
        ;;
    em)
        CFLAGS="${optFlags}"
        ;;
  esac
fi

if [ "$CC_FOR_BUILD" = "" ] ; then
    CC_FOR_BUILD="${CC} ${CFLAGS}"
fi

# Add automatic thread compilation flags, unless suppressed by --disable-auto-flags
if [ "$addflags" = "yes" ] ; then
    if [ "$threads" = "yes" ] ; then
        if [ "$threadFlags" != "" ] ; then
            CFLAGS="${CFLAGS} ${threadFlags}"
        fi
    fi
fi

cursesLib=-lcurses
ncursesLib=-lncurses

if [ "$disablecurses" = "yes" ]; then
  cursesLib=
  ncursesLib=
fi

if [ "$disableiconv" = "yes" ]; then
    iconvLib=
else
    iconvLib="-liconv"
fi

# Add automatic linking flags, unless suppressed by --disable-auto-flags
if [ "$addflags" = "yes" ] ; then
  case "${flagsm}" in
    *le)
        LDFLAGS="${LDFLAGS} -rdynamic"
        ;;
    *fb|*nb)
        LDFLAGS="${LDFLAGS} -rdynamic -L/usr/local/lib"
        ;;
    *ob)
        LDFLAGS="${LDFLAGS} -rdynamic -Wl,--export-dynamic -Wl,-zwxneeded -L/usr/local/lib"
        ;;
    *)
        ;;
  esac

  case "${flagsm}" in
    *le)
        LIBS="${LIBS} -lm -ldl ${ncursesLib} -lrt"
        ;;
    *fb|*ob)
        LIBS="${LIBS} ${iconvLib} -lm ${ncursesLib}"
        ;;
    *nb)
        if [ "$disableiconv" = "no" ]; then
            iconvLib="/usr/lib/i18n/libiconv_std.a"
        fi
        LIBS="${LIBS} ${iconvLib} -lm /usr/pkg/lib/libncurses.a"
        ;;
    *s2)
        LIBS="${LIBS} -lnsl -ldl -lm ${cursesLib} -lrt"
        ;;
    *osx)
        LIBS="${LIBS} ${iconvLib} -lm ${ncursesLib}"
        ;;
    *nt)
        LIBS="${LIBS} -lshell32 -luser32 -lole32 -lrpcrt4 -luuid"
        ;;
    *qnx)
        if [ "$disableiconv" = "no" ]; then
            iconvLib="/usr/local/lib/libiconv.so"
        fi
        LIBS="${LIBS} -lm ${iconvLib} -lsocket ${ncursesLib}"
        ;;
  esac
  if [ "$threadLibs" != "" ] ; then
    LIBS="${LIBS} ${threadLibs}"
  fi
fi

# more compile and link flags for c/Mf-unix and mats/Mf-unix
mdinclude=
mdcppflags=
mdcflags=
mdldflags=
mdlinkflags=
zlibConfigureEnv=
zlibConfigureFlags=
exeExtraDeps=
exePostStep=":"
exeSuffix=

# compile flags for c/Mf-unix and mats/Mf-unix
case "${flagsmuni}" in
    *le)
        mdcflags="-fPIC -shared"
        ;;
    *fb|*ob)
        mdcflags="-fPIC -shared"
        mdinclude="-I/usr/local/include -I/usr/X11R6/include"
        ;;
    *nb)
        mdcflags="-fPIC -shared"
        mdinclude="-I/usr/X11R7/include -I/usr/pkg/include -I/usr/pkg/include/ncurses -I/usr/X11R6/include"
        ;;
    *osx)
        mdcflags="-dynamiclib -undefined dynamic_lookup"
        ;;
    *s2)
        mdcflags="-fPIC -shared"
        mdcppflags="-DSOLARIS"
        ;;
    *qnx)
        mdcflags="-fPIC -shared"
        mdinclude="-I/usr/local/include"
        ;;
esac

# dynamic linking flags for c/Mf-unix
case "${flagsmuni}" in
    a6le)
        mdldflags="-melf_x86_64"
        ;;
    i3le)
        mdldflags="-melf_i386"
        ;;
    *le)
        ;;
    i3nb)
        mdldflags="-m elf_i386"
        ;;
    *fb|*ob|*nb)
        ;;
    *osx)
        ;;
    a6s2)
        mdldflags="-melf_x86_64"
        ;;
    i3s2)
        mdldflags="-melf_i386"
        ;;
    i3qnx)
        mdlinkflags="-Wl,--export-dynamic"
        mdldflags="-mi386nto"
        ;;
    em)
        mdlinkflags="-s EXIT_RUNTIME=1 -s ALLOW_MEMORY_GROWTH=1"
        ;;
esac

# pre- and post-link-executable step for c/Mf-unix
case "${flagsmuni}" in
    *nb)
        exePostStep='paxctl +m'
        ;;
    em)
        if [ "$empetite" == yes ]; then
            preloadBootFiles="$w/boot/$m/petite.boot"
        else
            preloadBootFiles="$w/boot/$m/petite.boot $w/boot/$m/scheme.boot"
        fi
        preloadBootFiles="$preloadBootFiles $moreBootFiles"
        exeSuffix=".html"
        ;;
esac

# zlib configure flags for c/Mf-unix
case "${flagsmuni}" in
    em)
        if  [ "$zlibLib" = "" ]; then
            zlibLib="-s USE_ZLIB=1"
            CFLAGS="${CFLAGS} -s USE_ZLIB=1"
            # not used, but this is how we might build our own zlib:
            zlibConfigureEnv="CROSS_PREFIX=em uname=wasm-em"
        fi
        ;;
esac

if [ "$w" = "$m" ] ; then
    configuringin=""
else
    configuringin=" in $w"
fi

if [ -f boot/$m/scheme.boot -o -f "$srcdir"/boot/$m/scheme.boot ] ; then
    echo "Configuring for $m$configuringin"
elif [ "$forceworkarea" = yes ] ; then
    echo "Configuring for $m$configuringin despite missing boot files"
else
  if [ "$m" = "" ] ; then
      maybem="<machine type>"
  else
      maybem=$m
  fi
  echo 'No suitable machine type found in "boot".'
  echo ""
  echo "Available machine types:"
  echo "  $machs"
  if [ "$machine_supplied" = "no" ] ; then
      echo ""
      if [ "$m" = "" ] ; then
          echo "If the problem is that the machine type was not inferred,"
          echo "you can try"
      else
          echo "If the problem is that the inferred machine type $m is"
          echo "not correct, you can try"
      fi
      echo "  $0 -m=<machine type>"
      echo "to specify one of the available machine types."
  fi
  echo ""
  if [ "$m" = "" ] ; then
      echo 'If no directory in "boot" exists for the correct machine type,'
      echo "then you can use Racket v7.1 or later with"
  else
      echo "Since no directory in "'"boot"'" exists for $m, you can try"
      echo "using Racket v7.1 or later with"
  fi
  echo "  racket rktboot/main.rkt --machine=$maybem"
  echo "to create the boot files, and then try $0 again."
  if [ -f boot/pb/scheme.boot ] ; then
      echo ""
      echo "Alternatively, a pb machine type is available, so try"
      echo "  $0 --pb"
      echo "  make $maybem.bootquick"
      echo "to create the boot files using a portable-bytecode build,"
      echo "and then try $0 again."
  fi
  exit 1
fi

case "$srcdir" in
    /*)
        upsrcdir=$srcdir
        ;;
    *)
        upsrcdir=../$srcdir
        ;;
esac

# Makefile to build and launch Zuo
sed -e 's/$(w)/'$w'/g'\
    "$srcdir"/makefiles/Makefile.in > Makefile

mkdir -p $w

# Stub Zuo script to lanch the real one, using "Makefile"
# to locate the workarea:
cp "$srcdir"/makefiles/buildmain.zuo main.zuo

# Some idea, but in the workarea, so it refers to "workarea.zuo" here:
cp "$srcdir"/makefiles/workmain.zuo $w/main.zuo

# The content of "$w/Makefile" records configuration decisions,
# and the Zuo build script takes it from there
cat > $w/Mf-config << END
srcdir=$srcdir
upsrcdir=$upsrcdir
m=$m
defaultm=$defaultm
mboot=$mboot
buildKernelOnly=$buildKernelOnly
mdinclude=$mdinclude
mdcppflags=$mdcppflags
mdcflags=$mdcflags
mdldflags=$mdldflags
mdlinkflags=$mdlinkflags
exeExtraDeps=$exeExtraDeps
exePostStep=$exePostStep
exeSuffix=$exeSuffix
zlibConfigureEnv=$zlibConfigureEnv
zlibConfigureFlags=
CC_FOR_BUILD=$CC_FOR_BUILD
CC=$CC
CPPFLAGS=$CPPFLAGS
CFLAGS=$CFLAGS
LD=$LD
LDFLAGS=$LDFLAGS
LIBS=$LIBS
AR=$AR
ARFLAGS=$ARFLAGS
RANLIB=$RANLIB
WINDRES=$WINDRES
cursesLib=$cursesLib
ncursesLib=$ncursesLib
zlibLib=$zlibLib
LZ4Lib=$LZ4Lib
warningFlags=$warningFlags
Kernel=$Kernel
installscriptname=$installscriptname
disablecurses=$disablecurses
disableiconv=$disableiconv
disablex11=$disablex11
enablelibffi=$libffi
preloadBootFiles=$preloadBootFiles
alwaysUseBootFile=$alwaysUseBootFile

InstallBin=$installbin
InstallLib=$installlib
InstallMan=$installman/man1
InstallCSUG=$installcsug
InstallReleaseNotes=$installreleasenotes
InstallOwner=$installowner
InstallGroup=$installgroup
TempRoot=$temproot
GzipManPages=$gzipmanpages
InstallSchemeName=$installschemename
InstallPetiteName=$installpetitename
InstallScriptName=$installscriptname
END
