include ../../config.mak

# because of OpenFST this application cannot be compiled statically, yet
#ifeq ($(BUILD_STATIC), yes)
#  LDFLAGS += -static
#endif

LDFLAGS += -ldl -lfst
CXXFLAGS += -g  -fpermissive

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


lem: main.cc lem.o lemfst.o $(LIB_PATH)/auttools.o $(LIB_PATH)/word.o \
      cmdline.c common_lem.o common.o symtab.o
	$(CXX) $(CXXFLAGS) -D _CMDLINE_FILE=$(CMDLINE_FILE) \
	main.cc lem.o lemfst.o $(LIB_PATH)/auttools.o \
	$(LIB_PATH)/word.o cmdline.c common.o common_lem.o \
	symtab.o -o lem $(LDFLAGS)

lem.o: lem.h lem.cc
	$(CXX) $(CXXFLAGS) -c -D _CMDLINE_FILE=$(CMDLINE_FILE) lem.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_lem.o: cmdline.h common_lem.h common_lem.cc
	$(CXX) $(CXXFLAGS) -c -D _CMDLINE_FILE=$(CMDLINE_FILE) common_lem.cc

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

cmdline.ggo: cmdline_lem.ggo ../common/cmdline_common.ggo
	cat cmdline_lem.ggo ../common/cmdline_common.ggo > cmdline.ggo

symtab.o: $(LIB_PATH)/symtab.h $(LIB_PATH)/symtab.cc
	$(CXX) $(CXXFLAGS) -c $(LIB_PATH)/symtab.cc

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

clean.cmdline:
	rm cmdline.* || true

lemfst.o: 
	$(CXX) -c lemfst.cpp $(CXXFLAGS) -o lemfst.o

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

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