#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/autoconf/ccpic2                      */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Thu Jan 14 10:31:33 1999                          */
#*    Last change :  Fri Sep  6 09:47:09 2002 (serrano)                */
#*    -------------------------------------------------------------    */
#*    Checking if the compiler and linker *require* -f[pic|PIC]        */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    flags                                                            */
#*---------------------------------------------------------------------*/
cc=gcc
cflags=""
tmp=/tmp
user=bigloo

#*---------------------------------------------------------------------*/
#*    We parse the arguments                                           */
#*---------------------------------------------------------------------*/
while : ; do
  case $1 in
    "")
      break;;

    --user=*)
      user="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --cc=*|-cc=*)
      cc="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --cflags=*|-cflags=*)
      cflags="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --cpicflags=*|-cpicflags=*)
      cpicflags="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --tmp=*|-tmp=*)
      tmp="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --ldopt=*|-ldopt=*)
      ldopt="`echo $1 | sed 's/^[-a-z]*=//'`";;

    -*)
      echo "Unknown option \"$1\", ignored" >&2;;
  esac
  shift
done

file=$tmp/actest$user
aout=$tmp/Xactest$user
obj=`basename $file`

#*---------------------------------------------------------------------*/
#*    compile                                                          */
#*---------------------------------------------------------------------*/
compile="$cc $cflags $file.c -o $aout >/dev/null 2> /dev/null"

#*---------------------------------------------------------------------*/
#*    Test                                                             */
#*---------------------------------------------------------------------*/
cat > $file-lib1.c <<EOF
int glob1 = 0;

int fun1 () { int x = -1; x++; glob1++; return glob1 + x++; }
EOF

cat > $file-lib2.c <<EOF
int glob2 = 0;

int fun2 () { int x = -1; x++; glob2++; return glob2 + x++; }
EOF

cat > $file.c <<EOF
extern int glob1, glob2;
int main () { 
   if( fun1() && fun2() && (glob1 > 0) && (glob2 > 0) ) 
      return 0;
   else 
      return 1;
}
EOF

#*---------------------------------------------------------------------*/
#*    Res                                                              */
#*---------------------------------------------------------------------*/
res=0

#*---------------------------------------------------------------------*/
#*    Compilation test 1                                               */
#*---------------------------------------------------------------------*/
if $cc $cflags -c $file-lib1.c -o $obj-lib1.o >/dev/null 2>&1
then
   true
else
   \rm -f $file*
   \rm -f $obj-lib*.o
   exit 2
fi

#*---------------------------------------------------------------------*/
#*    Compilation test 1                                               */
#*---------------------------------------------------------------------*/
if $cc $cflags -c $file-lib2.c -o $obj-lib2.o >/dev/null 2>&1
then
   true
else
   \rm -f $file*
   \rm -f $obj-lib*.o
   exit 3
fi

#*---------------------------------------------------------------------*/
#*    We build a library                                               */
#*---------------------------------------------------------------------*/
ld $ldopt -o $file.so $obj-lib1.o $obj-lib2.o > /dev/null 2> /dev/null
if [ ! -f $file.so ]; then
  \rm -f $file*
   \rm -f $obj-lib*.o
  exit 4
fi

#*---------------------------------------------------------------------*/
#*    linking test                                                     */
#*---------------------------------------------------------------------*/
if $cc $cflags -o $aout $file.c $file.so >/dev/null 2>&1
then
   true
else
   \rm -f $file*
   \rm -f $obj-lib*.o
   exit 5
fi

#*---------------------------------------------------------------------*/
#*    Execution test                                                   */
#*---------------------------------------------------------------------*/
res2=`$aout`

#*---------------------------------------------------------------------*/
#*    We try to run ranlib                                             */
#*---------------------------------------------------------------------*/
if [ "$res" = "2" ]; then
  echo ""
  \rm -f $file*
  \rm -f $obj-lib*.o
  exit 0
fi

if [ $res2 ]; then
  echo "";
  \rm -f $file*
  \rm -f $obj-lib*.o
  exit 0
fi

#*---------------------------------------------------------------------*/
#*    Compilation test 1                                               */
#*---------------------------------------------------------------------*/
if $cc $cflags $cpicflags -c $file-lib.c -o $obj-lib.o >/dev/null 2>&1
then
   true
else
   \rm -f $file*
   \rm -f $obj-lib*.o
   echo ""
   exit 2
fi

#*---------------------------------------------------------------------*/
#*    We build a library                                               */
#*---------------------------------------------------------------------*/
ld $ldopt -o $file.so $obj-lib.o > /dev/null 2> /dev/null
if [ ! -f $file.so ]; then
  \rm -f $file*
  \rm -f $obj-lib*.o
  echo ""
  exit 2
fi

#*---------------------------------------------------------------------*/
#*    linking test                                                     */
#*---------------------------------------------------------------------*/
if $cc $cflags $cpicflags -o $aout $file.c $file.so >/dev/null 2>&1
then
   true
else
   echo ""
   \rm -f $file*
   \rm -f $obj-lib*.o
   exit 2
fi

#*---------------------------------------------------------------------*/
#*    Execution test                                                   */
#*---------------------------------------------------------------------*/
res3=`$aout`

if [ $res3 ]; then
  echo $cpicflags;
else
  echo ""
fi

\rm -f $file*
\rm -f $obj-lib*.o
exit 0
