#! /bin/sh
#
#######################################################################
#
# Compile and link a Fortran file for NEC MPI.
#
# Version date : 12. May 2023
#
#----------------------------------------------------------------------

#
# Copyright (c) 1997-2023 NEC Corporation
#

#
#  (C) 1993 by Argonne National Laboratory and Mississipi State University.
#      All rights reserved.
#
#                                   COPYRIGHT
# 
# The following is a notice of limited availability of the code, and disclaimer
# which must be included in the prologue of the code and in all source listings
# of the code.
# 
# Copyright Notice
#  + 1993 University of Chicago
#  + 1993 Mississippi State University
# 
# Permission is hereby granted to use, reproduce, prepare derivative works, and
# to redistribute to others.  This software was authored by:
# 
# Argonne National Laboratory Group
# W. Gropp: (708) 252-4318; FAX: (708) 252-7852; e-mail: gropp@mcs.anl.gov
# E. Lusk: (708) 252-7852; FAX: (708) 252-7852; e-mail: lusk@mcs.anl.gov
# Mathematics and Computer Science Division
# Argonne National Laboratory, Argonne IL 60439
# 
# Mississippi State Group
# N. Doss:  (601) 325-2565; FAX: (601) 325-7692; e-mail: doss@erc.msstate.edu
# A. Skjellum:(601) 325-8435; FAX: (601) 325-8997; e-mail: tony@erc.msstate.edu
# Mississippi State University, Computer Science Department &
#  NSF Engineering Research Center for Computational Field Simulation
# P.O. Box 6176, Mississippi State MS 39762
# 
#                               GOVERNMENT LICENSE
# 
# Portions of this material resulted from work developed under a U.S.
# Government Contract and are subject to the following license: the Government
# is granted for itself and others acting on its behalf a paid-up, nonexclusive,
# irrevocable worldwide license in this computer software to reproduce, prepare
# derivative works, and perform publicly and display publicly.
# 
#                                   DISCLAIMER
# 
# This computer code material was prepared, in part, as an account of work
# sponsored by an agency of the United States Government.  Neither the United
# States, nor the University of Chicago, nor Mississippi State University, nor
# any of their employees, makes any warranty express or implied, or assumes any
# legal liability or responsibility for the accuracy, completeness, or
# usefulness of any information, apparatus, product, or process disclosed, or
# represents that its use would not infringe privately owned rights.
#
set -f
#######################################################################
#
#set -x
#
# Environment variables defining various locations and names:
#
# NMPI_INCLUDE : Directory containing the MPI include files.
# NMPI_LIBDIR  : Directory containing the directory lib64
#                which contains the libraries for linking.
# NMPI_LIBNAME : Base name of the MPI library

NMPI_ROOT=${NMPI_ROOT:-/opt/nec/ve/mpi/3.11.0}
NMPI_INCLUDE=${NMPI_INCLUDE:-$NMPI_ROOT/include}
NMPI_LIBDIR=${NMPI_LIBDIR:-$NMPI_ROOT}
NMPI_LIBNAME=${NMPI_LIBNAME:-mpi}
default_comp=nfort
default_comp_vh=gfortran
NMPI_FC=${NMPI_FC:-${NMPI_F90:-$default_comp}}
NMPI_FC_H=${NMPI_FC_H:-${NMPI_F90_H:-$default_comp_vh}}

#
# Local macros
#
DoLink=1
DoCompile=0
MPILOG=
Show=eval
cmodargs=
addobjs=
compileargs=
compileargs_mod=
compileargs_f08=
i8=0
libopt=
linkargs=
linkobjs=
HasDashC=0
HasProginf=0
msgq=0
objectfile=
objectinoption=
UsesPmpi=0
r8=0

staticmpi=

shared=0
static=0
staticnec=
dynamiclinker=0

cmod=
warn=
nowarn=1
tcpip=
ve=ve
veh_spec=

pic=0
ver=
vnd=
subdir=

add_gcc_rpath=0

veoargs=

mpifp16_set=0
mpifp16=""
mfp16_set=0
mfp16=""
#
#=======================================================================
#  Parse arguments
#=======================================================================
#
#   -mpilog    - Build version that generate MPE log files
#   -mpianim   - Build version that generates real-time animation
#
#
while [ $# -gt 0 ] ; do
    arg=$1
    shift
#   echo procssing arg $arg
    case "$arg" in
	-help)
        cat << EOF

