From c66dbd16637cadb9f5caa3392cf6a1c0abae7fad Mon Sep 17 00:00:00 2001
From: Jerome Duval <jerome.duval@gmail.com>
Date: Sat, 27 Sep 2014 10:57:28 +0000
Subject: Patch from 4.10.6


diff --git a/configure.in b/configure.in
index fba01ae..c6f1967 100644
--- a/configure.in
+++ b/configure.in
@@ -1320,6 +1320,28 @@ case "$target" in
         ;;
     esac
     ;;
+    
+*-haiku*)
+    AC_DEFINE(XP_UNIX)
+    AC_DEFINE(HAIKU)
+    AC_DEFINE(HAVE_SOCKLEN_T)
+    AC_DEFINE(HAVE_FCNTL_FILE_LOCKING)
+    DSO_CFLAGS="-fPIC"
+    USE_PTHREADS=1
+    IMPL_STRATEGY=_PTH
+    MDCPUCFG_H=_linux.cfg
+    if test -n "$USE_64"; then
+    	PR_MD_ASFILES=os_Linux_x86_64.s
+    else
+    	PR_MD_ASFILES=os_Linux_x86.s
+    fi
+    PR_MD_CSRCS=linux.c
+    MKSHLIB='$(CC) $(DSO_LDOPTS) -o $@'
+    DSO_CFLAGS=-fPIC
+    DSO_LDOPTS='-shared -Wl,-soname -Wl,$(notdir $@)'
+    _OPTIMIZE_FLAGS=-O2
+    AC_CHECK_LIB(network, gethostbyaddr, [OS_LIBS="$OS_LIBS -lnetwork"])
+	;;
 
 *-bsdi*)
     AC_DEFINE(XP_UNIX)
@@ -2557,7 +2579,7 @@ dnl The same goes for BeOS.
 dnl OS/2 has dlfcn in libc.
 
 case $target in
-*-darwin*|*-beos*|*-os2*)
+*-darwin*|*-beos*|*-haiku*|*-os2*)
     ;;
 *)
     AC_CHECK_LIB(dl, dlopen,
@@ -2663,7 +2685,7 @@ fi
 esac
 
 case "$target_os" in
-darwin*)
+darwin*|haiku*)
     _HAVE_PTHREADS=1
     ;;
 *)
diff --git a/pr/include/md/_linux.h b/pr/include/md/_linux.h
index 278f90c..669fb42 100644
--- a/pr/include/md/_linux.h
+++ b/pr/include/md/_linux.h
@@ -261,7 +261,8 @@ static inline PRInt32 _MD_ATOMIC_SET(PRInt32 *ptr, PRInt32 nv)
 #endif /* __arm__ */
 
 #define USE_SETJMP
-#if (defined(__GLIBC__) && __GLIBC__ >= 2) || defined(ANDROID)
+#if (defined(__GLIBC__) && __GLIBC__ >= 2) || defined(ANDROID) \
+	|| defined(HAIKU)
 #define _PR_POLL_AVAILABLE
 #endif
 #undef _PR_USE_POLL
@@ -282,7 +283,7 @@ static inline PRInt32 _MD_ATOMIC_SET(PRInt32 *ptr, PRInt32 nv)
 #define _PR_HAVE_GETADDRINFO
 #define _PR_INET6_PROBE
 #endif
-#ifndef ANDROID
+#if !defined(ANDROID) && !defined(HAIKU)
 #define _PR_HAVE_SYSV_SEMAPHORES
 #define PR_HAVE_SYSV_NAMED_SHARED_MEMORY
 #endif
diff --git a/pr/include/md/_pth.h b/pr/include/md/_pth.h
index eeeef04..ef60438 100644
--- a/pr/include/md/_pth.h
+++ b/pr/include/md/_pth.h
@@ -117,7 +117,8 @@
 	|| defined(HPUX) || defined(FREEBSD) \
 	|| defined(NETBSD) || defined(OPENBSD) || defined(BSDI) \
 	|| defined(NTO) || defined(DARWIN) \
-	|| defined(UNIXWARE) || defined(RISCOS)	|| defined(SYMBIAN)
+	|| defined(UNIXWARE) || defined(RISCOS)	|| defined(SYMBIAN) \
+	|| defined(HAIKU)
 #define _PT_PTHREAD_INVALIDATE_THR_HANDLE(t)  (t) = 0
 #define _PT_PTHREAD_THR_HANDLE_IS_INVALID(t)  (t) == 0
 #define _PT_PTHREAD_COPY_THR_HANDLE(st, dt)   (dt) = (st)
