#!/devtool/local/bin/perl
#
#    ========== licence begin  GPL
#    Copyright (C) 2001 SAP AG
#
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2
#    of the License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#    ========== licence end
#

#
#    @(#)toolvars-tml.pl
#    included by several scripts, defines system specific values
#


use Env;
use Config;
use CCVers;
use POSIX;

# CTS 1110865
@THREAD_CCFLAGS=();
@THREAD_LDFLAGS=();
@LDFLAGS=();
@LDXXFLAGS=();

if ($RELVER eq "")
{
	print "\n!!! WARNING:  RELVER not set!!!\n";
	exit 1;
}

if (! $ENV{INSTROOT})
{
	print "\n!!! WARNING:  INSTROOT not set!!!\n";
	exit 1;
}

# force set of compiler version
CCVers::setup_compiler_version();


$ADABAS_COPYRIGHT="Copyright © SAP AG 1997";
if ($RELVER eq "R61") {
	$ADABAS_VERSION="6.1";
}
elsif ($RELVER eq "R62") {
	$ADABAS_VERSION="6.2";
}
elsif ($RELVER eq "R70") {
	$ADABAS_VERSION="7.0";
}
elsif ($RELVER eq "R71") {
	$ADABAS_VERSION="7.1";
}
elsif ($RELVER eq "R72") {
	$ADABAS_VERSION="7.2";
}
elsif ($RELVER eq "R73") {
	$ADABAS_VERSION="7.3";
}
elsif ($RELVER eq "R74") {
	$ADABAS_VERSION="7.4";
}
elsif ($RELVER eq "R80") {
	$ADABAS_VERSION="8.0";
}


$SQLVERSION="REL30";$ENV{SQLVERSION} = $SQLVERSION; # export $SQLVERSION

$OSSPEC=SVR4;
$ENV{OSSPEC} = $OSSPEC;		# export $OSSPEC
$MACH = "SUN";
$ENV{MACH} = $MACH;		# export $MACH
$OS = "UNIX";
$ENV{OS} = $OS;			# export $OS

# CTS 1106697
unless ( $ENV{OS_VER} && $ENV{OS_REL} )
{
    local
    ($os_sysname, $os_nodename, $os_release, $os_version, $os_machine) =
    POSIX::uname ();
    $ENV{OS_VER} = $os_version;
    $ENV{OS_REL} = $os_release;
}
$OS_VER = $ENV{OS_VER};
$OS_REL = $ENV{OS_REL};


if ($ENV{BIT64} eq "1") {
	$TOOL_LIB = "$TOOL/lib/lib64";
} else {
	$TOOL_LIB = "$TOOL/lib";
}
$ENV{TOOL_LIB} = $TOOL_LIB;

$COMP="PTOC";
if ($RELVER eq "R61") {
	#@CONCOMOPTS = ( "-REL=R61" );
	@CONCOMOPTS = ( "-$SQLVERSION=1", "-REL=R61" );
	#@CONCOMOPTS = ( "-SAG=1", "-$SQLVERSION=1", "-REL=R61" );
}
else
{
	#@CONCOMOPTS = ( );
	@CONCOMOPTS = ( "-$SQLVERSION=1" );
	#@CONCOMOPTS = ( "-SAG=1", "-$SQLVERSION=1" );
}
push @CONCOMOPTS, "-RELVER=$RELVER";
push @CONCOMOPTS, "-DIAGNOSE=1" if defined( $ENV{DIAGNOSE} );
#
# Used for all systems using 64 bit compilation!
#
if ( $ENV{BIT64} eq "1" )
{
	push @CONCOMOPTS, "-BIT64=1";
	if ( $ENV{XCOMP} eq "1" )
	{
		push @CONCOMOPTS, "-XCOMP=1";
	}
}
# used for 32 KB pages  (PTS 1111111)
if ( $ENV{page32KB} )
{
	push @CONCOMOPTS, "-page32KB=1";
}

# jrg 2.9.1999

if ( not $CC )
{
    $CC = "cc";
}
#else take CC from the environment
if ( not $CXX )
{
	$CXX = "CC";
}




# CTS 1107045
# $LDDLL_C         - command of link c objects to shared lib
# $LDDLL_CXX       - command of link c++ shared lib
# $LDDLL_CXX_C     - command of link c++ objects to c shared lib
# to initialize $LDDLL
$LDDLL_C = $CC;
$LDDLL_CXX = $CXX;
$LDDLL_CXX_C = "ld";
#else take CXX from the environment PTS 1001861

