#GNU Makefile to build Library libuflib_userio.a 
#    and shareable libufshr.so
#
#06/07/10 C. Ludescher-Furth
#
#-------------------------------------------------------------------
# define system dependent flags, etc.
-include  ../share/Make.local
SHR = ../share/Make.flags
ifneq ("$(wildcard $(CODESYSDIR)/source/misc/makeflags.mk)","")
        SHR = $(CODESYSDIR)/source/misc/makeflags.mk
endif
include $(SHR)


ifdef DEBUG
	F90FLAGS = $(DF90FLAGS)
	FFLAGS = $(DFFLAGS)
	CFLAGS = $(DCFLAGS)
endif

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

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

ARC = $(OBJ)/lib/libuflib_userio.a
OBJDIR = $(OBJ)/obj/uflib_userio

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

ifndef LOCAL
  LDLIBS = -L$(OBJ)/lib $(LLOC) -lmds_sub -lufhdf -lmdstransp -ltrgraf \
	-lureadsub -lsg -ljc -lvaxonly -lportlib  
else
  CINCL := -I $(CODESYSDIR)/source/incl_cpp $(CINCL)
  LDLIBS = $(OBJ)/lib/mds_sub.a $(OBJ)/lib/ufhdf.a $(OBJ)/lib/mdstransp.a \
	$(OBJ)/lib/trgraf.a $(OBJ)/lib/ureadsub.a $(OBJ)/other/libsg.a \
	$(OBJ)/other/libjc.a $(OBJ)/lib/vaxonly.a \
	$(OBJ)/lib/portlib.a
endif

LDLS = -lmds_sub -lufhdf -lmdstransp -lureadsub -lsgdummy -lvaxonly -lportlib
datdir  = $(shell cd ../test; pwd)
srcdir  = $(shell pwd)
#DATA    = $(srcdir)/ACS37065.ECS
DATA    = $(datdir)/ACS37065.ECS

#=============================================================
# f90 modules

# fortran archive members
C  = $(subst .c,.o, $(wildcard *.c))
F  = $(subst .f,.o, $(wildcard *.f)) $(subst .F,.o, $(wildcard *.F)) $(subst .for,.o, $(wildcard *.for))
F9 = $(subst .f90,.o, $(wildcard *.f90)) $(subst .F90,.o, $(wildcard *.F90))
FALL = $(F) $(F9) 
#remove test program
FMEM =$(filter-out uftype.o,$(FALL))

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

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

.PHONY: clean realclean checkdir all

libs: FORCE $(ARC)

all:  libs 
	@echo done  

FORCE:
	@test -d $(OBJ)/lib || mkdir -p $(OBJ)/lib
	@test -d $(OBJ)/obj/uflib_userio || mkdir -p $(OBJ)/obj/uflib_userio
	@if test ! -f $(LIBDIR)/libufhdf.a; then \
	if test -f ../ufhdf/Makefile; then \
	(cd ../ufhdf; $(MAKE) libs); fi; fi

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

# pre-process/compile fortran 77 code
$(ARC)(%.o): %.F
	$(F_proc)
	$(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)

$(ARC): $(MEM)
	@$(RANLIB) $@ > /dev/null


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

exec:


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

realclean: clean
	@rm -f $(OBJ)/lib/libuflib_userio.a

install: 
	@test -d $(LIBDIR) || mkdir -p $(LIBDIR)
	@if test -f $(ARC); then \
	  umask 133; cp $(ARC) $(LIBDIR)/; \
	else test -f $(LIBDIR)/libuflib_userio.a || echo ">>> can not find $(ARC)";fi

uninstall:
	rm -f $(LIBDIR)/libuflib_userio.a
