PACKAGE = graphthing
VERSION = @PACKAGE_VERSION@

subdir = src
prefix = @prefix@

DISTFILES = GNUmakefile.in config.h.in depend.1 *.cc *.h *.hh *.y *.l *.gt \
		phrases.lang *.sh *.xpm

CFLAGS = @CFLAGS@ @EXTRA_CFLAGS@
CXXFLAGS = @CXXFLAGS@ @EXTRA_CFLAGS@ \
		@WX_CXXFLAGS@
LDFLAGS = @LDFLAGS@ @EXTRA_LDFLAGS@
INSTALL = @INSTALL@

LIBS = @LIBS@ @WX_LIBS@
#LIBS = @LIBS@ @WX_LIBS@ -lefence

PARSERS = gt. @NLS_PARSERS@

OBJS = aboutbox.o canvas.o edge.o exporter.o factory.o fancyfileselection.o \
       graph.o graph2.o gui.o gui_cb.o main.o math.o matrix.o matrixdialog.o \
	paramdialog.o polynomial.o undo.o vertex.o \
	@NLS_OBJS@

TEST_OBJS = edge.o factory.o graph.o graph2.o math.o matrix.o polynomial.o \
		vertex.o
CHROM_OBJS = edge.o factory.o graph.o graph2.o math.o matrix.o polynomial.o \
		vertex.o

BISON_OBJS = $(PARSERS:.=-bison.tab.o)
FLEX_OBJS = $(PARSERS:.=-flex.yy.o)
ALL_OBJS = $(OBJS) $(BISON_OBJS) $(FLEX_OBJS)

PROGNAME = graphthing@EXEEXT@

all:		$(PROGNAME) $(addsuffix @EXEEXT@,@EXTRA_PROGS@)

install:
	@INSTALL@ -d @prefix@/bin/
	@INSTALL@ -s -m 755 $(PROGNAME) @prefix@/bin/

clean:
	rm -f $(ALL_OBJS) unit_test.o chrom.o
	rm -f $(BISON_OBJS:.o=.cc) $(BISON_OBJS:.o=.hh)
	rm -f $(FLEX_OBJS:.o=.cc)
	rm -f $(PROGNAME) unit_test@EXEEXT@ chrom@EXEEXT@
	rm -f core gmon.out graphthing.exe.stackdump

distclean:	clean
	rm -f config.h GNUmakefile

$(PROGNAME):	$(ALL_OBJS) $(EXTRA_LIBS)
	@echo "Linking $@"
	@@CXX@ $(LDFLAGS) $^ $(LIBS) -o $@

unit_test@EXEEXT@:	unit_test.o $(TEST_OBJS) $(EXTRA_LIBS)
	@echo "Linking $@"
	@@CXX@ $(LDFLAGS) $^ $(LIBS) -o $@
chrom@EXEEXT@:	chrom.o $(CHROM_OBJS) $(EXTRA_LIBS)
	@echo "Linking $@"
	@@CXX@ $(LDFLAGS) $^ $(LIBS) -o $@

# Have faith that bison and flex produce good code
%.tab.o : %.tab.cc
	@echo "$< -> $@"
	@@CXX@ @CXXFLAGS@ @WX_CXXFLAGS@ -O -g -c $< -o $@
%.yy.o : %.yy.c
	@echo "$< -> $@"
	@@CXX@ @CXXFLAGS@ @WX_CXXFLAGS@ -O -g -c $< -o $@

%-bison.tab.cc : %-bison.y
	@echo "$< -> $@"
	@@YACC@ -d -p yy_$*_ -o $@ $<
%-flex.yy.c : %-flex.l
	@echo "$< -> $@"
	@@LEX@ -Pyy_$*_ -o$@ $<

lang.cc : phrases.h
phrases.h : phrases.lang
	@./init-phrases.sh

#%.o : %.c
#	@echo "$< -> $@"
#	@@CC@ $(CFLAGS) -c $< -o $@
%.o : %.cc
	@echo "$< -> $@"
	@@CXX@ $(CXXFLAGS) -c $< -o $@



################
# DEPENDENCIES #
################

dep:
	@echo -n "Generating dependancies... "
	@@CXX@ -MM @WX_CXXFLAGS@ $(OBJS:.o=.cc) unit_test.cc \
		| sed "s#[ ][-A-Za-z0-9./]*/wx/[-A-Za-z0-9./]*\.h##g" \
		| grep -v "^[ ]*\\\\$$" > depend.1
	@echo "done!"

ifeq (depend.1, $(wildcard depend.1))
include depend.1
endif



distdir = ../$(PACKAGE)-$(VERSION)/$(subdir)
dist:	GNUmakefile $(DISTFILES)
	for file in $(DISTFILES) ; do \
		ln $$file $(distdir) 2>/dev/null || \
			cp -p $$file $(distdir) ; \
	done
	mkdir $(distdir)/flags
	for file in flags/*.xpm ; do \
		ln $$file $(distdir)/flags 2>/dev/null || \
			cp -p $$file $(distdir)/flags ; \
	done