if (($RELVER ge "R72") && ($RELVER ne "RTOOL"))  # &gar PTS 1002110
{
	$LD = $CXX;
	$LDDLL_TYPE = "LDDLL_CXX";
}
else
{
	$LD	= $CC;
	$LDDLL_TYPE = "LDDLL_C";
}

# jrg 2.9.1999
# For relocatable objects (collection of object files) a different linker is used
# These definitions replace former system dependend setting in linkrel.pl!
#
# WIN32 uses $AR and $ARFLAGS...
$LDREL = "ld";

$AS		= $CC;
$AR		= "ar";
$NULL = "/dev/null";
# set C and CXX as environment variable
$ENV{CC}=$CC;
$ENV{CXX}=$CXX;

#
# Additional compiler flags may be set temporary in the environment
# before calling vmake.
#

if ( $ENV{BIT64} eq "1" )
{
	@ASFLAGS = ( "-xarch=v9" );
}
else
{
	@ASFLAGS = ();
}

# 98/06/19 &gar: for differences between C and C++-Compiling in UNIX !
# @CCFLAGS   -> FLAGS for both ( C, C++ )
# @CFLAGS    -> FLAGS for C only
# @CXXFLAGS  -> FLAGS for C++ only
  @CXXFLAGS = split /\s+/, $CXXFLAGS;
  @CFLAGS = split /\s+/, $CFLAGS;
@CCFLAGS = split /\s+/, $CCFLAGS;
push @CCFLAGS, "-D$SQLVERSION",  "-D$OSSPEC", "-D$MACH", "-DSAG";
push @CCFLAGS, "-DDIAGNOSE" if defined( $ENV{DIAGNOSE} );
push @CCFLAGS, "-DSAPDBMem_USE_SYSTEM_ALLOC" if defined( $ENV{SAPDBMem_USE_SYSTEM_ALLOC} );
@CCMODEANSI = ();
@CCMODENORM = ();
@CCMODELINT = ();

if ($ENV{APOMAKE})
{
	push @CCFLAGS,  "-DAPO_COM_SHORT=$ENV{APO_COM_SHORT}";

	push @CCFLAGS,  "-DAPO_PATCH_LEVEL=$ENV{APO_PATCH_LEVEL}" if ($ENV{APO_PATCH_LEVEL});
	#  "-DAPO_COM_RELEASE=$APO_COM_RELEASE",
	#  "-DAPO_COM_BUILD=$APO_COM_BUILD",


	push @CCFLAGS,  "-DBRANCH_INFO=$ENV{BRANCH_INFO}",
	                "-DLATEST_CHANGE_LIST=$ENV{LATEST_CHANGE_LIST}";
#	                "-DBRANCH_INFO_STR=\"$BRANCH_INFO\"",
#					"-DLATEST_CHANGE_LIST_STR=\"$LATEST_CHANGE_LIST\"";

	# BRANCH_INFO, BRANCH_INFO
	push @CCFLAGS, "-DSAPonUNIX", "-DSAPwithTHREADS";
	push @CCFLAGS, "-DSAP_JS_OS_VERSION=$OS_VER", "-DSAP_JS_OS_RELEASE=$OS_REL";
	push @CCFLAGS, "-DSAPonSUN", "+w";
}


# 1.8.2001 E.Z. PTS 1111111
if ( $ENV{page32KB} )
{
   push @CCFLAGS, "-Dpage32KB";
}


if ( $RELVER ge "R80" ) {
#
# gg 22.12.2001 put stlport directory in include search path
#
    push @CCFLAGS, "-I$WRK/incl/SAPDB/STLport/stlport";
#
# Some day this can be removed again if we can use src/SAPDB as new src...
#
    push @CCFLAGS, "-I$WRK/incl/SAPDB";
}
@EXCEPT_FLAG = ();
@RTTI_FLAG = ();

# check for LC_INC for APO
die "LC_INC is not defined in environment!\n" if ( $ENV{APOMAKE} && ! $ENV{LC_INC} );
push @CCFLAGS, "-I$ENV{LC_INC}" if ( $ENV{APOMAKE} && $ENV{LC_INC} );

	if ( $ENV{BIT64} ne "1" )
  {
   # -D_FILE_OFFSET_BITS=64 for large file support necessary (PTS 1108958)
   push @CCFLAGS, "-D_FILE_OFFSET_BITS=64" if ($RELVER ne "RTOOL");
  }

  if ( ($RELVER ge "R72") && ($RELVER ne "RTOOL")){

       # -mt forces multithreading. This implies -D_REENTRANT
       # -O forces 'correct' alignment of character arrays on local stack
       # no longer used after tests of Frank Hoffmann...
	   push @THREAD_CCFLAGS, "-mt";		# CTS 1110865
	   push @CCFLAGS, "-I$INSTROOT/incl", "-I$WRK/incl";


  } else {
#
# older versions ( < V7.2 ) do not support multithreading in any way...
#
    push @CCFLAGS, "-I$INSTROOT/incl", "-I$WRK/incl";

  }

