From 9be8fe9ed49e79f1c10dd389d6209b290e2efb40 Mon Sep 17 00:00:00 2001
From: Jerome Duval <jerome.duval@gmail.com>
Date: Sat, 16 Oct 2021 21:33:26 +0200
Subject: Haiku: port


diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8248866..4a8a692 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -269,7 +269,7 @@ if(_gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_IOS)
   set(_gRPC_ALLTARGETS_LIBRARIES ${CMAKE_DL_LIBS} m pthread)
 elseif(_gRPC_PLATFORM_ANDROID)
   set(_gRPC_ALLTARGETS_LIBRARIES ${CMAKE_DL_LIBS} m)
-elseif(UNIX)
+elseif(UNIX AND NOT HAIKU)
   set(_gRPC_ALLTARGETS_LIBRARIES ${CMAKE_DL_LIBS} rt m pthread)
 endif()
 
diff --git a/include/grpc/event_engine/port.h b/include/grpc/event_engine/port.h
index 9424586..5ee5327 100644
--- a/include/grpc/event_engine/port.h
+++ b/include/grpc/event_engine/port.h
@@ -20,7 +20,7 @@
 #if defined(GPR_ANDROID) || defined(GPR_LINUX) || defined(GPR_APPLE) ||     \
     defined(GPR_FREEBSD) || defined(GPR_OPENBSD) || defined(GPR_SOLARIS) || \
     defined(GPR_AIX) || defined(GPR_NACL) || defined(GPR_FUCHSIA) ||        \
-    defined(GRPC_POSIX_SOCKET)
+    defined(GPR_HAIKU) || defined(GRPC_POSIX_SOCKET)
 #define GRPC_EVENT_ENGINE_POSIX
 #include <arpa/inet.h>
 #include <netdb.h>
diff --git a/include/grpc/impl/codegen/port_platform.h b/include/grpc/impl/codegen/port_platform.h
index d9fdeed..145b60b 100644
--- a/include/grpc/impl/codegen/port_platform.h
+++ b/include/grpc/impl/codegen/port_platform.h
@@ -400,6 +400,30 @@
 #define GPR_HAS_PTHREAD_H 1
 #define GPR_GETPID_IN_UNISTD_H 1
 #define GRPC_ROOT_PEM_PATH "/config/ssl/cert.pem"
+#elif defined(__HAIKU__)
+#define GPR_PLATFORM_STRING "haiku"
+#ifndef _BSD_SOURCE
+#define _BSD_SOURCE
+#endif
+#define GPR_HAIKU 1
+#define GPR_CPU_POSIX 1
+#define GPR_GCC_ATOMIC 1
+#define GPR_POSIX_LOG 1
+#define GPR_POSIX_ENV 1
+#define GPR_POSIX_TMPFILE 1
+#define GPR_POSIX_STAT 1
+#define GPR_POSIX_STRING 1
+#define GPR_POSIX_SUBPROCESS 1
+#define GPR_POSIX_SYNC 1
+#define GPR_POSIX_TIME 1
+#define GPR_HAS_PTHREAD_H 1
+#define GPR_GETPID_IN_UNISTD_H 1
+#define GPR_SUPPORT_CHANNELS_FROM_FD 1
+#ifdef _LP64
+#define GPR_ARCH_64 1
+#else /* _LP64 */
+#define GPR_ARCH_32 1
+#endif /* _LP64 */
 #else
 #error "Could not auto-detect platform"
 #endif
