#!/bin/sh

### File: "build-gambit-bare-bones"

### Copyright (c) 2013 by Marc Feeley, All Rights Reserved.


# We must create the gambit.h header file from the gambit.h.in file.
# Normally this is done by the configure script.

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


# We must create the config.h header file by making a few assumptions
# about the C compiler, such as the existence of the stdio.h and
# stdlib.h header files.  The configure script normally tests for the
# existence of these header files.

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

echo "#define ___DONT_HAVE_WCHAR_H 1"          >> include/config.h

# uncomment these defines if the C compiler has gettimeofday
echo "#define HAVE_SYS_TIME_H 1"               >> include/config.h
echo "#define HAVE_GETTIMEOFDAY 1"             >> include/config.h

# uncomment this define if your machine is a little endian
echo "#define ___LITTLE_ENDIAN 1"               >> include/config.h

# uncomment these defines if the C compiler has getrusage
#echo "#define INCLUDE_sys_time_h 1"            >> include/config.h
#echo "#define INCLUDE_sys_resource_h 1"        >> include/config.h
#echo "#define INCLUDE_unistd_h 1"              >> include/config.h
#echo "#define HAVE_GETRUSAGE 1"                >> include/config.h

echo "#define ___GAMBCDIR \"/\""               >> include/config.h

# define the extension of object files created by the C compiler
#OBJ=".o"
OBJ=".8"

# how to call the C compiler to generate an object file
#CCOMP="cc -I../include -DHAVE_CONFIG_H"
CCOMP="pcc -I../include -DHAVE_CONFIG_H -c -D_BSD_EXTENSION -D_POSIX_SOURCE"

# how to call the C compiler to link object files into an executable binary
#CLINK="cc"
CLINK="8l"


# the following part of the shell script should not have to be modified

CCOMP_APP="$CCOMP"
CCOMP_LIB="$CCOMP -D___LIBRARY"
CCOMP_LIB_PR="$CCOMP_LIB -D___PRIMAL"

cd lib

echo "*** compiling runtime library"

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

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

$CCOMP_LIB_PR _gambc.c

cd ..

cd gsi

echo "*** compiling interpreter"

$CCOMP_LIB _gsilib.c
$CCOMP_LIB _gambcgsi.c
$CCOMP_APP _gsi.c
$CCOMP_APP _gsi_.c

$CLINK ../lib/*$OBJ *$OBJ

cd ..

cd gsc

echo "*** compiling compiler"

$CCOMP_LIB _host.c
$CCOMP_LIB _utils.c
$CCOMP_LIB _source.c
$CCOMP_LIB _parms.c
$CCOMP_LIB _env.c
$CCOMP_LIB _ptree1.c
$CCOMP_LIB _ptree2.c
$CCOMP_LIB _gvm.c
$CCOMP_LIB _back.c
$CCOMP_LIB _front.c
$CCOMP_LIB _prims.c
$CCOMP_LIB _assert.c
$CCOMP_LIB _asm.c
$CCOMP_LIB _x86.c
$CCOMP_LIB _codegen.c
$CCOMP_LIB _t-univ.c
$CCOMP_LIB _t-c-1.c
$CCOMP_LIB _t-c-2.c
$CCOMP_LIB _t-c-3.c
$CCOMP_LIB _gsclib.c
$CCOMP_LIB _gambcgsc.c
$CCOMP_APP _gsc.c
$CCOMP_APP _gsc_.c

$CLINK ../lib/*$OBJ *$OBJ

cd ..

echo "*** done"
