include ../../config.mak

SHELL = /bin/sh
LIB_PATH=../../lib
COMMON_PATH=../common
CMDLINE_FILE='"../dgp/cmdline.h"'

#vpath %.o  .

ifeq ($(BUILD_STATIC), yes)
	LDFLAGS += -static
endif

CXXFLAGS += -O2

sources = main.cc grammar.cc symbol.cc mgraph.cc sgraph.cc dgp0.cc cmdline.cc \
          $(COMMON_PATH)/common.cc global.cc

bin  = dgp

# plik *.o sa umieszczane w podkatalogu o
objs = $(sources:%.cc=%.o)

${bin}: ${objs}
	$(CXX) $(CXXFLAGS) -D _CMDLINE_FILE=$(CMDLINE_FILE) -o $@ ${objs} $(LDFLAGS)

include $(sources:.cc=.d)

%.o: %.cc
	$(CXX) -D _CMDLINE_FILE=$(CMDLINE_FILE) -c ${CXXFLAGS} -o $@ $<

%.d: %.cc
	$(CC) -MM $(CPPFLAGS) -D _CMDLINE_FILE=$(CMDLINE_FILE) $< > $@.$$$$; \
	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

# stare:
# cmdline.cc cmdline.h : cmdline.ggo
# 	gengetopt --c-extension=cc -i cmdline.ggo
# nowe
cmdline.cc cmdline.h: cmdline.ggo
	$(GENGETOPT) -i cmdline.ggo  --c-extension=cc --conf-parser

cmdline.ggo: cmdline_dgp.ggo ../common/cmdline_common.ggo
	cat cmdline_dgp.ggo ../common/cmdline_common.ggo > cmdline.ggo
# endnowe


clean:
	rm ${bin} ${objs} cmdline.cc cmdline.h
	rm -rf *.d

prof: dgp
	gprof dgp ~/tmp/dgp-pl/gmon.out > dgp.prof

.PHONY: install
install:
ifdef BIN_DIR
	install -m 0755 dgp $(BIN_DIR)
	install -m 0755 dgc $(BIN_DIR)
	install -m 0755 canonize $(BIN_DIR)
	install -m 0755 tre $(BIN_DIR)
endif

.PHONY: uninstall
uninstall:
ifdef BIN_DIR
	rm $(BIN_DIR)/dgp
	rm $(BIN_DIR)/dgc
	rm $(BIN_DIR)/canonize
	rm $(BIN_DIR)/tre
endif
