From 47a6918739cb06dafa970d0b528bed1951d95039 Mon Sep 17 00:00:00 2001
From: Shunsuke Shimizu <grafi@grafi.jp>
Date: Mon, 29 Aug 2022 04:13:55 +0900
Subject: [PATCH] build-sys: Fix macOS build

* Enable macOS specific modules (module-bonjour-publish,
  module-coreaudio-detect and module-coreaudio-device)
* Correctly set `PA_SOEXT` (.so, .dylib and .dll)
* Build `poll-posix.c` and `semaphore-osx.c`
* Drop linker flag `-Wl,-z,nodelete` on Darwin
* Drop linker flag `-Wl,--no-undefined` on Darwin
* Prefer to `clock_gettime` over compat impl for old Darwin
* Disable SCM credential on Darwin

Part-of: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/746>
---
 meson.build                  | 10 ++++++++--
 src/meson.build              |  9 +++++++--
 src/modules/meson.build      | 21 +++++++++++++++++----
 src/pulse/meson.build        | 12 ++++++++----
 src/pulsecore/core-rtclock.c | 25 ++++++++++++-------------
 src/pulsecore/creds.h        |  2 +-
 6 files changed, 53 insertions(+), 26 deletions(-)

diff --git a/meson.build b/meson.build
index 9f47b2f028..b678bb531a 100644
--- a/meson.build
+++ b/meson.build
@@ -150,7 +150,13 @@ cdata.set_quoted('PA_MACHINE_ID', join_paths(sysconfdir, 'machine-id'))
 cdata.set_quoted('PA_MACHINE_ID_FALLBACK', join_paths(localstatedir, 'lib', 'dbus', 'machine-id'))
 cdata.set_quoted('PA_SRCDIR', join_paths(meson.current_source_dir(), 'src'))
 cdata.set_quoted('PA_BUILDDIR', meson.current_build_dir())
-cdata.set_quoted('PA_SOEXT', '.so')
+if host_machine.system() == 'windows'
+  cdata.set_quoted('PA_SOEXT', '.dll')
+elif host_machine.system() == 'darwin'
+  cdata.set_quoted('PA_SOEXT', '.dylib')
+else
+  cdata.set_quoted('PA_SOEXT', '.so')
+endif
 cdata.set_quoted('PA_DEFAULT_CONFIG_DIR', pulsesysconfdir)
 cdata.set('PA_DEFAULT_CONFIG_DIR_UNQUOTED', pulsesysconfdir)
 cdata.set_quoted('PA_BINARY', join_paths(bindir, 'pulseaudio'))
@@ -426,7 +432,7 @@ cdata.set('MESON_BUILD', 1)
 # so we request the nodelete flag to be enabled.
 # On other systems, we don't really know how to do that, but it's welcome if somebody can tell.
 # Windows doesn't support this flag.
-if host_machine.system() != 'windows'
+if host_machine.system() != 'windows' and host_machine.system() != 'darwin'
   nodelete_link_args = ['-Wl,-z,nodelete']
 else
   nodelete_link_args = []
diff --git a/src/meson.build b/src/meson.build
index 9efb561d8a..2069d2803d 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -182,9 +182,14 @@ if host_machine.system() == 'windows'
 else
   libpulsecommon_sources += [
     'pulsecore/mutex-posix.c',
-    'pulsecore/semaphore-posix.c',
-    'pulsecore/thread-posix.c'
+    'pulsecore/poll-posix.c',
+    'pulsecore/thread-posix.c',
   ]
+  if host_machine.system() == 'darwin'
+    libpulsecommon_sources += ['pulsecore/semaphore-osx.c']
+  else
+    libpulsecommon_sources += ['pulsecore/semaphore-posix.c']
+  endif
 endif
 # FIXME: Do SIMD things
 
diff --git a/src/modules/meson.build b/src/modules/meson.build
index 1e12569dc5..3636ce0de5 100644
--- a/src/modules/meson.build
+++ b/src/modules/meson.build
@@ -8,15 +8,12 @@ all_modules = [
   [ 'module-always-sink', 'module-always-sink.c' ],
   [ 'module-always-source', 'module-always-source.c' ],
   [ 'module-augment-properties', 'module-augment-properties.c' ],
-#  [ 'module-bonjour-publish', 'macosx/module-bonjour-publish.c' ],
   [ 'module-card-restore', 'module-card-restore.c' ],
   [ 'module-cli', 'module-cli.c', [], [], [], libcli ],
   [ 'module-cli-protocol-tcp', 'module-protocol-stub.c', [], ['-DUSE_PROTOCOL_CLI', '-DUSE_TCP_SOCKETS'], [], libprotocol_cli ],
   [ 'module-cli-protocol-unix', 'module-protocol-stub.c', [], ['-DUSE_PROTOCOL_CLI', '-DUSE_UNIX_SOCKETS'], [], libprotocol_cli ],
   [ 'module-combine', 'module-combine.c' ],
   [ 'module-combine-sink', 'module-combine-sink.c' ],
-#  [ 'module-coreaudio-detect', 'macosx/module-coreaudio-detect.c' ],
-#  [ 'module-coreaudio-device', 'macosx/module-coreaudio-device.c' ],
   [ 'module-default-device-restore', 'module-default-device-restore.c', [], [], [], libprotocol_native ],
   [ 'module-detect', 'module-detect.c' ],
   [ 'module-device-manager', 'module-device-manager.c', [], [], [], libprotocol_native ],
@@ -78,6 +75,16 @@ if host_machine.system() != 'windows'
   ]
 endif
 
