MACHINE := $(shell script/harch)
export MACHINE

# This included config is only for the BUILDWITH variable.
include targets/$(MACHINE)/config.cache

HMAKE = src/hmake/Makefile* src/hmake/*.hs src/hmake/README* \
	src/hmake/HISTORY src/hmake/Summary* \
	src/interpreter/Makefile* src/interpreter/*.hs

LIBDIR  = lib
TARGDIR = targets
TARGETS = hmake_n hmake_b hmake_g chmake


##### compiler build + install scripts

all:   all-${BUILDWITH}
help:
	@echo "Common targets include:        all install clean realclean config"
	@echo "For a specific build-compiler: all-hbc  all-ghc  all-nhc98 all-gcc"

config:
	./configure --config
install: all
	./configure --install

all-hbc:   hmake_b
all-ghc:   hmake_g
all-gcc:   chmake
all-nhc:   hmake_n
all-nhc98: hmake_n


$(TARGETS): % : $(TARGDIR)/$(MACHINE)/%


$(TARGDIR)/$(MACHINE)/hmake_n: $(HMAKE)
	cd src/hmake;          $(MAKE) HC=nhc98 install
	cd src/interpreter;    $(MAKE) HC=nhc98 install
	touch $(TARGDIR)/$(MACHINE)/hmake_n
$(TARGDIR)/$(MACHINE)/hmake_b: $(HMAKE)
	cd src/hmake;          $(MAKE) HC=hbc install
	cd src/interpreter;    $(MAKE) HC=hbc install
	touch $(TARGDIR)/$(MACHINE)/hmake_b
$(TARGDIR)/$(MACHINE)/hmake_g: $(HMAKE)
	cd src/hmake;          $(MAKE) HC=ghc install
	cd src/interpreter;    $(MAKE) HC=ghc install
	touch $(TARGDIR)/$(MACHINE)/hmake_g
$(TARGDIR)/$(MACHINE)/chmake: $(HMAKEC)
	@echo "WARNING: hmake might not build correctly from C sources!"
	cd src/hmake;          $(MAKE) fromC
	cd src/interpreter;    $(MAKE) fromC
	touch $(TARGDIR)/$(MACHINE)/chmake



##### cleanup

clean:
	cd src/hmake;          $(MAKE) clean
	cd src/interpreter;    $(MAKE) clean

realclean: clean
	cd $(TARGDIR)/$(MACHINE);  rm -f $(TARGETS)
	cd $(TARGDIR);  rm -f hmakeC
	rm -f $(LIBDIR)/$(MACHINE)/*
	rm -f $(TARGDIR)/$(MACHINE)/config.cache
	rm -f $(TARGDIR)/$(MACHINE)/hmake.config
	rm -f script/hmake script/hi
