#
# Make.defs $Id: Make.defs,v 1.64 2003/03/14 23:44:57 marc Exp $
#

#
# Establish some defaults, assuming that they are not already set by the
# Makefile that includes this file.
#

#
# To destinguish between YMP or C90 ...
#
# Don't make the distinction for now, just use CRAY
#
UNAMEM := $(shell uname -m)
UNAMEN := $(shell uname -n)
ifeq ($(findstring CRAY, $(UNAMEM)), CRAY)
ifeq ($(findstring Y-MP, $(UNAMEM)), Y-MP)
    MACHINE := CRAY
endif
ifeq ($(findstring C90, $(UNAMEM)), C90)
    MACHINE := CRAY
endif
ifeq ($(findstring J90, $(UNAMEM)), J90)
    MACHINE := CRAY
endif
ifeq ($(findstring T3E, $(UNAMEM)), T3E)
    MACHINE := T3E
ifeq ($(findstring mcurie, $(UNAMEN)), mcurie)
    WHICHT3E := NERSC
endif
# currently both nersc t3es have the same home path names
ifeq ($(findstring pierre, $(UNAMEN)), pierre)
    WHICHT3E := NERSC
endif
ifeq ($(findstring seymour, $(UNAMEN)), seymour)
    WHICHT3E := NAVO
endif
ifeq ($(findstring yukon, $(UNAMEN)), yukon)
    WHICHT3E := ARSC
endif
endif
endif

UNAMES := $(shell uname -s)
ifeq ($(findstring CYGWIN_NT, $(UNAMES)), CYGWIN_NT)
    MACHINE := CYGWIN_NT
endif

ifndef MACHINE
    MACHINE := $(shell uname)
endif

#
# We'd also like to distinguish between SunOS and Solaris.
#
UNAMER := $(shell uname -r)
ifeq ($(MACHINE), SunOS)
    ifeq ($(shell awk 'BEGIN {print "$(UNAMER)"; print "5.0"}' /dev/null | \
            sort | head -1), 5.0)
        MACHINE := Solaris
    endif
endif
#
# Set special flag for escher.nersc.gov
#
ifeq ($(MACHINE), IRIX64)
  WHICHIRIX64 := GENERICSGI
  ifeq ($(findstring escher, $(UNAMEN)), escher)
    WHICHIRIX64 := ESCHER
  endif
  ifeq ($(findstring mahler, $(UNAMEN)), mahler)
    WHICHIRIX64 := MAHLER
  endif
endif

#
# Set special flag for the Linux cluster (n2001.lbl.gov)
#
ifeq ($(MACHINE), Linux)
  WHICHLINUX := GENERICLINUX
  ifeq ($(findstring n2001, $(UNAMEN)), n2001)
    WHICHLINUX := PCCLUSTER
  endif
  ifeq ($(findstring xfe1, $(UNAMEN)), xfe1)
    WHICHLINUX := ALPHACLUSTER
  endif
endif

ifeq ($(MACHINE), HP-UX)
    #
    # `-' is not a valid character in a preprocessing token.
    # That is to say, -DBL_HP-UX is not, strictly speaking, valid.
    #
    MACHINE := HPUX
endif

ifndef PRECISION
  PRECISION	= DOUBLE
endif

ifndef DEBUG
  DEBUG	= TRUE
endif

ifndef PROFILE
  PROFILE	= FALSE
endif

ifndef DIM
  DIM		= 2
endif

ifndef COMP
  COMP	= CC
endif

ifndef FCOMP
  FCOMP  = f77
endif

ifndef USE_THREADS
  USE_THREADS  = FALSE
endif

ifndef USE_MPI
  USE_MPI  = FALSE
endif

ifndef STRICTLY
  STRICTLY    = FALSE
endif

#
# Set some reasonable defaults.
#
CXX = CC
FC  = f77
CC  = cc
#
# Defaults for BoxLib style programs.
#
ARCH		= IEEE
FORTLINK  	= UNDERSCORE

