From 60442983746509d1ffa26db5e18074e57e92523f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Fr=C3=B6jdh?= Date: Tue, 12 May 2026 11:23:20 +0200 Subject: [PATCH] Build virtual servers on macOS (#1450) * macOS import guards to have servers compile * SPI mock since we don't do any actual transfer using the virtual server * /proc/self/exe alternative for macOS --- .../matterhornServer/src/MatterhornApp.cpp | 10 ++++++- .../slsDetectorServer/src/ASIC_Driver.c | 2 ++ .../slsDetectorServer/src/common.c | 27 +++++++++++++++++++ .../src/programViaBlackfin.c | 4 +++ .../src/slsDetectorServer_funcs.c | 24 +++++++++++++++++ 5 files changed, 66 insertions(+), 1 deletion(-) diff --git a/slsDetectorServers/matterhornServer/src/MatterhornApp.cpp b/slsDetectorServers/matterhornServer/src/MatterhornApp.cpp index a60b328a7..8c37df196 100644 --- a/slsDetectorServers/matterhornServer/src/MatterhornApp.cpp +++ b/slsDetectorServers/matterhornServer/src/MatterhornApp.cpp @@ -11,7 +11,15 @@ #include // gettid added in glibc 2.30 -#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30 +#if defined(__APPLE__) +#include +#include +static inline uint64_t gettid() { + uint64_t tid = 0; + pthread_threadid_np(nullptr, &tid); + return tid; +} +#elif __GLIBC__ == 2 && __GLIBC_MINOR__ < 30 #include #define gettid() syscall(SYS_gettid) #endif diff --git a/slsDetectorServers/slsDetectorServer/src/ASIC_Driver.c b/slsDetectorServers/slsDetectorServer/src/ASIC_Driver.c index 562396b16..adc01112d 100644 --- a/slsDetectorServers/slsDetectorServer/src/ASIC_Driver.c +++ b/slsDetectorServers/slsDetectorServer/src/ASIC_Driver.c @@ -11,8 +11,10 @@ #include #include +#ifndef __APPLE__ #include #include +#endif #include #include #include diff --git a/slsDetectorServers/slsDetectorServer/src/common.c b/slsDetectorServers/slsDetectorServer/src/common.c index 2bfc79bb7..cd7adab63 100644 --- a/slsDetectorServers/slsDetectorServer/src/common.c +++ b/slsDetectorServers/slsDetectorServer/src/common.c @@ -11,6 +11,12 @@ #include // stat #include // uname #include // readlink +#ifdef __APPLE__ +#include // PATH_MAX +#include // _NSGetExecutablePath +#include // uint32_t +#include // realpath +#endif extern int executeCommand(char *command, char *result, enum TLogLevel level); @@ -58,12 +64,33 @@ int getAbsPath(char *buf, size_t bufSize, char *fname) { // get path of current binary char path[bufSize]; memset(path, 0, bufSize); +#ifdef __APPLE__ + // macOS has no /proc; use _NSGetExecutablePath and canonicalize with + // realpath (the path returned may contain ".." or symlinks). + char raw[PATH_MAX]; + uint32_t rawSize = sizeof(raw); + if (_NSGetExecutablePath(raw, &rawSize) != 0) { + LOG(logWARNING, + ("Could not get current binary path for %s (buffer too small)\n", + fname)); + return FAIL; + } + char resolved[PATH_MAX]; + const char *src = realpath(raw, resolved) != NULL ? resolved : raw; + if (strlen(src) >= bufSize) { + LOG(logWARNING, + ("Current binary path too long for buffer (%s)\n", fname)); + return FAIL; + } + strcpy(path, src); +#else ssize_t len = readlink("/proc/self/exe", path, bufSize - 1); if (len < 0) { LOG(logWARNING, ("Could not readlink current binary for %s\n", fname)); return FAIL; } path[len] = '\0'; +#endif // get dir path and attach file name char *dir = dirname(path); diff --git a/slsDetectorServers/slsDetectorServer/src/programViaBlackfin.c b/slsDetectorServers/slsDetectorServer/src/programViaBlackfin.c index e5a281875..0f1c12d95 100644 --- a/slsDetectorServers/slsDetectorServer/src/programViaBlackfin.c +++ b/slsDetectorServers/slsDetectorServer/src/programViaBlackfin.c @@ -8,7 +8,9 @@ #include #include +#ifndef __APPLE__ #include +#endif #include // usleep /* global variables */ @@ -309,6 +311,7 @@ int preparetoCopyProgram(char *mess, char *functionType, FILE **fd, } // check available memory to copy program +#ifndef __APPLE__ { struct sysinfo info; sysinfo(&info); @@ -322,6 +325,7 @@ int preparetoCopyProgram(char *mess, char *functionType, FILE **fd, return FAIL; } } +#endif // open file to copy program *fd = fopen(TEMP_PROG_FILE_NAME, "w"); diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c index f4d9d930f..0359bb467 100644 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c @@ -20,10 +20,24 @@ #include #include #include +#ifndef __APPLE__ #include +#endif #include +#ifdef __APPLE__ +// spidev is Linux-only; provide a minimal stub so virtual builds compile. +// The real ioctl(SPI_IOC_MESSAGE(...)) calls are guarded by detector +// macros (XILINX_CHIPTESTBOARDD) that are never set on macOS. +struct spi_ioc_transfer { + unsigned long tx_buf; + unsigned long rx_buf; + unsigned int len; + unsigned char cs_change; +}; +#else #include +#endif // defined in the detector specific Makefile #ifdef EIGERD @@ -127,6 +141,10 @@ int sendError(int file_des) { } void setMemoryAllocationErrorMessage() { +#ifdef __APPLE__ + sprintf(mess, "Memory allocation error (%s). Please reboot", + getFunctionNameFromEnum((enum detFuncs)fnum)); +#else struct sysinfo info; sysinfo(&info); sprintf( @@ -134,6 +152,7 @@ void setMemoryAllocationErrorMessage() { "Memory allocation error (%s). Available space: %d MB. Please reboot", getFunctionNameFromEnum((enum detFuncs)fnum), (int)(info.freeram / (1024 * 1024))); +#endif #ifdef EIGERD strcat(mess, ".\n"); #else @@ -9784,12 +9803,17 @@ void receive_program_default(int file_des, enum PROGRAM_INDEX index, if (ret == OK) { src = malloc(filesize); if (src == NULL) { +#ifdef __APPLE__ + sprintf(mess, "Could not %s. Memory allocation failure.\n", + functionType); +#else struct sysinfo info; sysinfo(&info); sprintf(mess, "Could not %s. Memory allocation failure. Free " "space: %d MB\n", functionType, (int)(info.freeram / (1024 * 1024))); +#endif LOG(logERROR, (mess)); ret = FAIL; }