@@ -170,7 +171,7 @@
 	|| defined(LINUX) || defined(__GNU__)|| defined(__GLIBC__) \
 	|| defined(FREEBSD) || defined(NETBSD) || defined(OPENBSD) \
 	|| defined(BSDI) || defined(UNIXWARE) \
-	|| defined(DARWIN) || defined(SYMBIAN)
+	|| defined(DARWIN) || defined(SYMBIAN) || defined(HAIKU)
 #define PT_NO_SIGTIMEDWAIT
 #endif
 
@@ -201,7 +202,7 @@
 #endif /* defined(_PR_DCETHREADS) */
 
 #elif defined(LINUX) || defined(__GNU__) || defined(__GLIBC__) \
-	|| defined(FREEBSD) || defined(SYMBIAN)
+	|| defined(FREEBSD) || defined(SYMBIAN) || defined(HAIKU)
 #define PT_PRIO_MIN            sched_get_priority_min(SCHED_OTHER)
 #define PT_PRIO_MAX            sched_get_priority_max(SCHED_OTHER)
 #elif defined(NTO)
@@ -261,7 +262,8 @@ extern int (*_PT_aix_yield_fcn)();
 	|| defined(LINUX) || defined(__GNU__) || defined(__GLIBC__) \
 	|| defined(FREEBSD) || defined(NETBSD) || defined(OPENBSD) \
 	|| defined(BSDI) || defined(NTO) || defined(DARWIN) \
-	|| defined(UNIXWARE) || defined(RISCOS) || defined(SYMBIAN)
+	|| defined(UNIXWARE) || defined(RISCOS) || defined(SYMBIAN) \
+	|| defined(HAIKU)
 #define _PT_PTHREAD_YIELD()            	sched_yield()
 #else
 #error "Need to define _PT_PTHREAD_YIELD for this platform"
diff --git a/pr/include/md/_unixos.h b/pr/include/md/_unixos.h
index 04d9904..5c3aa45 100644
--- a/pr/include/md/_unixos.h
+++ b/pr/include/md/_unixos.h
@@ -15,7 +15,7 @@
  * not be redefined.
  */
 #if !defined(LINUX) && !defined(__GNU__) && !defined(__GLIBC__) \
-    && !defined(DARWIN)
+    && !defined(DARWIN) && !defined(HAIKU)
 #ifndef FD_SETSIZE
 #define FD_SETSIZE  4096
 #endif
@@ -46,7 +46,7 @@
  */
 #include <sys/time.h>
 #include <sys/types.h>
-#if defined(AIX) || defined(SYMBIAN)
+#if defined(AIX) || defined(SYMBIAN) || defined(HAIKU)
 #include <sys/select.h>
 #endif
 
diff --git a/pr/include/md/prosdep.h b/pr/include/md/prosdep.h
index 94d8945..3cd48da 100644
--- a/pr/include/md/prosdep.h
+++ b/pr/include/md/prosdep.h
@@ -52,7 +52,7 @@ PR_BEGIN_EXTERN_C
 #elif defined(IRIX)
 #include "md/_irix.h"
 
-#elif defined(LINUX) || defined(__GNU__) || defined(__GLIBC__)
+#elif defined(LINUX) || defined(__GNU__) || defined(__GLIBC__) || defined(HAIKU)
 #include "md/_linux.h"
 
 #elif defined(OSF1)
diff --git a/pr/src/io/prmapopt.c b/pr/src/io/prmapopt.c
index f92a76b..d155e48 100644
--- a/pr/src/io/prmapopt.c
+++ b/pr/src/io/prmapopt.c
@@ -102,15 +102,15 @@ PRStatus PR_CALLBACK _PR_SocketGetSocketOption(PRFileDesc *fd, PRSocketOptionDat
             case PR_SockOpt_McastLoopback:
             {
 #ifdef WIN32 /* Winsock */
-                BOOL bool;
+                BOOL value;
 #else
-                PRUint8 bool;
+                PRUint8 value;
 #endif
-                length = sizeof(bool);
+                length = sizeof(value);
                 rv = _PR_MD_GETSOCKOPT(
-                    fd, level, name, (char*)&bool, &length);
+                    fd, level, name, (char*)&value, &length);
                 if (PR_SUCCESS == rv)
-                    data->value.mcast_loopback = (0 == bool) ? PR_FALSE : PR_TRUE;
+                    data->value.mcast_loopback = (0 == value) ? PR_FALSE : PR_TRUE;
                 break;
             }
             case PR_SockOpt_RecvBufferSize:
