################################################################
# Start of Makefile
################################################################

# This rule goes first to make it the default choice
default		:: test

CC = gcc

CPPFLAGS += -I../includes 
CPPFLAGS += -D__HUGS__ 

CFLAGS	+= -Wall 
CFLAGS  += -W
CFLAGS  += -Wno-unused 
CFLAGS	+= -Wstrict-prototypes 
CFLAGS  += -Wmissing-prototypes 
CFLAGS  += -Wmissing-declarations
#CFLAGS	+= -Wredundant-decls 
#CFLAGS	+= -Wnested-externs
#CFLAGS	+= -Wshadow
CFLAGS	+= -Winline
CFLAGS	+= -Waggregate-return
CFLAGS	+= -Wpointer-arith
CFLAGS	+= -Wbad-function-cast
#CFLAGS	+= -Wcast-qual
#CFLAGS	+= -Wcast-align
#CFLAGS	+= -Wconversion

#CFLAGS	+= -ggdb3 -O0    # debug with gdb, minimal confusion
#CFLAGS	+= -pg -O2       # Profile with gprof
#CFLAGS	+= -pg -g        # Profile more with gprof
#CFLAGS	+= -pg -g -a     # Profile basic blocks with gprof (disnae work)
#CFLAGS	+= -O2 -g        # Run it - but put a little debugging help in
CFLAGS += -g -DDEBUG
#CFLAGS	+= -O6           # Just run it

C_FILES		= $(wildcard *.c)
S_FILES		= $(wildcard *.S)
HC_FILES	= $(wildcard *.hc)

LIBOBJS		+= $(patsubst %.c,%.o,$(C_FILES))
LIBOBJS		+= $(patsubst %.S,%.o,$(S_FILES))
LIBOBJS		+= $(patsubst %.hc,%.o,$(HC_FILES))

LIBRARY = libHSrts.a

%.o	: %.c
	@echo Compiling $<
	@$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
%.o	: %.S
	@echo Compiling $<
	@$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
%.o	: %.hc
	@echo Compiling $<
	@ $(CC) $(CFLAGS) $(CPPFLAGS) -xc -c $< -o $@

# We can build an archive
$(LIBRARY):	$(LIBOBJS)
	rm -f $@
	ar clqs $@ $^

# Or we can build a shared library
# (The shared library is nicer because it's linked with all the libs
#  that the rts depends on.  But it has the problem that the bfd code
#  can't see the symbols defined in the library - though that may be easy
#  to fix.)
$(LIBRARY:.a=.so):	$(LIBOBJS)
	rm -f $@
	$(CC) -shared $^ -L$(HOME)/lib -lm -lbfd -liberty -o $@ 

clean::
	$(RM) *.o *.a *.so
veryclean :: clean
tags:
	etags ../*/*.{c,h,hc,S}

################################################################
# Floppy disk for me to take home at night
################################################################

# We avoid using zip because we're fed up being bitten by the
# default=non-recursive bug

GHC_DIR  = fptools/ghc
TEST_DIR = $(GHC_DIR)/tests/rts
RTS_DIR  = $(GHC_DIR)/rts
LIB_DIR  = $(GHC_DIR)/lib
GMP_DIR  = $(GHC_DIR)/rts/gmp
INC_DIR  = $(GHC_DIR)/includes
HUGS_DIR = $(GHC_DIR)/interpreter

TARFILES += $(GHC_DIR)/CVS

TARFILES += $(INC_DIR)/*.h
TARFILES += $(INC_DIR)/CVS

TARFILES += $(RTS_DIR)/*.{c,h,hc,S} 
TARFILES += $(RTS_DIR)/comments
TARFILES += $(RTS_DIR)/adr 
TARFILES += $(RTS_DIR)/CVS 

TARFILES += $(TEST_DIR)/Makefile
TARFILES += $(TEST_DIR)/.gdbinit
TARFILES += $(TEST_DIR)/*.{c,h}
TARFILES += $(TEST_DIR)/CVS

TARFILES += $(GMP_DIR)

TARFILES += $(LIB_DIR)/*/CVS
TARFILES += $(LIB_DIR)/*/*.{lhs,hi-boot}
TARFILES += $(LIB_DIR)/*/cbits/*.{c,h}

TARFILES += $(HUGS_DIR)

tarfile:	
	cd ../../../$(GMP_DIR) && make clean
	cd ../../../$(HUGS_DIR) && make clean
	cd ../../..; tar zcvf rts.tgz $(TARFILES)
	ls -l ../../../rts.tgz
	echo todo: add unlit to tarfile

floppy:		tarfile
		mount /mnt/floppy
		- cp ../../../rts.tgz /mnt/floppy
		umount /mnt/floppy

################################################################
# Dependencies
################################################################

DEP_FILES	+= $(C_FILES:.c=.d)
DEP_FILES	+= $(S_FILES:.S=.d)
DEP_FILES	+= $(HC_FILES:.hc=.d)

include $(DEP_FILES)

#Copied from the gmake manual - builds a dependency file for every C file
%.d		: %.c
		@echo "Making dependency file $@"
		@$(SHELL) -ec '$(CC) -MM $(CPPFLAGS) $< \
		 | sed '\''s/\($*\)\.o[ :]*/\1.o $@ : /g'\'' > $@ \
		 ; [ -s $@ ] || rm -f $@'
%.d		: %.S
		@echo "Making dependency file $@"
		@$(SHELL) -ec '$(CC) -MM $(CPPFLAGS) $< \
		 | sed '\''s/\($*\)\.o[ :]*/\1.o $@ : /g'\'' > $@ \
		 ; [ -s $@ ] || rm -f $@'
%.d		: %.hc
		 @echo "Making dependency file $@"
		 @$(SHELL) -ec '$(CC) -MM $(CPPFLAGS) -xc $< \
		  | sed '\''s/\($*\)\.hc\.o[ :]*/\1.o $@ : /g'\'' > $@ \
		  ; [ -s $@ ] || rm -f $@'

veryclean:: 
	$(RM) $(DEP_FILES)

################################################################
# End of Makefile
################################################################
