From c21dc21f36798e2e941e5f2f0da7531c1a228bea Mon Sep 17 00:00:00 2001 From: Marcin Gasperowicz Date: Sat, 23 May 2020 15:31:30 +0200 Subject: [PATCH] Build: Make Lagom build under macOS (#2341) Lagom now builds under macOS. Only two minor adjustments were required: * LibCore TCP/UDP code can't use `SOCK_{NONBLOCK,CLOEXEC}` on macOS, use ioctl() and fcntl() instead * LibJS `Heap` code pthread usage ported to MacOS --- Libraries/LibCore/TCPServer.cpp | 10 ++++++++++ Libraries/LibCore/TCPSocket.cpp | 10 ++++++++++ Libraries/LibCore/UDPServer.cpp | 11 +++++++++++ Libraries/LibCore/UDPSocket.cpp | 11 +++++++++++ Libraries/LibJS/Heap/Heap.cpp | 10 +++++++++- 5 files changed, 51 insertions(+), 1 deletion(-) diff --git a/Libraries/LibCore/TCPServer.cpp b/Libraries/LibCore/TCPServer.cpp index bfc8a9ce61f..c751b3395a5 100644 --- a/Libraries/LibCore/TCPServer.cpp +++ b/Libraries/LibCore/TCPServer.cpp @@ -32,12 +32,22 @@ #include #include +#ifndef SOCK_NONBLOCK +# include +#endif namespace Core { TCPServer::TCPServer(Object* parent) : Object(parent) { +#ifdef SOCK_NONBLOCK m_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0); +#else + m_fd = socket(AF_INET, SOCK_STREAM, 0); + int option = 1; + ioctl(m_fd, FIONBIO, &option); + fcntl(m_fd, F_SETFD, FD_CLOEXEC); +#endif ASSERT(m_fd >= 0); } diff --git a/Libraries/LibCore/TCPSocket.cpp b/Libraries/LibCore/TCPSocket.cpp index ca23112e0f2..6029a824322 100644 --- a/Libraries/LibCore/TCPSocket.cpp +++ b/Libraries/LibCore/TCPSocket.cpp @@ -28,6 +28,10 @@ #include #include +#ifndef SOCK_NONBLOCK +# include +#endif + namespace Core { TCPSocket::TCPSocket(int fd, Object* parent) @@ -43,7 +47,13 @@ TCPSocket::TCPSocket(int fd, Object* parent) TCPSocket::TCPSocket(Object* parent) : Socket(Socket::Type::TCP, parent) { +#ifdef SOCK_NONBLOCK int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); +#else + int fd = socket(AF_INET, SOCK_STREAM, 0); + int option = 1; + ioctl(fd, FIONBIO, &option); +#endif if (fd < 0) { set_error(errno); } else { diff --git a/Libraries/LibCore/UDPServer.cpp b/Libraries/LibCore/UDPServer.cpp index ede8ff15fa6..462ce5f983c 100644 --- a/Libraries/LibCore/UDPServer.cpp +++ b/Libraries/LibCore/UDPServer.cpp @@ -31,12 +31,23 @@ #include #include +#ifndef SOCK_NONBLOCK +# include +#endif + namespace Core { UDPServer::UDPServer(Object* parent) : Object(parent) { +#ifdef SOCK_NONBLOCK m_fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0); +#else + m_fd = socket(AF_INET, SOCK_DGRAM, 0); + int option = 1; + ioctl(m_fd, FIONBIO, &option); + fcntl(m_fd, F_SETFD, FD_CLOEXEC); +#endif ASSERT(m_fd >= 0); } diff --git a/Libraries/LibCore/UDPSocket.cpp b/Libraries/LibCore/UDPSocket.cpp index 39d73412e7d..943419667a5 100644 --- a/Libraries/LibCore/UDPSocket.cpp +++ b/Libraries/LibCore/UDPSocket.cpp @@ -28,6 +28,10 @@ #include #include +#ifndef SOCK_NONBLOCK +# include +#endif + namespace Core { UDPSocket::UDPSocket(int fd, Object* parent) @@ -43,7 +47,14 @@ UDPSocket::UDPSocket(int fd, Object* parent) UDPSocket::UDPSocket(Object* parent) : Socket(Socket::Type::UDP, parent) { +#ifdef SOCK_NONBLOCK int fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0); +#else + int fd = socket(AF_INET, SOCK_DGRAM, 0); + int option = 1; + ioctl(fd, FIONBIO, &option); +#endif + if (fd < 0) { set_error(errno); } else { diff --git a/Libraries/LibJS/Heap/Heap.cpp b/Libraries/LibJS/Heap/Heap.cpp index f1739531e99..ae451897bad 100644 --- a/Libraries/LibJS/Heap/Heap.cpp +++ b/Libraries/LibJS/Heap/Heap.cpp @@ -37,7 +37,7 @@ #ifdef __serenity__ # include -#elif __linux__ +#elif __linux__ or __MACH__ # include #endif @@ -157,6 +157,14 @@ void Heap::gather_conservative_roots(HashTable& roots) ASSERT_NOT_REACHED(); } pthread_attr_destroy(&attr); +#elif __MACH__ + stack_base = (FlatPtr)pthread_get_stackaddr_np(pthread_self()); + pthread_attr_t attr = {}; + if (int rc = pthread_attr_getstacksize(&attr, &stack_size) != 0) { + fprintf(stderr, "pthread_attr_getstacksize: %s\n", strerror(-rc)); + ASSERT_NOT_REACHED(); + } + pthread_attr_destroy(&attr); #endif FlatPtr stack_reference = reinterpret_cast(&dummy);