#! /bin/sh -e

# Description: Ada support for GNU/k*BSD 
# Author: Aurelien Jarno <aurel32@debian.Org>
# Status: submitted.

dir=
if [ $# -eq 3 -a "$2" = '-d' ]; then
    pdir="-d $3"
    dir="$3/"
elif [ $# -ne 1 ]; then
    echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
    exit 1
fi
case "$1" in
    -patch)
        patch $pdir -f --no-backup-if-mismatch -p1 < $0
        ;;
    -unpatch)
        patch $pdir -f --no-backup-if-mismatch -R -p1 < $0
        ;;
    *)
        echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
        exit 1
esac
exit 0

# append the patch here and adjust the -p? flag in the patch calls.

--- src/gcc/ada/Makefile.in.orig	2008-02-09 13:03:14 +0100
+++ src/gcc/ada/Makefile.in	2008-02-09 13:03:33 +0100
@@ -866,6 +866,8 @@
   s-osinte.ads<s-osinte-kfreebsd-gnu.ads \
   s-osprim.adb<s-osprim-posix.adb \
   s-taprop.adb<s-taprop-linux.adb \
+  s-tasinf.ads<s-tasinf-linux.ads \
+  s-tasinf.adb<s-tasinf-linux.adb \
   s-taspri.ads<s-taspri-posix.ads \
   s-tpopsp.adb<s-tpopsp-posix-foreign.adb \
   system.ads<system-freebsd-x86.ads
--- src/gcc/ada/s-osinte-kfreebsd-gnu.ads.orig	2008-02-09 12:18:43 +0100
+++ src/gcc/ada/s-osinte-kfreebsd-gnu.ads	2008-02-09 13:00:36 +0100
@@ -223,7 +223,8 @@
    function sysconf (name : int) return long;
    pragma Import (C, sysconf);
 
-   SC_CLK_TCK : constant := 2;
+   SC_CLK_TCK          : constant := 2;
+   SC_NPROCESSORS_ONLN : constant := 84;
 
    -------------------------
    -- Priority Scheduling --
@@ -235,7 +236,7 @@
 
    function To_Target_Priority
      (Prio : System.Any_Priority) return Interfaces.C.int;
-   --  Maps System.Any_Priority to a POSIX priority.
+   --  Maps System.Any_Priority to a POSIX priority
 
    -------------
    -- Process --
@@ -255,6 +256,7 @@
 
    type Thread_Body is access
      function (arg : System.Address) return System.Address;
+   pragma Convention (C, Thread_Body);
 
    function Thread_Body_Access is new
      Unchecked_Conversion (System.Address, Thread_Body);
@@ -438,12 +440,31 @@
    pragma Import (C, pthread_getspecific, "pthread_getspecific");
 
    type destructor_pointer is access procedure (arg : System.Address);
+   pragma Convention (C, destructor_pointer);
 
    function pthread_key_create
      (key        : access pthread_key_t;
       destructor : destructor_pointer) return int;
    pragma Import (C, pthread_key_create, "pthread_key_create");
 
+   CPU_SETSIZE : constant := 1_024;
+
+   type bit_field is array (1 .. CPU_SETSIZE) of Boolean;
+   for bit_field'Size use CPU_SETSIZE;
+   pragma Pack (bit_field);
+   pragma Convention (C, bit_field);
+
+   type cpu_set_t is record
+      bits : bit_field;
+   end record;
+   pragma Convention (C, cpu_set_t);
+
+   function pthread_setaffinity_np
+     (thread     : pthread_t;
+      cpusetsize : size_t;
+      cpuset     : access cpu_set_t) return int;
+   pragma Import (C, pthread_setaffinity_np, "__gnat_pthread_setaffinity_np");
+
 private
 
    type sigset_t is array (1 .. 4) of unsigned;
--- src/gcc/ada/adaint.c	2008-02-10 03:14:20 +0100
+++ src/gcc/ada/adaint.c	2008-02-09 18:22:02 +0100
@@ -888,7 +888,7 @@
   strcpy (path, "GNAT-XXXXXX");
 
 #if (defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) \
-  || defined (linux)) && !defined (__vxworks)
+  || defined (linux) || defined(__GLIBC__)) && !defined (__vxworks)
   return mkstemp (path);
 #elif defined (__Lynx__)
   mktemp (path);
@@ -981,7 +981,7 @@
   }
 
 #elif defined (linux) || defined (__FreeBSD__) || defined (__NetBSD__) \
-  || defined (__OpenBSD__)
+  || defined (__OpenBSD__) || defined(__GLIBC__)
 #define MAX_SAFE_PATH 1000
   char *tmpdir = getenv ("TMPDIR");
 
@@ -3001,7 +3001,7 @@
 }
 #endif
 
-#if defined (linux)
+#if defined (linux) || defined(__GLIBC__)
 /* pthread affinity support */
 
 #ifdef CPU_SETSIZE
--- src/gcc/ada/socket.c	2007-10-19 15:14:33 +0200
+++ src/gcc/ada/socket.c	2008-02-09 18:23:40 +0100
@@ -206,7 +206,7 @@
   struct hostent *rh;
   int ri;
 
