[Crash-utility] [PATCH v2 1/1] Support cross-compilation

Bhupesh Sharma bhsharma at redhat.com
Mon Oct 12 19:46:34 UTC 2020


Hello Alexander,

Thanks for working on this. I did some work earlier on this after
discussion with Dave Anderson, but unfortunately I could never queue
it up for upstream consideration, due to lack of time.

I have some suggestions below:

On Sat, Oct 10, 2020 at 7:42 PM Alexander Egorenkov
<egorenar-dev at posteo.net> wrote:
>
> In order to support cross-compilation of crash-utilty,
> the configure tool compiled from configure.c must be built
> for the host architecture where the cross-compilation will run
> instead of the target architecture where the crash-utility shall run.
> Therefore, we need to support two C compilers in Maklefile,
> one for the host and one for the target. The old CC makefile variable
> shall represent the compiler for the target architecture and
> the new CONF_CC makefile variable shall represent the host compiler.
> Both variables differ only when a cross-compilation is performed.
> Furthermore, there must be a way to override the target architecture
> which is deduced from the preprocessor macros defined by the compiler
> used for the compilation of configure.c, because otherwise the configure
> tool will deduce host's architecture instead of the desired target.
> With the new preprocessor define CONF_HOST_ARCH, it is possible to
> set the desired target architecture for the compiled crash-utility.
> When cross-compiling, pass the value of CONF_HOST_ARCH via
> the CONF_CFLAGS makefile variable, e.g. like this:
>
>     make CONF_CC=gcc CC=s390x-linux-gnu-gcc CONF_FLAGS="-DCONF_HOST_ARCH=S390X"

I think we can make it simpler:

You can take an example of the linux kernel top-level Makefile (or
other similar Makefiles - e.g. busybox , which try to standardize the
cross compiling and selecting different sets of gcc/bin-utils). I have
copied the following text from linux/Makefile:

# Cross compiling and selecting different set of gcc/bin-utils
# ---------------------------------------------------------------------------
#
# When performing cross compilation for other architectures ARCH shall be set
# to the target architecture. (See arch/* for the possibilities).
# ARCH can be set during invocation of make:
# make ARCH=ia64
# Another way is to have ARCH set in the environment.
# The default ARCH is the host where make is executed.

# CROSS_COMPILE specify the prefix used for all executables used
# during compilation. Only gcc and related bin-utils executables
# are prefixed with $(CROSS_COMPILE).
# CROSS_COMPILE can be set on the command line
# make CROSS_COMPILE=ia64-linux-
# Alternatively CROSS_COMPILE can be set in the environment.
# Default value for CROSS_COMPILE is not to prefix executables
# Note: Some architectures assign CROSS_COMPILE in their arch/*/Makefile

1. So, we can set the underlying arch on basis of ARCH and
CROSS_COMPILE variables (see an example below):

# SUBARCH tells the usermode build what the underlying arch is.  That is set
# first, and if a usermode build is happening, the "ARCH=um" on the command
# line overrides the setting of ARCH below.  If a native build is happening,
# then ARCH is assigned, getting whatever value it gets normally, and
# SUBARCH is subsequently ignored.

ifneq ($(CROSS_COMPILE),)
SUBARCH := $(shell echo $(CROSS_COMPILE) | cut -d- -f1 | sed 's:^.*/::g')
else
SUBARCH := $(shell uname -m)
endif
SUBARCH := $(shell echo $(SUBARCH) | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ \
                     -e s/arm.*/arm/ -e s/sa110/arm/ \
                     -e s/s390x/s390/ -e s/parisc64/parisc/ \
                     -e s/ppc.*/powerpc/ -e s/mips.*/mips/ )

ARCH ?= $(SUBARCH)

2. We can do additional checks if $(ARCH) is set:

# Additional ARCH settings for x86
ifeq ($(ARCH),i386)
        SRCARCH := x86
endif
ifeq ($(ARCH),x86_64)
        SRCARCH := x86
endif

<.. and so on..>

3. Also, we can set CC and friends like the following example:

# Make variables (CC, etc...)

AS        = $(CROSS_COMPILE)as
CC        = $(CROSS_COMPILE)gcc
LD        = $(CC) -nostdlib
CPP        = $(CC) -E
AR        = $(CROSS_COMPILE)ar
NM        = $(CROSS_COMPILE)nm
STRIP        = $(CROSS_COMPILE)strip
OBJCOPY        = $(CROSS_COMPILE)objcopy
OBJDUMP        = $(CROSS_COMPILE)objdump
PKG_CONFIG    ?= $(CROSS_COMPILE)pkg-config

4. So eventually our build step reduces to something like:

make CROSS_COMPILE=s390x-linux-gnu- ARCH=s390x

or something on similar lines..

I hope this helps.

Regards,
Bhupesh

