# GNU Makefile Include file to setup FLAGS, etc.
#
# 05/21/99 C. Ludescher-Furth
#
#  make -f $CODESYSDIR/source/misc/flagtest.mk all
#                                                 to list settings (jec )
#
#--Mods
#    16Dec2008   Jim.conboy@jet.uk
#                Use filetest -d, not wildcard *, for directory tests..
#                ( wildcard hangs on JET batch-nnn machines .. )
#
#    17Dec2008   ludescher@pppl.gov
#                mere presence of a directory is not sufficient
#                it must contain libraries
#
#    10Oct2019   gwright@pppl.gov
#                Crudely cleanup machines obsolete 20 years ago (gbw)
#----------------------------------------------------------------------
#
LS = /bin/ls
ifndef LIBROOT
  ifdef _64
#    LIBROOT = /usr/local/lib64
#    ifneq ($(shell csh -fc "filetest -d $(LIBROOT)"),1)
    ifneq ("$(wildcard /usr/local/lib64/lib*)","")
        LIBROOT := /usr/local
    endif
  else
#    LIBROOT = /usr/local/lib
#    ifneq ($(shell csh -fc "filetest -d $(LIBROOT)"),1)
    ifneq ("$(wildcard /usr/local/lib/lib*)","")
        LIBROOT := /usr/local
    else
#      LIBROOT = /opt/local
#      ifeq ($(shell csh -fc "filetest -d $(LIBROOT)"),0)
#       LIBROOT := /usr/local
      ifneq ("$(wildcard /opt/local/lib*)","")
         LIBROOT := /opt/local
      endif
    endif
  endif
endif

ifndef PREFIX
 ifdef NTCCHOME
   PREFIX=$(NTCCHOME)
   PREFIX0=$(NTCC_ROOT)
 else
  ifneq ("$(wildcard /usr/ntcc/*)","")
        PREFIX=/usr/ntcc
  endif
 PREFIX0=$(PREFIX)
 endif
else
 PREFIX0=$(PREFIX)
endif

ifdef PREFIX
 ifndef LIBDIR
  LIBDIR  = $(PREFIX)/lib
 endif
 ifndef BINDIR
  BINDIR  = $(PREFIX)/bin
 endif
 ifndef MODDIR
  MODDIR  = $(PREFIX)/mod
 endif
 ifndef ETCDIR
  ETCDIR  = $(PREFIX0)/etc
 endif
 ifndef INCLDIR
  INCLDIR = $(PREFIX0)/include
 endif
 ifndef MANDIR
 MANDIR  = $(PREFIX0)/man
 endif
endif

# Fortran
ifndef FFLAGS
   FFLAGS   = -c -O
   DFFLAGS  = -c -g
endif
STATIC=
DYNAMIC=
ifndef DEFS
  DEFS=
endif
ifndef CDEFS
  CDEFS=
endif
OUT = -o
DOUBLE = -r8
# C
CFLAGS=-c -O
DCFLAGS=-c -g
OPT=
CPP=gcc
CPPFLAGS=-E -P -x c
FPPCOM=
CXXFLAGS=-c
DCXXFLAGS=-c -g
# Include/Modules
IOPT=-I
MFLAGS=-I
MODEXT=mod
MFFLAGS= -c -w
#python3
PYTHON=python3
DPY=
ifdef FPREPROC_DEBUG
  DPY= -info
endif
#Elvis flags for elvislib, define LITTLE if the system is little endian
ifndef ELVIS_FLAGS
  ELVIS_FLAGS = -DLITTLE
endif
# Linking
LD=ld
ifndef LDFLAGS
        LDFLAGS=
endif
MVU=
# Libraries

ifndef MDSPLUS_DIR
  ifneq ("$(wildcard /usr/local/mdsplus/lib/*)","")
       MDSPLUS_DIR = /usr/local/mdsplus
       MDSLIB = -L$(MDSPLUS_DIR)/lib -lMdsLib
  else
       ifndef MDSLIB
          MDSLIB=
       endif
  endif
