From 70494a8a57ace034b4ff842a180612567d4e06ba Mon Sep 17 00:00:00 2001 From: Laurent Rene de Cotret Date: Fri, 29 Nov 2024 14:47:49 -0500 Subject: [PATCH 1/3] Add conditional compilation specific to OSX --- cbits/flags.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cbits/flags.c b/cbits/flags.c index 62d389c89..067f531ad 100644 --- a/cbits/flags.c +++ b/cbits/flags.c @@ -422,7 +422,11 @@ static void process_options(int argc, char *argv[]) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-variable" /* Add 'used' so that the variable is not optimised away. */ +#if defined(__APPLE__) && defined(__MACH__) +__attribute__((section("__ACCELERATE_HS,.init_array"), used)) +#else __attribute__((section(".init_array"), used)) +#endif static void *process_options_ctor_entry = &process_options; #pragma GCC diagnostic pop #endif From a90ba872912970463dfcc96ab4e5edb1d1a3f016 Mon Sep 17 00:00:00 2001 From: Laurent Rene de Cotret Date: Fri, 29 Nov 2024 14:49:25 -0500 Subject: [PATCH 2/3] Trigger CI From 5dbb4e66deea23704298a459aa6ad57c20dcde36 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sat, 30 Nov 2024 09:43:38 +0100 Subject: [PATCH 3/3] Fix process_options constructor registration on macos It turns out that .init_array is a Linuxism, so we now use __attribute__((constructor)) on Mac, section(".init_array") on Linux, and still nothing on Windows because what even can we do. --- cbits/flags.c | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/cbits/flags.c b/cbits/flags.c index 067f531ad..7ecd4486f 100644 --- a/cbits/flags.c +++ b/cbits/flags.c @@ -411,22 +411,29 @@ static void process_options(int argc, char *argv[]) * in-place, but that is too much hackery. So we'll just disable +ACC parsing on * Windows. */ #ifndef _WIN32 -/* Register process_options() as a constructor function in the new style by - * putting a reference to it in the .init_array section. The advantage of this - * approach over simply using __attribute__((constructor)) is that this way, the - * function will predictably be called with the same arguments as main(). A - * simple constructor might _accidentally_ be called with the same arguments as - * main(), but it isn't defined to be, and sometimes will not be. (In - * particular, this failed with clang on Windows.) + +/* On MacOS, we use a constructor attribute, because .init_array seems to be a + * Linux-only thing. */ +#if defined(__APPLE__) && defined(__MACH__) +__attribute__((constructor)) +static void process_options_constructor(int argc, char *argv[]) { + process_options(argc, argv); +} +#else +/* On Linux(/BSD? Do we even support that?), register process_options() as a + * constructor function in the new style by putting a reference to it in the + * .init_array section. The advantage of this approach over simply using + * __attribute__((constructor)) is that this way, the function will predictably + * be called with the same arguments as main(). A simple constructor might + * _accidentally_ be called with the same arguments as main(), but it isn't + * defined to be, and sometimes will not be. (In particular, this failed with + * clang on Windows, which is a bad reason to do this on Linux, but whatever.) * Source: https://stackoverflow.com/a/37358751 */ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-variable" /* Add 'used' so that the variable is not optimised away. */ -#if defined(__APPLE__) && defined(__MACH__) -__attribute__((section("__ACCELERATE_HS,.init_array"), used)) -#else __attribute__((section(".init_array"), used)) -#endif static void *process_options_ctor_entry = &process_options; #pragma GCC diagnostic pop -#endif +#endif /* APPLE */ +#endif /* WIN32 */