> Signed-off-by: Alexander Egorenkov <egorenar-dev at posteo.net>
> ---
>
>
> v1 -> v2:
>  * Improved commit message
>  * Added a note how to cross-compile crash-utilty to README
>  * Moved CONF_CC makefile variable to correct place location
>
>
>  Makefile    |  3 ++-
>  README      |  4 +++
>  configure.c | 76 ++++++++++++++++++++++++++++-------------------------
>  3 files changed, 46 insertions(+), 37 deletions(-)
>
> diff --git a/Makefile b/Makefile
> index 7455410..d5985bc 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -25,6 +25,7 @@ PROGRAM=crash
>  #
>  TARGET=
>  GDB_CONF_FLAGS=
> +CONF_CC = ${CC}
>
>  ARCH := $(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ -e s/sa110/arm/)
>  ifeq (${ARCH}, ppc64)
> @@ -288,7 +289,7 @@ force:
>
>  make_configure: force
>         @rm -f configure
> -       @${CC} ${CONF_FLAGS} -o configure configure.c ${WARNING_ERROR} ${WARNING_OPTIONS}
> +       @${CONF_CC} ${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 bfbaef6..707bc63 100644
> --- a/README
> +++ b/README
> @@ -100,6 +100,10 @@
>    o  On an x86_64 host, an x86_64 binary that can be used to analyze
>       ppc64le dumpfiles may be built by typing "make target=PPC64".
>
> +  To cross-compile the crash utility e.g. for a s390x target:
> +
> +    $ make CONF_CC=gcc CC=s390x-linux-gnu-gcc CONF_CFLAGS="-DCONF_HOST_ARCH=S390X"
> +
>    Traditionally when vmcores are compressed via the makedumpfile(8) facility
>    the libz compression library is used, and by default the crash utility
>    only supports libz.  Recently makedumpfile has been enhanced to optionally
> diff --git a/configure.c b/configure.c
> index 7f6d19e..d736c56 100644
> --- a/configure.c
> +++ b/configure.c
> @@ -120,6 +120,45 @@ void add_extra_lib(char *);
>  #define MIPS    11
>  #define SPARC64 12
>
> +#ifndef CONF_HOST_ARCH
> +#ifdef __alpha__
> +#define CONF_HOST_ARCH ALPHA
> +#endif
> +#ifdef __i386__
> +#define CONF_HOST_ARCH X86
> +#endif
> +#ifdef __powerpc__
> +#define CONF_HOST_ARCH PPC
> +#endif
> +#ifdef __ia64__
> +#define CONF_HOST_ARCH IA64
> +#endif
> +#ifdef __s390__
> +#define CONF_HOST_ARCH S390
> +#endif
> +#ifdef __s390x__
> +#define CONF_HOST_ARCH S390X
> +#endif
> +#ifdef __powerpc64__
> +#define CONF_HOST_ARCH PPC64
> +#endif
> +#ifdef __x86_64__
> +#define CONF_HOST_ARCH X86_64
> +#endif
> +#ifdef __arm__
> +#define CONF_HOST_ARCH ARM
> +#endif
> +#ifdef __aarch64__
> +#define CONF_HOST_ARCH ARM64
> +#endif
> +#ifdef __mips__
> +#define CONF_HOST_ARCH MIPS
> +#endif
> +#ifdef __sparc_v9__
> +#define CONF_HOST_ARCH SPARC64
> +#endif
> +#endif // #ifndef CONF_HOST_ARCH
> +
>  #define TARGET_X86    "TARGET=X86"
>  #define TARGET_ALPHA  "TARGET=ALPHA"
>  #define TARGET_PPC    "TARGET=PPC"
> @@ -349,42 +388,7 @@ get_current_configuration(struct supported_gdb_version *sp)
>         static char buf[512];
>         char *p;
>
> -#ifdef __alpha__
> -        target_data.target = ALPHA;
> -#endif
> -#ifdef __i386__
> -        target_data.target = X86;
> -#endif
> -#ifdef __powerpc__
> -        target_data.target = PPC;
> -#endif
> -#ifdef __ia64__
> -        target_data.target = IA64;
> -#endif
> -#ifdef __s390__
> -        target_data.target = S390;
> -#endif
> -#ifdef __s390x__
> -        target_data.target = S390X;
> -#endif
> -#ifdef __powerpc64__
> -        target_data.target = PPC64;
> -#endif
> -#ifdef __x86_64__
> -        target_data.target = X86_64;
> -#endif
> -#ifdef __arm__
> -        target_data.target = ARM;
> -#endif
> -#ifdef __aarch64__
> -        target_data.target = ARM64;
> -#endif
> -#ifdef __mips__
> -        target_data.target = MIPS;
> -#endif
> -#ifdef __sparc_v9__
> -       target_data.target = SPARC64;
> -#endif
> +       target_data.target = CONF_HOST_ARCH;
>
>         set_initial_target(sp);
>
> --
> 2.28.0
>
>
> --
> Crash-utility mailing list
> Crash-utility at redhat.com
> https://www.redhat.com/mailman/listinfo/crash-utility
>




More information about the Crash-utility mailing list