################################################################################
#
# Created by Austin Lee
# for COMS4115, Columbia University
# E-mail: taehee@gmail.com
#
# Settings are specific to Mac OS X Leopard v10.5.4
# Assumptions:
# 1. Both Boost.Regex and antlr v2 are installed on the system
# 2. Both libraries are already in the system library path
# 3. Using GNU's gcc compiler (g++)
#
################################################################################

BOOST_DIR = boost-1_35
ANTLR_DIR = antlr

# do not build with debug boost library by default
# both of these libraries get built and installed
# with standard boost install
ifdef DEBUG_BOOST
BOOST_LIB = boost_regex-gccD
else
BOOST_LIB = boost_regex-gcc
endif

# It is not recommended to use debug version of antlr
# But to link with debug antlr lib, the source will
# need be reconfigured with --enable-debug
# and rebuilt with debug=1
# antlr will not name debug version differently so
# the resulting lib will need to be renamed to antlrD
ifdef DEBUG_ANTLR
ANTLR_LIB = antlrD
else
ANTLR_LIB = antlr
endif

ifndef SYS_INC_DIR
  SYS_INC_DIR = /usr/local/include
endif

INC = -I. -I$(SYS_INC_DIR)/$(BOOST_DIR) -I$(SYS_INC_DIR)/$(ANTLR_DIR) 
LIB = -l$(BOOST_LIB) -l$(ANTLR_LIB)
CXX := g++
CXX_OUT_FLAG := -o
CXX_FLAGS = -O2 -felide-constructors -pipe
LD := g++
LD_OUT_FLAG := -o
LD_FLAGS := 

ifdef DEBUG
  PRODUCT_MODE = debug
  CXX_FLAGS += -Wall -DDEBUG -D_DEBUG
else
  PRODUCT_MODE = release
  CXX_FLAGS += -DNDEBUG
endif

CXX_FLAGS += -c

ROOT = `pwd`/..

TML_DIR := $(ROOT)/tml/$(PRODUCT_MODE)/
OBJ_DIR := $(ROOT)/obj/$(PRODUCT_MODE)/
SRC_DIR := $(ROOT)/src/

MKDIRS = mkdirs

#ANTLR_JAR = $(ROOT)/antlr/antlr.jar 
ANTLR_CMD = antlr

TMLGrammarFile := grammar.g
TMLSrcFiles = TMLLexer.cpp TMLParser.cpp TMLTreeWalker.cpp \
	      pattern.cpp search.cpp tml_logger.cpp \
	      tml_common_defs.cpp TMLTreeWalkerHelper.cpp tml.cpp
TMLObjFiles = TMLLexer.o TMLParser.o TMLTreeWalker.o \
	      pattern.o search.o \
	      tml_common_defs.o TMLTreeWalkerHelper.o tml.o tml_logger.o
TML := $(TML_DIR)tml
GRAM := gram

TMLSRCFILES = $(addprefix $(SRC_DIR),$(TMLSrcFiles))
TMLOBJFILES = $(addprefix $(OBJ_DIR),$(TMLObjFiles))

foo := $(shell echo $(TMLSrcFiles))

all: $(GRAM) $(MKDIRS) $(TML)

gram:
	$(ANTLR_CMD) $(TMLGrammarFile)

mkdirs:
	mkdir -pv $(TML_DIR)
	mkdir -pv $(OBJ_DIR)

.PHONY: test 
test: 
	rm -f -r $(ROOT)/test
	cp -f -r $(ROOT)/src/test ../
	cp $(TML) ../test

$(TML): $(MKDIRS) $(TMLObjFiles)
	$(LD) $(LD_FLAGS) $(LD_OUT_FLAG) $@ $(TMLOBJFILES) $(LIB)

$(TMLObjFiles): %.o: %.cpp
	$(CXX) $(CXX_FLAGS) $< $(CXX_OUT_FLAG) $(OBJ_DIR)$@ $(INC) 

.PHONY: clean
clean:
	rm -f -r $(OBJ_DIR)*.o $(TML) TMLLexer.cpp TMLLexer.hpp \
		TMLParser.cpp TMLParser.hpp TMLTreeWalker.cpp \
		TMLTreeWalker.hpp TMLLexerTokenTypes.* *~ 
