#
#     Copyright (c) 2017-2018, NVIDIA CORPORATION.  All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto.  Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
#

# Compiler Configurations specific to Fortran
#

include rcfiles/ftnrc;
set F90X=f901;
set CUFX=;
set FORTRANMPI=$if($equal($MPITYPE,MPICH),fmpich mpichf90);
# -Mstack_arrays calls __builtin_alloc instead of the pgi-specific alloca
append F902ARGS=-x 54 0x10;
# Enable half precision support by default
append F901ARGS=-x 195 0x8000;

variable KEEPSTB is default();

# No tmp for array argument slice passed as argument with TARGET attribute
## append F901ARGS=-x 58 0x400000;
## append CGARGS=-x 58 0x400000);

switch -Mtarget_temps is
        helpgroup(opt)
        helpname(-M[no]target_temps)
        help(Enable [disable] a temporary when passing an array for a callee assumed-shape variable with the target attribute)
	append(F901ARGS=-y 58 0x400000) 
 	append(CGARGS=-y 58 0x400000);

switch -Mnotarget_temps is
        helpgroup(opt)
        help(Disable a temporary when passing an array for a callee assumed-shape variable with the target attribute)
	append(F901ARGS=-x 58 0x400000)
	append(CGARGS=-x 58 0x400000);

switch -i2 is
	help(Treat INTEGER as INTEGER*2)
	helpgroup(language)
	append(FARGS=-noi4);

switch -i4 is
	help(Treat INTEGER as INTEGER*4)
	helpgroup(language)
	append(FARGS=-i4);

switch -Mallocatable is
	help(Select semantics for assignments to allocatables)
	helpgroup(language)
	keyword(
	    95(	help(Select Fortran 1995 semantics)
		set(ALLOCATABLE95=1))
	    03(	help(Select Fortran 2003 semantics)
		set(ALLOCATABLE95=0))
	    90(	hide help(Select Fortran 1995 semantics)
		set(ALLOCATABLE95=1))
	    1990(hide help(Select Fortran 1995 semantics)
		set(ALLOCATABLE95=1))
	    1995(hide help(Select Fortran 1995 semantics)
		set(ALLOCATABLE95=1))
	    2003(hide help(Select Fortran 2003 semantics)
		set(ALLOCATABLE95=0))
	);

replace switch -c is
	help(Stop after assemble; output in object file)
	helpgroup(overall)
	set(KEEPOBJ=YES)
	set(ASMONLY=YES)
	set(DEFOUTFILE=)
	stopafter(Compile)
	;

switch -Mcfi is
	help(Enable Fortran 2018 CFI Descriptor support (enabled by default))
	helpgroup(language)
	append(F901ARGS=-y 68 0x100);

switch -Mnocfi is
	help(Disable Fortran 2018 CFI Descriptor support)
	helpgroup(language)
	append(F901ARGS=-x 68 0x100);

switch -Mchkptr is
	help(Check for NULL pointers)
	helpgroup(debug)
	append(F901ARGS=-x 70 4);

switch -Mcontiguous is
	help(Assume POINTERs are contiguous)
	helpgroup(language)
	append(CGARGS=-x 125 0x80000)
	append(F901ARGS=-x 125 0x80000);

switch -Mnocontiguous is hide
	help(Do not assume POINTERs are contiguous)
	helpgroup(language)
	append(CGARGS=-y 125 0x80000)
	append(F901ARGS=-y 125 0x80000);

switch -Mfixed is
	help(Assume fixed-format source)
	helpgroup(language)
	set(EXPLICITFORM=YES)
	append(FLANG1ARGS=-Mfixed)
	append(F901ARGS=-nofreeform);

switch -Mfree is
	help(Assume free-format source)
	helpgroup(language)
	helpname(-M[no]free[form])
	set(EXPLICITFORM=YES)
	append(FLANG1ARGS=-Mfree)
	append(F901ARGS=-freeform);

switch -Mnofree is hide
	help(Assume fixed-format source)
	helpgroup(language)
	set(EXPLICITFORM=YES)
	append(FLANG1ARGS=-Mfixed)
	append(F901ARGS=-nofreeform);

switch -Mfreeform is hide
	help(Assume free-format source)
	helpgroup(language)
	set(EXPLICITFORM=YES)
	append(FLANG1ARGS=-Mfree)
	append(F901ARGS=-freeform);

switch -Mnofreeform is hide
	help(Assume fixed-format source)
	helpgroup(language)
	set(EXPLICITFORM=YES)
	append(FLANG1ARGS=-Mfixed)
	append(F901ARGS=-nofreeform);

switch -Mi4 is
	help(Treat INTEGER as INTEGER*4)
	helpgroup(language)
	helpname(-M[no]i4)
	append(FARGS=-i4);

