Hi Paul,

Paul Eggert <egg...@cs.ucla.edu> writes:

> Yes, that should work, so long as the build host isn't a Microsoft
> Windows platform that would require $(BUILD_EXEEXT). And that should
> be good enough; we don't need the full power of AX_PROG_CC_FOR_BUILD
> from the Autoconf Archive.

Here is a patch that worked in an x86 docker container with an arm64
cross compiler.

Can we just assume that crc will always be used and depend on
build-cc.m4? I guess the more robust way to do things is to do the
following in bootstrap.conf:

    bootstrap_post_import_hook ()
    {
      $gnulib_tool --copy-file m4/build-cc.m4
    }

Like we do for tests/init.sh.

Collin

>From 9dab53ad3f15e0482020253f3f87f7be03748d53 Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Thu, 15 May 2025 22:02:26 -0700
Subject: [PATCH] build: allow make-prime-list to build when cross-compiling

* src/local.mk: Compile the make-prime-list in a temporary directory
using $(BUILD_CC) so it can be run even when $(CC) is a cross-compiler.
Add some comments.
(noinst_PROGRAMS): Remove src/make-prime-list.
(src_make_prime_list_LDADD): Remove variable.
* src/make-prime-list.c: Don't include config.h and attributes.h.
Remove unnecessary #undefs
(ATTRIBUTE_CONST, ATTRIBUTE_MALLOC): Define for the host compiler.
---
 src/local.mk          | 33 ++++++++++++++++++---------------
 src/make-prime-list.c | 22 ++++++++++++++--------
 2 files changed, 32 insertions(+), 23 deletions(-)

diff --git a/src/local.mk b/src/local.mk
index fd9dc81c2..833c714e1 100644
--- a/src/local.mk
+++ b/src/local.mk
@@ -36,8 +36,7 @@ pkglibexec_PROGRAMS = @pkglibexec_PROGRAMS@
 
 # Needed by the testsuite.
 noinst_PROGRAMS =		\
-  src/getlimits			\
-  src/make-prime-list
+  src/getlimits
 
 noinst_HEADERS =		\
   src/chown.h			\
@@ -150,11 +149,6 @@ src_ln_LDADD = $(LDADD)
 src_logname_LDADD = $(LDADD)
 src_ls_LDADD = $(LDADD)
 
-# This must *not* depend on anything in lib/, since it is used to generate
-# src/primes.h.  If it depended on libcoreutils.a, that would pull all lib/*.c
-# into BUILT_SOURCES.
-src_make_prime_list_LDADD =
-
 src_md5sum_LDADD = $(LDADD)
 src_mkdir_LDADD = $(LDADD)
 src_mkfifo_LDADD = $(LDADD)
@@ -556,15 +550,24 @@ $(top_srcdir)/src/dircolors.h: src/dcgen src/dircolors.hin
 # and it needs to be built on a widest-known-int architecture, so it's
 # built only if absent.  It is not cleaned because we don't want to
 # insist that maintainers must build on hosts that support the widest
-# known ints (currently 128-bit).
+# known ints (currently 128-bit).  It is built in a temporary directory
+# to avoid Gnulib and allow cross-compilers.  The BUILD_* definitions
+# come from Gnulib's gl_BUILD_CC which is invoked for the crc module.
 BUILT_SOURCES += $(top_srcdir)/src/primes.h
-$(top_srcdir)/src/primes.h:
-	$(AM_V_at)${MKDIR_P} src
-	$(MAKE) src/make-prime-list$(EXEEXT)
-	$(AM_V_GEN)rm -f $@ $@-t
-	$(AM_V_at)src/make-prime-list$(EXEEXT) 5000 > $@-t
-	$(AM_V_at)chmod a-w $@-t
-	$(AM_V_at)mv $@-t $@
+$(top_srcdir)/src/primes.h: $(top_srcdir)/src/make-prime-list.c
+	$(AM_V_GEN)if test -n '$(BUILD_CC)'; then \
+	  $(MKDIR_P) $(top_srcdir)/src/primes-tmp \
+	  && (cd $(top_srcdir)/src/primes-tmp \
+	      && $(BUILD_CC) $(BUILD_CPPFLAGS) $(BUILD_CFLAGS) \
+	        $(BUILD_LDFLAGS) -o make-prime-list$(EXEEXT) \
+	        $(abs_top_srcdir)/src/make-prime-list.c) \
+	  && rm -f $@ $@-t \
+	  && $(top_srcdir)/src/primes-tmp/make-prime-list$(EXEEXT) \
+	    5000 > $@-t \
+	  && chmod a-w $@-t \
+	  && mv $@-t $@ \
+	  && rm -rf $(top_srcdir)/src/primes-tmp; \
+	fi
 
 # false exits nonzero even with --help or --version.
 # test doesn't support --help or --version.
diff --git a/src/make-prime-list.c b/src/make-prime-list.c
index 35cf3627c..1b4c50f34 100644
--- a/src/make-prime-list.c
+++ b/src/make-prime-list.c
@@ -17,9 +17,6 @@ PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 You should have received a copy of the GNU General Public License along with
 this program.  If not, see https://www.gnu.org/licenses/.  */
 
-#include <config.h>
-
-#include <attribute.h>
 #include <inttypes.h>
 
 #include <limits.h>
@@ -29,11 +26,20 @@ this program.  If not, see https://www.gnu.org/licenses/.  */
 #include <stdlib.h>
 #include <errno.h>
 
-/* Deactivate "rpl_"-prefixed definitions of these symbols.  */
-#undef fclose
-#undef free
-#undef malloc
-#undef strerror
+/* This program is compiled in a separate directory to avoid linking to Gnulib
+   which may be cross-compiled.  Therefore, we also do not have config.h and
+   attribute.h.  Just define what we need.  */
+#if 2 < __GNUC__ + (95 <= __GNUC_MINOR__)
+# define ATTRIBUTE_CONST __attribute__ ((__const__))
+#else
+# define ATTRIBUTE_CONST
+#endif
+#if 3 < __GNUC__
+# define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
+#else
+# define ATTRIBUTE_MALLOC
+#endif
+
 
 /* An unsigned type that is no narrower than 32 bits and no narrower
    than unsigned int.  It's best to make it as wide as possible.
-- 
2.49.0

Reply via email to