This is a wrapper script to compile or link Fortran MPI
programs. The default behaviour is to invoke "$default_comp" compiler.
This can be overriden by setting environment variable NMPI_FC or NMPI_FC_H
before executing this script. You can also use the "-compiler"
option (see below for details).

By default, the script will link your program with static (.a)
NEC MPI libraries. To link with dynamic (.so) NEC MPI libraries,
read below about the -shared_mpi option.

In addition to the flags supported by the used Fortran compiler
("see ${NMPI_FC}'s or ${NMPI_FC_H}'s help"), the following special options are
supported:

    -compiler <compiler name>
               - Specifies Fortran compiler name
                 (The compiler name must be full path name or name of executable
                  if it is contained in your execution PATH).
                 The Fortran compiler name can also be set by environment variable
                 NMPI_FC or NMPI_FC_H for SX Aurora VE or scalar host, respectively.
                 This option has an higher priority than the environment variables
                 mentioned above.
                 Default is $default_comp for SX Aurora VE (variable NMPI_FC) and
                            $default_comp_vh for scalar or vector host (variable NMPI_FC_H).

    -i8 | -fdefault-integer[=-]8
               - Set the default integer and logical types to an 8 byte wide type.
    -r8 | -fdefault-real[=-]8
               - Set the default real to an 8 byte wide type.

                 If the wide of default integers, logicals and reals is set to 8 bytes,
                 a special NEC MPI library version is linked where MPI_INTEGER, MPI_LOGICAL,
                 and MPI_REAL corresponds to 8 byte wide types (same for C int's and float's).
                 The default "Double Precision" type remains 8 byte wide in this special
                 NEC MPI version (i.e., -fdefault-double-8 is used for gfortran) !

    -mpimsgq   - Build version that links MPI message queue support for debugger.
    -mpiprof   - Build version that links profiling libraries.
    -mpitrace  - Build version that generates traces.
    -mpiverify - Build version that links the collective verification library.

    -ve        - Compile and link for Aurora vector engine
    -vh | -sh  - Compile and link for Aurora vector host or scalar host

    -shared_mpi | -shared-mpi
               - use the NEC MPI shared libraries (.so)
    -static_mpi | -static-mpi
               - use the NEC MPI static libraries (.a)
                 This is the default.

    -show      - Show the commands that would be used without
                 running them.
    -mpifp16  <binary16|bfloat16>
               - MPI primitive data types NEC_MPI_FLOAT16 and MPI_REAL2 are
                 the specified format is assumed regardless of
                 the floating-point binary format option specification.
                 Other values are handled same as binary16.
    -help      - Give this help.

This should be used just like the usual Fortran compiler.
For example,
   $0 -c foo.f90
and
   $0 -o foo foo.o

EOF
	   exit 1 ;;
#
	-c)
	# If -c is NOT specified, then we need to perform a link step.
	   compileargs="$compileargs $arg"
	   DoLink=0
	   HasDashC=1
	   ;;
#
	-E | -S | -M | -MM)
	   compileargs="$compileargs $arg"
	   DoLink=0
           ;;
#
        -clear)
	   compileargs="$compileargs $arg"
	   cmodargs="$cmodargs $arg"
	   linkargs="$linkargs $arg"
           DoLink=1
           DoCompile=0
           HasDashC=0
           UsesPmpi=0
           MPILOG=
           HasProginf=0
           msgq=0
           shared=0
           static=0
           staticnec=
           staticmpi=
           pic=0
           dynamiclinker=0
           warn=
           objectfile=
           objectinoption=
           i8=0
           r8=0
           ve=ve
           veh_spec=
           add_gcc_rpath=0
           ;;
#
        -cmod)
           cmod=1
           compileargs_mod=
           ;;
#
#       -cmodx)
#          cmod=2
#          compileargs_mod=
#          ;;
#
	-compiler)
           if [ $# -gt 0 ] ; then
              NMPI_FC=$1
              NMPI_FC_H=$1
           fi
           shift
           ;;
#
	-echo)
	   set -x ;;
#
	-fdefault-integer*)
	   w=`expr "$arg" : "-fdefault-integer[=-]\([0-9*]\)"`
           if [ "$w" = "4" ] ; then
              i8=0
           elif [ "$w" = "8" ] ; then
              i8=1
           else
	      compileargs="$compileargs $arg"
	      cmodargs="$cmodargs $arg"
	      linkargs="$linkargs $arg"
           fi
           ;;

	-fdefault-real*)
	   w=`expr "$arg" : "-fdefault-real[=-]\([0-9*]\)"`
           if [ "$w" = "4" ] ; then
              r8=0
           elif [ "$w" = "8" ] ; then
              r8=1
           else
	      compileargs="$compileargs $arg"
	      cmodargs="$cmodargs $arg"
	      linkargs="$linkargs $arg"
           fi
           ;;
