# $Id: Makefile,v 1.25 2008/05/08 15:12:52 post Exp $

all: opt 

SHELL=bash

CC = g++
CFLAGS = -Wall -I. -ffloat-store
CFLAGS_OPT = $(CFLAGS) -O3 # -NDEBUG
# CFLAGS_DEBUG = $(CFLAGS) -g -pg # -DDEBUG
CFLAGS_DEBUG = $(CFLAGS) -g -ggdb3 -O0 # -DDEBUG
LDFLAGS = 

OBJ_OPT_DIR   = optimized/
OBJ_DEBUG_DIR = debugged/

SRC_FILES = main.cpp itg.cpp itgchart.cpp itgcorp.cpp itggrmr.cpp array.cpp digamma.cpp	\
	globals.cpp itgdec-cyk.cpp itgdec-cyk-agenda.cpp itgdec-cyk-bitri.cpp itgdec-cyk-hook-agenda.cpp itgdec-cyk-hook.cpp\
	itgdec-lzkbest.cpp hash.cpp ConfigFile.cpp itgdec.cpp \
	itgtrain.cpp itgparse.cpp ngram.cpp vocab.cpp feature.cpp

H_FILES = itg.h itgchart.cpp itgcorp.h itggrmr.h array.h globals.h digamma.h	\
	key.h hash.h ConfigFile.h itgdec.h itgtrain.h itgparse.h \
	itgdec-cyk.h itgdec-cyk-agenda.h itgdec-cyk-bitri.h itgdec-lzkbest.h itgdec-cyk-hook-agenda.h itgdec-cyk-hook.h\
	ngram.h vocab.h feature.h


OBJ_OPT_FILES = ${SRC_FILES:%.cpp=$(OBJ_OPT_DIR)%.o}
OBJ_DEBUG_FILES = ${SRC_FILES:%.cpp=$(OBJ_DEBUG_DIR)%.o}

HUB_SRC_FILES = hub.cpp itggrmr.cpp array.cpp ConfigFile.cpp globals.cpp vocab.cpp digamma.cpp
HUB_OBJ_FILES = ${HUB_SRC_FILES:%.cpp=$(OBJ_OPT_DIR)%.o}
HUB_DEBUG_FILES = ${HUB_SRC_FILES:%.cpp=$(OBJ_DEBUG_DIR)%.o}


tags: $(SRC_FILES) $(H_FILES)
	-etags *.cpp *.h

#### Automatic dependencies
dep: $(SRC_FILES)
	@echo -n "Creating new dependencies file..."
	@-if test -e dependencies; then rm dependencies; fi
	@(echo "#Automatically generated dependency list" >> dependencies ;\
	$(CC) -Wno-deprecated -MM *.cpp $(CFLAGS_OPT) | perl -e 'while(<>){s?^([^\:]+\.o:)?$(OBJ_OPT_DIR)\1?g;print;}'>>  dependencies)
	@$(CC) -Wno-deprecated -MM *.cpp $(CFLAGS_DEBUG) | perl -e 'while(<>){s?^([^\:]+\.o:)?$(OBJ_DEBUG_DIR)\1?g;print;}'>>  dependencies
	@echo "done."

-include dependencies

# make the directories if they don't exist
$(OBJ_OPT_DIR):
	@echo Creating $(OBJ_OPT_DIR)
	@-mkdir $(OBJ_OPT_DIR)

$(OBJ_DEBUG_DIR):
	@echo Creating $(OBJ_DEBUG_DIR)
	@-mkdir $(OBJ_DEBUG_DIR)

$(OBJ_OPT_DIR)%.o: %.cpp
	$(CC) $(CFLAGS_OPT) -c $< -o $@

$(OBJ_DEBUG_DIR)%.o: %.cpp
	$(CC) $(CFLAGS_DEBUG) -c $< -o $@

opt: itg

check:	opt
	$(MAKE) -C Test

#### Targets
debug: $(OBJ_DEBUG_DIR) $(OBJ_DEBUG_FILES)
	$(CC) $(CFLAGS_DEBUG) $(LDFLAGS) $(OBJ_DEBUG_FILES) -o itg-debug

itg: $(OBJ_OPT_DIR) $(OBJ_OPT_FILES)
	$(CC) $(CFLAGS_OPT) $(LDFLAGS) $(OBJ_OPT_FILES) -o itg

hub-debug: $(OBJ_DEBUG_DIR) $(HUB_DEBUG_FILES)
	$(CC) $(CFLAGS_OPT) $(LDFLAGS) $(HUB_DEBUG_FILES) -o hub-debug

hub: $(OBJ_OPT_DIR) $(HUB_OBJ_FILES)
	$(CC) $(CFLAGS_OPT) $(LDFLAGS) $(HUB_OBJ_FILES) -o hub

clean:
	@if test -e $(OBJ_OPT_DIR); then rm -rf $(OBJ_OPT_DIR); fi
	@if test -e $(OBJ_DEBUG_DIR); then rm -rf $(OBJ_DEBUG_DIR); fi
	@if test -e dependencies; then rm -f dependencies; fi
