#! /bin/sh
#
#######################################################################
#
# Compile and link a C 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/ve3/mpi/3.11.0}
NMPI_INCLUDE=${NMPI_INCLUDE:-$NMPI_ROOT/include}
NMPI_LIBDIR=${NMPI_LIBDIR:-$NMPI_ROOT}
NMPI_LIBNAME=${NMPI_LIBNAME:-mpi}
default_comp=ncc
default_comp_vh=gcc
NMPI_CC=${NMPI_CC:-$default_comp}
NMPI_CC_H=${NMPI_CC_H:-$default_comp_vh}

#
# Local macros
#
DoLink=1
DoCompile=0
MPILOG=
Show=eval
compiler=
compiler_host=
compileargs=
libopt=
linkargs=
linkobjs=
HasDashC=0
HasProginf=0
msgq=0
objectfile=
objectinoption=
UsesPmpi=0

staticmpi=

shared=0
static=0
staticnec=
dynamiclinker=0

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 C-language MPI
programs. The default behaviour is to invoke "$default_comp" compiler.
This can be overriden by setting environment variable NMPI_CC or NMPI_CC_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 C compiler
("see ${NMPI_CC}'s or ${NMPI_CC_H}'s help"), the following special options are
supported:

    -compiler <compiler name>
               - Specifies C compiler name
                 (The compiler name must be full path name or name of executable
                  if it is contained in your execution PATH).
                 The C compiler name can also be set by environment variable
                 NMPI_CC or NMPI_CC_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_CC) and
                            $default_comp_vh for scalar or vector host (variable NMPI_CC_H).
    -compiler_host <compiler name>
               - Specifies C host compiler name for scalar or vector host executables.
                 This flag may be required only if the external compiler "nvcc" is
                 used to link MPI executables for scalar or vector host.
                 This flag is used to determine the correct MPI libraries to be linked.
                 Default is $default_comp_vh (variable NMPI_CC_H).

    -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 C compiler.
For example,
   $0 -c foo.c
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
           ;;
#
	-MT | -MQ)
	   compileargs="$compileargs $arg '$1'"
	   shift
	   ;;
#
        -clear)
	   compiler=
	   compiler_host=
	   compileargs="$compileargs $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=
           ve=ve
           veh_spec=
           add_gcc_rpath=0
           ;;
#
        -compiler_host)
           if [ $# -gt 0 ] ; then
              compiler_host=$1
           fi
           shift
           ;;
#
        -compiler)
           if [ $# -gt 0 ] ; then
              compiler=$1
           fi
           shift
           ;;
#
	-echo)
	   set -x ;;
#
	-ftrace)
	   compileargs="$compileargs $arg"
	   linkargs="$linkargs $arg"
#
	   if [ $UsesPmpi = 0 ] ; then
	      UsesPmpi=1
	   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'
           ;;
#
	-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'
           ;;
#
        -o)
           if [ $# -gt 0 ] ; then
              objectfile=$1
              shift
           fi
           ;;
#
	-proginf)
           compileargs="$compileargs $arg"
           linkargs="$linkargs $arg"
           HasProginf=1
           ;;
#
	-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"
           linkargs="$linkargs $arg"
           pic=1
           ;;
#
        *-no-dynamic-linker*)
           compileargs="$compileargs $arg"
           linkargs="$linkargs $arg"
           dynamiclinker=2
           ;;
#
        *-dynamic-linker*)
           compileargs="$compileargs $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 or -mpilog or -mpiverify may be used."
	      exit 1
           fi
	   UsesPmpi=4
	   MPILOG='-lVT -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"
           linkargs="$linkargs $qarg"
           ;;
#
	-*)
	   compileargs="$compileargs $arg"
	   linkargs="$linkargs $arg"
	   ;;
