#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/bigloo/autoconf/gccattrs             */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Fri Aug 18 06:43:10 2017                          */
#*    Last change :  Mon Jul 10 14:03:07 2023 (serrano)                */
#*    Copyright   :  2017-23 Manuel Serrano                            */
#*    -------------------------------------------------------------    */
#*    Testing CC compiler attributes                                   */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    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

#*---------------------------------------------------------------------*/
#*    compile                                                          */
#*---------------------------------------------------------------------*/
compile="$CC $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 <stdlib.h>

# define inline         inline __attribute__ ((always_inline))
# define __noinline     __attribute__ ((noinline))
# define __pure         __attribute__ ((pure))
# define __const        __attribute__ ((const))
# define __noreturn     __attribute__ ((noreturn))
# define __malloc       __attribute__ ((malloc))
# define __must_check   __attribute__ ((warn_unused_result))
# define __deprecated   __attribute__ ((deprecated))
# define __used         __attribute__ ((used))
# define __unused       __attribute__ ((unused))
# define __packed       __attribute__ ((packed))
# define __align(x)     __attribute__ ((aligned (x)))
# define __align_max    __attribute__ ((aligned))
# define likely(x)      __builtin_expect (!!(x), 1)
# define unlikely(x)    __builtin_expect (!!(x), 0)

__malloc void *foo() {
  return malloc( 10 );
}

__noreturn void stop() {
  exit( 0 );
}

__noinline void* bar(void* dummy) {
  return dummy;    
}

int main( int argc, char *argv[] ) {
   if( foo() != 0 ) {
     return 1;
   } else {
     stop();
   }
}
EOF

#*---------------------------------------------------------------------*/
#*    Compilation test                                                 */
#*---------------------------------------------------------------------*/
if eval "$BUILDSH $compile"; then
   \rm -f $file.*
   rm -f $aout
   rm -rf $aout*
   echo 1
else
   echo 0
fi

exit 0