FORTPREP  	= perl $(PBOXLIB_HOME)/scripts/strip72 -c
MKDEP		= perl $(PBOXLIB_HOME)/scripts/mkdep
INSTALL		= sh   $(PBOXLIB_HOME)/scripts/install-sh -c
#
# SunOS ranlib has a problem if the library is installed 444 :-(
#
ifeq ($(MACHINE), SunOS)
INSTALL_DATA 	= $(INSTALL) -m 0644
else
INSTALL_DATA 	= $(INSTALL) -m 0444
endif
#
# We assume that INSTALL_ROOT is either set on the command line,
# set in the environment, or set by a controlling GNUmakefile.
# It is where the stuff is to be installed. For the guthamr stuff
# proper, we set this to GUTHAMR_HOME.  If this isn't set,
# we set it to where guthamr was installed.
#
ifndef INSTALL_ROOT
    INSTALL_ROOT = $(GUTHAMR_HOME)
endif

ifndef INSTALL_PREFIX
    INSTALL_PREFIX = $(INSTALL_ROOT)
endif

RANLIB = true

EXE_OUTPUT_OPTION      = -o $(objEXETempDir)/$*.o
LIB_OUTPUT_OPTION      = -o $(objTempDir)/$*.o
FORT_EXE_OUTPUT_OPTION = -o $(objEXETempDir)/$*.o
FORT_LIB_OUTPUT_OPTION = -o $(objTempDir)/$*.o
#
# Generic setup for using g++ ...
#
ifeq ($(COMP),g++)
    CXXPRFF    := -p
    CXXOPTF    += -O
    CXXDEBF    += -g
    CXXOPTF    += -g
    FULLWARN   := -Wall 
    FULLWARN   += -Wno-sign-compare
    CXX	       := g++ -ftemplate-depth-25
    __gcc_version := $(shell $(CXX) -dumpversion | head -1 | sed -e 's;.*  *;;')
    __gcc_major_version := $(shell $(CXX) -dumpversion | head -1 | sed -e 's;.*  *;;' | sed -e 's;\..*;;')
    __gcc_minor_version := $(shell $(CXX) -dumpversion | head -1 | sed -e 's;.*  *;;' | sed -e 's;[^.]*\.;;' | sed -e 's;\..*;;')
    DEFINES += -DBL_GCC_VERSION='$(__gcc_version)'
    DEFINES += -DBL_GCC_MAJOR_VERSION=$(__gcc_major_version)
    DEFINES += -DBL_GCC_MINOR_VERSION=$(__gcc_minor_version)
    ifneq ($(__gcc_major_version),3)
       GNU_STD_INCL = $(PBOXLIB_HOME)/BoxLib/std
    endif
endif
ifeq ($(FCOMP),g77)
    FC := g77
    FOPTF += -O
    fOPTF += -O
    FDEBF += -g
    fDEBF += -g
endif
ifeq ($(COMP),Intel)
   CXX := icc
   CXXOPTF += -g
   CXXOPTF += -O2
   # CXXOPTF += -axK
   CXXDEBF += -g
   __icc_version := $(shell $(CXX) -V 2>&1 1>/dev/null | grep Version)
   ifeq ($(findstring Version 5.0.1, $(__icc_version)), Version 5.0.1)
       ICC_VERSION := 5.0.1
       ICC_MAJOR_VERSION := 5
       ICC_MINOR_VERSION := 0
   else
   ifeq ($(findstring Version 6.0, $(__icc_version)), Version 6.0)
       ICC_VERSION := 6.0
       ICC_MAJOR_VERSION := 6
       ICC_MINOR_VERSION := 0
   else
   ifeq ($(findstring Version 7.0, $(__icc_version)), Version 7.0)
       ICC_VERSION := 7.0
       ICC_MAJOR_VERSION := 7
       ICC_MINOR_VERSION := 0
   endif
   endif
   endif
   DEFINES += -DBL_ICC_VERSION=$(ICC_VERSION)
   DEFINES += -DBL_ICC_MAJOR_VERSION=$(ICC_MAJOR_VERSION)
   DEFINES += -DBL_ICC_MINOR_VERSION=$(ICC_MINOR_VERSION)
