#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/autoconf/ccpic                       */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Thu Jan 14 10:31:33 1999                          */
#*    Last change :  Mon Dec 18 07:54:34 2017 (serrano)                */
#*    -------------------------------------------------------------    */
#*    Checking if the compiler and linker support -f[pic|PIC]          */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    flags                                                            */
#*---------------------------------------------------------------------*/
cflags=""

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

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

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

file=$TMP/actest$USER
aout=$TMP/Xactest$USER

# The default pic value
if [ "$cflags " = " " ]; then
  cflags=-fPIC
fi

#*---------------------------------------------------------------------*/
#*    gcc tuning                                                       */
#*---------------------------------------------------------------------*/
if [ "$CC" = "gcc" ]; then 
  eflags=-Werror
else
  eflags=
fi

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

#*---------------------------------------------------------------------*/
#*    The test C file                                                  */
#*---------------------------------------------------------------------*/
if( test -f $file.c ); then
   rm -f $file.c || exit $?
fi

#*---------------------------------------------------------------------*/
#*    Test                                                             */
#*---------------------------------------------------------------------*/
cat > $file.c <<EOF
#include <stdio.h>

void direction( int new_addr ) {
   static int *old_addr;
   static int flag = 0;

   if( !flag )
   {
      old_addr = &new_addr;
      flag = 1;
      direction( 2 );
   }
   else
   {
      old_addr > &new_addr ? puts( "1" ) : puts( "0" );
   }
}

int main( int argc, char *argv[] ) {
   direction( 1 );
}
EOF

#*---------------------------------------------------------------------*/
#*    Compilation test                                                 */
#*---------------------------------------------------------------------*/
if eval "$BUILDSH $compile"; then
   \rm -f $file.*
   echo $cflags
   exit 0
fi

#* cflags="-fPIE -pie"                                                 */
#* compile="$CC $eflags $cflags $file.c -o $aout >/dev/null"           */
#*                                                                     */
#* if eval "$BUILDSH $compile"; then                                   */
#*    \rm -f $file.*                                                   */
#*    echo $cflags                                                     */
#*    exit 0                                                           */
#* fi                                                                  */

cflags=-fPIC
compile="$CC $eflags $cflags $file.c -o $aout >/dev/null"

if eval "$BUILDSH $compile"; then
   \rm -f $file.*
   echo $cflags
   exit 0
fi

cflags=-fpic
compile="$CC $eflags $cflags $file.c -o $aout >/dev/null"

if eval "$BUILDSH $compile"; then
   \rm -f $file.*
   echo $cflags
   exit 0
fi

\rm -f $file.*
echo ""
exit 0
