Skip to content

Commit d2d7417

Browse files
authored
Merge branch 'jiixyj:macos' into macos
2 parents 473de1f + 6ef79f9 commit d2d7417

30 files changed

+760
-55
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ on: [push]
44

55
env:
66
BUILD_TYPE: RelWithDebInfo
7+
CFLAGS: -DALLOW_TIMER_SLACK
78

89
jobs:
9-
testlinux:
10-
runs-on: ubuntu-20.04
10+
test:
11+
strategy:
12+
matrix:
13+
os: [ubuntu-20.04, macos-12]
14+
runs-on: ${{ matrix.os }}
1115
steps:
1216
- uses: actions/checkout@v2
1317
- name: Create Build Environment

external/tree-macros/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ project(tree-macros LANGUAGES C)
44
add_library(tree-macros INTERFACE)
55
target_include_directories(tree-macros
66
INTERFACE "${CMAKE_CURRENT_LIST_DIR}/include")
7+
if(APPLE)
8+
target_compile_definitions(tree-macros INTERFACE __uintptr_t=uintptr_t)
9+
endif()
710

811
#
912

src/CMakeLists.txt

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
44
set(THREADS_PREFER_PTHREAD_FLAG ON)
55
find_package(Threads REQUIRED)
66

7+
find_package(tree-macros REQUIRED)
8+
find_package(queue-macros REQUIRED)
9+
710
add_library(rwlock OBJECT rwlock.c)
811
set_property(TARGET rwlock PROPERTY POSITION_INDEPENDENT_CODE ON)
912
target_link_libraries(rwlock PUBLIC Threads::Threads)
@@ -18,11 +21,15 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
1821
return()
1922
endif()
2023

21-
find_package(tree-macros REQUIRED)
22-
find_package(queue-macros REQUIRED)
24+
add_library(wrap OBJECT wrap.c)
25+
set_property(TARGET wrap PROPERTY POSITION_INDEPENDENT_CODE ON)
26+
target_link_libraries(wrap PUBLIC Threads::Threads)
27+
target_include_directories(wrap
28+
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>)
2329

2430
macro(add_compat_target _name _condition)
2531
add_library(compat_${_name} OBJECT compat_${_name}.c)
32+
target_link_libraries(compat_${_name} PUBLIC wrap)
2633
set_property(TARGET compat_${_name} PROPERTY POSITION_INDEPENDENT_CODE ON)
2734
target_compile_options(
2835
compat_${_name}
@@ -39,12 +46,13 @@ macro(add_compat_target _name _condition)
3946
endmacro()
4047

4148
include(CheckSymbolExists)
49+
4250
# FreeBSD 13 and NetBSD 10 support native eventfd descriptors. NetBSD 10
4351
# supports native timerfd descriptors. Prefer them if available.
4452
check_symbol_exists(eventfd "sys/eventfd.h" HAVE_EVENTFD)
4553
check_symbol_exists(timerfd_create "sys/timerfd.h" HAVE_TIMERFD)
4654

47-
check_symbol_exists(kqueue1 "sys/event.h" HAVE_KQUEUE1)
55+
check_symbol_exists(kqueue1 "sys/event.h;sys/time.h" HAVE_KQUEUE1)
4856
add_compat_target(kqueue1 "NOT;HAVE_KQUEUE1")
4957
check_symbol_exists(sigandset "signal.h" HAVE_SIGANDSET)
5058
check_symbol_exists(sigorset "signal.h" HAVE_SIGORSET)
@@ -71,6 +79,14 @@ if(NOT ALLOWS_ONESHOT_TIMERS_WITH_TIMEOUT_ZERO)
7179
evfilt_timer_quirks
7280
INTERFACE QUIRK_EVFILT_TIMER_DISALLOWS_ONESHOT_TIMEOUT_ZERO)
7381
endif()
82+
add_compat_target(pipe2 "APPLE")
83+
add_compat_target(socket "APPLE")
84+
add_compat_target(socketpair "APPLE")
85+
add_compat_target(itimerspec "APPLE")
86+
add_compat_target(sem "APPLE")
87+
add_compat_target(ppoll "APPLE")
88+
89+
target_link_libraries(rwlock PUBLIC $<BUILD_INTERFACE:compat_enable_sem>)
7490