endif
ifeq ($(FCOMP),Intel)
   FC := ifc
   fC := $(FC)
   FDEBF += -g
   fDEBF += -g
   FOPTF += -g
   fOPTF += -g
   FOPTF += -O2
   # FOPTF += -axK
   fOPTF += -O2
   # fOPTF += -axK
   fDEBF += -w
   fOPTF += -w
   ifneq ($(DIM),3)
     FDEBF += -CB -CU -CA
     fDEBF += -CB -CU -CA
   endif
   __ifc_version := $(shell $(FC) -V 2>&1 1>/dev/null | grep Version)
   override XTRALIBS += -lintrins -lIEPCF90 -lF90 -limf -lcprts -lcxa -lirc
   ifeq ($(findstring Version 5.0.1, $(__ifc_version)), Version 5.0.1)
       IFC_VERSION := 5.0.1
       IFC_MAJOR_VERSION := 5
       IFC_MINOR_VERSION := 0
       LIBRARY_LOCATIONS += $(IA32ROOT)/lib 
       override XTRALIBS += $(IA32ROOT)/lib/icrt.link
   else
   ifeq ($(findstring Version 6.0, $(__ifc_version)), Version 6.0)
       IFC_VERSION := 6.0
       IFC_MAJOR_VERSION := 6
       IFC_MINOR_VERSION := 0
       LIBRARY_LOCATIONS += /opt/intel/compiler60/ia32/lib 
   else
   ifeq ($(findstring Version 7.0, $(__ifc_version)), Version 7.0)
       IFC_VERSION := 7.0
       IFC_MAJOR_VERSION := 7
       IFC_MINOR_VERSION := 0
       LIBRARY_LOCATIONS += /opt/intel/compiler70/ia32/lib 
   endif
   endif
   endif
   ifeq ($(USE_THREADS),TRUE)
     override XTRALIBS += -lpthread
   endif
endif
#
# Generic setup for using cfront ...
#
ifeq ($(COMP),CFRONT)
    CLOSELIB    := TRUE
    ptRepDir	 = $(objTempDir)/ptrepository
    CXXLIBFLAGS += -ptr$(ptRepDir)
endif
#
# We can do library closure with KCC 3.x.
#
ifeq ($(findstring KCC, $(COMP)),KCC)
    __kcc_version := $(shell					   \
		echo 'int i;' > _X.cpp;				   \
		KCC -c -v _X.cpp 2>&1 1>/dev/null | fgrep 'KAI C' | head -1; \
		/bin/rm -f _X.cpp _X.o)
#DEFINES += -D__kcc_version='$(__kcc_version)'
#
# init null values (for brainerd)
#
    KCC_VERSION:=0.0
    KCC_MAJOR_VERSION:=0
    KCC_MINOR_VERSION:=0
ifeq ($(findstring KAI C++ 3.1, $(__kcc_version)), KAI C++ 3.1)
    KCC_VERSION:=3.1
    KCC_MAJOR_VERSION:=3
    KCC_MINOR_VERSION:=1
    CLOSELIB := TRUE
endif
ifeq ($(findstring KAI C++ 3.2, $(__kcc_version)), KAI C++ 3.2)
    KCC_VERSION:=3.2
    KCC_MINOR_VERSION:=2
    CLOSELIB := TRUE
endif
ifeq ($(findstring KAI C++ 3.3, $(__kcc_version)), KAI C++ 3.3)
    KCC_VERSION:=3.3
    KCC_MAJOR_VERSION:=3
    KCC_MINOR_VERSION:=3
    CLOSELIB := TRUE
    CXXLIBFLAGS = --one_per
endif
ifeq ($(findstring KAI C++ 3.4, $(__kcc_version)), KAI C++ 3.4)
    KCC_VERSION:=3.4
    KCC_MAJOR_VERSION:=3
    KCC_MINOR_VERSION:=4
    CLOSELIB := TRUE
    CXXLIBFLAGS = --one_per
endif
ifeq ($(findstring C++ 3.9, $(__kcc_version)), C++ 3.9)
    KCC_VERSION:=3.9
    KCC_MINOR_VERSION:=9
    KCC_MAJOR_VERSION:=3
    CLOSELIB:=TRUE
    CXXLIBFLAGS = --one_per
