# GNU Make to build SGLIB and XPLOT
# 08/11/98 C. Ludescher
# 08/18/98 D. Ernst - modified for Portland Group pgf77 v1.7-5
# 09/01/00 CAL : separate f77 / f90
#
# to use debug: make libs DEBUG=y
#

# Define FFLAGS, CFLAGS
# for NTCC modules include file is in ../share
# for TRANSP in $(CODESYSDIR)/source/misc/makeflags.mk
SHR = ../share/Make.flags
ifneq ("$(wildcard $(CODESYSDIR)/source/misc/makeflags.mk)","")
	SHR = $(CODESYSDIR)/source/misc/makeflags.mk
endif
-include ../share/Make.local
include $(SHR)

ifdef SHARED
   MAKE_SO=1
endif
# sglib special flags not covered by Make.flags:

FPP = gcc
CPPFLAGS := $(CPPFLAGS) -traditional

ifdef ABSOFT
	SGDEFS = -D__ABS
	NO_=Y
	FFLAGS := $(FFLAGS) -YCOM_NAMES=LCS
#                  may also need -YEXT_NAMES=LCS  for "lower case"
endif

ifdef __X1
	SGDEFS = -D__X1 -U_CRAY
	FPP=$(CPP)
endif

ifeq ($(MACHINE),CRAY)
	SGDEFS = -D_CRAY
	CRAYUP=Y
	FFLAGS := $(FFLAGS) -e0
	DFFLAGS := $(DFFLAGS) -e0
	FPP = cpp
endif
ifdef MKIBMFLAG
	NO_=Y
        FC90=xlf90
endif

ifeq ($(MACHINE),HP)
	NO_=Y
	CC := cc
endif

ifdef MKGCC
      FFLAGS :=  $(FFLAGS) -fno-range-check $(STATIC) $(FIXEDE) $(OUT) 
      DFFLAGS := $(DFFLAGS) -fno-range-check $(STATIC) $(FIXEDE) $(OUT)
else
      FFLAGS :=  $(FFLAGS) $(STATIC) $(FIXEDE) $(OUT) 
      DFFLAGS := $(DFFLAGS) $(STATIC) $(FIXEDE) $(OUT)
endif


# If LOCAL is not defined, all objects and executables go into default dir
ifndef LOCAL
	OBJ=../$(MACHINE)
  ifneq ($(MAKELEVEL),0)
# if OBJ was defined for main Makefile one level down
    ifeq ("${OBJ}",".")
	override OBJ=..
    endif
  endif
	LOCAL=$(OBJ)
	OBJDIR=$(OBJ)/obj/sglib/
        ifdef __CRAY
	  MDIR=$(OBJ)/lib
        else
          ifdef __X1
	    MDIR=$(OBJ)/lib
	  else
	    MDIR=$(OBJ)/mod
	  endif
        endif
	MPUB=$(MDIR)
	EXE=$(OBJ)/test/
	A=$(OBJ)/lib/
        LIBS=-L$(A) -ljc -lsg	
	TRLIBS= $(LLOC) -lportlib
	THISLIB=$(A)libjc.a $(A)libsg.a
else
# for TRANSP : $LOCAL is defined
	ifeq ("${LOCAL}",".")
          override LOCAL=..
	endif
	EXE=${LOCAL}/exe/
	A=${LOCAL}/other/
	OBJ=${LOCAL}
	OBJDIR=${LOCAL}/obj/sglib/
	MDIR=$(OBJDIR)
	MPUB=${LOCAL}/mod
	LIBS=$(A)libjc.a $(A)libsg.a
	TRLIBS=${LOCAL}/lib/portlib.a
	PREFIX=${LOCAL}
	TRINS=${CODESYSDIR}/sglib/
	THISLIB=$(A)libjc.a $(A)libsg.a
        ifdef MAKE_SO
	  CFLAGS := ${X_CFLAGS} ${CFLAGS}
	  FFLAGS := ${X_FFLAGS} ${FFLAGS}
        endif