@@ -252,13 +252,13 @@ PRStatus PR_CALLBACK _PR_SocketSetSocketOption(PRFileDesc *fd, const PRSocketOpt
             case PR_SockOpt_McastLoopback:
             {
 #ifdef WIN32 /* Winsock */
-                BOOL bool;
+                BOOL value;
 #else
-                PRUint8 bool;
+                PRUint8 value;
 #endif
-                bool = data->value.mcast_loopback ? 1 : 0;
+                value = data->value.mcast_loopback ? 1 : 0;
                 rv = _PR_MD_SETSOCKOPT(
-                    fd, level, name, (char*)&bool, sizeof(bool));
+                    fd, level, name, (char*)&value, sizeof(value));
                 break;
             }
             case PR_SockOpt_RecvBufferSize:
diff --git a/pr/src/md/unix/linux.c b/pr/src/md/unix/linux.c
index 1b485a0..8a17e4e 100644
--- a/pr/src/md/unix/linux.c
+++ b/pr/src/md/unix/linux.c
@@ -25,12 +25,14 @@ PRWord *_MD_HomeGCRegisters(PRThread *t, int isCurrent, int *np)
 
 #ifdef _PR_PTHREADS
 
+#ifndef HAIKU
 extern void _MD_unix_terminate_waitpid_daemon(void);
 
 void _MD_CleanupBeforeExit(void)
 {
     _MD_unix_terminate_waitpid_daemon();
 }
+#endif
 
 #else /* ! _PR_PTHREADS */
 
diff --git a/pr/src/md/unix/uxrng.c b/pr/src/md/unix/uxrng.c
index da2f7e9..cbef872 100644
--- a/pr/src/md/unix/uxrng.c
+++ b/pr/src/md/unix/uxrng.c
@@ -86,7 +86,7 @@ GetHighResClock(void *buf, size_t maxbytes)
 
 #elif (defined(LINUX) || defined(FREEBSD) || defined(__FreeBSD_kernel__) \
     || defined(NETBSD) || defined(__NetBSD_kernel__) || defined(OPENBSD) \
-    || defined(SYMBIAN) || defined(__GNU__))
+    || defined(SYMBIAN) || defined(__GNU__)) || defined(HAIKU)
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
diff --git a/pr/src/misc/prnetdb.c b/pr/src/misc/prnetdb.c
index b86248f..0f07192 100644
--- a/pr/src/misc/prnetdb.c
+++ b/pr/src/misc/prnetdb.c
@@ -54,7 +54,7 @@ PRLock *_pr_dnsLock = NULL;
  * Some return a pointer to struct protoent, others return
  * an int, and glibc's flavor takes five arguments.
  */
-#if defined(XP_BEOS) && defined(BONE_VERSION)
+#if defined(HAIKU) || (defined(XP_BEOS) && defined(BONE_VERSION))
 #include <arpa/inet.h>  /* pick up define for inet_addr */
 #include <sys/socket.h>
 #define _PR_HAVE_GETPROTO_R
diff --git a/pr/src/pthreads/ptio.c b/pr/src/pthreads/ptio.c
index 125f1f9..fe51684 100644
--- a/pr/src/pthreads/ptio.c
+++ b/pr/src/pthreads/ptio.c
@@ -28,7 +28,7 @@
 #include <sys/uio.h>
 #include <sys/file.h>
 #include <sys/ioctl.h>
-#if defined(DARWIN)
+#if defined(DARWIN) || defined(HAIKU)
 #include <sys/utsname.h> /* for uname */
 #endif
 #if defined(SOLARIS) || defined(UNIXWARE)
@@ -180,7 +180,8 @@ static PRBool _pr_ipv6_v6only_on_by_default;
     || defined(LINUX) || defined(__GNU__) || defined(__GLIBC__) \
     || defined(FREEBSD) || defined(NETBSD) || defined(OPENBSD) \
     || defined(BSDI) || defined(NTO) || defined(DARWIN) \
-    || defined(UNIXWARE) || defined(RISCOS) || defined(SYMBIAN)
+    || defined(UNIXWARE) || defined(RISCOS) || defined(SYMBIAN) \
+    || defined(HAIKU)
 #define _PRSelectFdSetArg_t fd_set *
 #else
 #error "Cannot determine architecture"
