From 0950df3509aaa296c6eda371862610ef85aa6bdd Mon Sep 17 00:00:00 2001 From: Mark Stalzer Date: Thu, 14 May 2026 11:57:19 -0400 Subject: [PATCH] macOS: plugin loading, hidapi, and alloca/stdlib.h fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit survive_plugins.unix.h: use .dylib extension and _NSGetExecutablePath() on Apple. Also fixes a latent Linux bug where readlink() return value was used as an array index without checking for error (-1). CMakeLists.txt: find hidapi on Apple via pkg-config rather than hardcoded Homebrew paths, which differ between Apple Silicon (/opt/homebrew) and Intel (/usr/local). cn_matrix.h: replace with unconditionally, but guard the re-inclusion of with _WIN32. does not exist on macOS; provides malloc on all platforms. MSVC does not declare alloca in — it lives in . Co-Authored-By: Claude Sonnet 4.6 --- libs/cnmatrix/include/cnmatrix/cn_matrix.h | 5 ++++- src/CMakeLists.txt | 5 +++++ src/survive_plugins.unix.h | 20 ++++++++++++++++++-- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/libs/cnmatrix/include/cnmatrix/cn_matrix.h b/libs/cnmatrix/include/cnmatrix/cn_matrix.h index 3e40b214..988a5155 100644 --- a/libs/cnmatrix/include/cnmatrix/cn_matrix.h +++ b/libs/cnmatrix/include/cnmatrix/cn_matrix.h @@ -21,7 +21,10 @@ #include -#include +#include +#ifdef _WIN32 +#include // alloca on MSVC +#endif #include "cnmatrix/cn_flt.h" #include "math.h" #include "string.h" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c56c0538..b2e49d29 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -56,6 +56,11 @@ if(USE_HIDAPI) add_definitions (-DHIDAPI) IF(WIN32) SET(SURVIVE_SRCS ${SURVIVE_SRCS} ../redist/hid-windows.c) + elseif(APPLE) + find_package(PkgConfig REQUIRED) + pkg_check_modules(HIDAPI REQUIRED hidapi) + list(APPEND ADDITIONAL_LIBRARIES ${HIDAPI_LIBRARIES}) + include_directories(${HIDAPI_INCLUDE_DIRS}) else() list(APPEND ADDITIONAL_LIBRARIES udev hidapi-libusb) endif() diff --git a/src/survive_plugins.unix.h b/src/survive_plugins.unix.h index 2f09c721..41a7c906 100644 --- a/src/survive_plugins.unix.h +++ b/src/survive_plugins.unix.h @@ -9,12 +9,19 @@ #include #include #include +#ifdef __APPLE__ +#include +#endif #include "assert.h" #pragma GCC diagnostic ignored "-Wpedantic" +#ifdef __APPLE__ +static const char* plugin_ext() { return ".dylib"; } +#else static const char* plugin_ext() { return ".so"; } +#endif #ifndef PATH_MAX #define PATH_MAX 4096 @@ -37,8 +44,17 @@ static const char *get_so_filename() { static const char *get_exe_filename() { static char exe_path[PATH_MAX] = { 0 }; if (exe_path[0] == 0) { - size_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path)); - exe_path[len] = 0; +#ifdef __APPLE__ + uint32_t size = sizeof(exe_path); + if (_NSGetExecutablePath(exe_path, &size) != 0) + exe_path[0] = 0; +#else + ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1); + if (len > 0) + exe_path[len] = 0; + else + exe_path[0] = 0; +#endif } return exe_path; }