+if host_machine.system() == 'darwin'
+  bonjour_dep = dependency('appleframeworks', modules : ['CoreFoundation'])
+  coreaudio_dep = dependency('appleframeworks', modules : ['CoreAudio'])
+  all_modules += [
+    [ 'module-bonjour-publish', 'macosx/module-bonjour-publish.c', [], [], [bonjour_dep] ],
+    [ 'module-coreaudio-detect', 'macosx/module-coreaudio-detect.c', [], [], [coreaudio_dep] ],
+    [ 'module-coreaudio-device', 'macosx/module-coreaudio-device.c', [], [], [coreaudio_dep] ],
+  ]
+endif
+
 # Modules enabled by headers
 
 if cc.has_header('linux/input.h')
@@ -298,6 +305,12 @@ all_modules += [
 # FIXME: meson doesn't support multiple RPATH arguments currently
 rpath_dirs = join_paths(privlibdir) + ':' + join_paths(modlibexecdir)
 
+if host_machine.system() != 'windows' and host_machine.system() != 'darwin'
+  no_undefined_args = ['-Wl,--no-undefined']
+else
+  no_undefined_args = []
+endif
+
 foreach m : all_modules
   name = m[0]
   sources = m[1]
@@ -315,7 +328,7 @@ foreach m : all_modules
     install_rpath : rpath_dirs,
     install_dir : modlibexecdir,
     dependencies : [thread_dep, libpulse_dep, libpulsecommon_dep, libpulsecore_dep, libintl_dep, platform_dep, platform_socket_dep] + extra_deps,
-    link_args : [nodelete_link_args, '-Wl,--no-undefined' ],
+    link_args : [nodelete_link_args, no_undefined_args],
     link_with : extra_libs,
     name_prefix : '',
     implicit_include_directories : false)
diff --git a/src/pulse/meson.build b/src/pulse/meson.build
index 1b82c807c8..40d407963e 100644
--- a/src/pulse/meson.build
+++ b/src/pulse/meson.build
@@ -70,11 +70,15 @@ if glib_dep.found()
   libpulse_headers += 'glib-mainloop.h'
 endif
 
-run_target('update-map-file',
-  command : [ join_paths(meson.source_root(), 'scripts/generate-map-file.sh'), 'map-file',
-              [ libpulse_headers, 'simple.h', join_paths(meson.build_root(), 'src', 'pulse', 'version.h') ] ])
+if host_machine.system() != 'windows' and host_machine.system() != 'darwin'
+  run_target('update-map-file',
+    command : [ join_paths(meson.source_root(), 'scripts/generate-map-file.sh'), 'map-file',
+                [ libpulse_headers, 'simple.h', join_paths(meson.build_root(), 'src', 'pulse', 'version.h') ] ])
 
-versioning_link_args = '-Wl,-version-script=' + join_paths(meson.source_root(), 'src', 'pulse', 'map-file')
+  versioning_link_args = ['-Wl,-version-script=' + join_paths(meson.source_root(), 'src', 'pulse', 'map-file')]
+else
+  versioning_link_args = []
+endif
 
 libpulse = shared_library('pulse',
   libpulse_sources,
diff --git a/src/pulsecore/core-rtclock.c b/src/pulsecore/core-rtclock.c
index 2c2e286319..d0cf157315 100644
--- a/src/pulsecore/core-rtclock.c
+++ b/src/pulsecore/core-rtclock.c
@@ -65,19 +65,7 @@ pa_usec_t pa_rtclock_age(const struct timeval *tv) {
 
 struct timeval *pa_rtclock_get(struct timeval *tv) {
 
-#if defined(OS_IS_DARWIN)
-    uint64_t val, abs_time = mach_absolute_time();
-    Nanoseconds nanos;
-
-    nanos = AbsoluteToNanoseconds(*(AbsoluteTime *) &abs_time);
-    val = *(uint64_t *) &nanos;
-
-    tv->tv_sec = val / PA_NSEC_PER_SEC;
-    tv->tv_usec = (val % PA_NSEC_PER_SEC) / PA_NSEC_PER_USEC;
-
-    return tv;
-
-#elif defined(HAVE_CLOCK_GETTIME)
+#if defined(HAVE_CLOCK_GETTIME)
     struct timespec ts;
 
 #ifdef CLOCK_MONOTONIC
@@ -97,6 +85,17 @@ struct timeval *pa_rtclock_get(struct timeval *tv) {
     tv->tv_sec = ts.tv_sec;
     tv->tv_usec = ts.tv_nsec / PA_NSEC_PER_USEC;
 
+    return tv;
+#elif defined(OS_IS_DARWIN)
+    uint64_t val, abs_time = mach_absolute_time();
+    Nanoseconds nanos;
+
+    nanos = AbsoluteToNanoseconds(*(AbsoluteTime *) &abs_time);
+    val = *(uint64_t *) &nanos;
+
+    tv->tv_sec = val / PA_NSEC_PER_SEC;
+    tv->tv_usec = (val % PA_NSEC_PER_SEC) / PA_NSEC_PER_USEC;
+
     return tv;
 #elif defined(OS_IS_WIN32)
     if (counter_freq > 0) {
diff --git a/src/pulsecore/creds.h b/src/pulsecore/creds.h
index b599b569ce..acfdd9d43e 100644
--- a/src/pulsecore/creds.h
+++ b/src/pulsecore/creds.h
@@ -34,7 +34,7 @@
 typedef struct pa_creds pa_creds;
 typedef struct pa_cmsg_ancil_data pa_cmsg_ancil_data;
 
-#if defined(SCM_CREDENTIALS) || defined(SCM_CREDS)
+#if (defined(SCM_CREDENTIALS) || defined(SCM_CREDS)) && !defined(OS_IS_DARWIN)
 
 #define HAVE_CREDS 1
 
-- 
GitLab

