[PATCH v2] newlib: libm: workaround ar duplicate member behavior

Mike Frysinger <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
GNU ar has undocumented behavior where it doesn't dedupe its inputs if
they're all on the same command line, so we have to dedupe ourselves.
---
v2
- use awk to dedupe the object list

 newlib/Makefile.am | 22 ++++++++++++++++++++++
 newlib/Makefile.in | 24 +++++++++++++++++++-----
 2 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/newlib/Makefile.am b/newlib/Makefile.am
index 80256952da3d..116332eb2ab9 100644
--- a/newlib/Makefile.am
+++ b/newlib/Makefile.am
@@ -85,6 +85,22 @@ toollib_DATA = $(CRT0) $(CRT1)
 
 CLEANFILES += libg.a
 
+## GNU ar has undocumented behavior when specifying the same name multiple times
+## in a single invocation, so we have to dedupe ourselves.
+## https://sourceware.org/PR28917
+AWK_UNIQUE_OBJS = $(AWK) '{ \
+  for (i = NF; i > 0; --i) { \
+    split($$i, parts, "/"); \
+    name = parts[length(parts)]; \
+    if (!(name in seen)) { \
+      objs[i] = $$i; \
+      seen[name] = 1; \
+    } \
+  } \
+  for (i in objs) \
+    print objs[i]; \
+}'
+
 # The functions ldexp, frexp and modf are traditionally supplied in
 # both libc.a and libm.a.  We build them in libm.a and copy them over,
 # along with some required supporting routines.
@@ -124,6 +140,12 @@ libm_a_CCASFLAGS = $(AM_CCASFLAGS) $(libm_a_CCASFLAGS_$(subst /,_,$(@D))) $(libm
 libm_a_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/libm/common $(libm_a_CPPFLAGS_$(subst /,_,$(@D))) $(libm_a_CPPFLAGS_$(subst /,_,$(@D)_$(<F)))
 $(libm_a_OBJECTS): stmp-targ-include
 
+libm.a: $(libm_a_OBJECTS) $(libm_a_DEPENDENCIES)
+	$(AM_V_at)rm -f $@
+	$(AM_V_AR)objs=`echo $(libm_a_OBJECTS) | $(AWK_UNIQUE_OBJS)` || exit $$?; \
+	  $(AR) $(ARFLAGS) $@ $$objs
+	$(AM_V_at)$(RANLIB) $@
+
 if HAVE_MULTISUBDIR
 $(BUILD_MULTISUBDIR):
 	$(MKDIR_P) $@
diff --git a/newlib/Makefile.in b/newlib/Makefile.in
index 2b60ef1ca96c..cc7afc70451b 100644
--- a/newlib/Makefile.in
+++ b/newlib/Makefile.in
@@ -1361,6 +1361,19 @@ toollib_LIBRARIES = libm.a \
 
 @HAVE_MULTISUBDIR_TRUE@BUILD_MULTISUBDIR = $(builddir)$(MULTISUBDIR)
 toollib_DATA = $(CRT0) $(CRT1)
+AWK_UNIQUE_OBJS = $(AWK) '{ \
+  for (i = NF; i > 0; --i) { \
+    split($$i, parts, "/"); \
+    name = parts[length(parts)]; \
+    if (!(name in seen)) { \
+      objs[i] = $$i; \
+      seen[name] = 1; \
+    } \
+  } \
+  for (i in objs) \
+    print objs[i]; \
+}'
+
 
 # The functions ldexp, frexp and modf are traditionally supplied in
 # both libc.a and libm.a.  We build them in libm.a and copy them over,
@@ -3248,11 +3261,6 @@ libm/machine/x86_64/libm_a-fetestexcept.$(OBJEXT):  \
 libm/machine/x86_64/libm_a-feupdateenv.$(OBJEXT):  \
 	libm/machine/x86_64/$(am__dirstamp)
 
-libm.a: $(libm_a_OBJECTS) $(libm_a_DEPENDENCIES) $(EXTRA_libm_a_DEPENDENCIES) 
-	$(AM_V_at)-rm -f libm.a
-	$(AM_V_AR)$(libm_a_AR) libm.a $(libm_a_OBJECTS) $(libm_a_LIBADD)
-	$(AM_V_at)$(RANLIB) libm.a
-
 mostlyclean-compile:
 	-rm -f *.$(OBJEXT)
 	-rm -f libm/common/*.$(OBJEXT)
@@ -8650,6 +8658,12 @@ libg.a: libc.a
 	$(AM_V_GEN)ln libc.a libg.a >/dev/null 2>/dev/null || cp libc.a libg.a
 $(libm_a_OBJECTS): stmp-targ-include
 
+libm.a: $(libm_a_OBJECTS) $(libm_a_DEPENDENCIES)
+	$(AM_V_at)rm -f $@
+	$(AM_V_AR)objs=`echo $(libm_a_OBJECTS) | $(AWK_UNIQUE_OBJS)` || exit $$?; \
+	  $(AR) $(ARFLAGS) $@ $$objs
+	$(AM_V_at)$(RANLIB) $@
+
 @HAVE_MULTISUBDIR_TRUE@$(BUILD_MULTISUBDIR):
 @HAVE_MULTISUBDIR_TRUE@	$(MKDIR_P) $@
 
-- 
2.34.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.