Subject: Re: bug#17590: [PATCH] build: libstdbuf.so: avoid new OS X link failure

From: Pádraig Brady <[email-masked]>
Date: Mon, 26 May 2014 09:25:05 +0100

On 05/25/2014 11:19 PM, Jim Meyering wrote:
> On Sun, May 25, 2014 at 1:31 PM, Pádraig Brady <[email-masked]> wrote:
>> On 05/25/2014 08:48 PM, Jim Meyering wrote:
>>> Without the attached patch, I'd get this new link failure on OS X:
>>>
>>> Undefined symbols for architecture x86_64:
>>>   "_libintl_gettext", referenced from:
>>>       _apply_mode in src_libstdbuf_so-libstdbuf.o
>>> ld: symbol(s) not found for architecture x86_64
>>> collect2: error: ld returned 1 exit status
>>> make[2]: *** [src/libstdbuf.so] Error 1
>>
>> Oh cool, I presume that's since I generalized the
>> stdbuf enablement check that stdbuf is now built
>> on Mac OS X. I presume it works too or you would
>> have seen the test failure.
>>
>> Change looks good.
> 
> Pushed.  Unfortunately, once past that link failure,
> the test of new-to-OSX stdbuf fails.  Here's stdbuf.log:

That shows that the test is correct,
and indicates that the buffering settings were ignored.

I did a very quick search which suggests something
like the attached might work (assuming the build params
we hardcode for building the shared lib are OK).

thanks,
Pádraig.
>From 47b39387fd18039c24325fdffd5f99435b5ee978 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <[email-masked]>
Date: Mon, 26 May 2014 09:19:16 +0100
Subject: [PATCH] stdbuf: support OS X

* src/stdbuf.c (set_LD_PRELOAD): Adjust to use Mac OS X
specific environment variables in __APPLE__ platforms.
Fixes http://bugs.gnu.org/17590
---
 src/stdbuf.c |   15 ++++++++++++---
 1 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/stdbuf.c b/src/stdbuf.c
index c648fc5..92ee282 100644
--- a/src/stdbuf.c
+++ b/src/stdbuf.c
@@ -187,7 +187,12 @@ static void
 set_LD_PRELOAD (void)
 {
   int ret;
-  char *old_libs = getenv ("LD_PRELOAD");
+#ifdef __APPLE__
+  char const *preload_env = "DYLD_INSERT_LIBRARIES";
+#else
+  char const *preload_env = "LD_PRELOAD";
+#endif
+  char *old_libs = getenv (preload_env);
   char *LD_PRELOAD;
 
   /* Note this would auto add the appropriate search path for "libstdbuf.so":
@@ -239,9 +244,9 @@ set_LD_PRELOAD (void)
   /* FIXME: Do we need to support libstdbuf.dll, c:, '\' separators etc?  */
 
   if (old_libs)
-    ret = asprintf (&LD_PRELOAD, "LD_PRELOAD=%s:%s", old_libs, libstdbuf);
+    ret = asprintf (&LD_PRELOAD, "%s=%s:%s", preload_env, old_libs, libstdbuf);
   else
-    ret = asprintf (&LD_PRELOAD, "LD_PRELOAD=%s", libstdbuf);
+    ret = asprintf (&LD_PRELOAD, "%s=%s", preload_env, libstdbuf);
 
   if (ret < 0)
     xalloc_die ();
@@ -249,6 +254,10 @@ set_LD_PRELOAD (void)
   free (libstdbuf);
 
   ret = putenv (LD_PRELOAD);
+#ifdef __APPLE__
+  if (ret == 0)
+    ret = putenv ("DYLD_FORCE_FLAT_NAMESPACE=y");
+#endif
 
   if (ret != 0)
     {
-- 
1.7.7.6