Currently, the cross compilation is not supported in
crash-utility, let's add this feature.

Tested(ubuntu-24.04) on: X86_64, X86, aarch64, s390x,
powerpc64, alpha, sparc64, mips, riscv64

E.g: cross build crash-utility for aarch64 on X86_64
 make CROSS_COMPILE=aarch64-linux-gnu- -j`nproc`
or
 make CROSS_COMPILE=aarch64-linux-gnu- -j`nproc` warn

Note: there are still some limitations for cross
compilation because some dependency libs that are not
cross-compiled, for example: snappy, lzo, zstd

Signed-off-by: Lianbo Jiang <liji...@redhat.com>
---
 Makefile    | 50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 README      |  5 +++++
 configure.c |  6 ++++++
 help.c      |  5 +++++
 4 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index 4e80658aaf49..28d4b853a916 100644
--- a/Makefile
+++ b/Makefile
@@ -27,11 +27,57 @@ PROGRAM=crash
 TARGET=
 GDB_CONF_FLAGS=
 
-ARCH := $(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ -e 
s/arm.*/arm/ -e s/sa110/arm/)
+# Supported arches for cross compilation: x86_64, x86, aarch64, s390x,
+# powerpc64, alpha, sparc64, mips, riscv64
+# E.g: cross compile crash-utility for aarch64 on X86_64
+# make CROSS_COMPILE=aarch64-linux-gnu- -j`nproc`
+# or
+# make CROSS_COMPILE=aarch64-linux-gnu- -j`nproc` warn
+#
+ifneq ($(CROSS_COMPILE),)
+ARCH := $(shell echo $(CROSS_COMPILE) | sed 's:^.*/::g' | cut -d- -f1)
+else
+ARCH := $(shell uname -m)
+endif
+
+ARCH := $(shell echo $(ARCH) | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ -e 
s/arm.*/arm/ -e s/sa110/arm/)
+
+CC     = $(CROSS_COMPILE)gcc
+CXX    = $(CROSS_COMPILE)g++
+HOSTCC = gcc
+
 ifeq (${ARCH}, ppc64)
 CONF_FLAGS = -m64
 endif
 
+ifneq ($(CROSS_COMPILE),)
+ifeq (${ARCH}, x86_64)
+CONF_TARGET_ARCH := X86_64
+else ifeq (${ARCH}, aarch64)
+CONF_TARGET_ARCH := ARM64
+else ifeq (${ARCH}, s390x)
+CONF_TARGET_ARCH := S390X
+else ifeq (${ARCH}, powerpc64)
+CONF_TARGET_ARCH := PPC64
+else ifeq (${ARCH}, ppc64le)
+CONF_TARGET_ARCH := PPC64
+else ifeq (${ARCH}, alpha)
+CONF_TARGET_ARCH := ALPHA
+else ifeq (${ARCH}, i386)
+CONF_TARGET_ARCH := X86
+else ifeq (${ARCH}, mips)
+CONF_TARGET_ARCH := MIPS
+else ifeq (${ARCH}, sparc64)
+CONF_TARGET_ARCH := SPARC64
+else ifeq (${ARCH}, riscv64)
+CONF_TARGET_ARCH := RISCV64
+else
+$(error The current Arch(${ARCH}) does not support cross compilation)
+endif
+CONF_FLAGS += -DCONF_TARGET_ARCH=${CONF_TARGET_ARCH}
+CONF_FLAGS += -DGDB_TARGET_DEFAULT="\"GDB_CONF_FLAGS=--host=$(shell echo 
$(CROSS_COMPILE) | sed -e 's:^.*/::g' -e 's/-$$//')\""
+endif
+
 #
 # GDB, GDB_FILES, GDB_OFILES and GDB_PATCH_FILES will be configured 
automatically by configure 
 #
@@ -313,7 +359,7 @@ force:
 
 make_configure: force
        @rm -f configure
