# Copyright (C) International Computer Science Institute, 1994.  COPYRIGHT  
# NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject 
# to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in    
# the file "Doc/License" of the Sather distribution.  The license is also   
# available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA.  
# -----> Please email comments to "sather-bugs@icsi.berkeley.edu". <-----

# Be sure to read the file Doc/Installation!
#
# You'll need to set these variables to be appropriate to your system.
#
# What you set CC to will automatically be used for compiling the
# garbage collector and used for ordinary (after the bootstrap) compiles
# as well.  Some systems (OSF/1) need to use cc for the GC instead of gcc,
# so you can set GC_CC to be something different.
#
# CFLAGS is only used during the bootstrap; CC options for regular
# compiles must be specified in System/CONFIG.proto.
#
# CS is the executable name.
#
# Set SHOME to an absolute path to the directory this Makefile is in.
#
# Don't forget to edit (or at least inspect) the file System/CONFIG.proto.

CPP= /lib/cpp -C -P
RANLIB= ranlib
CC= gcc 
CFLAGS= -O
GC_CC= ${CC}
MV= mv
CMP= cmp
CS= cs
SHOME= /u/davids/Sather/Working
AR= ar
RM= rm -f
TEST= test
GC_MKFL= Makefile

SYSTEM= System/GC/gc.a System/base.a System/MACROS System/CONFIG System/ITERS
BOOTCS= ${SHOME}/Boot/${CS}

# Make the boot compiler and give it a workout on itself and some test cases
all: beware Compiler/${CS} quick_test

# Make and test everything
testall: all fixedpoint_test

system: ${SYSTEM}

beware:
	if [ ! -z "${SATHER_COMMANDS}" ]; then \
	  echo "You must turn off the SATHER_COMMANDS environment variable."; \
	  exit 1; \
	fi

# The compiler which ends up in Compiler/ is the "fast" executable,
# which means that checking is turned off and optimizations are on.

Compiler/${CS}: ${SYSTEM} ${BOOTCS} Compiler/*.sa Library/*.sa
	cd Compiler; ${BOOTCS} -home ${SHOME} -com cs.commands -o ${CS} -fast -only_reachable

debug: ${SYSTEM} ${BOOTCS} Compiler/*.sa Library/*.sa
	cd Compiler; ${BOOTCS} -home ${SHOME} -com cs.commands -o ${CS} -check all -debug -only_reachable -output_C -deterministic

${BOOTCS}: ${SYSTEM}
	cd Boot/cs.code; make CC='${CC}' CFLAGS='${CFLAGS}' CS='${CS}'

# The next four rules just make the stuff in System/

System/MACROS: System/MACROS.proto
	${CPP} System/MACROS.proto > System/MACROS
System/CONFIG: System/CONFIG.proto
	${CPP} -DCC='${CC}' System/CONFIG.proto > System/CONFIG
System/base.a: System/*.c System/*.h
	cd System; ${CC} ${CFLAGS} -c *.c; ${AR} rc base.a *.o; 
	${RANLIB} System/base.a || cat /dev/null
System/GC/gc.a:
	cd System/GC; make -f '${GC_MKFL}' CFLAGS='${CFLAGS} -DSILENT' CC='${GC_CC}' RANLIB='${RANLIB}'

# There are two tests.  The first is the test classes in the library,
# which is pretty straightforward.  The second is a full fixed-point
# compilation of the compiler, which can take quite a while.

quick_test: Compiler/${CS}
	cd Test; ../Compiler/${CS} -home ${SHOME} -com tests.commands -o ${TEST}; ./${TEST}>test-output

# The fixed-point test does the following:
#
#    1. Compile using Compiler/cs.
#    2. Compile using result.
#    3. Compile using _that_ result.
#    4. Compare C files generated by (2) and (3).
#    5. Clean up.
#
# This can take quite a long time so I've removed it from the things
# that occur by default during a build.

fixedpoint_test: Compiler/${CS}
	cd Test; \
	echo "Compiling 1st..." ; \
	../Compiler/${CS} -com ../Compiler/cs.commands -home ${SHOME} -output_C -o ${CS} -O -deterministic -check all; \
	${MV} ${CS} first.${CS}; \
	echo "Compiling 2nd..." ; \
	./first.${CS}     -com ../Compiler/cs.commands -home ${SHOME} -output_C -o ${CS} -O -deterministic -check all; \
	${MV} ${CS} first.${CS}; \
	${RM} -r first.${CS}.code; \
	${MV} ${CS}.code first.${CS}.code; \
	echo "Compiling 3rd..." ; \
	./first.${CS}     -com ../Compiler/cs.commands -home ${SHOME} -output_C -o ${CS} -O -deterministic -check all; \
	echo "Checking for fixed point..." ; \
	${CMP} ${CS}.code/globals.c first.${CS}.code/globals.c; \
	${CMP} ${CS}.code/decs.h first.${CS}.code/decs.h; \
	${CMP} ${CS}.code/code0.c first.${CS}.code/code0.c; \
	${CMP} ${CS}.code/code1.c first.${CS}.code/code1.c; \
	${CMP} ${CS}.code/code2.c first.${CS}.code/code2.c; \
	${CMP} ${CS}.code/code3.c first.${CS}.code/code3.c; \
	${CMP} ${CS}.code/code4.c first.${CS}.code/code4.c; \
	${CMP} ${CS}.code/code5.c first.${CS}.code/code5.c; \
	echo "Everything okay, cleaning up..." ; \
	${RM} -r ${CS}.code first.${CS}.code first.${CS} ${CS}

clean:
	${RM} System/*.o System/base.a
	${RM} System/MACROS System/CONFIG
	cd System/GC; make -f ${GC_MKFL} clean
	${RM} Boot/${CS} Boot/${CS}.code/*.o
	${RM} -fr Compiler/${CS} Compiler/${CS}.code
	cd Test; ${RM} -fr ${CS}.code first.${CS}.code ${CS} first.${CS}
	${RM} Test/${TEST} Test/test-output

# You'll never want to do these
distribution: Compiler/cs
	cd Compiler; ./cs -com cs.commands -home .. -o cs -O -output_C -only_C -only_reachable; rm -fr ../Boot/cs.code; mv cs.code ../Boot/
	make clean
	cd ..; mv Working Sather; rm -f Sather.tar.Z; tar cf - Sather | compress > Sather.tar.Z; mv Sather Working
	make testall
