Skip to content

Commit 3e66d35

Browse files
committed
build: add Darwin/macOS support
Adds platform handling for macOS: - Guard GCC-only -Wno-stringop-truncation behind a compiler check and silence Clang warnings promoted to errors by -Werror. Probe each Clang flag with check_c_compiler_flag() and only apply it when the compiler is Clang, so older Clang or a GCC build on macOS still works. - Wire up empty RT/SOCKET libraries and test libraries for Darwin. - Use the macOS pthread_setname_np signature (current thread only) and skip pthread_condattr_setclock(CLOCK_MONOTONIC), which is unavailable. On macOS pthread_cond_timedwait() always uses CLOCK_REALTIME, so the deadline timespec is built from CLOCK_REALTIME there as well to avoid premature timeouts or excessively long waits. - In dlt-daemon.c, scope the Linux-only fifo_size local behind __linux__ to silence an unused-variable warning on Darwin, and replace the broken "#elif __QNX__" with "#elif defined(__QNX__)" so the branch is selected by macro presence rather than its value. Signed-off-by: Felipe Kellermann <felipek@me.com>
1 parent fd1fbf8 commit 3e66d35

6 files changed

Lines changed: 66 additions & 6 deletions

File tree

CMakeLists.txt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,34 @@ add_compile_options(
261261
-Wcast-qual
262262
-Wno-system-headers
263263
-Wno-unused-function
264-
-Wno-stringop-truncation
264+
$<$<C_COMPILER_ID:GNU>:-Wno-stringop-truncation>
265265
)
266266

267+
if(APPLE AND CMAKE_C_COMPILER_ID MATCHES "Clang")
268+
# Clang on macOS is stricter than GCC on some warnings that are promoted to
269+
# errors by -Werror above. Suppress the ones triggered by current code until
270+
# the upstream sources are fixed. Probe each flag so older Clang versions
271+
# (or a non-Clang compiler explicitly selected on macOS) don't fail the
272+
# build with "unknown warning option" turned into an error.
273+
include(CheckCCompilerFlag)
274+
foreach(_dlt_clang_flag
275+
-Wno-sign-conversion
276+
-Wno-implicit-int-conversion
277+
-Wno-shorten-64-to-32
278+
-Wno-strict-prototypes
279+
-Wno-static-in-inline
280+
-Wno-gnu-folding-constant
281+
-Wno-gnu-zero-variadic-macro-arguments
282+
-Wno-variadic-macro-arguments-omitted
283+
-Wno-deprecated-declarations)
284+
string(MAKE_C_IDENTIFIER "HAVE_CFLAG_${_dlt_clang_flag}" _dlt_have_var)
285+
check_c_compiler_flag("${_dlt_clang_flag}" ${_dlt_have_var})
286+
if(${_dlt_have_var})
287+
add_compile_options(${_dlt_clang_flag})
288+
endif()
289+
endforeach()
290+
endif()
291+
267292
if(WITH_DOC STREQUAL "OFF")
268293
set(PACKAGE_DOC "#")
269294
else()

src/daemon/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ endif()
5050
if("${CMAKE_SYSTEM_NAME}" MATCHES "Linux|CYGWIN|MSYS")
5151
set(RT_LIBRARY rt)
5252
set(SOCKET_LIBRARY "")
53+
elseif("${CMAKE_SYSTEM_NAME}" MATCHES "Darwin")
54+
set(RT_LIBRARY "")
55+
set(SOCKET_LIBRARY "")
5356
else()
5457
set(RT_LIBRARY "")
5558
set(SOCKET_LIBRARY socket)

src/daemon/dlt-daemon.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1958,7 +1958,9 @@ static int dlt_daemon_init_fifo(DltDaemonLocal *daemon_local)
19581958
{
19591959
int ret;
19601960
int fd = -1;
1961+
#ifdef __linux__
19611962
int fifo_size;
1963+
#endif
19621964

19631965
/* open named pipe(FIFO) to receive DLT messages from users */
19641966
umask(0);
@@ -5703,7 +5705,7 @@ int create_timer_fd(DltDaemonLocal *daemon_local,
57035705
timer_name, strerror(errno));
57045706
local_fd = DLT_FD_INIT;
57055707
}
5706-
#elif __QNX__
5708+
#elif defined(__QNX__)
57075709
/*
57085710
* Since timerfd is not valid in QNX, new threads are introduced
57095711
* to manage timers and communicate with main thread when timer expires.

src/lib/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ add_library(dlt ${dlt_LIB_SRCS})
3434
if("${CMAKE_SYSTEM_NAME}" MATCHES "Linux|CYGWIN|MSYS")
3535
set(RT_LIBRARY rt)
3636
set(SOCKET_LIBRARY "")
37+
elseif("${CMAKE_SYSTEM_NAME}" MATCHES "Darwin")
38+
set(RT_LIBRARY "")
39+
set(SOCKET_LIBRARY "")
3740
else()
3841
set(RT_LIBRARY "")
3942
set(SOCKET_LIBRARY socket)

src/lib/dlt_user.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3949,7 +3949,11 @@ void *dlt_user_trace_network_segmented_thread(void *unused)
39493949
/* Unused on purpose. */
39503950
(void)unused;
39513951
#ifdef DLT_USE_PTHREAD_SETNAME_NP
3952+
#ifdef __APPLE__
3953+
if (pthread_setname_np("dlt_segmented"))
3954+
#else
39523955
if (pthread_setname_np(dlt_user.dlt_segmented_nwt_handle, "dlt_segmented"))
3956+
#endif
39533957
dlt_log(LOG_WARNING, "Failed to rename segmented thread!\n");
39543958
#elif linux
39553959
if (prctl(PR_SET_NAME, "dlt_segmented", 0, 0, 0) < 0)
@@ -4835,7 +4839,11 @@ void *dlt_user_housekeeperthread_function(void *ptr)
48354839
#endif
48364840

