Paul Eggert wrote:
> > testing for the language could be done by testing for the full
> > weekday name of, say, Monday, in that language. I.e. by calling the
> > system's strftime("%A") for a particular date.
> > 
> > This would get rid of this link dependency on macOS. Does that sound
> > reasonable?
> 
> Yes, thanks, sounds good to me.

Done through this patch:


2025-11-19  Bruno Haible  <[email protected]>

        nstrftime, fprintftime: Reduce link requirements on macOS.
        Reported by Paul Eggert for GNU tar.
        * lib/strftime.c: On macOS, don't include localename.h.
        (my_strftime): On macOS, use strftime() instead of gl_locale_name_unsafe
        to test for the three particular locales.
        * modules/nstrftime (Depends-on): Add condition to localename-unsafe.
        (Link): Remove @INTL_MACOSX_LIBS@.
        * modules/nstrftime-limited (Depends-on): Add condition to
        localename-unsafe-limited.
        (Link): Remove @INTL_MACOSX_LIBS@.
        * modules/nstrftime-tests (Makefile.am): Link test-nstrftime* without
        @INTL_MACOSX_LIBS@.
        * modules/fprintftime (Link): Remove @INTL_MACOSX_LIBS@.

diff --git a/lib/strftime.c b/lib/strftime.c
index b78901f5fd..291c650b97 100644
--- a/lib/strftime.c
+++ b/lib/strftime.c
@@ -181,7 +181,9 @@ enum pad_style
 #if SUPPORT_NON_GREG_CALENDARS_IN_STRFTIME
 /* Support for non-Gregorian calendars.  */
 # include "localcharset.h"
-# include "localename.h"
+# if !(defined __APPLE__ && defined __MACH__)
+#  include "localename.h"
+# endif
 # include "calendars.h"
 # define CAL_ARGS(x,y) x, y,
 #else
@@ -1137,9 +1139,9 @@ my_strftime (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t 
maxsize)
 #if SUPPORT_NON_GREG_CALENDARS_IN_STRFTIME
   /* Recognize whether to use a non-Gregorian calendar.  */
   const struct calendar *cal = NULL;
-  struct calendar_date caldate;
   if (strcmp (locale_charset (), "UTF-8") == 0)
     {
+# if !(defined __APPLE__ && defined __MACH__)
       const char *loc = gl_locale_name_unsafe (LC_TIME, "LC_TIME");
       if (strlen (loc) >= 5 && !(loc[5] >= 'A' && loc[5] <= 'Z'))
         {
@@ -1149,15 +1151,45 @@ my_strftime (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t 
maxsize)
             cal = &persian_calendar;
           else if (memcmp (loc, "am_ET", 5) == 0)
             cal = &ethiopian_calendar;
-          if (cal != NULL)
-            {
-              if (cal->from_gregorian (&caldate,
-                                       tp->tm_year + 1900,
-                                       tp->tm_mon,
-                                       tp->tm_mday) < 0)
-                cal = NULL;
-            }
         }
+# else /* defined __APPLE__ && defined __MACH__ */
+      /* Nearly equivalent code for macOS, that avoids the need to link with
+         CoreFoundation.
+         It's not entirely equivalent, because it tests only for the language
+         (Thai, Farsi, Amharic) instead of also for the territory (Thailand,
+         Iran, Ethiopia).  */
+      /* Get the translation of "Monday" in the LC_TIME locale, by calling
+         the underlying strftime function.  */
+      struct tm some_monday; /* 2024-01-01 12:00:00 */
+      memset (&some_monday, '\0', sizeof (struct tm));
+      some_monday.tm_year = 2024 - 1900;
+      some_monday.tm_mon = 1 - 1;
+      some_monday.tm_mday = 1;
+      some_monday.tm_wday = 1; /* Monday */
+      some_monday.tm_hour = 12;
+      some_monday.tm_min = 0;
+      some_monday.tm_sec = 0;
+      char weekday_buf[32];
+      if (strftime (weekday_buf, sizeof (weekday_buf), "%A", &some_monday) > 0)
+        {
+          /* Test for the Thai / Farsi / Amharic translation of "Monday".  */
+          if (streq (weekday_buf, "จันทร์") || streq (weekday_buf, 
"วันจันทร์"))
+            cal = &thai_calendar;
+          else if (streq (weekday_buf, "ﺩﻮﺸﻨﺒﻫ") || streq (weekday_buf, 
"دوشنبه"))
+            cal = &persian_calendar;
+          else if (streq (weekday_buf, "ሰኞ"))
+            cal = &ethiopian_calendar;
+        }
+# endif
+    }
+  struct calendar_date caldate;
+  if (cal != NULL)
+    {
+      if (cal->from_gregorian (&caldate,
+                               tp->tm_year + 1900,
+                               tp->tm_mon,
+                               tp->tm_mday) < 0)
+        cal = NULL;
     }
 #endif
   bool tzset_called = false;