endif
ifeq ($(findstring KAI C++ 4.0, $(__kcc_version)), KAI C++ 4.0)
    KCC_VERSION:=4.0
    KCC_MINOR_VERSION:=0
    KCC_MAJOR_VERSION=4
    CLOSELIB := TRUE
    CXXLIBFLAGS = --one_per
endif
ifeq ($(findstring C++ 4.0, $(__kcc_version)), C++ 4.0)
    KCC_VERSION:=4.0
    KCC_MINOR_VERSION:=0
    KCC_MAJOR_VERSION:=4
    CLOSELIB:=TRUE
    CXXLIBFLAGS = --one_per
endif

    DEFINES += -DBL_KCC_VERSION=$(KCC_VERSION)
    DEFINES += -DBL_KCC_MAJOR_VERSION=$(KCC_MAJOR_VERSION)
    DEFINES += -DBL_KCC_MINOR_VERSION=$(KCC_MINOR_VERSION)
endif

# Fortran Suffix
FCOMPSuffix := .$(FCOMP)

include $(PBOXLIB_HOME)/mk/Make.$(MACHINE)

ifeq ($(DEBUG),TRUE)
    FFLAGS	+= $(FDEBF)
    fFLAGS	+= $(fDEBF)
    CXXFLAGS	+= $(CXXDEBF)
    CFLAGS	+= $(CDEBF)
    DebugSuffix := .DEBUG
else
    FFLAGS	+= $(FOPTF)
    fFLAGS	+= $(fOPTF)
    CXXFLAGS	+= $(CXXOPTF)
    CFLAGS	+= $(COPTF)
    CPPFLAGS	+= -DNDEBUG
    DebugSuffix :=
endif

ifeq ($(GPROF),TRUE)
   FFLAGS += -pg
   CXXFLAGS += -pg
   fFLAGS += -pg
   CFLAGS += -pg
endif

ifeq ($(PROFILE),TRUE)
    CPPFLAGS    += -DBL_PROFILING
    ProfSuffix	:= .PROF
else
    ProfSuffix	:=
endif

ifeq ($(USE_MPI),TRUE)
    MPISuffix	:= .MPI
    CPPFLAGS	+= -DBL_USE_MPI
    include $(PBOXLIB_HOME)/mk/Make.mpi
else
    MPISuffix	:=
endif

ifeq ($(USE_THREADS),TRUE)
    DEFINES += -DBL_THREADS
    ifeq ($(COMP),KCC)
        CXXFLAGS+=--thread_safe
    endif
    ifeq ($(COMP),mpKCC)
        CXXFLAGS+=--thread_safe
    endif
    ifeq ($(COMP),g++)
        CPPFLAGS+=-pthread
    endif
    ThreadsSuffix := .TS
else
    ThreadsSuffix :=
endif

ifeq ($(STRICTLY),TRUE)
    ifeq ($(COMP),KCC)
        CXXFLAGS += --strict
        CPPFLAGS += -D__KAI_STRICT
        CXXFLAGS += --display_error_number
        CXXFLAGS +=  --diag_suppress 450
    endif
    ifeq ($(COMP),g++)
        CXXFLAGS += $(FULLWARN)
    endif
endif

ifeq ($(PRECISION),FLOAT)
ifeq ($(MACHINE),CRAY)
    PrecisionSuffix :=
else
    PrecisionSuffix := .$(PRECISION)
endif
else
    PrecisionSuffix :=
endif

CPlusPlusSourceFiles =
C89SourceFiles	=
F77SourceFiles	=
F90SourceFiles	=
f77SourceFiles	=

objForLibs	=	$(CLIB_sources:%.cpp=%.o)			\
			$(FLIB_sources:%.F=%.o)				\
			$(F90LIB_sources:%.F90=%.o)			\
			$(cLIB_sources:%.c=%.o)				\
			$(fLIB_sources:%.f=%.o)				\
			$(f90LIB_sources:%.f90=%.o)			\