#
	-ftrace)
	   compileargs="$compileargs $arg"
	   cmodargs="$cmodargs $arg"
	   linkargs="$linkargs $arg"
#
	   if [ $UsesPmpi = 0 ] ; then
	      UsesPmpi=1
	   fi
           ;;
#
        -i8)
           i8=1
	   ;;
#
	-integer-size)
           if [ $# -gt 0 ] ; then
		if [ "$1" = "32" ] ; then
		    i8=0
		elif [ "$1" = "64" ] ; then
		    i8=1
		else
		    compileargs="$compileargs $arg"
		    cmodargs="$cmodargs $arg"
		    linkargs="$linkargs $arg"
		fi
	        shift
	    fi
	   ;;
#
	-l*)
	# This SHOULD be the -l<lib> argument.  Only for the linker
	   linkargs="$linkargs $arg"
	   ;;
#
	-mpianim)
#	   if [ $UsesPmpi = 2 ] ; then
#	      echo "Only one of -mpitrace or -vampir may be used."
#	      exit 1
#          fi
#	   UsesPmpi=2
#	   MPILOG='-lampi -lmpe -lX11 -lm'
           ;;
#
	-mpilog)
#	   if [ $UsesPmpi = 2 ] ; then
#	      echo "Only one of -mpitrace or -vampir may be used."
#	      exit 1
#          fi
#	   UsesPmpi=2
#	   MPILOG='-llmpi'
           ;;
#
        -mpimsgq | -msgq)
           msgq=1
           ;;
#
	-mpiprof)
	   if [ $UsesPmpi = 0 ] ; then
	      UsesPmpi=1
           fi
           ;;
#
	-mpitrace)
	   if [ $UsesPmpi -gt 1 -a $UsesPmpi -ne 2 ] ; then
	      echo "Only one of -mpitrace, -mpiverify or -vampir may be used."
	      exit 1
           fi
#
	   UsesPmpi=2
	   MPILOG='-ltmpi$w8'
	   ;;
#
	-mpiverify)
	   if [ $UsesPmpi -gt 1 -a $UsesPmpi -ne 3 ] ; then
	      echo "Only one of -mpitrace, -mpiverify or -vampir may be used."
	      exit 1
           fi
#
	   UsesPmpi=3
   	   MPILOG='-lvmpi$w8'
           ;;
#
        -nomod)
           cmod=0
           compileargs_mod=
	   ;;
#
        -o)
           if [ $# -gt 0 ] ; then
              objectfile=$1
              shift
           fi
           ;;
#
	-proginf)
           compileargs="$compileargs $arg"
           cmodargs="$cmodargs $arg"
           linkargs="$linkargs $arg"
           HasProginf=1
           ;;
#
        -r8)
           r8=1
	   ;;
#
	-real-size)
           if [ $# -gt 0 ] ; then
		if [ "$1" = "32" ] ; then
		    r8=0
		elif [ "$1" = "64" ] ; then
		    r8=1
		else
		    compileargs="$compileargs $arg"
		    cmodargs="$cmodargs $arg"
		    linkargs="$linkargs $arg"
		fi
	        shift
	    fi
	   ;;
	-shared)
           linkargs="$linkargs $arg"
           shared=1
           ;;
#
	-shared-mpi | -shared_mpi)
           staticmpi=0
           ;;
#
	-static-mpi | -static_mpi)
           staticmpi=1
           ;;
#
        -static-nec)
           linkargs="$linkargs $arg"
           staticnec=1
           ;;
#
        -static)
           linkargs="$linkargs $arg"
           static=1
           ;;
#
	-static* | -i-static)
           linkargs="$linkargs $arg"
           ;;
#
        -fPIC | -fpic)
           compileargs="$compileargs $arg"
           cmodargs="$cmodargs $arg"
           linkargs="$linkargs $arg"
           pic=1
           ;;
#
        *-no-dynamic-linker*)
           compileargs="$compileargs $arg"
           cmodargs="$cmodargs $arg"
           linkargs="$linkargs $arg"
           dynamiclinker=2
           ;;
#
        *-dynamic-linker*)
           compileargs="$compileargs $arg"
           cmodargs="$cmodargs $arg"
           linkargs="$linkargs $arg"
           dynamiclinker=1
           ;;