else
   ifndef MDSLIB
     MDSLIB = -L$(MDSPLUS_DIR)/lib -lMdsLib
   endif
endif

ifdef NO_MDSPLUS
  MDSLIB=
endif

ifdef LIBROOT
ifndef USRLIB
  USRLIB = $(LIBROOT)/lib
  LUSRLIB = -L$(LIBROOT)/lib
endif
endif

INSTALL = install -m
ifndef RANLIB
  ifeq "$(findstring  ccs, $(shell which ar))" "ccs"
     RANLIB := ar -s
  else
     RANLIB := ar ts
  endif
endif

AR = ar -r
TSTLNK = test -f

# Determine OS Type
sysname = $(shell uname -s)
hardware = $(shell uname -m)

ifeq "$(findstring  64, $(hardware))" "64"
   _64=TRUE
endif


#===========
# LINUX
#============
ifeq ($(sysname),Linux)
    MACHINE=LINUX
    OS=LINUX
    TSTLNK = test -L
    OPT = -Df2cFortran
    CPP=/lib/cpp
    CXXFLAGS= -c
    CFLAGS := -c -O -w
    ifndef FLIBROOT
        ifdef F90HOME
            FLIBROOT = $(F90HOME)
        endif
    endif
# Check if  Portland
    ifdef Portland
        MKPGIFLAG=TRUE
        NO_DEF=TRUE
    endif

    ifeq "$(findstring g77,$(FC))" "g77"
        NO_DEF=TRUE
        FFLAGS = -c -O3
        DFFLAGS = -c -g
    endif
    ifeq "$(findstring pgf90,$(FC))" "pgf90"
        MKPGIFLAG=TRUE
        NO_DEF=TRUE
    endif
    ifeq "$(findstring pgf95,$(FC))" "pgf95"
        MKPGIFLAG=TRUE
       NO_DEF=TRUE
    endif

    ifdef FORTRAN_VARIANT
        FVAR=TRUE
        NO_DEF=TRUE
        ifeq ("${FORTRAN_VARIANT}","Portland")
            MKPGIFLAG=TRUE
            ifeq ("${FORTRAN_TYPE}","90")
                FC=pgf90
            endif
        endif
        ifeq ("${FORTRAN_VARIANT}","Intel")
            MKINTEL=TRUE
        endif
        ifeq ("${FORTRAN_VARIANT}","GCC")
            MKGCC=TRUE
        endif
        ifeq ("${FORTRAN_VARIANT}","Cray")
            MKCRAY=TRUE
        endif
    endif
    ifdef AR_TIMESTAMP
        AR = ar rU
    endif
endif
#==============
# End LINUX
#==============

#===========
# Portland
#===========
  ifdef MKPGIFLAG
    ifneq ("$(wildcard $(PREFIX)/pgf90/*)","")
        LIBDIR  = $(PREFIX)/pgf90/lib
        MODDIR  = $(PREFIX)/pgf90/mod
    endif
    ifneq ("$(wildcard $(LIBROOT)/pgf90/*)","")
        FLIBROOT= $(LIBROOT)/pgf90
    endif
    ifeq "$(origin FC)" "default"
      FC = pgf95
    endif
    FC90=$(FC)
    ifeq "$(origin CC)" "default"
      CC = gcc
    endif
    DEFS := $(DEFS) -D__PGF90
    FFLAGS = -c -O -Mnoupcase -Mdalign -Mdefaultunit -Ktrap=fp -tp=x64
    DFFLAGS =  -c -g -Mnoupcase -Mdalign -Mdefaultunit -Minform=inform -Ktrap=fp -tp=x64 -Mbounds
    MPI_FC:=$(FC)
    MPI_DEFS:=$(DEFS)