objStandalone	= 	$(CEXE_sources:%.cpp=%.o)			\
			$(FEXE_sources:%.F=%.o)				\
			$(F90EXE_sources:%.F90=%.o)			\
			$(cEXE_sources:%.c=%.o)				\
			$(fEXE_sources:%.f=%.o)				\
			$(f90EXE_sources:%.f90=%.o)			\

objForExecs	= $(addprefix $(objEXETempDir)/, $(objStandalone))

instSources	= $(CLIB_headers) $(FLIB_headers) $(cLIB_headers) $(T_sources) $(T_headers) $(fLIB_headers) $(f90LIB_headers)

INCLUDE_LOCATIONS += . $(XTRAINCLOC) $(XINCL) $(GNU_STD_INCL)
LIBRARY_LOCATIONS += . $(XTRALIBLOC) $(XLIBL)

DEFINES		+= 	-DBL_SPACEDIM=$(DIM)				\
		  	-DBL_FORT_USE_$(FORTLINK)			\
		  	-DBL_$(MACHINE)					\
		  	$(XTRADEFS)

ifeq ($(PRECISION),FLOAT)
DEFINES += -DBL_USE_FLOAT
endif
ifeq ($(ARCH),CRAY)
DEFINES += -DBL_ARCH_CRAY
endif

includes	= $(addprefix -I, $(INCLUDE_LOCATIONS))
fincludes	= $(includes)

CPPFLAGS	+= $(DEFINES) $(includes)

libraries	= $(LIBRARIES) $(BL_MPI_LIBS) $(XTRALIBS)

LDFLAGS		+= $(addprefix -L, $(LIBRARY_LOCATIONS))

machineSuffix	= $(MACHINE).$(COMP)$(FCOMPSuffix)$(PrecisionSuffix)$(DebugSuffix)$(ProfSuffix)$(MPISuffix)$(ThreadsSuffix)
optionsSuffix	= $(DIM)d.$(machineSuffix)

optionsLib	= $(addprefix lib,					\
			$(addsuffix $(optionsSuffix).a, $(LBASE)))

instIncludeDir	= $(INSTALL_PREFIX)/include
instLibDir	= $(INSTALL_PREFIX)/lib/$(machineSuffix)
instBinDir	= $(INSTALL_PREFIX)/bin/$(machineSuffix)
instScriptDir	= $(INSTALL_PREFIX)/bin
instHtmlDir	= $(INSTALL_PREFIX)/html
instInfoDir	= $(INSTALL_PREFIX)/info
instMkDir	= $(INSTALL_PREFIX)/mk

executable	= $(addsuffix $(optionsSuffix).ex, $(EBASE))

depTempDir	= d/$(optionsSuffix)
depEXETempDir	= d/$(optionsSuffix).EXE
objTempDir	= o/$(optionsSuffix)
objEXETempDir   = o/$(optionsSuffix).EXE
f77TempDir	= f/$(optionsSuffix)

instLib		= $(instLibDir)/lib$(LBASE)$(DIM)d.a
#
# For source code mainipulation
#
CSRCALL		= $(CEXE_sources) $(CEXE_headers)			\
		  $(CLIB_sources) $(CLIB_headers) $(T_sources) $(T_headers)
FSRCALL		= $(FEXE_sources) $(FEXE_headers) $(FLIB_sources) 	\
                  $(FLIB_headers)
F90SRCALL	= $(F90EXE_sources) $(F90EXE_headers) $(F90LIB_sources) \
                  $(F90LIB_headers)
cSRCALL		= $(cEXE_sources) $(cEXE_headers) $(cLIB_sources) 	\
                  $(cLIB_headers)
fSRCALL		= $(fEXE_sources) $(fEXE_headers) $(fLIB_sources) 	\
                  $(fLIB_headers)
f90SRCALL	= $(f90EXE_sources) $(f90EXE_headers) $(f90LIB_sources) \
                  $(f90LIB_headers)
allSources	= $(CSRCALL) $(FSRCALL) $(F90SRCALL) $(cSRCALL) $(fSRCALL) $(f90SRCALL)


ifndef CLOSELIB
    CLOSELIB   := FALSE
endif

ifdef WARNINGS
    CXX += $(FULLWARN)
endif