#
	-show | -showcmd)
	   Show=echo ;;
#
	-tcpip)
	   tcpip=1 ;;
#
	-ve)
	   if [ -n "$veh_spec" -a "$veh_spec" != ve ] ; then
              echo "$0: Error: -ve and -vh/-sh options cannot be specified at the same time. Please specify -ve or -vh/-sh option exclusively."
              exit 1
           fi
           veh_spec=ve
	   ve=ve ;;
#
	-vh | -sh)
	   if [ -n "$veh_spec" -a "$veh_spec" != vh ] ; then
              echo "$0: Error: -ve and -vh/-sh options cannot be specified at the same time. Please specify -ve or -vh/-sh option exclusively."
              exit 1
           fi
           veh_spec=vh
	   ve=vh ;;
#
	-vampir | -vt)
	   if [ $UsesPmpi -gt 1 -a $UsesPmpi -ne 4 ] ; then
	      echo "Only one of -mpitrace or -vampir may be used."
	      exit 1
           fi
	   UsesPmpi=4
	   MPILOG='-lVT$w8 -lld'
           ;;
#
        -nowarn)
           nowarn=1
           ;;
#
        -add-gcc-rpath)
           add_gcc_rpath=1
           ;;
#
        -nonoglob)
           set +f
           ;;
#
        -mpifp16 )
           mpifp16_set=1
           mpifp16="$1"
           shift
           ;;
#
        -mfp16-format=*)
          mfp16_set=1
          mfp16="$arg"
          compileargs="$compileargs $arg"
          ;;