endif

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

# sglib modules
SGOBJ=$(A)libsg.a(sgiso.o) $(A)libsg.a(sgnco.o)  $(A)libsg.a(sgarea.o) \
      $(A)libsg.a(sgext.o) $(A)libsg.a(sggrf.o)  $(A)libsg.a(sgtcs.o)\
      $(A)libsg.a(sgiof.o) $(A)libsg.a(sgvarg.o) $(A)libsg.a(sgsys.o) \
      $(A)libsg.a(r8sgext.o) $(A)libsg.a(r8sggrf.o) $(A)libsg.a(r8sgiso.o) \
      $(A)libsg.a(r8sgnco.o) $(A)libsg.a(r8sgtcs.o) $(A)libsg.a(r4sgext.o) \
      $(A)libsg.a(r4sggrf.o) $(A)libsg.a(r4sgnco.o)
# jclib modules
JCOBJ=$(A)libjc.a(jcmenu.o)   $(A)libjc.a(jcprompt.o) $(A)libjc.a(jctic.o) \
      $(A)libjc.a(jcstring.o) $(A)libjc.a(jcsys.o)

# Fortran sources
F = $(filter-out %_inc.f, $(subst .F,.f, $(wildcard *.F)))

# f90 modules
#ifeq "$(findstring f9,$(FC))" "f9"
   Mods = $(subst .f,.o, $(wildcard *_mod.f))
   M := $(foreach mod,$(Mods),$(OBJDIR)$(mod))
   M9INC=$(MFLAGS)$(MDIR)
#else
#   M = 
#   M9INC=
#endif



# always use f90
	FCM := $(FC90) $(M9INC)
	SGDEFS := $(SGDEFS) -D__f90

RLIBS=$(A)libsg.a $(A)libjc.a
XLIBS:=$(TRLIBS) $(XLIBS) $(XFLIB)

# Pre-processing
# Note:
# AIX xlf and HP-UX f77 do not append "_" to subroutine and common names
# On AIX code needs to be pre-processed
# the XLF switches don't seem to work - or overdo 

ifdef NO_
  define cmp_varg
    sed "s/_(/(/g" sgvarg.c > sgvargm.c
    $(CC) $(CFLAGS) $(SGDEFS) -o $(OBJDIR)sgvarg.o sgvargm.c
    rm sgvargm.c
  endef
else
# compile as is
  define cmp_varg
    $(CC) $(CFLAGS) $(SGDEFS) -o $(OBJDIR)$% $<
  endef 
endif

ifdef NO_
  ifdef ABSOFT
    define cmp_sys
sed -e"s/_(/(/g" -e"s/sg_buff_/_CSG_BUFF/g" -e"s/sg_flag_/_CSG_FLAG/g" sgsys.c > sgsysm.c
      $(CC) $(CFLAGS) $(SGDEFS) -o $(OBJDIR)sgsys.o sgsysm.c
      rm sgsysm.c
    endef
# End ABSOFT
  else
    define cmp_sys
      sed "s/_;/;/g" sgbuff.h > sgbuffm.h
      sed -e"s/_(/(/g" -e"s/_\./\./g" -e"s/sgbuff/sgbuffm/g" sgsys.c > sgsysm.c
      $(CC) $(CFLAGS) $(SGDEFS) -o $(OBJDIR)sgsys.o sgsysm.c
      rm sgbuffm.h sgsysm.c
    endef
  endif
# End else ABSOFT

# End NO_
else
  ifdef CRAYUP
    define cmp_sys
      sed -e"s/_(/(/g" -e"s/sg_buff_/SG_BUFF/g" -e"s/sg_flag_/SG_FLAG/g" sgsys.c > sgsysm.c
      $(CC) $(CFLAGS) $(SGDEFS) -o $(OBJDIR)sgsys.o sgsysm.c
      rm sgsysm.c
    endef
