From d73a0ac0fc0272ec87809a73bf9253a71b153d8c Mon Sep 17 00:00:00 2001
From: Niklas Poslovski <ni.pos@yandex.com>
Date: Sat, 15 Jun 2024 10:31:08 +0200
Subject: [PATCH 01/14] LibCore: Don't use secure_getenv on Haiku

---
 Userland/Libraries/LibCore/Environment.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Userland/Libraries/LibCore/Environment.cpp b/Userland/Libraries/LibCore/Environment.cpp
index b5c3473169..dc6bf92948 100644
--- a/Userland/Libraries/LibCore/Environment.cpp
+++ b/Userland/Libraries/LibCore/Environment.cpp
@@ -94,7 +94,7 @@ Optional<StringView> get(StringView name, [[maybe_unused]] SecureOnly secure)
     // Note the explicit null terminators above.
 
     // FreeBSD < 14, and generic BSDs do not support secure_getenv.
-#if (defined(__FreeBSD__) && __FreeBSD__ >= 14) || !defined(AK_OS_BSD_GENERIC)
+#if (defined(__FreeBSD__) && __FreeBSD__ >= 14) || (!defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_HAIKU))
     char* result;
     if (secure == SecureOnly::Yes) {
         result = ::secure_getenv(builder.string_view().characters_without_null_termination());
-- 
2.52.0


From a35e3bd0fbaeea38b8dcdd307ead9f3d6ba77b5d Mon Sep 17 00:00:00 2001
From: Niklas Poslovski <ni.pos@yandex.com>
Date: Sat, 15 Jun 2024 17:14:47 +0200
Subject: [PATCH 02/14] LibGfx: Link LibGfx to libgnu, the GNU compatibility
 library, on Haiku

---
 Userland/Libraries/LibGfx/CMakeLists.txt | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/Userland/Libraries/LibGfx/CMakeLists.txt b/Userland/Libraries/LibGfx/CMakeLists.txt
index 16e4a659fd..f79f7c0403 100644
--- a/Userland/Libraries/LibGfx/CMakeLists.txt
+++ b/Userland/Libraries/LibGfx/CMakeLists.txt
@@ -111,6 +111,9 @@ set(SOURCES
 
 serenity_lib(LibGfx gfx)
 target_link_libraries(LibGfx PRIVATE LibCompress LibCore LibCrypto LibFileSystem LibRIFF LibTextCodec LibIPC LibUnicode LibURL)
+if (HAIKU)
+    target_link_libraries(LibGfx PRIVATE gnu)
+endif()
 
 set(generated_sources TIFFMetadata.h TIFFTagHandler.cpp)
 list(TRANSFORM generated_sources PREPEND "ImageFormats/")
-- 
2.52.0


From 66c062839f3432ba765df65a40db853932c48ee0 Mon Sep 17 00:00:00 2001
From: Niklas Poslovski <ni.pos@yandex.com>
Date: Sat, 15 Jun 2024 17:17:23 +0200
Subject: [PATCH 03/14] LibCore: Remove the Haiku-specific runtime_directory
 from StandardPaths

---
 Userland/Libraries/LibCore/StandardPaths.cpp | 2 --
 1 file changed, 2 deletions(-)

diff --git a/Userland/Libraries/LibCore/StandardPaths.cpp b/Userland/Libraries/LibCore/StandardPaths.cpp
index 995c992e8f..acd46eebbd 100644
--- a/Userland/Libraries/LibCore/StandardPaths.cpp
+++ b/Userland/Libraries/LibCore/StandardPaths.cpp
@@ -156,8 +156,6 @@ ErrorOr<ByteString> StandardPaths::runtime_directory()
 #elif defined(AK_OS_MACOS)
     builder.append(home_directory());
     builder.append("/Library/Application Support"sv);
-#elif defined(AK_OS_HAIKU)
-    builder.append("/boot/system/var/shared_memory"sv);
 #elif defined(AK_OS_LINUX)
     auto uid = getuid();
     builder.appendff("/run/user/{}", uid);
-- 
2.52.0


From 6bf10a4c91156567782ef6d2c5b2a0c1be80a684 Mon Sep 17 00:00:00 2001
From: Niklas Poslovski <ni.pos@yandex.com>
Date: Sat, 15 Jun 2024 17:18:44 +0200
Subject: [PATCH 04/14] LibCore: Don't do fchmod at sockets on Haiku

---
 Userland/Libraries/LibCore/Process.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Userland/Libraries/LibCore/Process.cpp b/Userland/Libraries/LibCore/Process.cpp
index 02a3ce570e..04740450b2 100644
--- a/Userland/Libraries/LibCore/Process.cpp
+++ b/Userland/Libraries/LibCore/Process.cpp
@@ -436,7 +436,7 @@ ErrorOr<int> IPCProcess::create_ipc_socket(ByteString const& socket_path)
     TRY(System::fcntl(socket_fd, F_SETFD, FD_CLOEXEC));
 #endif
 
-#if !defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_GNU_HURD)
+#if !defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_GNU_HURD) && !defined(AK_OS_HAIKU)
     TRY(System::fchmod(socket_fd, 0600));
 #endif
 
-- 
2.52.0


From c789c07dd39c15cd32849e667a56da5235975044 Mon Sep 17 00:00:00 2001
From: Niklas Poslovski <ni.pos@yandex.com>
Date: Sat, 15 Jun 2024 17:21:18 +0200
Subject: [PATCH 05/14] LibCore: Avoid creating existing directories on Haiku

---
 Userland/Libraries/LibCore/Directory.cpp | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/Userland/Libraries/LibCore/Directory.cpp b/Userland/Libraries/LibCore/Directory.cpp
index 482ae4ecce..4a17a33b35 100644
--- a/Userland/Libraries/LibCore/Directory.cpp
+++ b/Userland/Libraries/LibCore/Directory.cpp
@@ -74,6 +74,18 @@ ErrorOr<void> Directory::ensure_directory(LexicalPath const& path, mode_t creati
 
     TRY(ensure_directory(path.parent(), creation_mode));
 
+#ifdef AK_OS_HAIKU
+    // Haiku throws an error when trying to create a directory that already exists, but is part of the read-only packagefs.
+    // This can be worked-around by checking if the directory exists first, and then return without recreating it.
+    auto st_or_error = System::stat(path.string());
+    if (!st_or_error.is_error())
+    {
+        auto st = st_or_error.release_value();
+        if S_ISDIR(st.st_mode)
+            return {};
+    }
+#endif
+
     auto return_value = System::mkdir(path.string(), creation_mode);
     // We don't care if the directory already exists.
     if (return_value.is_error() && return_value.error().code() != EEXIST)
-- 
2.52.0


From 1176fe4ee6b6e387e33dbb5a17fb86528ae5c86d Mon Sep 17 00:00:00 2001
From: Niklas Poslovski <ni.pos@yandex.com>
Date: Sat, 15 Jun 2024 17:23:34 +0200
Subject: [PATCH 06/14] Meta: Link AK to libbsd, the BSD compatibility library,
 on Haiku

---
 Meta/Lagom/CMakeLists.txt | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/Meta/Lagom/CMakeLists.txt b/Meta/Lagom/CMakeLists.txt
index 9414783308..a84e0aed11 100644
--- a/Meta/Lagom/CMakeLists.txt
+++ b/Meta/Lagom/CMakeLists.txt
@@ -375,6 +375,10 @@ install(TARGETS LibC LibCrypt LibSystem NoCoverage EXPORT LagomTargets)
 # AK
 add_serenity_subdirectory(AK)
 lagom_lib(AK ak SOURCES ${AK_SOURCES})
+if (HAIKU)
+    # Haiku has some BSD-compatibility functions that we need in libbsd
+    target_link_libraries(AK PRIVATE bsd)
+endif()
 find_package(Backtrace)
 if (Backtrace_FOUND AND NOT "${Backtrace_LIBRARIES}" STREQUAL "")
     target_link_libraries(AK PRIVATE ${Backtrace_LIBRARIES})
-- 
2.52.0


From b50f8a5d0dce2059af81e74b5b7dac19412101e4 Mon Sep 17 00:00:00 2001
From: Niklas Poslovski <ni.pos@yandex.com>
Date: Sat, 15 Jun 2024 17:24:18 +0200
Subject: [PATCH 07/14] Meta: Link LibCoreMinimal to libnetwork on Haiku

---
 Meta/Lagom/CMakeLists.txt | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/Meta/Lagom/CMakeLists.txt b/Meta/Lagom/CMakeLists.txt
index a84e0aed11..277576e137 100644
--- a/Meta/Lagom/CMakeLists.txt
+++ b/Meta/Lagom/CMakeLists.txt
@@ -391,6 +391,10 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "NetBSD")
     # NetBSD has its shm_open and shm_unlink functions in librt so we need to link that
     target_link_libraries(LibCoreMinimal PRIVATE rt)
 endif()
+if (HAIKU)
+    # Haiku has networking related functions in the network library
+    target_link_libraries(LibCoreMinimal PRIVATE network)
+endif()
 
 # LibMain
 add_serenity_subdirectory(Userland/Libraries/LibMain)
-- 
2.52.0


From 3fa6ce22adc79cca3e7dfde7ac71478261a9312a Mon Sep 17 00:00:00 2001
From: Niklas Poslovski <ni.pos@yandex.com>
Date: Sat, 13 Jul 2024 19:33:15 +0200
Subject: [PATCH 08/14] LibCore: Fall back to pipe instead of pipe2 on Haiku

---
 Userland/Libraries/LibCore/System.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp
index 1220d6a825..218c0c7f64 100644
--- a/Userland/Libraries/LibCore/System.cpp
+++ b/Userland/Libraries/LibCore/System.cpp
@@ -1637,7 +1637,7 @@ ErrorOr<Array<int, 2>> pipe2(int flags)
 {
     Array<int, 2> fds;
 
-#if defined(__unix__)
+#if defined(__unix__) && !defined(AK_OS_HAIKU)
     if (::pipe2(fds.data(), flags) < 0)
         return Error::from_syscall("pipe2"sv, -errno);
 #else
-- 
2.52.0


From 5db0f6cd7607a0b339743509d26efe47e0a90586 Mon Sep 17 00:00:00 2001
From: Niklas Poslovski <ni.pos@yandex.com>
Date: Wed, 16 Oct 2024 16:15:08 +0200
Subject: [PATCH 09/14] LookupServer: Define sin_len for mdns_addr on Haiku

---
 Userland/Services/LookupServer/MulticastDNS.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Userland/Services/LookupServer/MulticastDNS.h b/Userland/Services/LookupServer/MulticastDNS.h
index 65a5580541..8a0845fdea 100644
--- a/Userland/Services/LookupServer/MulticastDNS.h
+++ b/Userland/Services/LookupServer/MulticastDNS.h
@@ -37,7 +37,7 @@ private:
     Name m_hostname;
 
     static constexpr sockaddr_in mdns_addr {
-#if defined(AK_OS_BSD_GENERIC) || defined(AK_OS_GNU_HURD)
+#if defined(AK_OS_BSD_GENERIC) || defined(AK_OS_GNU_HURD) || defined(AK_OS_HAIKU)
         .sin_len = sizeof(struct sockaddr_in),
 #endif
         .sin_family = AF_INET,
-- 
2.52.0


From 0db82438ec52f4a10b89cd7f782bc05f81860194 Mon Sep 17 00:00:00 2001
From: Niklas Poslovski <ni.pos@yandex.com>
Date: Wed, 16 Oct 2024 16:31:37 +0200
Subject: [PATCH 10/14] LookupServer: Link to libnetwork on Haiku

---
 Userland/Services/LookupServer/CMakeLists.txt | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/Userland/Services/LookupServer/CMakeLists.txt b/Userland/Services/LookupServer/CMakeLists.txt
index 185d0a5ecd..d56cdeb5e9 100644
--- a/Userland/Services/LookupServer/CMakeLists.txt
+++ b/Userland/Services/LookupServer/CMakeLists.txt
@@ -22,3 +22,6 @@ set(GENERATED_SOURCES
 
 serenity_bin(LookupServer)
 target_link_libraries(LookupServer PRIVATE LibCore LibDNS LibIPC LibMain)
+if (HAIKU)
+    target_link_libraries(LookupServer PRIVATE network)
+endif()
\ No newline at end of file
-- 
2.52.0


From fe6e0b1b42b1500fd0108737d2de9c8a6db65ebc Mon Sep 17 00:00:00 2001
From: Niklas Poslovski <ni.pos@yandex.com>
Date: Wed, 16 Oct 2024 16:26:03 +0200
Subject: [PATCH 11/14] LibLine: Don't register VWERASE key handler for Haiku

---
 Userland/Libraries/LibLine/Editor.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/Userland/Libraries/LibLine/Editor.cpp b/Userland/Libraries/LibLine/Editor.cpp
index 4b1fdd8def..85f54b40ea 100644
--- a/Userland/Libraries/LibLine/Editor.cpp
+++ b/Userland/Libraries/LibLine/Editor.cpp
@@ -179,8 +179,10 @@ void Editor::set_default_keybinds()
     register_key_input_callback(Key { 't', Key::Alt }, EDITOR_INTERNAL_FUNCTION(transpose_words));
 
     // Register these last to all the user to override the previous key bindings
+#if !defined(AK_OS_HAIKU)
     // Normally ^W. `stty werase \^n` can change it to ^N (or something else).
     register_key_input_callback(m_termios.c_cc[VWERASE], EDITOR_INTERNAL_FUNCTION(erase_word_backwards));
+#endif
     // Normally ^U. `stty kill \^n` can change it to ^N (or something else).
     register_key_input_callback(m_termios.c_cc[VKILL], EDITOR_INTERNAL_FUNCTION(kill_line));
     register_key_input_callback(m_termios.c_cc[VERASE], EDITOR_INTERNAL_FUNCTION(erase_character_backwards));
-- 
2.52.0


From 4b1e3cbf9f707aa52a937da765ce72f588f37466 Mon Sep 17 00:00:00 2001
From: nipos <ni.pos@yandex.com>
Date: Thu, 4 Jun 2026 17:03:32 +0200
Subject: [PATCH 12/14] Meta: Use default linker on Haiku

---
 Meta/CMake/use_linker.cmake | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Meta/CMake/use_linker.cmake b/Meta/CMake/use_linker.cmake
index d1628d706a..e4faf4d310 100644
--- a/Meta/CMake/use_linker.cmake
+++ b/Meta/CMake/use_linker.cmake
@@ -4,7 +4,7 @@
 # SPDX-License-Identifier: BSD-2-Clause
 #
 
-if (NOT APPLE AND NOT LAGOM_USE_LINKER)
+if (NOT APPLE AND NOT HAIKU AND NOT LAGOM_USE_LINKER)
     find_program(LLD_LINKER NAMES "ld.lld")
     if (LLD_LINKER)
         message(STATUS "Using LLD to link Lagom.")
-- 
2.52.0


From 0a3a32c63af811dcc59fd00402a8362f1c28dfb3 Mon Sep 17 00:00:00 2001
From: nipos <ni.pos@yandex.com>
Date: Thu, 4 Jun 2026 17:08:58 +0200
Subject: [PATCH 13/14] SSHServer: Link to libbsd on Haiku

---
 Userland/Services/SSHServer/CMakeLists.txt | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/Userland/Services/SSHServer/CMakeLists.txt b/Userland/Services/SSHServer/CMakeLists.txt
index 35529ebdaf..06395478ee 100644
--- a/Userland/Services/SSHServer/CMakeLists.txt
+++ b/Userland/Services/SSHServer/CMakeLists.txt
@@ -12,3 +12,8 @@ set(SOURCES
 
 serenity_bin(SSHServer)
 target_link_libraries(SSHServer PRIVATE LibCore LibCrypto LibMain LibSSH)
+
+if (HAIKU)
+    # Haiku has arc4random_buf in libbsd.
+    target_link_libraries(SSHServer PRIVATE bsd)
+endif()
-- 
2.52.0


From bbe821017b9605f1cc7747aa191bf1ed8c81547f Mon Sep 17 00:00:00 2001
From: nipos <ni.pos@yandex.com>
Date: Thu, 4 Jun 2026 17:14:57 +0200
Subject: [PATCH 14/14] Meta: Don't look for PulseAudio on Haiku

---
 Meta/Lagom/CMakeLists.txt | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/Meta/Lagom/CMakeLists.txt b/Meta/Lagom/CMakeLists.txt
index 277576e137..fd06f23585 100644
--- a/Meta/Lagom/CMakeLists.txt
+++ b/Meta/Lagom/CMakeLists.txt
@@ -134,7 +134,10 @@ if (ENABLE_LAGOM_LADYBIRD AND (ENABLE_FUZZERS OR ENABLE_COMPILER_EXPLORER_BUILD)
     )
 endif()
 
-CHECK_INCLUDE_FILE(pulse/pulseaudio.h HAVE_PULSEAUDIO)
+if (NOT HAIKU)
+    # Haiku doesn't have or need PulseAudio
+    CHECK_INCLUDE_FILE(pulse/pulseaudio.h HAVE_PULSEAUDIO)
+endif()
 
 add_library(JSClangPlugin INTERFACE)
 add_library(GenericClangPlugin INTERFACE)
-- 
2.52.0