#
        *)
	   ext=`expr "$arg" : '.*\(\..*\)'`
	   if [ \
	       "$ext" = ".c"   -o "$ext" = ".i"   -o \
	       "$ext" = ".C"   -o "$ext" = ".cc"  -o \
	       "$ext" = ".cpp" -o "$ext" = ".cp"  -o \
	       "$ext" = ".cxx" -o "$ext" = ".c++" -o \
	       "$ext" = ".ii"  -o                    \
               "$ext" = ".cu"  -o  "$ext" = ".gpu" -o "$ext" = ".ptx" -o \
	       "$ext" = ".S"   -o "$ext" = ".s"      \
	   ] ; 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
       if [ -n "$compiler" ] ; then
          NMPI_CC=$compiler
       fi
       out=`$NMPI_CC -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 [ "$mpifp16_set" -eq 1 ];
then
    if [ "$mpifp16" = "binary16" ] ; then
      compileargs="$compileargs -DNEC_MPI_FLOAT16=NEC_MPI_BINARY16"
      compileargs="$compileargs -DNEC_MPI_FLOAT16_INT=NEC_MPI_BINARY16_INT"
      compileargs="$compileargs -DMPI_2REAL2=NEC_MPI_2BINARY16"
    elif [ "$mpifp16" = "bfloat16" ] ; then
      compileargs="$compileargs -DNEC_MPI_FLOAT16=NEC_MPI_BFLOAT16"
      compileargs="$compileargs -DNEC_MPI_FLOAT16_INT=NEC_MPI_BFLOAT16_INT"
      compileargs="$compileargs -DMPI_2REAL2=NEC_MPI_2BFLOAT16"
    else
      compileargs="$compileargs -DNEC_MPI_FLOAT16=NEC_MPI_BINARY16"
      compileargs="$compileargs -DNEC_MPI_FLOAT16_INT=NEC_MPI_BINARY16_INT"
      compileargs="$compileargs -DMPI_2REAL2=NEC_MPI_2BINARY16"
    fi
  
elif [ "$mfp16_set" -eq 1 ];
then
    case "$mfp16" in
      *ieee)
        compileargs="$compileargs -DNEC_MPI_FLOAT16=NEC_MPI_BINARY16"
        compileargs="$compileargs -DNEC_MPI_FLOAT16_INT=NEC_MPI_BINARY16_INT"
        compileargs="$compileargs -DMPI_2REAL2=NEC_MPI_2BINARY16"
        ;;
      *bfloat)
        compileargs="$compileargs -DNEC_MPI_FLOAT16=NEC_MPI_BFLOAT16"
        compileargs="$compileargs -DNEC_MPI_FLOAT16_INT=NEC_MPI_BFLOAT16_INT"
        compileargs="$compileargs -DMPI_2REAL2=NEC_MPI_2BFLOAT16"
        ;;
      * )
        compileargs="$compileargs -DNEC_MPI_FLOAT16=NEC_MPI_BINARY16"
        compileargs="$compileargs -DNEC_MPI_FLOAT16_INT=NEC_MPI_BINARY16_INT"
        compileargs="$compileargs -DMPI_2REAL2=NEC_MPI_2BINARY16"
        ;;
      esac
  
else
    compileargs="$compileargs -DNEC_MPI_FLOAT16=NEC_MPI_BINARY16"
    compileargs="$compileargs -DNEC_MPI_FLOAT16_INT=NEC_MPI_BINARY16_INT"
    compileargs="$compileargs -DMPI_2REAL2=NEC_MPI_2BINARY16"
fi
#
#=======================================================================
#  compile and link
#=======================================================================
#
status=2
#
if [ -z "$compiler" ] ; then
   if [ "$ve" = "vh" ] ; then
      NMPI_CC=$NMPI_CC_H
      nvcc=`expr "$NMPI_CC" : ".*\(nvcc\)"`
   fi
else
   NMPI_CC=$compiler
   if [ "$ve" = "vh" ] ; then
      nvcc=`expr "$compiler" : ".*\(nvcc\)"`
      if [ -n "$compiler_host" ] ; then
         NMPI_CC_H=$compiler_host
      elif [ -z "$nvcc" ] ; then
         NMPI_CC_H=$compiler
      fi
   fi
fi
#
# Compile
#
if [ $DoCompile = 1 ] ; then
   compileargs="$compileargs -D_REENTRANT"
#
   if [ -n "$nvcc" ] ; then
      compileargs="$compileargs --compiler-options -pthread"
   else
      compileargs="$compileargs -pthread"
   fi
#
   if [ "$ve" = "ve" ] ; then
      if [ $HasProginf != 1 ] ; then
         compileargs="$compileargs -proginf"
      fi
   fi
#
   if [ $HasDashC != 1 ] ; then
      compileargs="$compileargs -c"
   fi
#
   if [ "$NMPI_INCLUDE" != "/usr/include" ] ; then
      compileargs="$compileargs -I$NMPI_INCLUDE"
   fi
#
   $Show $NMPI_CC $compileargs
   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
      if [ "$ve" = "vh" ] ; then
         compiler_host_basename=`basename "$NMPI_CC_H"`

         if [ "$compiler_host_basename" = "icc" ] ; then
            vnd="intel"
            ver="`$NMPI_CC_H --version 2>/dev/null | head -n 1 | awk '{print $3}'`"
         elif [ "$compiler_host_basename" = "icx" ] ; then
            vnd="oneapi"
            ver="`$NMPI_CC_H --version 2>/dev/null | head -n 1 | awk '{print $5}'`"
         elif [ "$compiler_host_basename" = "nvc" ] ; then
            vnd="nvidia"
            ver="`$NMPI_CC_H --version | sed /^$/d  | head -n 1 | awk '{print $2}' | cut -d "-" -f 1`"
         else
            vnd="gnu"
            ver="`$NMPI_CC_H --version 2>/dev/null | head -n 1 | awk '{print $3}'`"
         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

      else
         subdir=
      fi
#
      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" ] ; 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
#
      if [ -n "$nvcc" ] ; then
         libadd="--linker-options -Bdynamic,-rpath=$libdir,--enable-new-dtags -L$libdir -lmpi_mem -Xcompiler -pthread -lrt -lm -ldl"
         hookargs="-lmpi_cuda"
         linkobjs="$linkobjs $hookargs"
      else
         libadd="-Wl,-Bdynamic -Wl,-rpath=$libdir -Wl,--enable-new-dtags -L$libdir -lmpi_mem -pthread -lrt -lm -ldl"
         hookargs="-Wl,--push-state -Wl,-Bdynamic -L$libdir -lmpi_veo -lmpi_cuda -Wl,--pop-state"
         linkobjs="$linkobjs $hookargs"
      fi
#
      if [ "$static" = 1 ] ; then
         if [ "$dynamiclinker" = 0 -a -z "$nvcc" ] ; then
            libadd="$libadd -Wl,-dynamic-linker,/lib64/ld-linux-x86-64.so.2"
         fi
      fi
#
      if [ $add_gcc_rpath = 1 ] ; then
         if [ "$vnd" = "gnu" ] ; then
            gcc_path=`which $NMPI_CC_H 2>/dev/null`
            if [ -n "$gcc_path" ] ; then
               gcc_bindir=`dirname $gcc_path`
               gcc_prefix=`dirname $gcc_bindir`
               if [ -z "$nvcc" ] ; then
                  libadd="$libadd -Wl,-rpath=$gcc_prefix/lib64"
               fi
            fi
         fi
      fi
#
   fi
#
   name=$NMPI_LIBNAME
#
   if [ $msgq -eq 1 ] ; then
      liblist="-l${name}msgq -l${name}"
   else
      liblist="-l${name}"
   fi
#
   if [ $UsesPmpi -gt 0 ] ; then
      eval mpilog=\"$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 -a -z "$nvcc" ] ; 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_CC} $linkobjs $linkargs $libopt $shlibs $liblist $libadd 
   status=$?
fi
#
exit $status
