# Home directory of the Mercury compiler.
#
MERCURY_HOME=$(HOME)/Developer/mercury-rotd-2008-11-30

# Binary files to be built.
#
BIN=mercury_main

# Mercury source files.
#
MFILES=mercury_main.m CPlusPlus1_mint.m CPlusPlus2_mint.m

# C++ compiler flags.
#
CXXFLAGS=-Wall

#------------------------------------------------------------------------------#
# You shouldn't need to edit anything below.

ifeq ($(shell uname -s), Darwin)
# on Darwin, Mercury doesn't seem to support asm_fast (although it
# perhaps could for Intel-based systems?)
GRADE?=hlc.gc
else
# try asm_fast.gc otherwise
GRADE?=asm_fast.gc
endif

# on BSD, without deactivating suffix rules, any "non-suffixed"
# target would be matched by some sort of default rule (using
# $(CC)), not by our rule.
.SUFFIXES:

mercury_main:

.PHONY: compile
compile: $(BIN)

.PRECIOUS: %.o
%.o: %.cc CPlusPlusMain.h CPlusPlus1.h CPlusPlus1_mint.mh CPlusPlus2.h CPlusPlus2_mint.mh
	$(CXX) $(CXXFLAGS) -I$(MERCURY_HOME)/lib/mercury/inc -I$(MERCURY_HOME)/lib/mercury/conf -c $*.cc

mercury_main: $(MFILES) CPlusPlusMain.o CPlusPlus1.o CPlusPlus2.o
	mmc --make \
		--link-executable-command=$(CXX) \
		--link-object CPlusPlusMain.o \
		--link-object CPlusPlus1.o \
		--link-object CPlusPlus2.o \
		--grade $(GRADE) \
		$(MCFLAGS) \
		$@

.PRECIOUS: %.mh
%.mh: %.m
	mmc --make \
		--grade $(GRADE) \
		$(MCFLAGS) \
		$*.o

.PHONY: clean
clean:
	rm -rf $(BIN) Mercury \
		$(addsuffix .err, $(basename $(MFILES))) \
		$(addsuffix .mh, $(basename $(MFILES))) \
		CPlusPlusMain.o CPlusPlus1.o CPlusPlus2.o