-#ifdef __linux__
+#if defined(__linux__) || defined(__GLIBC__)
   (void) gethostbyname_r (name, ret, buf, buflen, &rh, h_errnop);
 #else
   rh = gethostbyname_r (name, ret, buf, buflen, h_errnop);
@@ -223,7 +223,7 @@
   struct hostent *rh;
   int ri;
 
-#ifdef __linux__
+#if defined(__linux__) || defined(__GLIBC__)
   (void) gethostbyaddr_r (addr, len, type, ret, buf, buflen, &rh, h_errnop);
 #else
   rh = gethostbyaddr_r (addr, len, type, ret, buf, buflen, h_errnop);
@@ -239,7 +239,7 @@
   struct servent *rh;
   int ri;
 
-#ifdef __linux__
+#if defined(__linux__) || defined(__GLIBC__)
   (void) getservbyname_r (name, proto, ret, buf, buflen, &rh);
 #else
   rh = getservbyname_r (name, proto, ret, buf, buflen);
@@ -255,7 +255,7 @@
   struct servent *rh;
   int ri;
 
-#ifdef __linux__
+#if defined(__linux__) || defined(__GLIBC__)
   (void) getservbyport_r (port, proto, ret, buf, buflen, &rh);
 #else
   rh = getservbyport_r (port, proto, ret, buf, buflen);
--- src/gcc/ada/gsocket.h.orig	2008-02-10 11:50:18 +0100
+++ src/gcc/ada/gsocket.h	2008-02-10 11:50:38 +0100
@@ -167,7 +167,7 @@
 
 #if defined (_AIX) || defined (__FreeBSD__) || defined (__hpux__) || defined (__osf__) || defined (_WIN32) || defined (__APPLE__)
 # define HAVE_THREAD_SAFE_GETxxxBYyyy 1
-#elif defined (sgi) || defined (linux) || (defined (sun) && defined (__SVR4) && !defined (__vxworks))
+#elif defined (sgi) || defined (linux) || defined (__GLIBC__) || (defined (sun) && defined (__SVR4) && !defined (__vxworks))
 # define HAVE_GETxxxBYyyy_R 1
 #endif
 
--- src/gcc/ada/sysdep.c.orig	2008-02-10 11:55:28 +0100
+++ src/gcc/ada/sysdep.c	2008-02-10 11:58:15 +0100
@@ -342,7 +342,8 @@
   || (defined (__osf__) && ! defined (__alpha_vxworks)) || defined (WINNT) \
   || defined (__MACHTEN__) || defined (__hpux__) || defined (_AIX) \
   || (defined (__svr4__) && defined (i386)) || defined (__Lynx__) \
-  || defined (__CYGWIN__) || defined (__FreeBSD__) || defined (__OpenBSD__)
+  || defined (__CYGWIN__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
+  || defined (__GLIBC__)
 
 #ifdef __MINGW32__
 #if OLD_MINGW
@@ -399,7 +400,8 @@
     || (defined (__osf__) && ! defined (__alpha_vxworks)) \
     || defined (__CYGWIN32__) || defined (__MACHTEN__) || defined (__hpux__) \
     || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
-    || defined (__Lynx__) || defined (__FreeBSD__) || defined (__OpenBSD__)
+    || defined (__Lynx__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
+    || defined (__GLIBC__)
   char c;
   int nread;
   int good_one = 0;
@@ -418,7 +420,8 @@
 #if defined(linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
     || defined (__osf__) || defined (__MACHTEN__) || defined (__hpux__) \
     || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
-    || defined (__Lynx__) || defined (__FreeBSD__) || defined (__OpenBSD__)
+    || defined (__Lynx__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
+    || defined (__GLIBC__)
       eof_ch = termios_rec.c_cc[VEOF];
 
       /* If waiting (i.e. Get_Immediate (Char)), set MIN = 1 and wait for
@@ -845,7 +848,7 @@
 /* Darwin, Free BSD, Linux, Tru64, where there exists a component tm_gmtoff
    in struct tm */
 #elif defined (__APPLE__) || defined (__FreeBSD__) || defined (linux) ||\
-     (defined (__alpha__) && defined (__osf__))
+     (defined (__alpha__) && defined (__osf__)) || defined (__GLIBC__)
   *off = tp->tm_gmtoff;
 
 /* All other platforms: Treat all time values in GMT */
--- src/gcc/ada/link.c.orig	2008-02-10 11:59:07 +0100
+++ src/gcc/ada/link.c	2008-02-10 11:59:14 +0100
@@ -153,7 +153,7 @@
 unsigned char __gnat_using_gnu_linker = 1;
 const char *__gnat_object_library_extension = ".a";
 
-#elif defined (linux)
+#elif defined (linux) || defined(__GLIBC__)
 const char *__gnat_object_file_option = "";
 const char *__gnat_run_path_option = "";
 char __gnat_shared_libgnat_default = SHARED;