MPI_FFLAGS:=$(FFLAGS)
MPI_DFFLAGS:=$(DFFLAGS)
MPI_CC:=$(CC)
MPI_CFLAGS:=$(CFLAGS)
MPI_DCFLAGS  := $(DCFLAGS)
MPI_CXX      := $(CXX)
MPI_CXXFLAGS := $(CXXFLAGS)
MPI_CDEFS    := $(CDEFS)
MPI_LDFLAGS  := $(LDFLAGS)

    ifndef FORTLIBS
     ifdef F90HOME
#       FORTLIBS = -L$(F90HOME)/lib -lpgf90 -lpgf90_rpm1 -lpgf902 -lpgf90rtl -lpgftnrtl -lm -lpgc -lpgsse1 -lpgsse2 $(F90HOME)/lib/gdix86.so
       F_MAIN=$(F90HOME)/lib/f90main.o
# For pgf 7.2
       FORTLIBS = -L$(F90HOME)/lib -lpgf90 -lpgf90_rpm1 -lpgf902 -lpgf90rtl -lpgftnrtl -lm -lpgc -lrt -lstd -lC
      else
        FORTLIBS = -L/usr/pgi/linux86/lib -lpgf90 -lpgf90_rpm1 -lpgf902 -lpgf90rtl -lpgftnrtl -lm -lpgc
     endif
    endif

    ifndef CLIBS
       ifdef GCC_PATH
         CLIBS = -L$(GCC_PATH)/lib -lstdc++
       else
#        CLIBS = -L/usr/lib/gcc-lib/i386-redhat-linux/egcs-2.96 -lgcc -lstdc++
#  if using pgi C
         CLIBS = -lstd -lpgc -lC
#         CLIBS = -L/usr/local/gcc32/lib -lstdc++
       endif
    endif
    USEFC=Y
    SHLIBS = $(FORTLIBS)
    LD=pgf90
    X_CFLAGS = -fPIC
    X_FFLAGS = -fPIC
    X_LDFLAGS = -shared
#    XFLIB=-lpgftnrtl
#    XFLIB= -L$(F90HOME)/lib -lpgc -lpgsse1 -lpgsse2
  endif
# End Portland
#===============


#================
# GFORTRAN / GCC
#================
  ifdef MKGCC
    USEFC=Y
    ifeq "$(origin FC)" "default"
      ifdef FORTRAN_COMMAND
        FC = ${FORTRAN_COMMAND}
      else
        FC=gfortran
      endif
    endif
    FC90=$(FC)
    FIXEDE = -ffixed-line-length-132
    DYNAMIC=
    MFLAGS= -I
    MODEXT=mod
    ifeq "$(origin CC)" "default"
      ifdef CC_COMMAND
        CC = ${CC_COMMAND}
      else
        CC=gcc
      endif
    endif
    ifeq "$(origin CXX)" "default"
      ifdef CX_COMMAND
        CXX = ${CX_COMMAND}
      else
        CXX=g++
      endif
    endif
    ifdef _64
      Bit=64
      FFLAGS = -c -O -m64 -ffpe-trap=invalid,zero,overflow -fno-range-check -fdollar-ok -cpp
      DFFLAGS = -c -g -m64 -ffpe-trap=invalid,zero,overflow -fno-range-check -fdollar-ok -cpp
      CFLAGS = -c -O -m64
      CXXFLAGS =  -c -O -m64
    else
      FFLAGS = -c -O -ffpe-trap=invalid,zero,overflow -fno-range-check -fdollar-ok -cpp
      DFFLAGS = -c -g -ffpe-trap=invalid,zero,overflow -fno-range-check -fdollar-ok -cpp
      CFLAGS = -c -O
      CXXFLAGS =  -c -O
    endif
    ifndef FORTLIBS
      FORTLIBS=-L$(F90HOME)/lib${Bit} -L/lib${Bit} -L/usr/lib${Bit} -lgfortran -lm
    endif
    FDOUBLE = -fdefault-real-8 -fdefault-double-8

    ifeq ($(sysname),Darwin)
       FFLAGS := $(FFLAGS) -fno-common
       DFFLAGS := $(DFFLAGS) -fno-common
       CFLAGS := $(CFLAGS) -fno-common -Wno-return-type
       CXXFLAGS := $(CXXFLAGS) -fno-common -Wno-return-type
    endif
