TOP = ..
SRCDIR = .

MODULES = config fileutil util ast ksi xobjects xmldump module bind cast \
	  type patrange chini codegen gont linker ciface main
C_SOURCES = lex.c support.c keywords.c parser.tab.c

include $(TOP)/aux/rules.generic

GLIBS = xml.xga

CFLAGS += -I $(SRCDIR)

all: $(STAGE_ROOT)gontc

$(STAGE_ROOT)gontc: $(STAGE)gontc
	cp $< $@

$(STAGE)gontc: $(OBJS)
	$(GONTC) $(GFLAGS) -g -o $@ $(GLIBS) $(OBJS)
	if [ "$(STAGE)" = stage0/ ] ; then cp $(STAGE)gontc . ; fi

clean: clean-stage
	rm -f $(STAGE)gontc gontc core
	rm -f cgont/*
	
nuke: clean
	rm -f parser.tab.[ch] parser.output keywords.c

keywords.c: keywords.gperf
	gperf -c -N lookup_keyword -t -D $< > $@

parser.tab.c parser.tab.h: parser.y
	bison -b parser -d -t -v $<

parser.h: parser.tab.h cgont/ast.h cgont/cast.h cgont/list.h cgont/std.h

lex.c: support.h parser.h parser.tab.h

parser.tab.c: cgont/ast.h cgont/cast.h cgont/list.h cgont/std.h parser.h

rx = 's|.*/(.)([^/]*)\.h|\U$$1\E$$2|'
cgont/%.h:
	mkdir -p cgont
	$(GONTC) $(GFLAGS) -ciface $(shell echo $@ | perl -p -e $(rx)) > $@

$(STAGE)ast.xgo:: $(STAGE)util.xgi
$(STAGE)util.xgi:: $(STAGE)ast.xgi
$(STAGE)bind.xgo:: $(STAGE)xmldump.xgi
$(STAGE)xmldump.xgo:: $(STAGE)bind.xgi
$(STAGE)module.xgo:: $(STAGE)xmldump.xgi