#
# Some day this can be removed again if we can use src/SAPDB as new src...
#
  if (( $RELVER ge "R73" ) && ( ! $ENV{APOMAKE} )) {
  		push @CCFLAGS, "-I$WRK/incl/SAPDB";
    }



#
# jrg 24.9.1999
# This is needed since SOLARIS7 does not support i.e. sys_errlist[] on 64Bit...
# NOTE: If you have problem on a new solaris version, modify the comparison...
#
	$OSVERSION = `uname -r`;
	chop($OSVERSION);
	if (( $OSVERSION eq "5.7" ) || ( $OSVERSION eq "5.8" )) {
	    push @CCFLAGS, "-DSOLARIS7";
    }

    push @CFLAGS, "-Xt", "-xCC";
	if ( $ENV{BIT64} eq "1" )
	{
		push @CCFLAGS, "-xtarget=ultra", "-xarch=v9", "-DBIT64";
	}
    @LINTFLAGS = (@CCFLAGS, @CFLAGS);



if ($ENV{APOMAKE})
{
	@OPTIMIZE = ("-O2");
}
# "OPTIMIZE" now depends on "VMAKE_VERSION" (needed on AIX)
elsif ($CURR_VERSION =~ /^f/i) {
    # the old value, it was used for everything
	@OPTIMIZE = ("-xO4");
}
else {
	# VMAKE_VERSION is quick or slow
	# one level less than fast - not yet really tested
	# the idea is to combine range checks with less risky code
	@OPTIMIZE = ("-xO3");
}

# CTS 1103440
@DEBUG_FLAGS = ("-g");


# CTS 1108431
@NO_FLAGS = ();



# cba 98/07/16 : cause of differeces of C- and C++-Linker
# we may have different linker options on some UNIX-platforms
# we use @LDFLAGS for C-linking and @LDXXFLAGS for C++-Linking
# &gar 99/06/03: for linkdll new variable DLDFLAGS and DLDXXFLAGS
# ( PTS 1103001 )
# initialize
# CTS 1107045
# @DLD_CXX_C_FLAGS_START, DLD_CXX_C_FLAGS_END
# @DLD_CXX_C_FLAGS_START  options before objects
# @DLD_CXX_C_FLAGS_END    options after objects
@THREAD_LDFLAGS = ("-mt");   # CTS 1110865
if ($OSVERSION ne "5.7") {
	push @LDFLAGS, "-B", "static";
}
@THREAD_LDFLAGS = ("-mt");   # CTS 1110865
@DLDFLAGS = @DLDXXFLAGS = ("-G");
if ( $ENV{BIT64} eq "1" )
{
	push @LDXXFLAGS, "-xtarget=ultra", "-xarch=v9";
	push @LDFLAGS, "-xtarget=ultra", "-xarch=v9";
	push @DLDXXFLAGS, "-xtarget=ultra", "-xarch=v9";
	push @DLDFLAGS, "-xtarget=ultra", "-xarch=v9";
	@DLD_CXX_C_FLAGS_START = (
	"-G",
	"-R/opt/SUNWspro/lib/v9:/usr/ccs/lib/sparcv9:".
	"/usr/lib/sparcv9:/opt/SUNWspro/lib/v9",
	"/opt/SUNWspro/SC5.0/lib/v9/crti.o");
	@DLD_CXX_C_FLAGS_END = (
	"-L/opt/SUNWspro/lib/v9",
	"-lc",
	"-lCrun",
	"/opt/SUNWspro/SC5.0/lib/v9/crtn.o");
}
else
{
	@DLD_CXX_C_FLAGS_START = (
	"-G",
	"-R/opt/SUNWspro/lib:/usr/ccs/lib:/usr/lib:/opt/SUNWspro/lib",
	"/opt/SUNWspro/SC5.0/lib/crti.o");
	@DLD_CXX_C_FLAGS_END = (
	"-L/opt/SUNWspro/lib",
	"-lc", "-lCrun",
	"/opt/SUNWspro/SC5.0/lib/crtn.o");
}

#############################
# options for Multipasslink #
#############################

@KEEP_SYMBOLS_FIRST_OPTION = ();
@KEEP_SYMBOLS_OPTION_BEFORE_LIB = ();
@KEEP_SYMBOLS_OPTION_AFTER_LIB = ();

