#!/usr/bin/perl
# ############################################################################
# (c) Copyright Virata Limited 1998
#
# Virata Limited Confidential and Proprietary
#
# The following software shell script ("Software") is strictly confidential and
# is proprietary to Virata Limited ("Virata").  It may only be read, used,
# copied, adapted, modified or otherwise dealt with by you if you have
# entered into a confidentiality agreement with Virata and then subject to the
# terms of that confidentiality agreement and any other applicable agreement
# between you and Virata.  If you are in any doubt as to whether you are
# entitled to access, read, use, copy, adapt, modify or otherwise deal with
# the Software or whether you are entitled to disclose the Software to any
# other person you should contact Virata.  If you have not entered into a
# confidentiality agreement with Virata granting access to this Software you
# should forthwith return all media, copies and printed listings containing
# the Software to Virata.
#
# Virata reserves the right to take legal action against you should you breach
# the above provisions.
#
# If you are unsure, or to report violations, please contact
# support@virata.com
# ############################################################################

# Respong to a -h option 
if ( $ARGV[0] eq "-h" )
{
    print <<"ENDHELP";
Usage: simulator_make_cat destination obj1 obj2 ...
simulator_make_cat concatenates a number of object files into a single objecn. 
It serves the same purpose in building a simulator image that catobj serves when
building a native ATMos image.
ENDHELP
    die "\n";
}

$temp_file = "/tmp/sim_$$";

# Check command line and read off arguments.
if ($#ARGV lt 1)
{
    die "Incorrect number of arguments supplied at command line.\n";
}

$destination_file = shift @ARGV;
$component_files = join(' ', @ARGV);

# Read the base directory of the current directory.
chop ($base_dir = `pwd`);
$base_dir =~ s/^.*\/([^\/]+)$/$1/;
$base_dir = "./$base_dir";

# Now see if which category the file is in and do different actions
# based on this:
#
# (a) In syslink.input and Shared:
#       ld -r $link_warnings -o$destination_file $component_files
# (b) In syslink.input and Object:
#       ld -r $link_warnings [--defsym <blah>] --retain-symbols-file <blah> -o$destination_file $component_files
# (c) Otherwise:
#       Same as (a)

open(SYSLINK, "<../syslink.input") || die "Couldn't open syslink.input: $!\n";

$allobjs = "";
$link_warnings = "--warn-common";
$final_command = "";



while(<SYSLINK>)
{
  # Each line in the syslink.input file is of the following format:
  #
  # (Shared|Object) *.o (x=y)*

  # Read the *.o part and split it into directory and file components.
  chomp;
  ($type, $filename, $stuff) = split(/\s+/, $_, 3);
  $filename =~ /^(.*)\/([^\/]+)$/;
  ($dir, $file) = ($1, $2);

  # See if this line in the syslink.input file corresponds to the
  # file we are building.
  if ($base_dir eq $dir && $destination_file eq $file)
  {
      $allobjs .= " $filename";

      # Prepare the file to hold the list of symbols to retain if doing an
      # Object build ((b) in the previous comment).
      unlink "$temp_file";
      open(SYMS, ">>$temp_file") || die "Couldn't open $temp_file: $!\n";

      $final_command .= "ld -r $link_warnings";
      
      if ($type eq "Shared")
      {
	  # Just do an ordinary link.
	  $final_command .= " -o$destination_file $component_files";
      }
      elsif ($type eq "Object")
      {
	  # Do a link with flair.
	  foreach $thing (split (/\s+/, $stuff))
	  {
	      ($lhs, $rhs) = split (/=/, $thing);
	      $lhs =~ s/^\_//;
	      $rhs =~ s/^\_//;
	      $final_command .= " --defsym $rhs=$lhs";
	      print SYMS "$rhs\n";
	  }
	  $final_command .= " --retain-symbols-file $temp_file -o$destination_file $component_files";
      }
      else
      {
	  print STDERR "Warning: ignoring unrecognised directive '$type'\n";
      }

      close SYMS;
  }
}

close SYSLINK;

if ($final_command eq "")
{
    $final_command = "ld -r $link_warnings -o$destination_file $component_files";
}

print "$final_command\n";
unlink $destination_file;
$retval = system($final_command);
unlink "$temp_file";

exit $retval;
