# Makefile for DNABEND

#BASE    = ..
BASE = $(shell pwd | sed -e 's/\/source//g')
INCLS	= $(BASE)/include
SOURCE	= $(BASE)/source
BIN	= $(BASE)/bin
GSLINCL = /usr/include
#CFLAGS	= -I$(INCLS) -g -Wall -p -DVERBOSE
CFLAGS	= -I$(INCLS) -I$(GSLINCL) -O2
#CFLAGS  = -I$(INCLS) -I$(GSLINCL) -g -Wall -p -DVERBOSE
LIBS    = -lm -lgsl -lgslcblas

# OBJS lists the files that do *not* contain a "main" and are used in the current build
# cf: grep main *.c -L | sed 's/\.c/.o/g'
OBJS	= options.o fastafile.o sequence.o dna_parameters.o equations.o vector.o geometry.o matrix.o xyzframe.o bstable.o minimize.o anneal.o

# VPATH allows make to consider files in all our directories
VPATH   = $(BIN):$(SOURCE):$(INCLS):$(GSLINCL)
CC = g++-4.4


# utility functions #######################################################

.PHONY: print clean
print:
	@echo BASE=$(BASE)
	@echo BIN=$(BIN)
	@echo SEDBIN=$(SEDBIN)
	@echo CFLAGS=$(CFLAGS)
	@echo LIBS=$(LIBS)
	@echo OBJS=$(OBJS)
	@echo INCLS=$(INCLS)
	@echo GSLINCL=$(GSLINCL)

clean:
	rm $(BIN)/*.o $(SOURCE)/.*.d
	rm -i $(BIN)/*



# define new pattern rules for compilation #################################
# (includes path info & dependency compatibility)

# object files
$(BIN)/%.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $< 

# executables
%: %.c $(OBJS)
	$(CC) $(CFLAGS) -o $(BIN)/$@ $^ $(LIBS)


# automatic dependency ###########################################

CC = g++-4.4
CCM = g++-4.4
CCMFLAG = -M  # CCMFLAG is the flag needed to generate dependencies

# DEPS variable contains a list of all dependency makefiles
DEPS = $(patsubst %.c,.%.d,$(wildcard *.c))

# pattern rule for creating dependency files
SEDBIN  = $(subst /,\\/,$(BIN))# escapes the path separator, "/" for sed
.%.d: %.c
	@echo -n making $@ from $<...
	@$(SHELL) -ec "$(CCM) $(CCMFLAG) $(CFLAGS) $< | sed -e 's/$*.o/$(SEDBIN)\/$*.o $@/g' > $@"
# 
	@echo done.

include $(DEPS)


# shortcuts #############################################################


# etags for emacs jumps
tags:	TAGS
TAGS:   $(wildcard *.c) $(wildcard $(INCLS)/*.h)
	etags -C $(INCLS)/*.h $(SOURCE)/*.c 