@KEEP_SYMBOLS_FIRST_OPTION=("-z",  "allextract" );

#
# For relocatable objects (collection of object files) a different linker is used
# These definitions replace system dependend setting in linkrel.pl!
#
# WIN32 uses AR and AS_FLAGS
#
# SUN:
# There is no specific option that tells ld   to  link  64-bit
# objects;  the  class of the first object that gets processed
# by ld  determines whether it is to perform  a  32-bit  or  a
# 64-bit link edit.
# AIX:
# 32bit and 64bit relocatable object may even coexist...
#
	@LDREL_FLAGS = ( "-r", "-L$TOOL_LIB" );

# &jrg 14.9.1999
#
# C++ compilation sometimes needs to setup a path to a directory for
# the template cache. In this template cache instances of template
# functions with external linkage are stored.
# Since template instances may differ depending on
# VMAKE_VERSION (fast,slow...) for each version a seperate
# template cache is forced
#
#
# -ptr option does not work YET with the current compiler... This is a bug
# that SUN CC promised to fix... Meanwhile instantiation is done static...
#
# push @CXXFLAGS, "-ptr$WRK/$VMAKE_VERSION";
# push @LDXXFLAGS, "-ptr$WRK/$VMAKE_VERSION";
# push @DLDXXFLAGS, "-ptr$WRK/$VMAKE_VERSION";
#
push @CXXFLAGS, "-instances=static" unless ($ENV{APOMAKE});

# &gar PTS 1001828
@PRF_FLAGS = "-xpg";

# &gar OFLAGS for call options
# used in link... and comp...
# always initialize here with ()
@OFLAGS=();

$SR_ID =   "static char ident_sr[] =";
$MF_ID =   "static char ident_mf[] =";
$PC_ID =   "static char ident_pc[] =";
$CC_ID  =  "static char ident_cc[] =";
$ID_END = ";";
$BUILD_ID      = "#ident";
$BUILD_ID_END  = "";

@PCFLAGS= ( "-qn", "-e" );
@ARFLAGS = ( "rcs" );
$RANLIB = "ar ts";

if ($ENV{APOMAKE})
{	@LIBPC=(); }
else
{
	@LIBPC = ( "$TOOL_LIB/libsqlptc.a" );	# static, not dynamic lib
	@LIBPC_PROF = ( "$TOOL_LIB/libsqlptc_p.a" );	# static, not dynamic lib
}



@LIBSCR = "-lcurses";
# 12.9.2002 Solaris8 do no longer support static linked libm
@LIBMATH = "-Bdynamic -lm";
@DYNAMIC_LIB = ("-Bdynamic");  # &gar PTS 1002144
@LIBNET = ( "-lsocket", "-lnsl" );
@LIBCSCI    = ();

#
# PTS 1002837 jrg 26.5.1999
@LIBMPROC = ();

# CTS 1106257
@LIBLD=();
if (( $RELVER ge "R72") && ($RELVER ne "RTOOL"))
{	@LIB_PTHREAD=("-lpthread"); }
else
{	@LIB_PTHREAD=(); }
@LIBAIO = ();


if (( $RELVER ge "R72") && ($RELVER ne "RTOOL")) {
	if ( $OSVERSION eq "5.6" ) {
		@LIBAIO = ("-lposix4");
	} else {
		@LIBAIO = ("-lrt");
	}
}

# &gar PTS 1106517
@LDSHMFLAGS=();

# jrg PTS 1110802
# FSt PTS 1115519
if ( $RELVER eq "R62")
{
   @LDSHMLIBS=();
}
else
{
   @LDSHMLIBS=( "-Bstatic", "-ldemangle", "-Bdynamic" );
}

if ( $SQLVERSION eq "REL30" )  { $TRACETYPE = "tsp_trace" }
	else { $TRACETYPE = "tracetext" }


##########
$PATH_SEPARATOR=":";

###########
# $PREPROCESS_FLAGS
@PP_FLAGS=("-P");

###########
if ($ENV{APOMAKE})
{
	$SYMBOL_DIR = "$INSTROOT/sap";
	$MAP_DIR    = "$INSTROOT/sap";
}
else
{
	$SYMBOL_DIR = "$INSTROOT/symbols";
	$MAP_DIR    = "$INSTROOT/Support/Mapfiles";
}

$SYMBOL_DIR = $ENV{FORCED_SYMBOL_DIR} if ($ENV{FORCED_SYMBOL_DIR});
$MAP_DIR = $ENV{FORCED_MAP_DIR} if ($ENV{FORCED_MAP_DIR});


