include ../../config.mak

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

LDFLAGS +=
CXXFLAGS += -O2 -fpermissive

LIB_PATH=../lib
COMMON_PATH=../common
CMDLINE_FILE='"../cor/cmdline.h"'

all: cor

cor: main.cc corr.o $(LIB_PATH)/word.o \
	$(LIB_PATH)/auttools.o cmdline.c common_cor.o common.o 
	$(CXX) $(CXXFLAGS) -D _CMDLINE_FILE=$(CMDLINE_FILE) main.cc corr.o common.o \
	$(LIB_PATH)/word.o $(LIB_PATH)/auttools.o cmdline.c common_cor.o \
	-o cor $(LDFLAGS)

corr.o: corr.cc corr.hh
	$(CXX) $(CXXFLAGS) -c corr.cc

common.o: $(COMMON_PATH)/cmdline_common.ggo $(COMMON_PATH)/common.cc \
	 $(COMMON_PATH)/common.h
	$(CXX) $(CXXFLAGS) -c -D _CMDLINE_FILE=$(CMDLINE_FILE) $(COMMON_PATH)/common.cc

common_cor.o: cmdline.h common_cor.cc common_cor.h
	$(CXX) $(CXXFLAGS) -c -D _CMDLINE_FILE=$(CMDLINE_FILE) common_cor.cc

cmdline.c cmdline.h: cmdline.ggo
	$(GENGETOPT) -i cmdline.ggo --conf-parser

cmdline.ggo: cmdline_cor.ggo $(COMMON_PATH)/cmdline_common.ggo
	cat cmdline_cor.ggo $(COMMON_PATH)/cmdline_common.ggo > cmdline.ggo

.PHONY: install
install:
ifdef BIN_DIR
	install -m 0755 cor $(BIN_DIR)
endif

.PHONY: uninstall
uninstall:
ifdef BIN_DIR
	rm $(BIN_DIR)/cor
endif

clean: clean.cmdline
	rm *.o || true
	rm cor || true

clean.cmdline:
	rm cmdline.* || true

