DHDR=dbi.h dbi_error_text.h
DGHDR=dbi_gen.h
OHDR=../optgen/opt.h
HDR=../optgen/opt_defs.h ../optgen/global.h
ALLHDR=$(HDR) $(DHDR) $(DGHDR) $(OHDR) 

CCC = g++

# Compiler flags.
 
# Use macros instead of function calls where applicable.
B=-DUSE_MACROS 

# C can be set to -g for debugging.
C=-g

# Debug flag is off. Set to 1 for debug on. Have to set 
# H=-DPRINT=1 so print routines get linked in.
D=-DDEBUG=0

# Always =1 since exhaustive search is only option now.
E=-DEXHAUSTIVE=1

# Dump storage if terminate abnormally.
F=-DDUMP_ON_ABEND

# Perform safety ('sanity') checks. Use this during development and
# turn off (G=) for maximum performance.
G=-DSAFETY=1

# If DEBUG > 1, set H=-DPRINT=1 so print routines get linked in.
# If using print_plan function, need to set this also.
H=-DPRINT=1

# If want to see internal representation of returned plan or
# statistics (number of plans, etc.), set I=-DPRINT_OUTPUT.
# For statistics, also set K=-DGET_STATS.
# If statistics are collected, they will be printed even if
# print routines are not linked in, if this flag is 'on'.
I=

# J not used.
J=

# Set K=-DGET_STATS to compile in code to collect statistics.
K=-DGET_STATS

# Optimization flag. Set to O=-O4 for example.
O=-O2

CFLAGS=$B $C $D $E $F $G $H $I $O $J $K

# Set include path to directory this makefile is in.
INCLUDE_PATH=-I../optimizer

DBI_SRC=dbi.c dbi_error_text.c
DBI_OBJ=dbi.o dbi_error_text.o

GEN_SRC=dbi_gen.c match_trans_rule.c match_impl_rule.c
GEN_OBJ=dbi_gen.o match_trans_rule.o match_impl_rule.o

TEST_SRC=t0.C t1.C

TEST_OBJ=t0.o t1.o

OPT_OBJ=opt.o 

MAIN_OBJ=vol_opt.o find_bp.o optimize.o gen_all_trans.o
STOR_OBJ=expr_str.o
UTIL_OBJ=error_text.o opt_print.o opt_util.o

MAIN_SRC=../optgen/vol_opt.c ../optgen/find_bp.c ../optgen/optimize.c \
         ../optgen/gen_all_trans.c
STOR_SRC=../optgen/expr_str.c
UTIL_SRC=../optgen/error_text.c ../optgen/opt_print.c \
         ../optgen/opt_util.c
OPT_SRC=$(MAIN_SRC) $(STOR_SRC) $(UTIL_SRC)

vol_opt.o: ../optgen/vol_opt.c $(ALLHDR)
	$(CCC) -c $(CFLAGS) $(INCLUDE_PATH) ../optgen/vol_opt.c

find_bp.o: ../optgen/find_bp.c $(ALLHDR)
	$(CCC) -c $(CFLAGS) $(INCLUDE_PATH) ../optgen/find_bp.c

optimize.o: ../optgen/optimize.c $(ALLHDR)
	$(CCC) -c $(CFLAGS) $(INCLUDE_PATH) ../optgen/optimize.c

gen_all_trans.o: ../optgen/gen_all_trans.c $(ALLHDR)
	$(CCC) -c $(CFLAGS) $(INCLUDE_PATH) ../optgen/gen_all_trans.c

expr_str.o: ../optgen/expr_str.c $(ALLHDR)
	$(CCC) -c $(CFLAGS) $(INCLUDE_PATH) ../optgen/expr_str.c

error_text.o: ../optgen/error_text.c $(ALLHDR)
	$(CCC) -c $(CFLAGS) $(INCLUDE_PATH) ../optgen/error_text.c

opt_print.o: ../optgen/opt_print.c $(ALLHDR)
	$(CCC) -c $(CFLAGS) $(INCLUDE_PATH) ../optgen/opt_print.c

opt_util.o: ../optgen/opt_util.c $(ALLHDR)
	$(CCC) -c $(CFLAGS) $(INCLUDE_PATH) ../optgen/opt_util.c

dbi.o: $(DBI_SRC)
	$(CCC) -c $(CFLAGS) $(INCLUDE_PATH) dbi.c

dbi_error_text.o: $(DBI_SRC)
	$(CCC) -c $(CFLAGS) $(INCLUDE_PATH) dbi_error_text.c

# Make optimizer.
opt: $(MAIN_OBJ) $(STOR_OBJ) $(UTIL_OBJ) 
	ld -o opt.o -r $(MAIN_OBJ) $(STOR_OBJ) $(UTIL_OBJ)

# Make test program. Need to link in math library.
t0: t0.o $(OPT_OBJ) $(DBI_OBJ)
	$(CCC) -o t0 $(J) t0.o $(OPT_OBJ) $(DBI_OBJ) $(GEN_OBJ) -lm

t0.o: t0.C $(GEN_OBJ)
	$(CCC) -c $(CFLAGS) t0.C

t1: t1.o $(OPT_OBJ) $(DBI_OBJ)
	$(CCC) -o t1 $(J) $(OPT_OBJ) $(DBI_OBJ) t1.o $(GEN_OBJ) -lm

t1.o: $(GEN_OBJ)
	$(CCC) -c $(CFLAGS) t1.c

# Make generated DBI code. Uses model.input.
dbigen $(GEN_OBJ): ../optgen/gen $(DHDR) model.input
	../optgen/gen
	mv temp_dbi_gen.c dbi_gen.c
	mv temp_dbi_gen.h dbi_gen.h
	mv temp_match_trans_rule.c match_trans_rule.c
	mv temp_match_impl_rule.c match_impl_rule.c 
	$(CCC) $(CFLAGS) -c dbi_gen.c 
	$(CCC) $(CFLAGS) -c match_trans_rule.c 
	$(CCC) $(CFLAGS) -c match_impl_rule.c 

# RCS.
SAVE=$(DBI_SRC) $(DHDR) model.input $(TEST_SRC)
REV=
MSG=no log message

# RCS.
ci:
	ci -q -f -tinitial $(REV) -m'$(MSG)' $(SAVE)

# RCS.
co:
	co -q -l $(REV) $(SAVE)

# Clean up directory.
clean:
	rm -f $(MAIN_OBJ) $(STOR_OBJ) $(UTIL_OBJ)
	rm -f *.o debug.out debug2.out temp_dbi_gen.c temp_dbi_gen.h 
	rm -f temp_match_trans_rule.c temp_match_impl_rule.c
	rm -f match_impl_rule.c match_trans_rule.c dbi_gen.c dbi_gen.h
	rm -f t0 core t1

# Run lint. Need to make dbigen first.
LINT_SRC=$(DBI_SRC) $(TEST_SRC) $(OPT_SRC) $(GEN_SRC)
li.out: $(LINT_SRC)
	lint $(INCLUDE_PATH) $(B) $(D) $(E) $(F) $(G) $(H) $(K) $(LINT_SRC) > li.out