# End CRAY
else
  ifdef __X1
    define cmp_sys
      sed -e"s/sg_buff_/SG_BUFF_/g" -e"s/sg_flag_/SG_FLAG_/g" sgsys.c > sgsysm.c
      $(CC) $(CFLAGS) $(SGDEFS) -o $(OBJDIR)sgsys.o sgsysm.c
      rm sgsysm.c
    endef
# End __X1

# compile as is
  else

    define cmp_sys
      $(CC) $(CFLAGS) $(SGDEFS) -o $(OBJDIR)$% $<
    endef
  endif
endif
endif

# Compile .f
define f_comp
$(FC) $(FFLAGS) $(OBJDIR)$%  $<
@$(AR) $@ $(OBJDIR)$%
@rm $(OBJDIR)$%
endef

# Function to compile pre-processed .F
define f_proc
$(FCM) -I./ -I$(OBJDIR) $(FFLAGS) $(OBJDIR)$%  $<
@$(AR) $@ $(OBJDIR)$%
@rm $(OBJDIR)$%
endef

define f90_comp
$(FC90) $(FFLAGS) $(OBJDIR)$%  $<
@$(AR) $@ $(OBJDIR)$%
@rm $(OBJDIR)$%
endef

# Function to compile pre-processed .F
define f90_proc
$(FC90) $(M9INC) -I./ -I$(OBJDIR) $(FFLAGS) $(OBJDIR)$%  $<
@$(AR) $@ $(OBJDIR)$%
@rm $(OBJDIR)$%
endef

# Rules
#==================================================================

# libjc.a / libsg.a

libs: FORCE $(M) $(A)libjc.a $(A)libsg.a
	@echo made sglib/jclib

all:  FORCE libs exec
	@echo  

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

.PHONY: cleanall clean cleanexe chkdirs exec

# make sure Object directory exists
# if standalone distribution: make portlib
FORCE:  chkdirs
	@if test ! -d $(MDIR); then \
	mkdir -p $(MDIR); fi
	@if test ! -d $(A); then \
	mkdir $(A); fi
	@if test ! -f $(LIBDIR)/libportlib.a; then \
	if test -f ../portlib/Makefile; then \
	(cd ../portlib; $(MAKE) libs); fi; fi


# Pre-process .F
$(OBJDIR)%.f: %.F
	$(FPP) $(CPPFLAGS) $(SGDEFS) - < $< |grep -v '^$$' > $@

# Compile f90 modules
# MFFLAGS should really be same as F90FLAGS ?
$(OBJDIR)%_mod.o: %_mod.f
	$(FC90) $(MFFLAGS) $@ $<
	$(AR) $(A)libsg.a $@
	if test -z "$(__X1)$(__CRAY)"; then \
	if test $< = sglib_mod.f; then \
	mv *.$(MODEXT) $(MPUB)/; \
	else mv *.$(MODEXT) $(MDIR)/; fi; fi


chkdirs:
	@if test ! -d $(OBJDIR); then \
	mkdir -p $(OBJDIR); fi
	@if test ! -d $(EXE); then \
	mkdir $(EXE); fi

# make sure link exists to sglib.a / jclib.a for TRANSP 
$(A)libsg.a: $(SGOBJ)
	$(RANLIB) $@ > /dev/null
	@if test ! -h $(A)sglib.a -a -f $(A)sglib.a; then \
	mv $(A)sglib.a $(A)sglib.a~; fi
	@if test ! -h $(A)sglib.a; then \
	(cd $(A); ln -s libsg.a sglib.a); fi

$(A)libjc.a:  $(JCOBJ)
	$(RANLIB) $@ > /dev/null
	@if test ! -h $(A)jclib.a -a -f $(A)jclib.a; then \
	mv $(A)jclib.a $(A)jclib.a~; fi
	@if test ! -h $(A)jclib.a; then \
	(cd $(A); ln -s libjc.a jclib.a); fi