# to build shared libraries
    X_LDFLAGS = -shared
    X_FFLAGS = -fPIC
    X_CFLAGS = -fPIC

    MPI_FC  := mpif90
    MPI_CC  := mpicc
    MPI_CXX := mpic++
    MPI_DEFS:= $(DEFS)
MPI_FFLAGS:=$(FFLAGS)
MPI_DFFLAGS:=$(DFFLAGS)
MPI_CFLAGS:=$(CFLAGS)
MPI_DCFLAGS  := $(DCFLAGS)
MPI_CXXFLAGS := $(CXXFLAGS)
MPI_CDEFS    := $(CDEFS)
MPI_LDFLAGS  := $(LDFLAGS)
    CLIBS = -lc -lstdc++
    ifndef F_MAIN
       ifdef GCC4_HOME
         F_MAIN = -L/usr/pppl/gcc/4.4.1-x86-64/lib/gcc/x86_64-unknown-linux-gnu/4.4.1 -lgfortranbegin
       else
         F_MAIN = -lgfortranbegin -lfrtbegin
       endif
    endif
    ifdef GLOBUS_LOCATION
       ifndef GCC4_HOME
         MDSLIB = -L$(MDSPLUS_DIR)/lib -lMdsLib -L$(GLOBUS_LOCATION)/lib -lglobus_xio_gcc64
       endif
    endif
    ifndef BLAS
       ifdef BLASHOME
         BLAS = -L$(BLASHOME) -lblas
       endif
    endif
    ifndef LAPACK
       ifdef LAPACKHOME
         LAPACK = -L$(LAPACKHOME) -llapack
       endif
    endif
  endif

# End Gfortran /GCC
#=====================


#==========
# Intel
#==========
  ifdef MKINTEL
    ifeq "$(origin FC)" "default"
      FC=ifort
    endif
    FC90=$(FC)
    ifdef SuSE
      MPI_LDFLAGS =  -lmpi
      ifdef F90HOME
        ifeq "$(findstring 10.,$(F90HOME))" "10."
           MKL10 = 1
        else
          MKL11 = 1
        endif
      else
        MKL11 = 1
      endif
    endif
    ifdef RHEL6
      MKL11 = 1
    endif
    ifdef RHEL7
      MKL11 = 1
    endif
    ifdef CENTOS6
      MKL11 = 1
    endif
    ifdef CENTOS7
      MKL11 = 1
    endif

# 06/11/2013 CLF: remove  -heap-arrays 20 : it does not work with newer Intel
#    FFLAGS = -c -O -nowarn -ftz -heap-arrays 20 -auto-scalar -traceback -align dcommons
    FFLAGS = -c -O -nowarn -ftz -auto-scalar -traceback -align dcommons
    DFFLAGS = -c -g -w -check -fpe0 -auto-scalar -traceback -align dcommons
    MPI_FFLAGS =  -c -O -nowarn  -ftz -traceback -align dcommons -auto-scalar
    MPI_DFFLAGS = -c -g -check -fpe0 -traceback -align dcommons -auto-scalar
    FDOUBLE = -r8
    FIXEDE = -132
    DYNAMIC=-auto
    MFLAGS= -I
    MODEXT=mod
    ifndef FLIBROOT
      ifdef F90HOME
        FLIBROOT = $(F90HOME)
      else
        ifneq ("$(wildcard $(LIBROOT)/intel-9.0/*)","")
          IFC_V=intel-9.0
        endif
        FLIBROOT = $(LIBROOT)/$(IFC_V)
      endif
    endif
    ifndef FORTLIBS
       ifdef _64
           ifneq ("$(wildcard $(FLIBROOT)/libifport.*)","")
              FDIR =  $(FLIBROOT)
           else
              FDIR =  $(FLIBROOT)/lib
           endif