#################################################
# vmake environment variables must be set by hand
#################################################

if ( ! $DEVENV ) {
#   $DEVENV = "YES";		# Pascal vsp-modules
    $DEVENV = "NO";		# Assembler or C vsp-modules
}
$ENV{DEVENV} = $DEVENV; 	# export $DEVENV

undef($CCOMP);
$CCOMP = "GENC";
$ENV{CCOMP} = $CCOMP;
undef($CPPCOMP);
undef($COBCOMP);
undef($FORCOMP);
undef($PASCOMP);


# ================================================================

$DLL_TYPE = "so";

push @CCFLAGS, "-KPIC";			# "position independent code" required
$ENV{DLL_TYPE} = $DLL_TYPE;		# can be exported even if not set

$ARCH_TYPE = ".a";
$ENV{ARCH_TYPE} = $ARCH_TYPE;		# can be exported even if not set

#################################################
#  IGNORE following strings in output
#  separate with "|"
#################################################

# string for compile
#
$CC_IGNORE="";

# string for link
$LNK_IGNORE="";

sub setvar {
    my ($varname, $commonDefault, $internalDefault) = @_;
    if (!defined ($internalDefault)) {
        $internalDefault = $commonDefault;
    }
    my ($value);
    if ($ENV{$varname}) {
        $value = $ENV{$varname};
    }
    else {
        if ($ENV{SAPDB_INTERNAL}) {
            $value = $internalDefault;
        }
        else {
            $value = $commonDefault;
        }
        $ENV{$varname} = $value;
    }
    eval '$' . "$varname = '$value'";
}

setvar ('PYTHON', 'NO', "$TOOL/Python");
setvar ('PYTHON_INCLUDE', $ENV{PYTHON} . '/include');
setvar ('ACTIVEPERL_INCLUDE', 'NO', 'NO');
setvar ('ACTIVEPERL_LIB', 'NO', 'NO');
setvar ('COREPERL_INCLUDE', $Config{'archlib'} . "/CORE");
setvar ('COREPERL_LIB', $Config{'archlib'} . "/CORE");
setvar ('JDK_INCLUDE', 'NO', "$TOOL/java/include");
setvar ('JAVA_HOME', 'NO', "$TOOL/java");

# set installation directory for libraries and scripting interfaces
if ($ENV{BIT64} eq "1" && $MACH ne "ALPHA" && $MACH ne "IA64")
{
    #all 64-bit-platforms should use lib/lib64
    $ENV{INSTLIB} = $INSTROOT."/lib/lib64";
    $ENV{INSTMISC} = $INSTROOT."/misc/lib64";
} else {
    $ENV{INSTLIB} = $INSTROOT."/lib";
    $ENV{INSTMISC} = $INSTROOT."/misc";
}

unless (defined $ENV{INSTRUNTIME}) {
	require GetInstDir;
	$ENV{INSTRUNTIME} = GetInstDir::GetInstRuntime ();
}

unless (defined $ENV{SAPDBSDK}) {
	require GetInstDir;
	$ENV{SAPDBSDK} = GetInstDir::GetSDKRoot ();
}

unless (defined $ENV{SAPDBSDKLIB}) {
	require GetInstDir;
	$ENV{SAPDBSDKLIB} = GetInstDir::GetSDKRootLib ();
}

unless (defined $ENV{SAPDBSDKPGM}) {
	require GetInstDir;
	$ENV{SAPDBSDKPGM} = GetInstDir::GetSDKRootPgm ();
}

$TraceFile = "$WRK/$VMAKE_VERSION/prot/trace.prot";
sub ToolTrace {
	# usage ToolTrace($string, \@daten)
	if ($ENV{TOOLTRACE}) {
		local($string, $r_daten) = @_;
		open(TOOLTRACE, ">>$TraceFile") ||
			die "Can't open $TraceFile (append): $!\n";
		print TOOLTRACE sprintf($string, @$r_daten);
		close(TOOLTRACE);
	}
}

#
# Override toolvars variables through env variable
#    example: export TOOLVARS_PATCH="OS=WIN32;OSSPEC=WIN32;MACH=I386"
#    useful to generate vmake dependencies for any platform without
#    remote compilation
#
if ($ENV{TOOLVARS_PATCH}) {
    my @entries = split (';', $ENV{TOOLVARS_PATCH});
    foreach my $entry (@entries) {
        if ($entry =~ /(.*)=(.*)/) {
            eval '$' . $1 . "=" . $2 . ";";
            $ENV{$1} = $2;
        }
    }
}

# initializing flush of output buffer
$|=1; print "";