-       @${CC} ${CONF_FLAGS} -o configure configure.c ${WARNING_ERROR} 
${WARNING_OPTIONS}
+       @${HOSTCC} ${CONF_FLAGS} -o configure configure.c ${WARNING_ERROR} 
${WARNING_OPTIONS}
 
 clean: make_configure
        @./configure ${CONF_TARGET_FLAG} -q -b
diff --git a/README b/README
index d7e7d9b97899..4ce55626e501 100644
--- a/README
+++ b/README
@@ -84,6 +84,11 @@
     $ cd crash-8.0.6
     $ make
 
+  To cross compile the crash utility for aarch64 on x86_64:
+    $ make CROSS_COMPILE=aarch64-linux-gnu- -j`nproc`
+
+  Supported arches for cross compilation: x86_64, x86, aarch64, s390x, 
powerpc64, alpha, sparc64, mips, riscv64
+
   The initial build will take several minutes  because the embedded gdb module
   must be configured and built.  Alternatively, the crash source RPM file
   may be installed and built, and the resultant crash binary RPM file 
installed.
diff --git a/configure.c b/configure.c
index 3386cdcbbe94..e0626318b8d5 100644
--- a/configure.c
+++ b/configure.c
@@ -169,7 +169,9 @@ void add_extra_lib(char *);
 #define TARGET_CFLAGS_LOONGARCH64     "TARGET_CFLAGS="
 #define TARGET_CFLAGS_LOONGARCH64_ON_X86_64    "TARGET_CFLAGS="
 
+#ifndef GDB_TARGET_DEFAULT
 #define GDB_TARGET_DEFAULT        "GDB_CONF_FLAGS="
+#endif
 #define GDB_TARGET_ARM_ON_X86     "GDB_CONF_FLAGS=--target=arm-elf-linux"
 #define GDB_TARGET_ARM_ON_X86_64  "GDB_CONF_FLAGS=--target=arm-elf-linux 
CFLAGS=-m32 CXXFLAGS=-m32"
 #define GDB_TARGET_X86_ON_X86_64  "GDB_CONF_FLAGS=--target=i686-pc-linux-gnu 
CFLAGS=-m32 CXXFLAGS=-m32"
@@ -386,6 +388,7 @@ get_current_configuration(struct supported_gdb_version *sp)
        static char buf[512];
        char *p;
 
+#ifndef CONF_TARGET_ARCH
 #ifdef __alpha__
         target_data.target = ALPHA;
 #endif
@@ -432,6 +435,9 @@ get_current_configuration(struct supported_gdb_version *sp)
 #ifdef __loongarch64
        target_data.target = LOONGARCH64;
 #endif
+#else
+       target_data.target = CONF_TARGET_ARCH;
+#endif
 
        set_initial_target(sp);
 
diff --git a/help.c b/help.c
index 87eb9eed2eef..c1d8eca44f22 100644
--- a/help.c
+++ b/help.c
@@ -9618,6 +9618,11 @@ char *README[] = {
 README_ENTER_DIRECTORY,
 "    $ make",
 "",
+"  To cross compile the crash utility for aarch64 on x86_64: ",
+"    $ make CROSS_COMPILE=aarch64-linux-gnu- -j`nproc`",
+"",
+"  Supported arches for cross compilation: x86_64, x86, aarch64, s390x, 
powerpc64, alpha, sparc64, mips, riscv64",
+"",
 "  The initial build will take several minutes  because the embedded gdb 
module",
 "  must be configured and built.  Alternatively, the crash source RPM file",
 "  may be installed and built, and the resultant crash binary RPM file 
installed.",
-- 
2.47.1
--
Crash-utility mailing list -- devel@lists.crash-utility.osci.io
To unsubscribe send an email to devel-le...@lists.crash-utility.osci.io
https://${domain_name}/admin/lists/devel.lists.crash-utility.osci.io/
Contribution Guidelines: https://github.com/crash-utility/crash/wiki

Reply via email to