#          FORTLIBS = -L$(FLIBROOT)/lib  -lifport -lintlc -limf -lifcore -ldl -lsvml -lm -lirc
          FORTLIBS = -L$(FDIR) -lifport -lintlc -limf -lifcore -ldl -lm -lirc
       else
#         FORTLIBS = -L$(FLIBROOT)/lib -lifport -lifcore -lcprts -lunwind -lm -lcxa -lirc
# rga, the above caused preact_init to segfault on SGI
          FORTLIBS = -L$(FLIBROOT)/lib -lifport -limf -lifcore -lunwind -lm -lirc -lcxaguard -lipr
       endif
    endif
    ifdef INTEL_CXXLIB_ICC
       CC = icc
       CXX= icpc
       CFLAGS     = -c -O -w -mp
       MPI_CFLAGS = -c -O -w -mp
       DCFLAGS     = -c -g -w -mp
       MPI_DCFLAGS = -c -g -w -mp
#       CFLAGS = -c -O -w -mp -cxxlib-icc
#       CXXFLAGS =  -c -O -mp -w  -cxxlib-icc
       CXXFLAGS     =  -c -O -mp -w
       DCXXFLAGS     = -c -g -mp -w
       MPI_CXXFLAGS =  -c -O -mp -w
       MPI_DCXXFLAGS = -c -g -mp -w
       ifdef MKL11
         MPI_FC = mpif90
         MPI_CC = mpicc
         MPI_CXX = mpicxx
         CLIBS =
         XLIBS = -lc
         XFLIB = -L$(F90HOME)/lib/intel64 -lifcore -lifport
#         F_MAIN = $(CCHOME)/lib/intel64/for_main.o
         F_MAIN = $(FDIR)/for_main.o
       else
         CLIBS = -L$(CCHOME)/lib -lcprts -lcxa
         XLIBS = -L$(CCHOME)/lib -lcprts -lcxa
         F_MAIN = $(F90HOME)/lib/for_main.o
       endif
    else
       ifdef INTEL_CC
         ifndef GCC_VERSION
           GCC_VERSION=410
         endif
         CC = icc
         CXX= icpc
         CFLAGS        = -c -O -w -mp -gcc-version=$(GCC_VERSION)
         DCFLAGS       = -c -g -w -mp -gcc-version=$(GCC_VERSION)
         CXXFLAGS      = -c -O -w -mp -gcc-version=$(GCC_VERSION)
         DCXXFLAGS     = -c -g -w -mp -gcc-version=$(GCC_VERSION)
         MPI_CFLAGS    = -c -O -w -mp -gcc-version=$(GCC_VERSION)
         MPI_DCFLAGS   = -c -g -w -mp -gcc-version=$(GCC_VERSION)
         MPI_CXXFLAGS  = -c -O -w -mp -gcc-version=$(GCC_VERSION)
         MPI_DCXXFLAGS = -c -g -w -mp -gcc-version=$(GCC_VERSION)
        ifdef SuSE
          ifdef MKL11
             MPI_FC:=mpiifort
             MPI_CC:=mpiicc
             MPI_CXX:=mpiicpc
             F_MAIN = $(FLIBROOT)/for_main.o
          else
             F_MAIN = $(F90HOME)/lib/for_main.o
          endif
        else
          F_MAIN = $(F90HOME)/lib/for_main.o
        endif

       else
         CC=gcc
         CXX=g++
         CFLAGS     = -c -O2 -m64 -ansi
         MPI_CFLAGS = -c -O2 -m64 -ansi
         DCFLAGS     = -c -g -m64 -ansi
         MPI_DCFLAGS = -c -g -m64 -ansi
         CXXFLAGS     =  -c -m64
         MPI_CXXFLAGS =  -c -m64
         DCXXFLAGS     =  -c -g -m64
         MPI_DCXXFLAGS =  -c -g -m64
         F_MAIN = ${FLIBROOT}/for_main.o
       endif
    endif
    ifndef MPI_FC
      MPI_FC:=$(FC)
      MPI_CC:=$(CC)
      MPI_CXX:=$(CXX)
    endif
    ifndef CLIBS
       ifndef SuSE
         CLIBS = -L/usr/lib/gcc-lib/ia64-redhat-linux/3.2.3 -lstdc++
       else
         ifdef _64
            CLIBS = -L/usr/lib64/gcc/x86_64-suse-linux/4.3 -lstdc++
         else
            CLIBS = -L/usr/lib/gcc/ia64-suse-linux/4.1.2 -lstdc++
         endif
         ifdef CCHOME
            CLIBS := $(CLIBS) -L$(CCHOME)/lib -lipr
         endif
         NO_TERMCAP = 1
       endif
    endif

    ifndef BLAS
       ifdef BLASHOME
          ifdef MKL11
            ifeq "$(findstring em64t,$(BLASHOME))" "em64t"
               BLAS = -L$(BLASHOME) -lmkl_intel_lp64
            else
               BLAS = -L$(BLASHOME) -lacml
            endif
          else
            BLAS = -L$(BLASHOME) -lmkl_lapack -lmkl_ipf -lguide -lpthread
          endif
       else
          BLAS = -L/usr/local/mkl61/lib/64 -lguide -lmkl_lapack -lmkl_ipf
