#! /bin/sh -f
#
# shld.aix outfile-base version infile infile ...
#
# This shell script provides a wrapper for ld under AIX in order to
# create the .exp file required for linking.  Its arguments consist
# of the name and arguments that would normally be provided to the
# ld command.  This script extracts the names of the object files
# from the argument list, creates a .exp file describing all of the
# symbols exported by those files, and then invokes "ldCmd" to
# perform the real link.
#
# Inspired by ldAix from the tcl distribution.

args=''
outfile=''
ofiles=''
libdirs=''

die () {
  echo "$1" >&2
  exit 1
}

for arg in ` echo "$LIBRARY_PATH" | tr : ' ' `
do
  libdirs="$libdirs '-L$arg'"
done

while [ $# -gt 0 ]
do
  case "$1" in
  -o)
    shift
    test $# -gt 0 || die "missing arg for -o"
    outfile="$1"
    ;;
  *.o | *.o0)
    ofiles="$ofiles '$1'"
    args="$args '$1'"
    ;;
  -L*)
    libdirs="$libdirs '$1'"
    ;;
  -nolibs)
    ;;
  *)
    args="$args '$1'"
    ;;
  esac
  shift
done

test X"$ofiles" != X || die "no input files"
test X"$outfile" != X || die "no output file"

outfile=` echo "$outfile" | sed -e 's/\.\(so\|o\)$//' `.@SOEXT@

# Create the export file from all of the object files, using nm followed
# by sed editing.  Here are some tricky aspects of this:
#
# 1. Nm produces different output under AIX 4.1 than under AIX 3.2.5;
#    the following statements handle both versions.
# 2. Use the -g switch to nm instead of -e under 4.1 (this shows just
#    externals, not statics;  -g isn't available under 3.2.5, though).
# 3. Eliminate lines that end in ":": these are the names of object
#    files (relevant in 4.1 only).
# 4. Eliminate entries with the "U" key letter;  these are undefined
#    symbols (relevant in 4.1 only).
# 5. Eliminate lines that contain the string "0|extern" preceded by space;
#    in 3.2.5, these are undefined symbols (address 0).
# 6. Eliminate lines containing the "unamex" symbol.  In 3.2.5, these
#    are also undefined symbols.
# 7. If a line starts with ".", delete the leading ".", since this will
#    just cause confusion later.
# 8. Eliminate everything after the first field in a line, so that we're
#    left with just the symbol name.

nmopts="-g -C"
osver=`uname -v`
if test $osver -eq 3; then
  nmopts="-e -C"
fi
rm -f lib.exp
echo "#! " >lib.exp
eval "/usr/ccs/bin/nm $nmopts -h $ofiles" | sed -e '/:$/d' -e '/ U /d' -e '/_GLOBAL_./d' -e '/[ 	]0|extern/d' -e '/unamex/d' -e 's/^\.//' -e 's/[ 	|].*//' | sort | uniq >>lib.exp


ldcmd="@SHARED_CC@ @LDSOFLAGS@ @DLFLAGS@ $libdirs @LDFLAGS@ \
 -Wl,-bE:lib.exp,-bnoentry -o '$outfile' $args @LIBS@ -lstdc++"

echo "$ldcmd"
eval "$ldcmd"
exit $?
