#!/bin/sh
#
# File: "build.sh"
#
# This script is a simple sequence of calls to the C compiler
# to build Gambit.

# Which C compiler to use?

CC="cc"
#CC="gcc"

# Which preprocessor definitions to add?

CC_DEFS=""
#CC_DEFS="-D___SINGLE_HOST"

CC_DEFS="$CC_DEFS -D___SYS_TYPE_CPU=\"unknown\" -D___SYS_TYPE_VENDOR=\"unknown\" -D___SYS_TYPE_OS=\"unknown\""

CC_DEFS="$CC_DEFS -D___GAMBCDIR=\"/tmp\""

# C compiler setup

CC_COMMON="time $CC -I../include $CC_DEFS"
CC_GEN="$CC_COMMON -c"
CC_LIB="$CC_GEN -DHAVE_CONFIG_H -D___LIBRARY"
CC_LIB_PR="$CC_LIB -D___PRIMAL"
CC_APP="$CC_GEN"

# We can't rely on sed being available so we generate gambit.h
# from gambit.h.in by prefixing it with the needed declarations.
# The definitions given below work for most platforms but
# you may want to hand-tune them (look at gambit.h.in to see what
# these symbols mean).

echo "#ifndef ___VOIDSTAR_WIDTH"                > include/gambit.h
echo "#define ___VOIDSTAR_WIDTH ___LONG_WIDTH" >> include/gambit.h
echo "#endif"                                  >> include/gambit.h
echo "#ifndef ___MAX_CHR"                      >> include/gambit.h
echo "#define ___MAX_CHR 0x10ffff"             >> include/gambit.h
echo "#endif"                                  >> include/gambit.h
cat include/gambit.h.in                        >> include/gambit.h

# Create config.h manually with relatively safe guesses

echo "#define HAVE_STDLIB_H 1"                > include/config.h
echo "#define HAVE_STRING_H 1"               >> include/config.h
echo "#define HAVE_ERRNO_H 1"                >> include/config.h

cd lib

echo "*** Compiling runtime system (hand-written C code)"

$CC_LIB_PR main.c
$CC_LIB_PR setup.c
$CC_LIB_PR mem.c
$CC_LIB_PR os.c
$CC_LIB_PR os_base.c
$CC_LIB_PR os_time.c
$CC_LIB_PR os_shell.c
$CC_LIB_PR os_files.c
$CC_LIB_PR os_dyn.c
$CC_LIB_PR os_tty.c
$CC_LIB_PR os_io.c
$CC_LIB_PR c_intf.c

echo "*** Compiling runtime system (C code generated from Scheme)"

$CC_LIB_PR _kernel.c
$CC_LIB_PR _system.c
$CC_LIB_PR _num.c
$CC_LIB_PR _std.c
$CC_LIB_PR _eval.c
$CC_LIB_PR _io.c
$CC_LIB_PR _nonstd.c
$CC_LIB_PR _thread.c
$CC_LIB_PR _repl.c

$CC_LIB_PR _gambc.c

cd ..

cd gsi

echo "*** Compiling gsi"

$CC_LIB _gsilib.c
$CC_LIB _gambcgsi.c
$CC_APP _gsi.c
$CC_APP _gsi_.c

echo "*** Linking gsi.exe"

$CC -o gsi.exe ../lib/*.o *.o

cd ..

cd gsc

echo "*** Compiling gsc"

$CC_LIB _host.c
$CC_LIB _utils.c
$CC_LIB _source.c
$CC_LIB _parms.c
$CC_LIB _env.c
$CC_LIB _ptree1.c
$CC_LIB _ptree2.c
$CC_LIB _gvm.c
$CC_LIB _back.c
$CC_LIB _front.c
$CC_LIB _prims.c
$CC_LIB _t-c-1.c
$CC_LIB _t-c-2.c
$CC_LIB _t-c-3.c
$CC_LIB _gsclib.c
$CC_LIB _gambcgsc.c
$CC_APP _gsc.c
$CC_APP _gsc_.c

echo "*** Linking gsc.exe"

$CC -o gsc.exe ../lib/*.o *.o

cd ..
