#CC      := g++

RANLIB   := ranlib
AR       := ar rcu
SED      := sed

WARN     :=-Wall -Wstrict-prototypes -Winline
OPTIMIZE :=-Os -g
INCLUDE  :=-I. -I../base -I..

ifeq ($(CC),gcc)
	DFLAGS := -MM
else
	DFLAGS := -M
endif

CFLAGS		:= $(OPTIMIZE) $(WARN) $(INCLUDE)
LFLAGS		:= -L. -L./_libs -L../base/_libs
#LFLAGS		+= -lMallocDebug
LIBS		:= -lm  -lSkipDB  -lIoBase

ifeq ($(PLATFORM), Darwin) 
	CFLAGS += -falign-loops=16
endif

### PLATFORM #################################################################
#
# Platform specifics compile time options:
#   NEEDS_DL - Needs -ldl for dlopen and friends.
#   SANE_POPEN - popen() can return a bidirectional FILE*
#

NEEDS_DL	:= Linux syllable SunOS
SANE_POPEN	:= FreeBSD Darwin
OS		:= $(shell uname -s)

ifneq (,$(findstring $(OS),$(NEEDS_DL)))
	LIBS += -ldl 
endif

ifneq (,$(findstring $(OS),$(SANE_POPEN)))
	CFLAGS += -DSANE_POPEN
endif

ifeq ($(OS),IRIX)
	RANLIB := touch
endif 

ifeq ($(OS),Darwin)
	CFLAGS += -mdynamic-no-pic
endif


### FILES ####################################################################

EXE      := _tests/skipdbtest
LIB      := _libs/libSkipDB.a
OBJDIR   := _objs

sources  := $(wildcard *.c)
objects  := $(addprefix _objs/,$(sources:.c=.o))
#depends := $(objects:.o=.d)


### RULES ####################################################################

all:
	mkdir -p _includes
	cp *.h _includes
	mkdir -p _libs
	mkdir -p _objs
	cd ../base; $(MAKE) 
	$(MAKE) $(LIB) 
	$(MAKE) $(EXE)

#lib:
#	cd ../base; $(MAKE) 
#	$(MAKE) $(LIB) 	

#-include $(depends)

$(OBJDIR)/%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

#$(OBJDIR)/%.d: %.c
#	@set -e; rm -f $@; \
#	 $(CC) $(DFLAGS) $(CPPFLAGS) $< | \
#	 sed 's,\($*\)\.o[ :]*,$(OBJDIR)/\1.o $@ : ,g' > $@


$(LIB): $(objects) 
	$(AR) $@ $(objects)
	$(RANLIB) $@
	cp *.h _includes
	#cp base/*.h _include

$(EXE): _tests/skipdbtest.c $(LIB)
	$(CC) $(CFLAGS) $(LFLAGS) -o $(EXE) _tests/skipdbtest.c $(LIBS)

clean:
	-rm -rf _objs
	-rm -rf _include
	-rm -f $(LIB) $(EXE)
	-rm default.*
	cd ../base; $(MAKE) clean
	-rm -rf build
	-rm -rf *.udbData

#install:
#	mkdir /usr/local/include/udb
#	cp _include/*.h /usr/local/include/udb
#	cp libudb.a /usr/local/lib

test:
	$(MAKE)
	./_tests/skipdbtest
	-rm default.*


