# 
#     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 for Linux/x86 64-bit targets
#

set TARGET=linux86-64;
set DEFLIBDIR=/usr/lib64;
set DEFSTDOBJDIR=/usr/lib64;
set DEFPRED=-predicate "#machine(x86_64) #lint(off) #system(posix) #cpu(x86_64)";

append DEFDEFS=$if($equal($GNU_EXTENSIONS,1),unix linux);
append DEFDEFS=__linux;
append DEFDEFS=__linux__;
append DEFDEFS=__ELF__;
append DEFDEFS=__NO_MATH_INLINES;
append DEFDEFS=__LP64__;
append DEFDEFS=__x86_64;
append DEFDEFS=__x86_64__;
append DEFDEFS=__LONG_MAX__=9223372036854775807L;
append DEFDEFS=__SIZE_TYPE__="unsigned long int";
append DEFDEFS=__PTRDIFF_TYPE__="long int";
append DEFDEFS=$if($X86_64TARGET, __amd64 __amd64__ __k8 __k8__);
append DEFDEFS=$SSEDEFS;
#
# Append "__extension__=" to DEFDEFS if target is Linux86-64-nollvm.
# LLVM targets add __extension__ to global variables USRDDEF and USRDEFDEF.
#
append DEFDEFS=$if($equal($SYS64,linux86-64-nollvm),__extension__=);

variable PGILD is default(-T $lookup($COMPBASE/$COMPSYS/$COMPVER/$(COMPLIBPREFIX)lib,nvhpc.ld));
set DEFLDARGS=-m elf_x86_64 -dynamic-linker $LDSO $PGILD $if($NEEDNVHPCLDSYMS,$NVHPCLDSYMS);
set LINUXCOMMON=-quad;
set NOLLALIGN=;
set MALIGN=;
set ILPALIGN=;
set DEFCPPDEFS=__PGI __NVCOMPILER $DEF_GNU_SOURCE $DEF_PGCG_SOURCE;
set HOSTCPP1ARGS=$ifn($or(--nollalign,-Mnollalign),--llalign);

variable MEDIUM is default();

# NEW MATH NAMES
append CGARGS=-x 164 0x800000;

command nocpp is hide
	help(Please use C++ compiler for C++ source files)
	suffix($CPPSUFFIX)
	echo($warning(Please use pgc++ for C++ source files: $input));

switch -mcmodel is
	help(Set the memory model)
	helpgroup(target)
	keyword(
	    small(help(Small model, total memory size < 2GB)
		append(CGARGS=-y 135 1 -y 68 0x1)
		append(F901ARGS=-y 68 0x1)
		set(STDRPATHSO=))
	    medium(help(Medium model, total memory size > 2GB, object size < 2GB; implies -Mlarge_arrays)
		append(CGARGS=-x 135 1 -x 68 0x1)
		append(F901ARGS=-x 68 0x1)
		set(STDRPATHSO=-rpath $COMPBASE/$COMPSYS/$COMPVER/$COMPLIBSUBDIR)
		set(MEDIUM=-mcmodel=medium)
		set(LLCMODEL=-code-model=medium))
	)
	onekeyword;

switch -Mlfs is
	help(Link with library directory for large file support)
	helpgroup(linker);

switch -shared is
	help(Used to produce shared libraries";" implies -fpic)
	helpgroup(linker)
	set(DYNAMICLINK=1)
	append(USRDDEF=-D__PIC__)
	append(USRDEFDEF=-def __PIC__)
	set(FPIC=-shared)
	set(LCRT1=)
	set(LCRTB=$GCCDIR/crtbeginS.o)
	set(LEVENTI=)
	set(LCRTE=$GCCDIR/crtendS.o)
	set(LNSPGC=)
	set(FTNMAIN=)
	set(STDRPATHSO=-rpath $COMPBASE/$COMPSYS/$COMPVER/$COMPLIBSUBDIR)
	append(CGARGS=-x 62 8)
	set(LDSHARED=-shared $if($LOCSCRIPT,$lookup($COMPBASE/$COMPSYS/$COMPVER/$(COMPLIBPREFIX)lib,nvhpcloc.ld)));

replace switch -Bdynamic is
        help(Passed to linker; specify dynamic binding)
        helpgroup(linker)
        set(PGISTATICX=)
        set(PGIUNSTATICX=)
        set(COMPLIBSTATICDIR=lib)
        set(COMPLIBSTATIC=$COMPBASE/$COMPSYS/$COMPVER/$COMPLIBSTATICDIR)
        # STDRPATH is reset below, so NVFLANG rpath must be added
        # explicitly to STDRPATHSO. Maybe we just need to stop
        # reseting STDRPATH (note that this only happens for x86-64)?
        set(STDRPATHSO=$if($isdir($FLANGLIBDIR),-rpath $FLANGLIBDIR) -rpath $COMPBASE/$COMPSYS/$COMPVER/$COMPLIBSUBDIR)
 	set(STDRPATH=)
        positional(linker);


set COMPLIBDIR=$COMPGLIBDIR lib;

# For native:      give error
# For llvm target: honor fPIC and ignore -code-model=medium in llvmrc
#                   $LLCMODEL is not applicable to native.
error($if($and($and($notequal($MEDIUM,),$notequal($FPIC,)), $equal($COMPSYS, linux86-64-nollvm)), Switches $FPIC and $MEDIUM are not supported together));
