I wrote:
> And here's the cross-compilation handling that I promised to add.
> 
> 
> 2024-10-31  Bruno Haible  <br...@clisp.org>
> 
>       crc: Don't attempt to run a compiled C program when cross-compiling.

Here's an improvement:


2024-10-31  Bruno Haible  <br...@clisp.org>

        crc: Support generating the tables also when cross-compiling.
        * m4/build-cc.m4: New file.
        * modules/crc (Files): Add it.
        (configure.ac): Invoke gl_BUILD_CC. Don't set GL_CROSS_COMPILING.
        (Makefile.am): Use $(BUILD_CC) etc. instead of $(CC) etc.

>From 0bf1479317d0df7a034052202a2d07aef21fd999 Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Fri, 1 Nov 2024 00:17:20 +0100
Subject: [PATCH] crc: Support generating the tables also when cross-compiling.

* m4/build-cc.m4: New file.
* modules/crc (Files): Add it.
(configure.ac): Invoke gl_BUILD_CC. Don't set GL_CROSS_COMPILING.
(Makefile.am): Use $(BUILD_CC) etc. instead of $(CC) etc.
---
 ChangeLog      |  8 ++++++++
 m4/build-cc.m4 | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++
 modules/crc    | 12 +++++-------
 3 files changed, 66 insertions(+), 7 deletions(-)
 create mode 100644 m4/build-cc.m4

diff --git a/ChangeLog b/ChangeLog
index 59a709aae8..aa0b1a538f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2024-10-31  Bruno Haible  <br...@clisp.org>
+
+	crc: Support generating the tables also when cross-compiling.
+	* m4/build-cc.m4: New file.
+	* modules/crc (Files): Add it.
+	(configure.ac): Invoke gl_BUILD_CC. Don't set GL_CROSS_COMPILING.
+	(Makefile.am): Use $(BUILD_CC) etc. instead of $(CC) etc.
+
 2024-10-31  Bruno Haible  <br...@clisp.org>
 
 	malloc-posix, realloc-posix: Fix incorrect expansion of AC_FUNC_MALLOC.
diff --git a/m4/build-cc.m4 b/m4/build-cc.m4
new file mode 100644
index 0000000000..980066039e
--- /dev/null
+++ b/m4/build-cc.m4
@@ -0,0 +1,53 @@
+# build-cc.m4
+# serial 1
+dnl Copyright (C) 2024 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+dnl This file is offered as-is, without any warranty.
+
+dnl Written by Bruno Haible.
+
+dnl When the build environment ($build_os) is different from the target runtime
+dnl environment ($host_os), a build step that requires execution of the built
+dnl program must use a native compiler.
+dnl In order to specify this compiler, the user can specify the following
+dnl variables (as environment variables or as assignment arguments to
+dnl 'configure'):
+dnl   - BUILD_CC       - the C compiler
+dnl   - BUILD_CPPFLAGS - compiler options used during preprocessing
+dnl   - BUILD_CFLAGS   - compiler options used during compilation to object code
+dnl   - BUILD_LDFLAGS  - compiler options used durint linking
+dnl These are the same conventions as used e.g. by glibc.
+dnl
+dnl gl_BUILD_CC provides these variables.
+dnl If not cross-compiling, the values are the same as those of
+dnl CC, CPPFLAGS, CFLAGS, LDFLAGS respectively (so that the user does not have
+dnl to specify them twice).
+dnl If no native compiler was found, BUILD_CC is set to empty.
+dnl
+dnl This macro is intentionally simple.
+dnl If you need things like BUILD_OBJEXT, BUILD_EXEEXT, etc., consider using
+dnl AX_PROG_CC_FOR_BUILD from the Autoconf Archive.
+
+AC_DEFUN_ONCE([gl_BUILD_CC],
+[
+  AC_REQUIRE([AC_PROG_CC])
+  dnl $cross_compiling is 'yes' if executables created by the C compiler $CC
+  dnl can be run in the build environment, or 'no' otherwise.
+  if test $cross_compiling = yes; then
+    if test -z "$BUILD_CC"; then
+      dnl If the user has not specified BUILD_CC, try gcc, then cc.
+      AC_CHECK_PROGS([BUILD_CC], [gcc cc])
+    fi
+  else
+    BUILD_CC="$CC"
+    BUILD_CPPFLAGS="$CPPFLAGS"
+    BUILD_CFLAGS="$CFLAGS"
+    BUILD_LDFLAGS="$LDFLAGS"
+  fi
+  AC_SUBST([BUILD_CC])
+  AC_SUBST([BUILD_CPPFLAGS])
+  AC_SUBST([BUILD_CFLAGS])
+  AC_SUBST([BUILD_LDFLAGS])
+])
diff --git a/modules/crc b/modules/crc
index f3122ddd3b..618623eee7 100644
--- a/modules/crc
+++ b/modules/crc
@@ -6,6 +6,7 @@ lib/crc.h
 lib/crc.c
 lib/crc-generate-table.c
 m4/crc.m4
+m4/build-cc.m4
 
 Depends-on:
 stdint
@@ -13,28 +14,25 @@ endian
 
 configure.ac:
 AC_REQUIRE([gl_CRC_SLICE_BY_8])
-# Set to 'yes' if executables created by the C compiler $CC can be run in the
-# build environment, or to 'no' otherwise.
-GL_CROSS_COMPILING=$cross_compiling
-AC_SUBST([GL_CROSS_COMPILING])
+gl_BUILD_CC
 AC_PROG_MKDIR_P
 
 Makefile.am:
 lib_SOURCES += crc.c
 
 # Generate crc-sliceby8.h.
-# Do so only when not cross-compiling.
+# Use a native compiler when cross-compiling.
 # Don't use any Gnulib modules (since libgnu.a will only be available after
 # this directory is built!).  Therefore, don't use any of the Automake variables
 # $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(AM_CFLAGS)
 # $(AM_LDFLAGS).  And do the compilation in a temporary directory, where
 # gnulib-generated stdio.h and stdlib.h files are not visible.
 $(srcdir)/crc-sliceby8.h: $(srcdir)/crc-generate-table.c
-	if test @GL_CROSS_COMPILING@ = no; then \
+	if test -n '$(BUILD_CC)'; then \
 	  $(MKDIR_P) crc-tmp \
 	  && abs_srcdir=`cd $(srcdir) && pwd` \
 	  && (cd crc-tmp \
-	      && $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o crc-generate-table $$abs_srcdir/crc-generate-table.c) \
+	      && $(BUILD_CC) $(BUILD_CPPFLAGS) $(BUILD_CFLAGS) $(BUILD_LDFLAGS) -o crc-generate-table $$abs_srcdir/crc-generate-table.c) \
 	  && crc-tmp/crc-generate-table $(srcdir)/crc-sliceby8.h-t \
 	  && rm -rf crc-tmp \
 	  && mv $(srcdir)/crc-sliceby8.h-t $(srcdir)/crc-sliceby8.h; \
-- 
2.34.1

Reply via email to