
CC = g++

SOURCES = main.cc common.cc stats.cc fill.cc types.cc ccforest.cc \
	nodata.cc plateau.cc direction.cc water.cc  \
	filldepr.cc grid.cc genericWindow.cc \
	flow.cc sweep.cc weightWindow.cc

HEADERS = common.h stats.h streamutils.h types.h \
	option.h fill.h main.h \
	nodata.h ccforest.h	sortutils.h 3scan.h\
	plateau.h direction.h genericWindow.h \
	water.h grass2str.h filldepr.h unionFind.h \
	grid.h genericWindow.h flow.h sweep.h \
	weightWindow.h


OBJ := $(patsubst %.cc,%.o,$(SOURCES))
FLOAT_OBJ = $(patsubst %.cc, $(OBJARCH)/FLOAT/%.o, $(SOURCES))
SHORT_OBJ = $(patsubst %.cc, $(OBJARCH)/SHORT/%.o, $(SOURCES))


#Note: 	if a header file is modified, the .o files do not get rebuilt..
#		header files should be included as prerequisites, but does not work 
#		because of GRASS scripts 
$(OBJARCH)/FLOAT/%.o:%.cc
	$(CC) -c $(CFLAGS) -DELEV_FLOAT $< -o $@

$(OBJARCH)/SHORT/%.o:%.cc
	$(CC) $(CFLAGS)  -DELEV_SHORT -c $< -o $@



IOSTREAM_DIR =  ./IOStream
IOSTREAM_INC = $(IOSTREAM_DIR)/include
IOSTREAM_LIBDIR = $(IOSTREAM_DIR)/lib
IOSTREAM_LIB =  -liostream


WARNING_FLAGS   = -Wformat  -Wparentheses  -Wpointer-arith -Wno-conversion \
  -Wimplicit-int -Wimplicit-function-declaration -Wreturn-type	\
  -Wcomment  -Wno-sign-compare


CFLAGS += -Wall  -I$(IOSTREAM_INC) \
		-DUSER=\"$(USER)\" \
		-DNODATA_FIX -D_FILE_OFFSET_BITS=64

LDFLAGS += -L$(IOSTREAM_LIBDIR) -O3

LIBS = $(GISLIB) 
DEPLIBS = $(DEPGISLIB)



all: force $(BIN_CMD)/r.terraflow $(BIN_CMD)/r.terraflow.short

force:
	-mkdir $(OBJARCH)/FLOAT ; true
	-mkdir $(OBJARCH)/SHORT ; true

$(BIN_CMD)/r.terraflow: $(FLOAT_OBJ) $(GISLIB)  $(DEPLIBS)
	(cd IOStream/lib ; make)
	$(CC) -DELEV_FLOAT $(LDFLAGS) -o $@ $(FLOAT_OBJ) $(LIBS) $(MATHLIB) \
	$(XDRLIB) $(IOSTREAM_LIB)

$(BIN_CMD)/r.terraflow.short: $(SHORT_OBJ) $(GISLIB)  $(DEPLIBS)
	(cd IOStream/lib ; make)
	$(CC) -DELEV_SHORT $(LDFLAGS) -o $@ $(SHORT_OBJ) $(LIBS) $(MATHLIB) \
	$(XDRLIB) $(IOSTREAM_LIB)



$(GISLIB):	#
$(DEPGISLIB):	#


clean: 
	-rm $(OBJARCH)/FLOAT/*
	-rm $(OBJARCH)/SHORT/*