# sglib
$(A)libsg.a(sgiso.o): sgiso.f sgiso_inc
	$(f_comp)

$(A)libsg.a(sgnco.o): $(OBJDIR)sgnco.f sgnco_inc sgtcs_inc
	$(f_proc)

$(A)libsg.a(sgarea.o): sgarea.f sgarea_inc
	$(f_comp)

$(A)libsg.a(sgext.o): $(OBJDIR)sgext.f
	$(f_proc)

$(A)libsg.a(sggrf.o): $(OBJDIR)sggrf.f
	$(f_proc)

$(A)libsg.a(sgtcs.o): $(OBJDIR)sgtcs.f sgtcs_inc
	$(f_proc)

$(OBJDIR)sgiof_inc: sgiof_inc.F
	$(FPP) $(CPPFLAGS) $(SGDEFS) - < $< |grep -v '^$$' > $@	

$(A)libsg.a(sgiof.o): $(OBJDIR)sgiof.f $(OBJDIR)sgiof_inc sgtcs_inc $(OBJDIR)sgvgds_inc
	$(f_proc)

$(A)libsg.a(sgsys.o): sgsys.c sgbuff.h
	$(cmp_sys)
	@$(AR) $@ $(OBJDIR)$%
	@rm $(OBJDIR)$%

$(A)libsg.a(sgvarg.o): sgvarg.c sgvarg_code.h 
	$(cmp_varg)
	@$(AR) $@ $(OBJDIR)$%
	@rm $(OBJDIR)$%

$(A)libsg.a(r8sgiso.o): r8sgiso.f 
	$(f90_comp)

$(A)libsg.a(r8sgext.o): r8sgext.f 
	$(f90_comp)

$(A)libsg.a(r8sggrf.o): r8sggrf.f 
	$(f90_comp)

$(A)libsg.a(r8sgnco.o): r8sgnco.f 
	$(f90_comp)

$(A)libsg.a(r8sgtcs.o): r8sgtcs.f 
	$(f90_comp)

$(A)libsg.a(r4sgext.o): r4sgext.f 
	$(f90_comp)

$(A)libsg.a(r4sggrf.o): r4sggrf.f 
	$(f90_comp)

$(A)libsg.a(r4sgnco.o): r4sgnco.f 
	$(f90_comp)


# jclib
$(A)libjc.a(jcmenu.o): jcmenu.f jcticcom
	$(f_comp)

$(A)libjc.a(jcprompt.o): jcprompt.F jcticcom
	$(FPP) $(CPPFLAGS) $(SGDEFS) - < $< |grep -v '^$$' > $(OBJDIR)$*.f
	$(FC90) -I./ $(FFLAGS) $(OBJDIR)$%  $(OBJDIR)$*.f
	@$(AR) $@ $(OBJDIR)$%
	@rm $(OBJDIR)$%	

$(A)libjc.a(jcstring.o): jcstring.f
	$(f_comp)

$(A)libjc.a(jctic.o): jctic.f jcticcom
	$(f_comp)

$(A)libjc.a(jcsys.o): $(OBJDIR)jcsys.f
	$(f_proc)

$(OBJDIR)xplot_inc: xplot_inc.F
	$(FPP) $(CPPFLAGS) $(SGDEFS) - < $< |grep -v '^$$' > $@	

$(OBJDIR)sgvgds_inc: sgvgds_inc.F
	$(FPP) $(CPPFLAGS) $(SGDEFS) - < $< |grep -v '^$$' > $@	


# Makefile is devided, to avoid installing test programs, 
# if sglib was included in NTCC module by default
#
-include $(TRINS)Make_install

realclean: clean cleanexe
	echo THISLIB = $(THISLIB)
	@rm -f $(THISLIB)

cleanexe:
	@rm -f $(EXE)tstp10 $(EXE)tstcon $(EXE)xplot
	@rm -f tstiso tstone tstopt
clean:
	@rm -f -r $(OBJDIR)



