#GNU Makefile to build mdstransp Library libmdstransp.a 
#
#04/22/00 C. Ludescher
#

#=================================================================
# Instructions to build shareable image libTrmdsShr.so
#=================================================================
#
# I. build shareable image
#
#   If NTCC module:
#
#      1. make SHARED=Y so
#         libTrmdsShr.so will be in ../<MACHINE>/lib/libTrmdsShr.so
#
#   If TRANSP distribution:
#
#      1. cd $CODESYSDIR/mdstransp
#      2. make -f makefile.inf SHARED=Y OBJ=$LOCAL so
#         libTrmdsShr.so will be in $LOCAL/lib/libTrmdsShr.so
#      3. rm $LOCAL/obj/mdstransp/*.o
#
# II. Install library
#
#     copy libTrmdsShr.so into your "site specific" library directory
#     the library location must be in the LD_LIBRARY_PATH of 
#         - the MDSplus Server
#         - the users 
#     At PPPL the library is in /p/beast/<machine>/lib

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
SHR = ../share/Make.flags
ifneq ("$(wildcard $(CODESYSDIR)/source/misc/makeflags.mk)","")
        SHR = $(CODESYSDIR)/source/misc/makeflags.mk
endif
include $(SHR)

ifdef MDSPLUS_DIR
	INCFLAGS := $(IOPT)$(MDSPLUS_DIR)/include $(INCFLAGS)
else
	FPPFLAGS := $(FPPFLAGS)  -D__NOMDSPLUS
	CDEFS := $(CDEFS) -D__NOMDSPLUS
endif

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


ARC = $(OBJ)/lib/libmdstransp.a
OBJDIR = $(OBJ)/obj/mdstransp

#=============================================================

C =  $(subst .c,.o,$(wildcard *.c)) 

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

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

OBJECTS = $(foreach file, $(ALLM),$(OBJDIR)/$(file))
ifndef LOCAL 
  SLDLIBS = -L$(OBJ)/lib $(LLOC) -lvaxonly -lportlib
else
   SLDLIBS = $(OBJ)/lib/vaxonly.a $(OBJ)/lib/portlib.a
endif

.PHONY: clean realclean checkdir all

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

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

FORCE:
	@test -d $(OBJ)/lib || mkdir -p $(OBJ)/lib
	@test -d $(OBJ)/obj/mdstransp || mkdir -p $(OBJ)/obj/mdstransp

 
# compile c
$(ARC)(%.o): %.c
	$(c_proc)
	$(make_ar)

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

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

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

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

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


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

$(OBJDIR)/%.o: %.c
	$(CC) $(CFLAGS) $(INCFLAGS) -o $@ $< 

$(OBJDIR)/%.o: %.F
	$(F_proc)

$(OBJDIR)/%.o: %.f
	$(FC) $(FFLAGS) $(MODFLAGS) -I./ $(INCFLAGS) $(OUT) $@ $<
	$(mv_mod)

$(OBJDIR)/%.o: %.for
	$(F_proc)

$(OBJDIR)/%.o: %.f90
	$(F90_proc)

so: FORCE $(OBJ)/lib/libTrmdsShr.so
$(OBJ)/lib/libTrmdsShr.so: $(OBJECTS)
	$(LD) -o $@ $(X_LDFLAGS) $(OBJECTS) $(SLDLIBS) $(LUSRLIB) $(MDSLIB) $(XLIBS) $(SHLIBS)

#
#---------------------------------------------------
exec:
	@echo 

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

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

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

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