#         BLAS = -lblas -L/usr/lib/gcc-lib/ia64-redhat-linux/3.2.3 -lg2c
       endif
    endif
    ifndef LAPACK
       ifdef LAPACKHOME
         ifdef MKL11
           ifeq "$(findstring em64t,$(LAPACKHOME))" "em64t"
              LAPACK = -L$(LAPACKHOME) -lmkl_lapack -lmkl_sequential -lmkl_core
           else
              LAPACK = -L$(LAPACKHOME) -lacml
           endif
         else
           LAPACK = -L$(LAPACKHOME) -lmkl_lapack -lmkl_ipf -lguide -lpthread
         endif
       else
         LAPACK = -lmkl_lapack -lmkl_ipf -lguide -lpthread
       endif
    endif
    ifdef GLOBUS_LOCATION
       ifdef RHEL6
         MDSLIB = -L$(MDSPLUS_DIR)/lib -lMdsLib
       else
          ifdef CENTOS6
            MDSLIB = -L$(MDSPLUS_DIR)/lib -lMdsLib
          else
             ifdef CENTOS7
               MDSLIB = -L$(MDSPLUS_DIR)/lib -lMdsLib
             else
                MDSLIB = -L$(MDSPLUS_DIR)/lib -lMdsLib -L$(GLOBUS_LOCATION)/lib -lglobus_xio_gcc64
#           MDSLIB = -L$(MDSPLUS_DIR)/lib -lMdsLib -lRoam -L$(GLOBUS_LOCATION)/lib -lglobus_xio_gcc64
             endif
          endif
       endif
    endif
    LD=$(FC90)
    X_LDFLAGS = -shared
    X_FFLAGS = -fPIC
    X_CFLAGS = -fPIC
    RPATH= -rpath $(LIBDIR)
  endif
#
# End Intel
#=============


#=============================
# Handle Libraries / Includes
#==============================


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

# C++
ifndef CXX
  CXX=g++
endif


ifndef BLAS
ifndef SUNPERF
  BLAS   = -lblas
endif
endif

ifndef LAPACK
ifndef SUNPERF
        LAPACK = -llapack
endif
endif

ifndef FFTW
        FFTW = -lfftw
endif

#       OBJ=$(shell pwd)/$(MACHINE)

OBJ=../$(MACHINE)
OBJDIR = $(OBJ)/obj
ifndef MDIR
  MDIR = $(OBJ)/mod
endif

MFFLAGS = $(FFLAGS)