switch -Mnoi4 is hide
	help(Treat INTEGER as INTEGER*4)
	helpgroup(language)
	append(FARGS=-noi4);

switch -Milm is hide
	help(Stop after generating ilm output)
	helpgroup(overall)
	set(KEEPIL=YES)
	set(ILMONLY=YES)
	append(F901ARGS=-x 50 0x10)
	stopafter(Compile);

switch -Mkeepcci is hide
	help(Keep the cci intermediate file)
	helpgroup(overall)
	set(KEEPCCI=YES);

switch -Mkeepinlonly is hide
	help(Keep the inline only intermediate file)
	helpgroup(overall)
	set(KEEPINLONLY=YES);

switch -Mkeepilm is hide
	help(Keep the ilm intermediate file)
	helpgroup(overall)
	set(KEEPIL=YES)
	set(KEEPSTB=YES)
	append(F901ARGS=-x 50 0x10);

switch -module dir is
	help(Add search directory for module information files)
	helpgroup(language)
	append(FLANG1ARGS=-module $path($dir))
	append(F901ARGS=-moddir $path($dir));

switch -r8 is
	help(Treat REAL as REAL*8)
	helpgroup(language)
	append(FLANG1ARGS=-r8)
	append(F901ARGS=-x 124 0x8 -x 124 0x80000);

switch -Wh,arg is
	help(Pass argument to F90 front end)
	helpgroup(overall)
	append(F901ARGS=$replace($arg,",", ));


set FTNMAIN=$ifn($WINAPP,$lookup($COMPLIBOBJ,f90main.$HOSTOBJSUFFIX),$if($or($equal($TARGET,win64),$equal($TARGET,win64)),$lookup($COMPLIBOBJ,f90win64main.$HOSTOBJSUFFIX),$lookup($COMPLIBOBJ,f90win32main.$HOSTOBJSUFFIX)));
set DEFFTNRTL=$replace($PGF90LIB,$LIBSW,$LIBSSW);
set FTNUNIXALT=f90alt.$HOSTOBJSUFFIX;

switch -fsuffix=suff is early
	help(Define a Fortran suffix)
	helpgroup(language)
	append(USERF90SUFFIX=$suff);

switch -FSUFFIX=suff is early
	help(Define a Fortran suffix requiring preprocessing)
	helpgroup(language)
	append(USERF90PPSUFFIX=$suff);


replace switch -S is
	help(Stop before assembling; output in .s file)
	helpgroup(overall)
	stopafter(Compile)
	set(KEEPASM=YES)
	set(DEFOUTFILE=)
	append(F901ARGS=-x 50 0x10)
	set(COMPILEONLY=YES);

replace switch -Msmartalloc is hide
    helpgroup(opt)
    helpname(-M[no]smartalloc)
    help(Enable optimized malloc routines)
    append(CGARGS=-x 129 0x20000000)
    nokeyword()
    keyword(
	huge(
	helpname([no]huge)
	help(Link with the huge TLB page library)
	set(HUGEN=0)
	append(CGARGS=-x 129 0x10000000)
	set(HUGELIB=$if($NOPGIRPATH,$PGISTATIC)
	    -lhugetlbfs_nvhpc
	    $if($NOPGIRPATH,-u __pgi_setup_morecore))
	)
	nohuge( # disable previous -Msmartalloc=huge
	hide
	set(HUGEN=0)
	append(CGARGS=-y 129 0x14000000)
	set(HUGELIB=)
	)
	hugebss(    if($equal($TARGET,linux86-64))
	help(Put the BSS section in huge pages)
	append(CGARGS=-x 129 0x4000000)
	set(HUGELIB=$if($NOPGIRPATH,$PGISTATIC)
	    -lhugetlbfs_nvhpc
	    $if($NOPGIRPATH,-u __pgi_setup_elflink))
	set(COMPLIBOBJ=$COMPBASE/$COMPSYS/$COMPVER/$COMPLIBSUBDIR)
	set(STDRPATHSO=-rpath $COMPBASE/$COMPSYS/$COMPVER/$COMPLIBSUBDIR)
	append(DEFLDARGS=-T $COMPBASE/$COMPSYS/$COMPVER/$quote($COMPLIBPREFIX)lib/hugebss.ld)
	)
    );

replace switch -Mhugetlb is
    help(Link with the huge TLB page library)
    helpgroup(linker)
    set(HUGEN=0)
    append(CGARGS=-x 129 0x10000000)
    set(HUGELIB=$if($NOPGIRPATH,$PGISTATIC)
	-lhugetlbfs_nvhpc
	$if($NOPGIRPATH,-u __pgi_setup_morecore));

switch -Memit-dwarf-common-blocks-name is
	help(Enable generation of DWARF name for Fortran COMMON blocks)
	helpgroup(debug)
	append(CGARGS=-x 183 0x40000000);
