#!/usr/bin/perl
#
# @(#)link_opts		1998-05-09
#
# common link options
#
# Gert Großmann (SAP AG, Basis Entwicklung Berlin)
#
# no package name, because it should run in the name space of the caller
use File::Basename;

my( $i, $maxi ) = ( 0, $#ARGV );
while( $_ = $ARGV[ $i ], ( /^-/ && $i<=$maxi ) )
{
    last if /^--$/;
    local $toolopt = undef; 
    if (/^-tooloption=(.*)/) {
        $toolopt = $1;
	}
	if (/^-toolopt=(.*)/) {
	    $toolopt =$1;
	}
	if (defined $toolopt) {
        if      ( $toolopt =~ /^CXX$/ )
        {
&if $OS in [ UNIX ]
            $LD = $CXX;
			$LDDLL = $LDDLL_CXX;
&else
			# under NT ignored	
&endif
			splice @ARGV, $i, 1; next;
        }
        if      ( $toolopt =~ /^CC$/ )  #PTS 1002110
        {
&if $OS in [ UNIX ]  
			$LD = $CC;
			$LDDLL = $LDDLL_C;
&else
			# under NT ignored	
&endif
			splice @ARGV, $i, 1; next;
        }

		if      ( $toolopt =~ /^CXX_CC$/ )  #CTS 1107045
        {
&if $OS in [ UNIX ]  
			$LD = $CC;
			$LDDLL = $LDDLL_CXX_C; 
&else
			# under NT ignored	
&endif
			splice @ARGV, $i, 1; next;
        }



        elsif ( $toolopt =~ /^nolinkdebug/ )
        {
            $NOLINKDEBUG = 1;
        }
        elsif( $toolopt =~ /^BuildNrFunction=(.*)/i)
        { 
            ToolLib::SetBuildNrModule($1);
        }
        else 
        {
            warn "unknown tool option $toolopt \n";
        }
        splice @ARGV, $i, 1; next;
    }

    if ( /^-linkoption=(.*)/)
    {
		local $linkopt = $1;
		if ( $linkopt =~ /^lib=(.+)/ )
		{
	        local $libname = $1.$ARCH_TYPE;
	        if ( $libname =~ /^ptocnts/i || $libname =~ /^ptocntr/i ) 
	        {
	          @LIBPC = ( "$TOOL/lib/$libname" );
	          ToolTrace("linklnk: link with @LIBPC (temporary)\n");
	        }
	        else 
	        { 
	            push @Lib,$libname; 
	        }
		}
		else
		{
			push @OFLAGS, $1;
		}
        splice @ARGV, $i, 1; next;
    }

    if ( /^-k$/ )
    {
        @KEEPFLAG = "-k";
        splice @ARGV, $i, 1; next;
    }
    elsif (/^-g$/) 
    { 
        $DEBUG = "y";
        warn "Warning: make with debug flag, but it's a productive make!\n" 
            if $ENV{RELSTAT} =~ /^prod/i;
        splice @ARGV, $i, 1; next;
    }
    elsif(/^-BuildNrFunction\s*=\s*(.*)/i)
    { 
        ToolLib::SetBuildNrModule($1);
        splice @ARGV, $i, 1; next;
    }
    elsif (/^-o$/) 
    {
        $OUTFILE = $ARGV[ $i + 1 ];
        $OUTFILE =~ /(.*)\.[fqs]/, $PROG = $1 ne "" ? $1 : $OUTFILE ;
        splice @ARGV, $i, 2; next;
    }
    elsif(/^-p$/) 
    { 
&if $OSSPEC in [ DYNIX, HPUX, MI_ULT, OSF1, SCO, SUNOS, SVR4 ]
        @LIBPC = $LIBPC_PROF;
&endif
&if $OSSPEC in [ AIX, MI_ULT, OSF1, SCO, SINIX, SUNOS, SVR4 ]
        push @OFLAGS, "-p";
&endif
&if $OSSPEC in [ HPUX ]
        push @OFLAGS, "-G";
&endif
&if $MACH = SUN AND $OSSPEC = SVR4
        @LIBDYN = "-ldl";
&endif
&if $OSSPEC in [ WIN32 ]
		$PROF = "penter.lib"; 
&endif
        splice @ARGV, $i, 1; next;
	}
    elsif(/^-Y$/) 
    {  
        # ignore -Y
        splice @ARGV, $i, 1; next;
    }
    elsif (/^-x=(.*)/) 
    { 
        $SCRIPT = $1;
        splice @ARGV, $i, 1; next;
    }
    if (/^-noscrlib$/) 
    { 
        @LIBSCR = ();
        splice @ARGV, $i, 1; next;
    }
&if $OSSPEC in [ WIN32 ]
    elsif (/^-stack=(.*)/) 
    {
        push @OFLAGS, "-stack:$1";
        splice @ARGV, $i, 1; next;
    }
    elsif(/^-ico=.*|^-dlg=.*|^-rcinc=.*|^-inc=.*/) 
    { 
        push @RCINCL, $_;
        splice @ARGV, $i, 1; next;
    }
    elsif(/^-rc=(.*)/)  
    { 
        $RCFILE = $1; 
        splice @ARGV, $i, 1; next;
    }
    elsif(/^-map:.*/)
    { 
        $MAP = $_; 
        splice @ARGV, $i, 1; next;
    }
    elsif(/^-nolinkdebug/)  
    { 
        $NOLINKDEBUG = 1;
        splice @ARGV, $i, 1; next;
    }
&endif
	elsif(/^-lib=(.*)/ && defined( $1 )) 
    {
        my $lib = $1;
        my $dir = dirname($lib);
& if $OSSPEC in [ WIN32 ]
        local $libname = ($dir ne "." ? $dir."/" : "").basename($lib, '.lib').".lib";
&else
        local $libname = ($dir ne "." ? $dir."/" : "").basename($lib, '.a').".a";
&endif
        if ( $libname =~ /^ptocnts/i || $libname =~ /^ptocntr/i ) 
        {
          @LIBPC = ( "$TOOL/lib/$libname" );
          ToolTrace("linklnk: link with @LIBPC (temporary)\n");
        }
        else 
        { 
            push @Lib,$libname; 
        }
        splice @ARGV, $i, 1; next;
	}

    $i++;
}

