TOP = .
include $(TOP)/aux/make.config


.PHONY: all lib compiler
all: lib compiler

lib compiler: boot/gontc
	$(MAKE) -C $@

boot/gontc:
	$(MAKE) -C boot
	
$(TOP)/aux/make.config: $(TOP)/aux/make.config.in configure
	sh configure

.PHONY: boot bootstrap cycle clean stage stage-md5 clean0 stage-clean check
.PHONY: boot-clean world

world boot: bootstrap

cycle: boot-clean bootstrap

bootstrap: stages compare check
	@echo
	@echo "				Testing OK."
	@echo

stages:
	$(MAKE) STAGE=stage0/ PREV_STAGE=boot/ stage
	$(MAKE) STAGE=stage1/ OPTFLAGS=-O2 PREV_STAGE=stage0/ stage
	$(MAKE) STAGE=stage2/ OPTFLAGS=-O2 PREV_STAGE=stage1/ stage

stage:
	$(MAKE) -C lib
	$(MAKE) -C compiler
	$(MAKE) stage-md5

stage-md5:
	set -e; dir=`pwd`; \
	for f in compiler lib/* ; do \
		cd $$dir; \
		test -d $$f/$(STAGE) || continue; \
		cd $$f/$(STAGE); \
		find -name '*.xg[ioa]' -o -name '*.ksi' | \
			sort | xargs md5sum | sed -e "s|  *| $$f:|"; \
	done > $(subst /,,$(STAGE)).md5
	
compare:
	if [ "`md5sum < stage1.md5`" != "`md5sum < stage2.md5`" ] ; then \
		diff -u stage1.md5 stage2.md5 ; \
		echo 2>&1 "Bootstrap comparison failure" ; \
		false ; \
	fi
	
stage-clean:
	rm -rf $(STAGE)
	rm -rf compiler/$(STAGE)
	rm -rf lib/*/$(STAGE)
	rm -f $(subst /,,$(STAGE)).md5

boot-clean:
	$(MAKE) STAGE=stage1/ stage-clean
	$(MAKE) STAGE=stage2/ stage-clean
	
clean0:
	$(MAKE) STAGE=stage0/ stage-clean

clean: clean0 boot-clean
	set -e; for f in boot compiler doc lib ; do $(MAKE) -C $$f clean ; done
	rm -f aux/make.config config.log compiler/config.g

check:
	$(MAKE) -C t/ PREFIX="../stage2/"

VER := $(shell cat VERSION)
DISTDIR := gont-$(VER)

dist: changelogs
	$(MAKE) -C doc all
	$(MAKE) -C boot copy
	rm -rf $(DISTDIR)
	mkdir $(DISTDIR)
	find . ! -name $(DISTDIR) -mindepth 1 -maxdepth 1 | \
		xargs tar cf - | \
		(cd $(DISTDIR) && tar xf -)
	$(MAKE) -C $(DISTDIR) clean
	rm -f $(DISTDIR)/compiler/parser.output
	rm -rf $(DISTDIR)/doc
	tar cf - doc | (cd $(DISTDIR) && tar xf -)
	find $(DISTDIR) -name CVS -o -name .svn -o -name '*.tar.gz' | \
		xargs rm -rf
	$(MAKE) -C $(DISTDIR)/doc clean-aux
	chmod +x $(DISTDIR)/configure
	tar cf - $(DISTDIR) | bzip2 --best > $(DISTDIR).tar.bz2
	rm -rf $(DISTDIR)

www:
	$(MAKE) -C doc all
	rm -rf www-doc
	mkdir www-doc
	(cd doc && tar cf - .) | (cd www-doc && tar xf -)
	find www-doc -name CVS -o -name '*.tar.gz' | xargs rm -rf
	$(MAKE) -C www-doc clean-aux
	(cd www-doc && tar zcf -) | ssh team 'cd gontwww && tar vzxf -'
	rm -rf www-doc

FORCE:

ChangeLog: FORCE
	update-svn-changelog -x '^(lib|compiler)/'
	
compiler/ChangeLog: FORCE
	cd compiler; update-svn-changelog

lib/ChangeLog: FORCE
	cd lib; update-svn-changelog

changelogs: ChangeLog compiler/ChangeLog lib/ChangeLog

cl: changelogs