# if $(PREFIX)/lib exists,
# define LLOC to be added when linking
ifneq ("$(wildcard $(LIBDIR)/libportlib.a)","")
        LLOC=-L$(LIBDIR)
endif

#include directories
ifneq ("$(wildcard ../incl_cpp/*)","")
        INCFLAGS := $(IOPT)../incl_cpp
endif
ifneq ("$(wildcard ../inclshare/*)","")
        INCFLAGS := $(INCFLAGS) $(IOPT)../inclshare
endif

ifneq ("$(wildcard ../include/*)","")
        INCFLAGS := $(IOPT)../include
endif

ifneq ("$(wildcard ../include/cpp/*)","")
        INCFLAGS := $(INCFLAGS) $(IOPT)../include/cpp
endif

DEFS  := $(DEFS) -D__F90 -D__$(OS) -D__UNIX
CDEFS := $(CDEFS) -D__$(OS)

ifdef MPI_ACTIVATE
   DEFS  := $(DEFS) -D__MPI
   CDEFS := $(CDEFS) -D__MPI
endif
ifdef NO_MDSPLUS
   DEFS  := $(DEFS) -D__NOMDSPLUS
   CDEFS := $(CDEFS) -D__NOMDSPLUS
endif

#include for C code
ifdef LIBROOT
  CINCL = $(IOPT)$(LIBROOT)/include $(IOPT)../include
else
  CINCL = $(IOPT)../include
endif

ifdef INCLDIR
ifneq ("$(wildcard $(INCLDIR)/*)","")
        INCFLAGS := $(INCFLAGS) $(IOPT)$(INCLDIR)
        CINCL := $(CINCL) $(IOPT)$(INCLDIR)
endif
endif

#include directory for f90 modules
        MODFLAGS := $(MFLAGS)$(MDIR)
# if some modules (e.g. ezcdf) were already installed, need to add also
ifdef MODDIR
  ifneq ("$(wildcard $(MODDIR)/*)","")
        MODFLAGS := $(MODFLAGS) $(MFLAGS)$(MODDIR)
  endif
endif

# define location of Python preprocessor
PYDIR = ../fpreproc
ifneq ("$(wildcard $(ETCDIR)/fppfile.py)","")
        PYDIR=$(ETCDIR)
else
# for TRANSP in $(CODESYSDIR)/source/bpython
   ifneq ("$(wildcard $(CODESYSDIR)/source/bpython/fppfile.py)","")
      PYDIR=$(CODESYSDIR)/source/bpython
   endif
endif
PREFPP = $(PYTHON) $(PYDIR)/fppfile.py

# check if gnu command line editing libs exist

ifndef NO_EDITLIBS
 ifndef TERMCAP
   ifndef NO_TERMCAP
     TERMCAP = -ltermcap
   endif
 endif
 ifndef EDITLIBS
  ifdef _64
   ifneq ("$(wildcard /usr/lib64/libreadline*)","")
        EDITLIBS = -L/usr/lib64 -lreadline -lhistory $(TERMCAP)
   else
     ifneq ("$(wildcard /usr/lib/libreadline*)","")
        EDITLIBS = -L/usr/lib -lreadline -lhistory $(TERMCAP)
     endif
   endif
  else
   ifneq ("$(wildcard $(USRLIB)/libreadline*)","")
        EDITLIBS = -lreadline -lhistory $(TERMCAP)
   else
     ifneq ("$(wildcard /usr/lib/libreadline*)","")
        EDITLIBS = -lreadline -lhistory $(TERMCAP)
     else
        ifneq ("$(wildcard /usr/ccs/lib/libtermcap*)","")
           EDITLIBS = -L/usr/local/lib -lreadline -lhistory -L/usr/ccs/lib -ltermcap
        endif
     endif
   endif
  endif
 endif
endif

#check if MDSplus exists
ifeq ("$(wildcard $(MDSPLUS_DIR)/lib/*)","")
        MDSLIB=
endif


