###########################################################################
#
# File: Makefile
###########################################################################
# List sources here (target sources should only be listed below)
SRC	= 
# List target sources here. (target sources compile to executables, 
#                            i.e. they contain   int main() {  }
TARGETSRC= test.cc ex_op_overload.cc setsubmatrix.cc
# List library sources here. (target sources compile to executables, 
LIBSRC	= 
LIBRARY = libdmv.a



# Does this system have Atlas?  Where? (If no, then use standard blas,
# found in /usr/lib)
ATLAS	= no
ATLASPATH = /n/cmass/home/u/regroff/Atlas/Linux_PIIISSE1/lib

# do the correct thing if Atlas is installed
ifeq ($(ATLAS),yes)
ifndef ATLASPATH
$(error ATLAS is set to "yes", but ATLASPATH is undefined $(ATLASPATH))
endif
BLASLIBPATH += $(ATLASPATH)
BLASLIB	+= -llapack -lf77blas -latlas -lg2c 
BLASFLAG = -DDMV_USE_F77_BLAS
else
BLASLIBPATH += /usr/lib
BLASLIB	+= -llapack -lblas -lg2c 
BLASFLAG = -DDMV_USE_F77_BLAS
endif

# defines for conditional compiles
DEFINES	+= $(BLASFLAG)	-DDMV_USE_F77_LAPACK

# Use FLAGS for compiler options (not defines, libraries, or includes)
# Extra flags that might be good sometimes -Wshadow
FLAGS   = -DVERBOSE -Wall -Wstrict-prototypes  -Wmissing-prototypes
# in g++, use  -g for debug, -O for optimization
FLAGS	+= -g
LIBPATH	+= -L../../lib -L$(BLASLIBPATH)
INC     += -I../../include/dmv
LIB    	+=  -ldmv $(BLASLIB) -lm 
CXX	= g++ 
CMM	= g++ -MM 
# Tools 
RM      = rm  
SED     = sed
MV      = mv
AR	= ar
LN	= ln -sf
CD	= cd

# construct list of targets, target objects (contains main(){}),
# source objects (SRCOBJ) (does not contain main(){}), and 
# all objects (lists both source objects and target objects)
TARGET	= $(TARGETSRC:.cc=)
TRGOBJ	= $(TARGETSRC:.cc=.o)
LIBOBJ	= $(LIBSRC:.cc=.o)
SRCOBJ  = $(SRC:.cc=.o)
OBJ	= $(LIBOBJ) $(SRCOBJ) $(TRGOBJ)


all: $(TARGET)


$(TARGET): % : %.o $(SRCOBJ) $(LIBRARY)
	$(CXX) -o $@  $< $(SRCOBJ) $(LIBPATH) $(LIB)

$(OBJ): %.o: %.cc .dependencies
	$(CXX) -o $@ -c $<  $(FLAGS) $(DEFINES) $(INC) 

$(LIBRARY):
	make -C ../dmv


cleanall: clean
	rm  -f $(TARGET) $(LIBRARY) 

clean:  cleanobj cleandep
	rm -f  *~ include/*~ core

cleanobj:
	rm -f $(OBJ) 

cleandep:
	rm -f .dependencies

.dependencies dep: 
	@echo "Making dependencies..." 
	for i in $(SRC) $(TARGETSRC) $(LIBSRC); do $(CMM) $(DEFINES) $(INC) $$i; done > .dependencies



# Dependencies 
# if a target is given to make (MAKECMDGOALS) check if it is a "clean target"
# if not a clean target, define DEPINC
ifdef MAKECMDGOALS
DEPINC	= $(filter-out clean cleanall cleanobj cleandep, $(MAKECMDGOALS))
else
DEPINC	= yes
endif
# if DEPINC is nonempty, include dependencies
ifneq ($(DEPINC),)
-include .dependencies 
endif

.PHONY: all lib cleanall clean cleanobj cleandep