diff --git a/src/core/lib/gpr/tls.h b/src/core/lib/gpr/tls.h
index 5c9b69e..d989134 100644
--- a/src/core/lib/gpr/tls.h
+++ b/src/core/lib/gpr/tls.h
@@ -144,7 +144,7 @@ class PthreadTlsImpl : TlsTypeConstrainer<T> {
 #else
 
 #define GPR_THREAD_LOCAL(type) \
-  thread_local typename grpc_core::TlsTypeConstrainer<type>::Type
+  __thread typename grpc_core::TlsTypeConstrainer<type>::Type
 
 #endif
 
diff --git a/src/core/lib/iomgr/port.h b/src/core/lib/iomgr/port.h
index 3e32429..28e9b11 100644
--- a/src/core/lib/iomgr/port.h
+++ b/src/core/lib/iomgr/port.h
@@ -180,6 +180,16 @@
 // TODO(rudominer) Check this does something we want.
 #define GRPC_POSIX_SOCKETUTILS 1
 #define GRPC_TIMER_USE_GENERIC 1
+#elif defined(GPR_HAIKU)
+#define GRPC_HAVE_ARPA_NAMESER 1
+#define GRPC_HAVE_IFADDRS 1
+#define GRPC_HAVE_IPV6_RECVPKTINFO 1
+#define GRPC_HAVE_UNIX_SOCKET 1
+#define GRPC_POSIX_FORK 1
+#define GRPC_POSIX_NO_SPECIAL_WAKEUP_FD 1
+#define GRPC_POSIX_SOCKET 1
+#define GRPC_POSIX_SOCKETUTILS 1
+#define GRPC_POSIX_WAKEUP_FD 1
 #elif !defined(GPR_NO_AUTODETECT_PLATFORM)
 #error "Platform not recognized"
 #endif
-- 
2.30.2


From f00bad0d226184c3360be70b3894140786c98427 Mon Sep 17 00:00:00 2001
From: Jerome Duval <jerome.duval@gmail.com>
Date: Fri, 22 Oct 2021 14:54:43 +0200
Subject: patch needed to help build and run tests on Haiku


diff --git a/tools/run_tests/python_utils/jobset.py b/tools/run_tests/python_utils/jobset.py
index 48503d3..500aafe 100755
--- a/tools/run_tests/python_utils/jobset.py
+++ b/tools/run_tests/python_utils/jobset.py
@@ -58,6 +58,8 @@ def platform_string():
         return 'mac'
     elif platform.system() == 'Linux':
         return 'linux'
+    elif platform.system() == 'Haiku':
+        return 'haiku'
     else:
         return 'posix'
 
diff --git a/tools/run_tests/run_tests.py b/tools/run_tests/run_tests.py
index 9fa3b4b..5ea7fa0 100755
--- a/tools/run_tests/run_tests.py
+++ b/tools/run_tests/run_tests.py
@@ -273,6 +273,19 @@ class CLanguage(object):
                 # see https://github.com/grpc/grpc/blob/b5b8578b3f8b4a9ce61ed6677e19d546e43c5c68/tools/run_tests/artifacts/artifact_targets.py#L253
                 self._cmake_configure_extra_args.append('-DOPENSSL_NO_ASM=ON')
 
+            if self.platform == 'haiku':
+                self._cmake_configure_extra_args.append('-DCMAKE_CXX_STANDARD=17')
+                self._cmake_configure_extra_args.append('-DCMAKE_POSITION_INDEPENDENT_CODE=NO')
+                self._cmake_configure_extra_args.append('-DgRPC_ABSL_PROVIDER=package')
+                self._cmake_configure_extra_args.append('-DgRPC_BENCHMARK_PROVIDER=package')
+                self._cmake_configure_extra_args.append('-DgRPC_CARES_PROVIDER=package')
+                self._cmake_configure_extra_args.append('-DgRPC_ZLIB_PROVIDER=package')
+                self._cmake_configure_extra_args.append('-DgRPC_PROTOBUF_PROVIDER=package')
+                self._cmake_configure_extra_args.append('-DgRPC_PROTOBUF_PACKAGE_TYPE=MODULE')
+                self._cmake_configure_extra_args.append('-DgRPC_RE2_PROVIDER=package')
+                self._cmake_configure_extra_args.append('-DgRPC_SSL_PROVIDER=package')
+                self._cmake_configure_extra_args.append('-DgRPC_ZLIB_PROVIDER=package')
+
     def test_specs(self):
         out = []
         binaries = get_c_tests(self.args.travis, self.test_lang)
-- 
2.30.2


From 11c8a037deda0da7031e015cf338180da21213d3 Mon Sep 17 00:00:00 2001
From: Jerome Duval <jerome.duval@gmail.com>
Date: Fri, 26 Nov 2021 20:17:18 +0100
Subject: protobuf minimum version 1.16.0


diff --git a/cmake/protobuf.cmake b/cmake/protobuf.cmake
index f23f65d..83fad47 100644
--- a/cmake/protobuf.cmake
+++ b/cmake/protobuf.cmake
@@ -55,7 +55,7 @@ if(gRPC_PROTOBUF_PROVIDER STREQUAL "module")
     set(gRPC_INSTALL FALSE)
   endif()
 elseif(gRPC_PROTOBUF_PROVIDER STREQUAL "package")
-  find_package(Protobuf REQUIRED ${gRPC_PROTOBUF_PACKAGE_TYPE})
+  find_package(Protobuf 1.16.0 REQUIRED ${gRPC_PROTOBUF_PACKAGE_TYPE})
 
   # {Protobuf,PROTOBUF}_FOUND is defined based on find_package type ("MODULE" vs "CONFIG").
   # For "MODULE", the case has also changed between cmake 3.5 and 3.6.
-- 
2.30.2