#
        *\"* | *\'* | *\ * | *\$* | *\(* | *\)* | \<* | *\>* | *\;* | *\&* | *\|* | *\`* )
           qarg=$arg
           qarg=${qarg//\"/\\\"}
           qarg=${qarg//\'/\\\'}
           qarg=${qarg//\ /\\\ }
           qarg=${qarg//\$/\\\$}
           qarg=${qarg//\(/\\\(}
           qarg=${qarg//\)/\\\)}
           qarg=${qarg//\</\\\<}
           qarg=${qarg//\>/\\\>}
           qarg=${qarg//\;/\\\;}
           qarg=${qarg//\&/\\\&}
           qarg=${qarg//\|/\\\|}
           qarg=${qarg//\`/\\\`}
           compileargs="$compileargs $qarg"
           cmodargs="$cmodargs $qarg"
           linkargs="$linkargs $qarg"
           ;;
#
	-*)
	   compileargs="$compileargs $arg"
	   cmodargs="$cmodargs $arg"
	   linkargs="$linkargs $arg"
	   ;;
#
        *)
	   ext=`expr "$arg" : '.*\(\..*\)'`
           lext=`echo $ext | tr '[:upper:]' '[:lower:]'`
	   if [ "$lext" = '.f'   -o "$lext" = '.for' -o \
	        "$lext" = '.ftn' -o "$lext" = '.fpp' -o \
	        "$lext" = '.f90' -o "$lext" = '.f95' -o \
	        "$lext" = '.f03' -o "$lext" = '.f08' -o \
	        "$lext" = '.i'   -o "$lext" = '.i90' -o \
	        "$lext" = '.c'   -o "$lext" = '.s'   -o \
	        "$lext" = '.cuf'                        \
	   ] ; then
	       DoCompile=1
	       compileargs="$compileargs $arg"
	       if [ -f "$arg" ] ; then
	          fname=`basename $arg $ext`
#        linkobjs="$linkobjs $fname.o"
	          linkargs="$linkargs $fname.o"
#
                  if [ -n "$objectinoption" ] ; then
	             addobjs="-o $fname.o"
                  fi
               fi
	   elif [ "$ext" = ".o" -o "$ext" = ".a" ] ; then
	      if [ $HasDashC = 1 ] ; then
	         compileargs="$compileargs $arg"
              else
	         DoLink=1
		 warn=1
#            linkobjs="$linkobjs $arg"
	         linkargs="$linkargs $arg"
              fi
	   else
            compileargs="$compileargs '$arg'"
	    linkargs="$linkargs $arg"
	   fi
	   ;;
    esac
done
#
# if -static-nec does not supported. the old compiler's static library is linked
#
if [ "$ve" = "ve" ] ; then
   if [ -z "$staticnec" ] ; then
       out=`$NMPI_FC -static-nec --version 2>&1`
       if [ $? = 0 ] ; then
           orig_IFS="$IFS"
           IFS=$'\n'
           found=0
           for line in $out
           do
               if echo "$line" | grep -q "Warning: unrecognized option '-static-nec'" ; then
                   found=1
                   break
               fi
           done
           if [ $found = 1 ] ; then
               staticnec=1
           else
              if [ "$static" = 1 -a "$shared" = 0 ]
              then
                 linkargs="$linkargs -static-nec"
                 staticnec=1
              else
                 staticnec=0
              fi
           fi
           IFS="$orig_IFS"
       else
           staticnec=1
       fi
   fi
else
   staticnec=0
fi
if [ -z "$staticmpi" ] ; then
    if [ "$shared" = 1 ] ; then
        staticmpi=0
    elif [ "$static" = 1 ] ; then
        staticmpi=1
    else
        staticmpi=1
    fi
fi
#
if [ -n "$objectfile" ] ; then
   if [ $DoLink = 1 ] ; then
      linkargs="$linkargs -o $objectfile"
      compileargs="$compileargs $addobjs"
   elif [ $HasDashC = 1 ] ; then
      compileargs="$compileargs -o $objectfile"
   else
      linkargs="$linkargs -o $objectfile"
      compileargs="$compileargs -c $addobjs"
      DoLink=1
   fi
fi
#
if [ "$ve" = "vh" ] ; then
   compiler_host_basename=`basename "$NMPI_FC_H"`
fi
#
w8=
if [ $i8 -eq 1 ] ; then
  if [ "$ve" = "vh" ] ; then
     if [ "$compiler_host_basename" = "ifort" ] ; then
        i8opt="-i8"
     elif [ "$compiler_host_basename" = "gfortran" ] ; then
        i8opt="-fdefault-integer-8"
     elif [ "$compiler_host_basename" = "nvfortran" ] ; then
        i8opt="-i8"
     elif [ "$compiler_host_basename" = "ifx" ] ; then
        i8opt="-integer-size 64"
     fi
  else
     i8opt="-fdefault-integer=8"
  fi

  compileargs="$compileargs $i8opt"
  cmodargs="$cmodargs $i8opt"

  if [ $r8 -eq 1 ] ; then
     w8=w
# elif [ $Dolink = 1 ] ; then
#    Warning
  fi
fi

if [ $r8 -eq 1 ] ; then
   if [ "$ve" = "vh" ] ; then
      if [ "$compiler_host_basename" = "ifort" ] ; then
         r8opt="-r8"
      elif [ "$compiler_host_basename" = "gfortran" ] ; then
         r8opt="-fdefault-real-8"
         if [ "$w8" = "w" ] ; then
            r8opt="$r8opt -fdefault-double-8"
         fi
     elif [ "$compiler_host_basename" = "nvfortran" ] ; then
        r8opt="-r8"
     elif [ "$compiler_host_basename" = "ifx" ] ; then
        r8opt="-real-size 64"
      fi
   else
      r8opt="-fdefault-real=8"
   fi

   compileargs="$compileargs $r8opt"
   cmodargs="$cmodargs $r8opt"
fi


if [ "$ve" = "vh" ] ; then
     if [ "$compiler_host_basename" = "ifort" -o "$compiler_host_basename" = "ifx" ] ; then
	pp="-fpp"
     else
	pp="-cpp"
     fi
else
	pp="-fpp"
fi

if [ "$mpifp16_set" -eq 1 ];
then
    if [ "$mpifp16" = "binary16" ] ; then
      compileargs="$compileargs $pp -DMPI_REAL2=NEC_MPI_BINARY16 -Dmpi_real2=NEC_MPI_BINARY16"
      compileargs="$compileargs -DNEC_MPI_FLOAT16_INT=NEC_MPI_BINARY16_INT -Dnec_mpi_float16_int=NEC_MPI_BINARY16_INT"
      compileargs="$compileargs -DMPI_2REAL2=NEC_MPI_2BINARY16 -Dmpi_2real2=NEC_MPI_2BINARY16"
    elif [ "$mpifp16" = "bfloat16" ] ; then
      compileargs="$compileargs $pp -DMPI_REAL2=NEC_MPI_BFLOAT16 -Dmpi_real2=NEC_MPI_BFLOAT16"
      compileargs="$compileargs -DNEC_MPI_FLOAT16_INT=NEC_MPI_BFLOAT16_INT -Dnec_mpi_float16_int=NEC_MPI_BFLOAT16_INT"
      compileargs="$compileargs -DMPI_2REAL2=NEC_MPI_2BFLOAT16 -Dmpi_2real2=NEC_MPI_2BFLOAT16"
    else
      compileargs="$compileargs $pp -DMPI_REAL2=NEC_MPI_BINARY16 -Dmpi_real2=NEC_MPI_BINARY16"
      compileargs="$compileargs -DNEC_MPI_FLOAT16_INT=NEC_MPI_BINARY16_INT -Dnec_mpi_float16_int=NEC_MPI_BINARY16_INT"
      compileargs="$compileargs -DMPI_2REAL2=NEC_MPI_2BINARY16 -Dmpi_2real2=NEC_MPI_2BINARY16"
    fi

elif [ "$mfp16_set" -eq 1 ];
then
    case "$mfp16" in
      *ieee)
        compileargs="$compileargs $pp -DMPI_REAL2=NEC_MPI_BINARY16 -Dmpi_real2=NEC_MPI_BINARY16"
        compileargs="$compileargs -DNEC_MPI_FLOAT16_INT=NEC_MPI_BINARY16_INT -Dnec_mpi_float16_int=NEC_MPI_BINARY16_INT"
        compileargs="$compileargs -DMPI_2REAL2=NEC_MPI_2BINARY16 -Dmpi_2real2=NEC_MPI_2BINARY16"
        ;;
      *bfloat)
        compileargs="$compileargs $pp -DMPI_REAL2=NEC_MPI_BFLOAT16 -Dmpi_real2=NEC_MPI_BFLOAT16"
        compileargs="$compileargs -DNEC_MPI_FLOAT16_INT=NEC_MPI_BFLOAT16_INT -Dnec_mpi_float16_int=NEC_MPI_BFLOAT16_INT"
        compileargs="$compileargs -DMPI_2REAL2=NEC_MPI_2BFLOAT16 -Dmpi_2real2=NEC_MPI_2BFLOAT16"
        ;;
      * )
        compileargs="$compileargs $pp -DMPI_REAL2=NEC_MPI_BINARY16 -Dmpi_real2=NEC_MPI_BINARY16"
        compileargs="$compileargs -DNEC_MPI_FLOAT16_INT=NEC_MPI_BINARY16_INT -Dnec_mpi_float16_int=NEC_MPI_BINARY16_INT"
        compileargs="$compileargs -DMPI_2REAL2=NEC_MPI_2BINARY16 -Dmpi_2real2=NEC_MPI_2BINARY16"
        ;;
      esac

else
    compileargs="$compileargs $pp -DMPI_REAL2=NEC_MPI_BINARY16 -Dmpi_real2=NEC_MPI_BINARY16"
    compileargs="$compileargs -DNEC_MPI_FLOAT16_INT=NEC_MPI_BINARY16_INT -Dnec_mpi_float16_int=NEC_MPI_BINARY16_INT"
    compileargs="$compileargs -DMPI_2REAL2=NEC_MPI_2BINARY16 -Dmpi_2real2=NEC_MPI_2BINARY16"
fi
#
#=======================================================================
#  compile and link
#=======================================================================
#
# libdir = Name of the directory containing the libraries and modules
#          (including -L)
#
status=2
cpp=
#
if [ "$ve" = "vh" ] ; then
   NMPI_FC=$NMPI_FC_H
#
# Which compiler should be used ?
#
   compiler_host_basename=`basename "$NMPI_FC"`
#
   subdir=

   if [ "$compiler_host_basename" = "ifort" ] ; then
      vnd="intel"
      ver="`$NMPI_FC --version 2>/dev/null | head -n 1 | awk '{print $3}'`"
   elif [ "$compiler_host_basename" = "ifx" ] ; then
      vnd="oneapi"
      ver="`$NMPI_FC --version 2>/dev/null | head -n 1 | awk '{print $3}'`"
   elif [ "$compiler_host_basename" = "nvfortran" ] ; then
      vnd="nvidia"
      ver="`$NMPI_FC --version | sed /^$/d  | head -n 1 | awk '{print $2}' | cut -d "-" -f 1`"
   else
      vnd="gnu"
      ver="`$NMPI_FC --version 2>/dev/null | head -n 1 | awk '{print $4}'`"
   fi

   if [ -z "$ver" ] ; then
      subdir="/${vnd}/default"
   else
      subdir="/${vnd}/${ver}"
      if [ ! -d "$NMPI_LIBDIR/lib64/${ve}${subdir}" ] ; then
         subdir="/${vnd}/default"
      fi
   fi

   if [ "$compiler_host_basename" = "ifort" ] ; then
      MODDIR=$NMPI_LIBDIR/lib64/vh${subdir}/module${w8}
      compileargs_mod="-I$MODDIR"
      compileargs_f08="-std08"
      cpp=-fpp
   elif [ "$compiler_host_basename" = "ifx" ] ; then
      MODDIR=$NMPI_LIBDIR/lib64/vh${subdir}/module${w8}
      compileargs_mod="-I$MODDIR"
      compileargs_f08="-std08"
      cpp=-cpp
   elif [ "$compiler_host_basename" = "nvfortran" ] ; then
      MODDIR=$NMPI_LIBDIR/lib64/vh${subdir}/module${w8}
      compileargs_mod="-I$MODDIR"
      #compileargs_f08="-std08"
      #cpp=-fpp
   else
      MODDIR=$NMPI_LIBDIR/lib64/vh${subdir}/module${w8}
      compileargs_mod="-I$MODDIR"
      compileargs_f08="-std=f2008ts"
#     cpp="$cpp -DDONT_HAVE_QUADRUPLE"
   fi

else
   subdir=
   MODDIR=$NMPI_LIBDIR/lib64/ve/module${w8}
   compileargs_mod="-I$MODDIR"
fi
#
# Compile module file
#
  if [ -n "$cmod" ] ; then
     compileargs_mod=
     linkobjs="$linkobjs mpi_f08.o"
#
     if [ $cmod != 0 ] ; then
        incdir=$NMPI_INCLUDE
        if [ "$ve" = "vh" ] ; then
           if [ -d "$NMPI_INCLUDE/vh" ] ; then
              incdir=$NMPI_INCLUDE/vh
           fi
           define=${NMPI_CMOD_DEFINES_VH:-}
        else
           define=${NMPI_CMOD_DEFINES:-}
        fi
#
        if [ $cmod = 2 ] ; then
           define="$define -DDONT_USE_MPI_INTERFACES"
        fi
#
        if [ -n "$w8" ] ; then
           define="$define -D_W8"
        fi
#
        #$Show $NMPI_FC $cmodargs $cpp $define -c $incdir/mpif.f90
        $Show /usr/bin/cpp -P $cpp $define $incdir/mpif.f90 mpif.TMP.f90
        $Show $NMPI_FC $cmodargs -o mpif.o -c mpif.TMP.f90
	rm -f mpif.TMP.f90
#
        $Show /usr/bin/cpp -P $cpp $define $incdir/mpi_f08.f90 mpi_f08.TMP.f90
        $Show $NMPI_FC $cmodargs $compileargs_f08 -o mpi_f08.o -c mpi_f08.TMP.f90
	rm -f mpi_f08.TMP.f90
     fi

  else
#
     if [ -e "$MODDIR/mpi_f08.o" ] ; then
        linkobjs="$linkobjs $MODDIR/mpi_f08.o"
     fi
  fi
#
# Compile Fortran file
#
if [ $DoCompile = 1 ] ; then
#
   if [ "$ve" = "ve" ] ; then
      if [ $HasProginf != 1 ] ; then
         compileargs="$compileargs -proginf"
      fi
   fi
#
   if [ $HasDashC != 1 ] ; then
      compileargs="$compileargs -c"
   fi
#
   $Show ${NMPI_FC} $compileargs $compileargs_mod -I$NMPI_INCLUDE
   status=$?
   if [ $status != 0 ] ; then
      exit $status
   fi
fi
#
# Link executable
#
if [ $DoLink = 1 ] ; then
#
   if [ "$ve" = "ve" ] ; then
      if [ "$staticmpi" = 0 -a "$staticnec" = 1 ] ; then
         echo "$0: Warning: Linking with a mix of MPI shared and Compiler static libraries." 1>&2
      fi
   fi

   # stlibopt
   # 0: do nothing
   # 1: transform -lmpi to libmpi.a
   # 2: transform -lmpi to -Wl,--push-state -Wl,-Bdynamic -lmpi -Wl,--pop-state

   stlibopt=0
   if [ "$shared" = 1 ] ; then
      if [ "$staticmpi" = 0 ] ; then
         stlibopt=0
      else
         stlibopt=1
      fi
   elif [ "$static" = 1 ] ; then
      if [ "$staticmpi" = 0 ] ; then
         stlibopt=2
      else
         stlibopt=0
      fi
   else
      if [ "$staticmpi" = 0 ] ; then
         stlibopt=0
      else
         stlibopt=1
      fi
   fi

   if [ "$NMPI_LIBDIR" != "/usr" ] ; then
      libdir="$NMPI_LIBDIR/lib64/${ve}${subdir}"
      libopt="-L$libdir"
      if [ "$stlibopt" = "0" ] ; then
#        shlibs="-Wl,-rpath,$libdir"
#        shlibs="-Wl,--export-dynamic"
         shlibs=
      fi
   else
      libdir=
      libopt=
      if [ "$stlibopt" = "0" ] ; then
	 shlibs=
      fi
   fi
#
   if [ "$ve" = "ve" ] ; then
      if [ $HasProginf != 1 ] ; then
         linkargs="$linkargs -proginf"
      fi
#
      libadd="-Wl,-Bdynamic -Wl,-rpath=$libdir -Wl,--enable-new-dtags -L$libdir -lmpi_mem -lveio -lsysve -pthread -lrt -ldl"
   else
      if [ "$vnd" = "gnu" -a "$ver" != "" ] ; then
         nums="`echo ${ver} | sed 's/\./ /g'`"
         vernum="`printf "%02d%02d%02d" $nums`"
         if [ "${vernum}" -lt "090100" ] ; then

            if [ "$staticmpi" = 0 ] ; then
               if [ "$pic" = 0 ] ; then
                  dummy_gfc="$libdir/mpir_dummy_gfc.o"
               else
                  dummy_gfc="$libdir/mpir_dummy_gfc_PIC.o"
               fi
               if [ -e "$dummy_gfc" ] ; then
                  linkargs="$linkargs $dummy_gfc"
               fi
            fi

         fi
      fi
#
      libadd="-Wl,-Bdynamic -Wl,-rpath=$libdir -Wl,--enable-new-dtags -L$libdir -lmpi_mem -pthread -lrt -lm -ldl"
      veoargs="-Wl,--push-state -Wl,-Bdynamic -L$libdir -lmpi_veo -Wl,--pop-state"
#
      if [ "$static" = 1 ] ; then
         if [ "$dynamiclinker" = 0 ] ; then
            libadd="$libadd -Wl,-dynamic-linker,/lib64/ld-linux-x86-64.so.2"
         fi
      fi
      linkobjs="$linkobjs $veoargs"

#
      if [ $add_gcc_rpath = 1 ] ; then
         if [ "$vnd" = "gnu" ] ; then
            gcc_path=`which $NMPI_FC 2>/dev/null`
            if [ -n "$gcc_path" ] ; then
               gcc_bindir=`dirname $gcc_path`
               gcc_prefix=`dirname $gcc_bindir`
               libadd="$libadd -Wl,-rpath=$gcc_prefix/lib64"
            fi
         fi
      fi
#
   fi
#
   name=$NMPI_LIBNAME${w8}
#
   if [ $msgq -eq 1 ] ; then
      liblist="-l${NMPI_LIBNAME}msgq${w8} -l${name}"
   else
      liblist="-l${name}"
   fi
#
   if [ $UsesPmpi -gt 0 ] ; then
      eval mpilog=\"$MPILOG -ldp${name} -lf${name} $MPILOG -ldp${name}\"
#
      liblist="$mpilog -lp${name} $liblist"
   fi
#
   if [ "$stlibopt" = 1 ] ; then
#     liblist="-Bstatic $liblist -Bdynamic"
      libstat=$liblist
      liblist=
      libopt=
      for name in $libstat
      do
         sub=`expr $name : "-l\(.*mpi.*\)"`
         if [ -z "$sub" ] ; then
            liblist="$liblist $name"
         else
            liblist="$liblist $libdir/lib$sub.a"
         fi
      done
#
   elif [ "$stlibopt" = 2 ] ; then
      liblist="-Wl,--push-state -Wl,-Bdynamic $liblist -Wl,--pop-state"
   fi
#
   if [ "$static" = 1 ] ; then
       1>&2 cat - <<EOT
$0: Warning: -static is specified but an executable will be linked dynamically.
Note: -static is not recommended. MPI compile commands append -Wl,-Bdynamic
to the end of command line to force dynamic linking because NEC MPI requires
dynamically linked MPI memory management library and system libraries.
The mix of -static and -Wl,-Bdynamic may lead unexpected behavior.
Please consider -Wl,-Bstatic and -Wl,-Bdynamic instead of -static.
EOT
   fi
#
   $Show ${NMPI_FC} $linkobjs $linkargs $libopt $shlibs $liblist  $libadd
   status=$?
fi
#
exit $status