ifndef MODULESHOME
   ifdef FLIBROOT
     ifdef LIBROOT
        LUSRLIB  = -L$(FLIBROOT)/lib -L$(LIBROOT)/lib
     endif
   endif
endif
#
# End Libraries / Includes
#===========================
#
#===================================================
# Define Function to pre-process/compile c code
#===================================================
# result is %.o
define c_proc
      $(CC) $(CFLAGS) -I./ $(INCFLAGS) $(CDEFS) -o $(OBJDIR)/$*.o $<
endef
define cxx_proc
      $(CXX) $(CXXFLAGS) -I./ $(INCFLAGS) $(CDEFS) -o $(OBJDIR)/$*.o $<
endef
define rm_f
   @if test -z "$(DEBUG)"; then \
     rm -f $(OBJDIR)/$*.f*; fi
   $(mv_mod)
endef

#            --------------------------------------------------
#===================================================
# Functions for Fortran pre/processing using python
#===================================================
# USE for ARCHIVES

# define function to pre-process/compile fixed length fortran 90 code
define F_proc
        @$(PREFPP) $< $(OBJDIR)/$*.f $(INCFLAGS) $(DEFS) $(DPY) $(FPPCOM)
        @$(FC) $(FFLAGS) $(MODFLAGS) -I./ $(INCFLAGS) $(FIXED) $(OUT) $(OBJDIR)/$*.o $(OBJDIR)/$*.f
        $(rm_f)
endef


# define function to pre-process/compile free form fortran 90 code
define F90_proc
        @$(PREFPP) $< $(OBJDIR)/$*.f90 $(INCFLAGS) $(DEFS) -free $(DPY) $(FPPCOM)
        @$(FC90) $(FFLAGS) $(MODFLAGS) -I./ $(INCFLAGS) $(OUT) $(OBJDIR)/$*.o $(OBJDIR)/$*.f90
$(rm_f)
endef

#                    -----------------------------
#
# functions for fortran pre/processing using python
#==================================================
# Note: these are to support TRANSP conventions
#       all code is pre-processed
# USE for executable test programs - when  $* contains $OBJECTDIR

#Fixed Form Fortran 90
define proc_F
$(PREFPP) $< $*.f $(INCFLAGS) $(DEFS) $(DPY) $(FPPCOM)
$(FC) $(FFLAGS) $(MODFLAGS) -I./ $(INCFLAGS) $(FIXED) $(OUT) $*.o $*.f
$(rm_f)
endef

# Free Form Fortran 90
define proc_F90
$(PREFPP) $< $*.f90 $(INCFLAGS) $(DEFS) -free $(DPY) $(FPPCOM)
$(FC) $(FFLAGS) $(MODFLAGS) -I./ $(INCFLAGS) $(OUT) $*.o $*.f90
$(rm_f)
endef


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

# processing of f90 modules
#==========================

# move f90 modules to "$MDIR" dir
# Note:
# mv $*.$(MODEXT) $(MDIR)
# does not work, since module may have different name than file
# Also: module files are always in CWD

#ifdef MVU
#define mv_mod
#   mv $(shell  echo $(*F) | tr 'a-z' 'A-Z').$(MODEXT) $(MDIR)/
#endef
#endif

define mv_mod
   @list=`/bin/ls *.$(MODEXT) 2> /dev/null`; \
   for i in  $$list; do \
     mv $$i $(MDIR)/; done
endef

define rm_Warn
   @cat /tmp/ntcc_ld.out |sed -e '/Warning: alignment/d'
   @rm -f /tmp/ntcc_ld.out
endef

#--------------------------------------------------------------------
# ARCHIVES
#==========

define make_ar
        @$(AR) $(ARC)  $(OBJDIR)/$*.o
        @$(mv_mod)
        @if test -z "$(__X1)"; then \
        rm -f $(OBJDIR)/$*.o; fi
endef

-include ../share/Make.overwrite
-include share/Make.overwrite