7591
add_library(
7692
epoll-shim
@@ -80,8 +96,7 @@ add_library(
8096
kqueue_event.c
8197
signalfd.c
8298
signalfd_ctx.c
83-
timespec_util.c
84-
wrap.c)
99+
timespec_util.c)
85100
if(NOT HAVE_EVENTFD)
86101
target_sources(epoll-shim PRIVATE eventfd.c eventfd_ctx.c)
87102
endif()
@@ -94,11 +109,14 @@ target_link_libraries(
94109
epoll-shim
95110
PRIVATE Threads::Threads #
96111
$<BUILD_INTERFACE:queue-macros::queue-macros>
97-
$<BUILD_INTERFACE:tree-macros::tree-macros>
112+
$<BUILD_INTERFACE:tree-macros::tree-macros> #
98113
$<BUILD_INTERFACE:evfilt_timer_quirks>
99114
$<BUILD_INTERFACE:compat_enable_kqueue1>
115+
$<BUILD_INTERFACE:compat_enable_ppoll>
116+
$<BUILD_INTERFACE:compat_enable_itimerspec>
100117
$<BUILD_INTERFACE:compat_enable_sigops>
101-
$<BUILD_INTERFACE:rwlock>)
118+
$<BUILD_INTERFACE:rwlock>
119+
$<BUILD_INTERFACE:wrap>)
102120
if(HAVE_TIMERFD)
103121
target_compile_definitions(epoll-shim PRIVATE HAVE_TIMERFD)
104122
endif()

src/compat_itimerspec.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "compat_itimerspec.h"

src/compat_itimerspec.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef COMPAT_ITIMERSPEC_H
2+
#define COMPAT_ITIMERSPEC_H
3+
4+
#ifdef COMPAT_ENABLE_ITIMERSPEC
5+
6+
#include <time.h>
7+
8+
struct itimerspec {
9+
struct timespec it_interval;
10+
struct timespec it_value;
11+
};
12+
#endif
13+
14+
#endif

src/compat_kqueue1.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ compat_kqueue1_impl(int *fd_out, int flags)
3838
goto out;
3939
}
4040
}
41+
#ifdef __APPLE__
42+
else {
43+
if ((r = fcntl(fd, F_GETFD, 0)) < 0 ||
44+
fcntl(fd, F_SETFD, r & ~FD_CLOEXEC) < 0) {
45+
ec = errno;
46+
goto out;
47+
}
48+
}
49+
#endif
4150

4251
if (flags & O_NONBLOCK) {
4352
if ((r = real_fcntl(fd, F_GETFL)) < 0) {

src/compat_pipe2.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include "compat_pipe2.h"
2+
3+
#include <errno.h>
4+
#include <stdlib.h>
5+
6+
#include <unistd.h>
7+
8+
#include "wrap.h"
9+
10+
static errno_t
11+
compat_pipe2_impl(int pipefd[2], int flags)
12+
{
13+
errno_t ec;
14+
15+
if (flags & ~(O_CLOEXEC | O_NONBLOCK)) {
16+
return EINVAL;
17+
}
18+
19+
int p[2];
20+
if (pipe(p) < 0) {
21+
return errno;
22+
}
23+
24+
{
25+
int r;
26+
27+
if (flags & O_NONBLOCK) {
28+
if ((r = fcntl(p[0], F_GETFL, 0)) < 0 ||
29+
fcntl(p[0], F_SETFL, r | O_NONBLOCK) < 0 ||
30+
(r = fcntl(p[1], F_GETFL, 0)) < 0 ||
31+
fcntl(p[1], F_SETFL, r | O_NONBLOCK) < 0) {
32+
ec = errno;
33+
goto out;
34+
}
35+
}
36+
37+
if (flags & O_CLOEXEC) {
38+
if ((r = fcntl(p[0], F_GETFD, 0)) < 0 ||
39+
fcntl(p[0], F_SETFD, r | FD_CLOEXEC) < 0 ||
40+
(r = fcntl(p[1], F_GETFD, 0)) < 0 ||
41+
fcntl(p[1], F_SETFD, r | FD_CLOEXEC) < 0) {
42+
ec = errno;
43+
goto out;
44+
}
45+
}
46+
}
47+
48+
pipefd[0] = p[0];
49+
pipefd[1] = p[1];
50+
51+
return 0;
52+
53+
out:
54+
(void)real_close(p[0]);
55+
(void)real_close(p[1]);
56+
return ec;
57+
}
58+
59+
int
60+
compat_pipe2(int pipefd[2], int flags)
61+
{
62+
errno_t ec = compat_pipe2_impl(pipefd, flags);
63+
if (ec != 0) {
64+
errno = ec;
65+
return -1;
66+
}
67+
return 0;
68+
}

src/compat_pipe2.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef COMPAT_PIPE2_H
2+
#define COMPAT_PIPE2_H
3+
4+
#include <fcntl.h>
5+
#include <unistd.h>
6+
7+
int compat_pipe2(int pipefd[2], int flags);
8+
#ifdef COMPAT_ENABLE_PIPE2
9+
#define pipe2 compat_pipe2
10+
#endif
11+
12+
#endif

0 commit comments

Comments
 (0)