[Mesa-dev] [PATCH] gen_matypes: fix cross-compiling with gcc

Matt Turner mattst88 at gmail.com
Wed Feb 1 08:00:53 PST 2012


On Tue, Jan 31, 2012 at 6:07 PM, Mike Frysinger <vapier at gentoo.org> wrote:
> The current gen_matypes logic assumes that the host compiler will produce
> information that is useful for the target compiler.  Unfortunately, this
> is not the case whenever cross-compiling.
>
> When we detect that we're cross-compiling and using GCC, use the target
> compiler to produce assembly from the gen_matypes.c source, then process
> it with a shell script to create a usable header.  This is similar to how
> the linux kernel creates its asm-offsets.c file.
>
> Signed-off-by: Mike Frysinger <vapier at gentoo.org>
> ---
> Note: please keep me on cc as i'm not subscribed
>
>  configs/autoconf.in        |    2 ++
>  configure.ac               |    6 ++++++
>  src/mesa/x86/Makefile      |   18 +++++++++++++++---
>  src/mesa/x86/gen_matypes.c |   35 +++++++++++++++++++++++++++++------
>  4 files changed, 52 insertions(+), 9 deletions(-)
>
> diff --git a/configs/autoconf.in b/configs/autoconf.in
> index bb8f2c3..4ad49ea 100644
> --- a/configs/autoconf.in
> +++ b/configs/autoconf.in
> @@ -213,3 +213,5 @@ ifneq ($(LLVM_VERSION),)
>  endif
>
>  HAVE_XF86VIDMODE = @HAVE_XF86VIDMODE@
> +
> +GEN_ASM_OFFSETS = @GEN_ASM_OFFSETS@
> diff --git a/configure.ac b/configure.ac
> index 0e03af7..25c30d9 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -106,6 +106,7 @@ AC_MSG_RESULT([$acv_mesa_CLANG])
>
>  dnl If we're using GCC, make sure that it is at least version 3.3.0.  Older
>  dnl versions are explictly not supported.
> +GEN_ASM_OFFSETS=no
>  if test "x$GCC" = xyes -a "x$acv_mesa_CLANG" = xno; then
>     AC_MSG_CHECKING([whether gcc version is sufficient])
>     major=0
> @@ -123,7 +124,12 @@ if test "x$GCC" = xyes -a "x$acv_mesa_CLANG" = xno; then
>     else
>         AC_MSG_RESULT([yes])
>     fi
> +
> +    if test "x$cross_compiling" = xyes; then
> +        GEN_ASM_OFFSETS=yes
> +    fi
>  fi
> +AC_SUBST([GEN_ASM_OFFSETS])
>
>
>  MKDEP_OPTIONS=-fdepend
> diff --git a/src/mesa/x86/Makefile b/src/mesa/x86/Makefile
> index 9716dc2..0dba5a2 100644
> --- a/src/mesa/x86/Makefile
> +++ b/src/mesa/x86/Makefile
> @@ -13,8 +13,7 @@ INCLUDE_DIRS = \
>        -I../math \
>        -I../tnl
>
> -
> -default: gen_matypes matypes.h
> +default: matypes.h
>
>  clean:
>        -rm -f matypes.h gen_matypes
> @@ -24,9 +23,22 @@ gen_matypes: gen_matypes.c
>        $(HOST_CC) $(ARCH_FLAGS) $(INCLUDE_DIRS) $(HOST_CFLAGS) gen_matypes.c -o gen_matypes
>
>  # need some special rules here, unfortunately
> -matypes.h: ../main/mtypes.h ../tnl/t_context.h gen_matypes
> +matypes.h: ../main/mtypes.h ../tnl/t_context.h
> +
> +ifeq ($(GEN_ASM_OFFSETS),yes)
> +
> +matypes.h: gen_matypes.c
> +       $(CC) $(ARCH_FLAGS) $(INCLUDE_DIRS) $(CFLAGS) gen_matypes.c -DASM_OFFSETS -S -o - | \
> +               sed -n '/^->/{s:^->::;/[$$]/{s:^:#define :;s:[$$]::};p}' > $@
> +
> +else
> +
> +default: gen_matypes
> +matypes.h: gen_matypes
>        ./gen_matypes > matypes.h
>
> +endif
> +
>  common_x86_asm.o: matypes.h
>  3dnow_normal.o: matypes.h
>  3dnow_xform1.o: matypes.h
> diff --git a/src/mesa/x86/gen_matypes.c b/src/mesa/x86/gen_matypes.c
> index 97f71f9..b359bb8 100644
> --- a/src/mesa/x86/gen_matypes.c
> +++ b/src/mesa/x86/gen_matypes.c
> @@ -52,7 +52,7 @@ do {                                                                  \
>    printf( "\n" );                                                     \
>    printf( "/* ====================================================="  \
>           "========\n" );                                              \
> -   printf( " * Offsets for %s\n", x );                                 \
> +   printf( " * Offsets for " x "\n" );                                 \
>    printf( " */\n" );                                                  \
>    printf( "\n" );                                                     \
>  } while (0)
> @@ -61,20 +61,43 @@ do {                                                                        \
>  do {                                                                   \
>    printf( "\n" );                                                     \
>    printf( "/*\n" );                                                   \
> -   printf( " * Flags for %s\n", x );                                   \
> +   printf( " * Flags for " x "\n" );                                   \
>    printf( " */\n" );                                                  \
>    printf( "\n" );                                                     \
>  } while (0)
>
> -#define OFFSET( s, t, m )                                              \
> -   printf( "#define %s\t%lu\n", s, (unsigned long) offsetof( t, m ) );
> +#ifdef ASM_OFFSETS
>
> -#define SIZEOF( s, t )                                                 \
> -   printf( "#define %s\t%lu\n", s, (unsigned long) sizeof(t) );
> +/*
> + * Format the asm output in a special way that we can manipulate
> + * after the fact and turn into the final header for the target.
> + */
> +
> +#define DEFINE_UL( s, ul )                                             \
> +   __asm__ __volatile__ ( "\n->" s " %0 " : : "i" (ul) )
> +
> +#define DEFINE( s, d )                                                 \
> +   DEFINE_UL( s, d )
> +
> +#define printf( x )                                                    \
> +   __asm__ __volatile__ ( "\n->" x )
> +
> +#else
> +
> +#define DEFINE_UL( s, ul )                                             \
> +   printf( "#define %s\t%lu\n", s, (unsigned long) (ul) );
>
>  #define DEFINE( s, d )                                                 \
>    printf( "#define %s\t0x%" PRIx64 "\n", s, (uint64_t) d );
>
> +#endif
> +
> +#define OFFSET( s, t, m )                                              \
> +   DEFINE_UL( s, offsetof( t, m ) )
> +
> +#define SIZEOF( s, t )                                                 \
> +   DEFINE_UL( s, sizeof(t) )
> +
>
>
>  int main( int argc, char **argv )
> --
> 1.7.8.4

This is a good idea. We'll want to be able to do this to build
builtin_compiler too.

Matt


More information about the mesa-dev mailing list