diff --git a/modules/fprintftime b/modules/fprintftime
index 121073af1f..3700a3b45e 100644
--- a/modules/fprintftime
+++ b/modules/fprintftime
@@ -21,7 +21,6 @@ Include:
 "fprintftime.h"
 
 Link:
-@INTL_MACOSX_LIBS@
 
 License:
 GPL
diff --git a/modules/nstrftime b/modules/nstrftime
index ee52e89825..4cb87d28af 100644
--- a/modules/nstrftime
+++ b/modules/nstrftime
@@ -22,7 +22,7 @@ extensions
 intprops
 libc-config
 localcharset
-localename-unsafe
+localename-unsafe [case "$host_os" in darwin*) false ;; *) true ;; esac]
 bool
 stdckdint-h
 stdint-h
@@ -38,7 +38,6 @@ Include:
 "strftime.h"
 
 Link:
-@INTL_MACOSX_LIBS@
 $(LIBTHREAD)
 
 License:
diff --git a/modules/nstrftime-limited b/modules/nstrftime-limited
index 9f53b621a7..18fc11a51a 100644
--- a/modules/nstrftime-limited
+++ b/modules/nstrftime-limited
@@ -23,7 +23,7 @@ extensions
 intprops
 libc-config
 localcharset
-localename-unsafe-limited
+localename-unsafe-limited [case "$host_os" in darwin*) false ;; *) true ;; 
esac]
 bool
 stdckdint-h
 time_rz
@@ -38,7 +38,6 @@ Include:
 "strftime.h"
 
 Link:
-@INTL_MACOSX_LIBS@
 
 License:
 LGPL
diff --git a/modules/nstrftime-tests b/modules/nstrftime-tests
index 88f4b85074..80dae9e024 100644
--- a/modules/nstrftime-tests
+++ b/modules/nstrftime-tests
@@ -49,16 +49,16 @@ check_PROGRAMS += \
   test-nstrftime-TH \
   test-nstrftime-IR \
   test-nstrftime-ET
-test_nstrftime_LDADD = $(LDADD) $(SETLOCALE_LIB) @INTL_MACOSX_LIBS@ 
$(LIBTHREAD)
-test_nstrftime_DE_LDADD = $(LDADD) $(SETLOCALE_LIB) @INTL_MACOSX_LIBS@ 
$(LIBTHREAD)
-test_nstrftime_TH_LDADD = $(LDADD) $(SETLOCALE_LIB) @INTL_MACOSX_LIBS@ 
$(LIBTHREAD)
-test_nstrftime_IR_LDADD = $(LDADD) $(SETLOCALE_LIB) @INTL_MACOSX_LIBS@ 
$(LIBTHREAD)
-test_nstrftime_ET_LDADD = $(LDADD) $(SETLOCALE_LIB) @INTL_MACOSX_LIBS@ 
$(LIBTHREAD)
+test_nstrftime_LDADD = $(LDADD) $(SETLOCALE_LIB) $(LIBTHREAD)
+test_nstrftime_DE_LDADD = $(LDADD) $(SETLOCALE_LIB) $(LIBTHREAD)
+test_nstrftime_TH_LDADD = $(LDADD) $(SETLOCALE_LIB) $(LIBTHREAD)
+test_nstrftime_IR_LDADD = $(LDADD) $(SETLOCALE_LIB) $(LIBTHREAD)
+test_nstrftime_ET_LDADD = $(LDADD) $(SETLOCALE_LIB) $(LIBTHREAD)
 
 if OS_IS_NATIVE_WINDOWS
 TESTS += test-nstrftime-w32utf8.sh
 noinst_PROGRAMS += test-nstrftime-w32utf8
-test_nstrftime_w32utf8_LDADD = $(LDADD) test-nstrftime-windows-utf8.res 
$(SETLOCALE_LIB) @INTL_MACOSX_LIBS@ $(LIBTHREAD)
+test_nstrftime_w32utf8_LDADD = $(LDADD) test-nstrftime-windows-utf8.res 
$(SETLOCALE_LIB) $(LIBTHREAD)
 test-nstrftime-windows-utf8.res : $(srcdir)/windows-utf8.rc
        $(WINDRES) -i $(srcdir)/windows-utf8.rc -o 
test-nstrftime-windows-utf8.res --output-format=coff
 MOSTLYCLEANFILES += test-nstrftime-windows-utf8.res




Reply via email to