#GNU Makefile to build Library libezcdf.a 
#
#01/07/09 C. Ludescher-Furth : created

ifneq ($(MAKELEVEL),0)
# if OBJ was defined for main Makefile one level down
ifeq ("${OBJ}",".")
	override OBJ=..
endif
endif

# define system dependent flags, etc.
-include  ../share/Make.local
include ../share/Make.flags


ifdef DEBUG
	FFLAGS = $(DFFLAGS)
endif

ifdef SHARED
	FFLAGS := $(FFLAGS) $(X_FFLAGS)
endif

ARC = $(OBJ)/lib/libezcdf.a
OBJDIR = $(OBJ)/obj/ezcdf

# don't rebuild library if using installed one in $PREFIX 
ifeq ($(MAKELEVEL),0)
	THISLIB=$(ARC)
endif

#=============================================================
# f90 modules
#public Module names
Mnams = ezcdf_inqvar.mod  ezcdf_attrib.mod ezcdf_opncls.mod ezcdf_genget.mod ezcdf_genput.mod ezcdf.mod 

# source file names
Mobjs = ezcdf_inqvar.o ezcdf_attrib.o ezcdf_opncls.o ezcdf_genget.o ezcdf_genput.o ezcdf.o 

 
MODS = $(foreach file,$(Mobjs),$(ARC)($(file)))

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

# fortran archive members
F  = $(subst .f,.o, $(wildcard *.f)) $(subst .for,.o, $(wildcard *.for)) $(subst .F,.o, $(wildcard *.F))
F90 = $(subst .f90,.o, $(wildcard *.f90)) $(subst .F90,.o, $(wildcard *.F90))
FMEM = $(F) $(F90) 

MEM = $(foreach file,$(FMEM),$(ARC)($(file)))

OBJECTS = $(foreach file,$(Mobjs) $(FMEM), $(OBJDIR)/$(file))


srcdir = $(shell pwd)

.PHONY: clean realclean checkdir all exec

libs: FORCE  $(ARC)
 
all:  libs exec 
	@echo done  

$(OLDLIB): timestamp.inf
	@echo "--- DETECTED ezcdf.a source update"
	@echo "--- Re-Making $(ARC)"
	@$(MAKE) libs

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

chkdirs:
	@test -d $(OBJ)/obj/ezcdf || mkdir -p $(OBJ)/obj/ezcdf
	@test -d $(OBJ)/test || mkdir $(OBJ)/test
 
$(ARC): $(MODS) $(MEM)
	@$(RANLIB) $@ > /dev/null


so: FORCE $(OBJ)/lib/libezcdf.so

$(OBJ)/lib/libezcdf.so: $(OBJECTS)
	$(LD) $(X_LDFLAGS) -o $@ $(RPATH) $(OBJECTS)

 
#pre-process/compile fixed length fortran 90 code
$(ARC)(%.o): %.F
	$(F_proc)
	$(make_ar)

#  compile fixed length fortran 90 code
$(ARC)(%.o): %.f
	$(FC) $(FFLAGS) $(MODFLAGS) -I./ $(INCFLAGS) $(OUT) $(OBJDIR)/$*.o $<
	$(make_ar)

$(ARC)(%.o): %.for
	$(FC) $(FFLAGS) $(MODFLAGS) -I./ $(INCFLAGS) $(OUT) $(OBJDIR)/$*.o $<
	$(make_ar)


#  compile free form fortran 90 code
$(ARC)(%.o): %.f90
	$(FC90) $(FFLAGS) $(MODFLAGS) -I./ $(INCFLAGS) $(OUT) $(OBJDIR)/$*.o $<
	$(make_ar)

# pre-process/compile free form fortran 90 code
$(ARC)(%.o): %.F90
	$(F90_proc)
	$(make_ar)

#
#---------------------------------------------------
#

exec:
	@echo " " 

chktest:
	@for i in $(DATA); do \
	(cd $(OBJ)/test; $(TSTLNK) $$i || ln -s $(srcdir)/$$i $$i;) done 




#-------------------------------------------------------------------------
install:
	@test -d $(LIBDIR) || mkdir -p $(LIBDIR)
	@umask 133; cp $(OBJ)/lib/libezcdf.a $(LIBDIR)/
	@test -d $(MODDIR) || mkdir $(MODDIR)
	@(cd $(MDIR); umask 133; cp $(MODULES) $(MODDIR)/)

uninstall:
	rm -f $(LIBDIR)/libezcdf.a
	(cd $(MODDIR); rm -f $(MODULES))

clean: 
	@rm -f $(OBJDIR)/*

realclean: clean
	@rm -f $(ARC)
	@if test -d $(MDIR); then \
	  (cd $(MDIR); rm -f $(MODULES)); fi