@@ -3256,7 +3257,7 @@ static PRIOMethods _pr_socketpollfd_methods = {
     || defined(AIX) || defined(FREEBSD) || defined(NETBSD) \
     || defined(OPENBSD) || defined(BSDI) || defined(NTO) \
     || defined(DARWIN) || defined(UNIXWARE) || defined(RISCOS) \
-    || defined(SYMBIAN)
+    || defined(SYMBIAN) || defined(HAIKU)
 #define _PR_FCNTL_FLAGS O_NONBLOCK
 #else
 #error "Can't determine architecture"
diff --git a/pr/src/pthreads/ptthread.c b/pr/src/pthreads/ptthread.c
index 4ab564a..751454a 100644
--- a/pr/src/pthreads/ptthread.c
+++ b/pr/src/pthreads/ptthread.c
@@ -21,7 +21,7 @@
 #include <signal.h>
 #include <dlfcn.h>
 
-#ifdef SYMBIAN
+#if defined(SYMBIAN) || defined(HAIKU)
 /* In Open C sched_get_priority_min/max do not work properly, so we undefine
  * _POSIX_THREAD_PRIORITY_SCHEDULING here.
  */
@@ -1417,7 +1417,7 @@ static void suspend_signal_handler(PRIntn sig)
 #if !defined(FREEBSD) && !defined(NETBSD) && !defined(OPENBSD) \
     && !defined(BSDI) && !defined(UNIXWARE) \
     && !defined(DARWIN) && !defined(RISCOS) \
-    && !defined(SYMBIAN) /*XXX*/
+    && !defined(SYMBIAN) && !defined(HAIKU) /*XXX*/
         PRIntn rv;
 	    sigwait(&sigwait_set, &rv);
 #endif
@@ -1461,7 +1461,7 @@ static void pt_SuspendSet(PRThread *thred)
     PR_LOG(_pr_gc_lm, PR_LOG_ALWAYS, 
 	   ("doing pthread_kill in pt_SuspendSet thred %p tid = %X\n",
 	   thred, thred->id));
-#if defined(SYMBIAN)
+#if defined(SYMBIAN) 
     /* All signal group functions are not implemented in Symbian OS */
     rv = 0;
 #else
diff --git a/pr/tests/Makefile.in b/pr/tests/Makefile.in
index 9ebd923..5a3276e 100644
--- a/pr/tests/Makefile.in
+++ b/pr/tests/Makefile.in
@@ -369,7 +369,7 @@ LIBPTHREAD = -lpthread
 ifeq ($(OS_ARCH),AIX)
 LIBPTHREAD = -lpthreads
 endif
-ifeq (,$(filter-out FreeBSD OpenBSD BSD_OS QNX Darwin OpenUNIX,$(OS_ARCH)))
+ifeq (,$(filter-out Haiku FreeBSD OpenBSD BSD_OS QNX Darwin OpenUNIX,$(OS_ARCH)))
 LIBPTHREAD =
 endif
 ifeq ($(OS_ARCH)$(basename $(OS_RELEASE)),HP-UXB.10)
@@ -450,6 +450,15 @@ $(OBJDIR)/prpoll: $(OBJDIR)/prpoll.o
 	$(PURE) $(CC) $(XCFLAGS) $< $(LDOPTS) $(LIBPLC) $(LIBNSPR) -lsocket $(EXTRA_LIBS) -o $@
 endif
 
+ifeq ($(OS_ARCH),Haiku)
+ifeq ($(USE_IPV6),1)
+$(OBJDIR)/gethost: $(OBJDIR)/gethost.o
+	$(PURE) $(CC) $(XCFLAGS) $< $(LDOPTS) $(LIBPLC) $(LIBNSPR) -lnetwork $(EXTRA_LIBS) -o $@
+endif
+$(OBJDIR)/prpoll: $(OBJDIR)/prpoll.o
+	$(PURE) $(CC) $(XCFLAGS) $< $(LDOPTS) $(LIBPLC) $(LIBNSPR) -lnetwork $(EXTRA_LIBS) -o $@
+endif
+
 ifeq ($(USE_PTHREADS), 1)
 $(OBJDIR)/attach: $(OBJDIR)/attach.o
 	$(PURE) $(CC) $(XCFLAGS) $< $(LDOPTS) $(LIBPLC) $(LIBNSPR) $(LIBPTHREAD) $(EXTRA_LIBS) -o $@
-- 
1.8.3.4