48374841
#ifdef DLT_USE_PTHREAD_SETNAME_NP
4842+
#ifdef __APPLE__
4843+
if (pthread_setname_np("dlt_housekeeper"))
4844+
#else
48384845
if (pthread_setname_np(dlt_housekeeperthread_handle, "dlt_housekeeper"))
4846+
#endif
48394847
dlt_log(LOG_WARNING, "Failed to rename housekeeper thread!\n");
48404848
#elif linux
48414849
if (prctl(PR_SET_NAME, "dlt_housekeeper", 0, 0, 0) < 0)
@@ -7349,6 +7357,18 @@ void dlt_user_test_corrupt_message_size(int enable, int16_t size)
73497357
#endif
73507358

73517359

7360+
/*
7361+
* Clock used to derive deadlines for pthread_cond_timedwait(). On macOS that
7362+
* function always uses CLOCK_REALTIME (pthread_condattr_setclock() is not
7363+
* available), so the deadline has to be built from CLOCK_REALTIME or the
7364+
* timed wait can return immediately or block far longer than intended.
7365+
*/
7366+
#ifdef __APPLE__
7367+
# define DLT_COND_TIMEDWAIT_CLOCK CLOCK_REALTIME
7368+
#else
7369+
# define DLT_COND_TIMEDWAIT_CLOCK CLOCK_MONOTONIC
7370+
#endif
7371+
73527372
int dlt_start_threads()
73537373
{
73547374
struct timespec time_to_wait, single_wait;
@@ -7357,12 +7377,17 @@ int dlt_start_threads()
73577377
atomic_bool dlt_housekeeper_running = false;
73587378

73597379
/*
7360-
* Configure the condition varibale to use CLOCK_MONOTONIC.
7361-
* This makes sure we're protected against changes in the system clock
7380+
* Configure the condition variable to use CLOCK_MONOTONIC.
7381+
* This makes sure we're protected against changes in the system clock.
7382+
* On macOS, pthread_condattr_setclock() is unavailable and
7383+
* pthread_cond_timedwait() always uses CLOCK_REALTIME, so the deadline
7384+
* has to be computed from CLOCK_REALTIME there (see DLT_TIMER_CLOCK).
73627385
*/
73637386
pthread_condattr_t attr;
73647387
pthread_condattr_init(&attr);
7388+
#ifndef __APPLE__
73657389
pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
7390+
#endif
73667391
pthread_cond_init(&dlt_housekeeper_running_cond, &attr);
73677392

73687393
if (pthread_create(&(dlt_housekeeperthread_handle),
@@ -7373,7 +7398,7 @@ int dlt_start_threads()
73737398
return -1;
73747399
}
73757400

7376-
clock_gettime(CLOCK_MONOTONIC, &now);
7401+
clock_gettime(DLT_COND_TIMEDWAIT_CLOCK, &now);
73777402
/* wait at most 10s */
73787403
time_to_wait.tv_sec = now.tv_sec + 10;
73797404
time_to_wait.tv_nsec = now.tv_nsec;
@@ -7399,7 +7424,7 @@ int dlt_start_threads()
73997424
* this makes sure we don't block too long
74007425
* even if we missed the signal
74017426
*/
7402-
clock_gettime(CLOCK_MONOTONIC, &now);
7427+
clock_gettime(DLT_COND_TIMEDWAIT_CLOCK, &now);
74037428
if (now.tv_nsec >= 500000000) {
74047429
single_wait.tv_sec = now.tv_sec + 1;
74057430
single_wait.tv_nsec = now.tv_nsec - 500000000;

tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ if("${CMAKE_SYSTEM_NAME}" MATCHES "Linux|CYGWIN")
2727
set(LIBRARIES "")
2828
elseif("${CMAKE_SYSTEM_NAME}" MATCHES "QNX")
2929
set(LIBRARIES regex)
30+
elseif("${CMAKE_SYSTEM_NAME}" MATCHES "Darwin")
31+
set(LIBRARIES "")
3032
else()
3133
set(LIBRARIES socket)
3234
endif()

0 commit comments

Comments
 (0)