# -*-Makefile-*-
# $Id: makefile.inf,v 1.2 2006-02-06 22:12:43 Ludescher-Furth Exp $
# Use gmake!

# Any definition such as F90 and OPT can be overloaded
# using gmake F90=.. 
# 
PACKAGE = supralu
ifneq ($(MAKELEVEL),0)
# if OBJ was defined for main Makefile one level down
ifeq ("${OBJ}",".")
	override OBJ=..
endif
endif

-include ../share/Make.local 
include ../share/Make.flags


# for HP : Optimizer does not handle f90 interfaces
ifeq ($(MACHINE),HP)
	FFLAGS = -c
endif
# also problem on AIX
ifeq ($(MACHINE),RS6000)
	FFLAGS = -c
endif

ifdef DEBUG
	FFLAGS = $(DFFLAGS)
	CFLAGS = $(DCFLAGS)
endif
 
OBJDIR = $(OBJ)/obj/supralu
MDIR = $(OBJ)/mod

srcdir = $(shell pwd)
 
ARC = $(OBJ)/lib/libsupralu.a

# don't rebuild library if using installed one in $PREFIX 
ifeq ($(MAKELEVEL),0)
	THISLIB=$(ARC)
endif
 
# f90 modules
Mobjs = supralu_mod.o
MODS = $(foreach file,$(Mobjs),$(ARC)($(file)))

ifeq ($(MODUP),Y)
 MODS0=$(foreach m,$(Mobjs),$(shell  echo $(m) | tr 'a-z' 'A-Z'))
 MODULES=$(foreach m,$(MODS0),$(subst .O,.$(MODEXT),$(m)))
else
 MODULES = $(foreach m,$(Mobjs),$(subst .o,.$(MODEXT),$(m)))
endif 


#################################
ifndef SUPERLU 
   SUPERLU = -L$(SUPERLU_ROOT)/lib -lsuperlu
   SUPERLU_SRC = $(SUPERLU_DIR)/include
endif

ifdef SUPERLU_4
   CDEFS := $(CDEFS) -D__SUPERLU_4
endif

LDLIBS = -L$(OBJ)/lib -l$(PACKAGE) \
	$(LLOC) -lezcdf \
	$(NETCDF) \
	$(SUPERLU) \
	$(BLAS) \
	$(CLIBS)

DRIVERS_F90 = supralu_test.f90
DRIVERS_O = $(subst .f90,.o,$(DRIVERS_F90))
DRIVERS = $(subst .f90,,$(DRIVERS_F90))

# library members
MEMC = $(subst .c,.o, $(filter-out $(wildcard *module.c), $(wildcard *.c)))
MEM = $(foreach m,$(MEMC),$(ARC)($(subst .c,.o,$(m))))

libs: FORCE $(ARC)

all: libs exec

exec: $(OBJ)/test/$(DRIVERS)


FORCE:
	@test -d $(OBJ)/lib || mkdir -p $(OBJ)/lib
	@test -d $(OBJ)/mod || mkdir -p $(OBJ)/mod
	@test -d $(OBJDIR) || mkdir -p $(OBJDIR)
 
 

$(ARC): $(MODS) $(MEM)
	@$(RANLIB) $@ > /dev/null
 
exec: ckdirs $(OBJ)/test/$(DRIVERS) 
 
ckdirs: FORCE
	@test -d $(OBJ)/test || mkdir -p $(OBJ)/test

$(ARC)(%.o): %.f90
	$(FC90) $(FFLAGS) $(MODFLAGS) $(INCFLAGS) $(OUT) $(OBJDIR)/$*.o $<
	$(make_ar)

$(ARC)(%.o): %.c
	$(CC) $(CFLAGS) $(CDEFS) -I$(SUPERLU_SRC) -o $(OBJDIR)/$*.o $<
	$(make_ar)

$(OBJDIR)/%.o: %.f90
	$(FC90) $(FFLAGS) $(MODFLAGS) $(INCFLAGS) $(OUT) $@ $<

$(OBJ)/test/$(DRIVERS): $(OBJDIR)/$(DRIVERS_O) $(THISLIB) 
	$(FC90) $(LDFLAGS) -o $@ $< $(LDLIBS) 

clean:
	rm -f *.o

realclean: clean
	rm -f *.mod *.a $(DRIVERS)

