diff mbox

[kvm-unit-tests,1/6] libcflat: add PRI(dux)32 format types

Message ID 20170111162841.15569-2-alex.bennee@linaro.org (mailing list archive)
State New, archived
Headers show

Commit Message

Alex Bennée Jan. 11, 2017, 4:28 p.m. UTC
So we can have portable formatting of uint32_t types. However there is
a catch. Different compilers can use legally subtly different types
though so we need to probe the compiler defined intdef.h first.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
 Makefile       |  1 +
 configure      | 13 +++++++++++++
 lib/libcflat.h |  9 +++++++++
 3 files changed, 23 insertions(+)

Comments

Paolo Bonzini Jan. 12, 2017, 12:29 p.m. UTC | #1
On 11/01/2017 17:28, Alex Bennée wrote:
> So we can have portable formatting of uint32_t types. However there is
> a catch. Different compilers can use legally subtly different types
> though so we need to probe the compiler defined intdef.h first.

Interesting, what platform has long uint32_t?  I thought the issue was
whether 64-bit is long or "long long".

Paolo

> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
> ---
>  Makefile       |  1 +
>  configure      | 13 +++++++++++++
>  lib/libcflat.h |  9 +++++++++
>  3 files changed, 23 insertions(+)
> 
> diff --git a/Makefile b/Makefile
> index a32333b..9822d9a 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -55,6 +55,7 @@ CFLAGS += $(fomit_frame_pointer)
>  CFLAGS += $(fno_stack_protector)
>  CFLAGS += $(fno_stack_protector_all)
>  CFLAGS += $(wno_frame_address)
> +CFLAGS += $(if $(U32_LONG_FMT),-D__U32_LONG_FMT__,)
>  
>  CXXFLAGS += $(CFLAGS)
>  
> diff --git a/configure b/configure
> index 995c8fa..127868c 100755
> --- a/configure
> +++ b/configure
> @@ -109,6 +109,18 @@ if [ -f $testdir/run ]; then
>      ln -fs $testdir/run $testdir-run
>  fi
>  
> +# check if uint32_t needs a long format modifier
> +cat << EOF > lib_test.c
> +#include <inttypes.h>
> +EOF
> +
> +$cross_prefix$cc lib_test.c -E | grep "typedef" | grep "long" | grep "uint32_t" &> /dev/null
> +exit=$?
> +if [ $exit -eq 0 ]; then
> +    u32_long=true
> +fi
> +rm -f lib_test.c
> +
>  # check for dependent 32 bit libraries
>  if [ "$arch" != "arm" ]; then
>  cat << EOF > lib_test.c
> @@ -155,4 +167,5 @@ TEST_DIR=$testdir
>  FIRMWARE=$firmware
>  ENDIAN=$endian
>  PRETTY_PRINT_STACKS=$pretty_print_stacks
> +U32_LONG_FMT=$u32_long
>  EOF
> diff --git a/lib/libcflat.h b/lib/libcflat.h
> index 380395f..e80fc50 100644
> --- a/lib/libcflat.h
> +++ b/lib/libcflat.h
> @@ -58,12 +58,21 @@ typedef _Bool		bool;
>  #define true  1
>  
>  #if __SIZEOF_LONG__ == 8
> +#  define __PRI32_PREFIX
>  #  define __PRI64_PREFIX	"l"
>  #  define __PRIPTR_PREFIX	"l"
>  #else
> +#if defined(__U32_LONG_FMT__)
> +#  define __PRI32_PREFIX        "l"
> +#else
> +#  define __PRI32_PREFIX
> +#endif
>  #  define __PRI64_PREFIX	"ll"
>  #  define __PRIPTR_PREFIX
>  #endif
> +#define PRId32  __PRI32_PREFIX	"d"
> +#define PRIu32  __PRI32_PREFIX	"u"
> +#define PRIx32  __PRI32_PREFIX	"x"
>  #define PRId64  __PRI64_PREFIX	"d"
>  #define PRIu64  __PRI64_PREFIX	"u"
>  #define PRIx64  __PRI64_PREFIX	"x"
>
Alex Bennée Jan. 12, 2017, 12:39 p.m. UTC | #2
Paolo Bonzini <pbonzini@redhat.com> writes:

> On 11/01/2017 17:28, Alex Bennée wrote:
>> So we can have portable formatting of uint32_t types. However there is
>> a catch. Different compilers can use legally subtly different types
>> though so we need to probe the compiler defined intdef.h first.
>
> Interesting, what platform has long uint32_t?  I thought the issue was
> whether 64-bit is long or "long long".

I haven't run into that one. This came up with the arm-none-eabi-gcc on
my overdrive01 box. According to the toolchain guys there is no particular
reason a 32bit compiler can't use long for its natural word length.

The native compiler on Debian armhf doesn't do this but the
arm-none-eabi-gcc compilers on both 64bit and 32bit Debian need this.

>
> Paolo
>
>> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
>> ---
>>  Makefile       |  1 +
>>  configure      | 13 +++++++++++++
>>  lib/libcflat.h |  9 +++++++++
>>  3 files changed, 23 insertions(+)
>>
>> diff --git a/Makefile b/Makefile
>> index a32333b..9822d9a 100644
>> --- a/Makefile
>> +++ b/Makefile
>> @@ -55,6 +55,7 @@ CFLAGS += $(fomit_frame_pointer)
>>  CFLAGS += $(fno_stack_protector)
>>  CFLAGS += $(fno_stack_protector_all)
>>  CFLAGS += $(wno_frame_address)
>> +CFLAGS += $(if $(U32_LONG_FMT),-D__U32_LONG_FMT__,)
>>
>>  CXXFLAGS += $(CFLAGS)
>>
>> diff --git a/configure b/configure
>> index 995c8fa..127868c 100755
>> --- a/configure
>> +++ b/configure
>> @@ -109,6 +109,18 @@ if [ -f $testdir/run ]; then
>>      ln -fs $testdir/run $testdir-run
>>  fi
>>
>> +# check if uint32_t needs a long format modifier
>> +cat << EOF > lib_test.c
>> +#include <inttypes.h>
>> +EOF
>> +
>> +$cross_prefix$cc lib_test.c -E | grep "typedef" | grep "long" | grep "uint32_t" &> /dev/null
>> +exit=$?
>> +if [ $exit -eq 0 ]; then
>> +    u32_long=true
>> +fi
>> +rm -f lib_test.c
>> +
>>  # check for dependent 32 bit libraries
>>  if [ "$arch" != "arm" ]; then
>>  cat << EOF > lib_test.c
>> @@ -155,4 +167,5 @@ TEST_DIR=$testdir
>>  FIRMWARE=$firmware
>>  ENDIAN=$endian
>>  PRETTY_PRINT_STACKS=$pretty_print_stacks
>> +U32_LONG_FMT=$u32_long
>>  EOF
>> diff --git a/lib/libcflat.h b/lib/libcflat.h
>> index 380395f..e80fc50 100644
>> --- a/lib/libcflat.h
>> +++ b/lib/libcflat.h
>> @@ -58,12 +58,21 @@ typedef _Bool		bool;
>>  #define true  1
>>
>>  #if __SIZEOF_LONG__ == 8
>> +#  define __PRI32_PREFIX
>>  #  define __PRI64_PREFIX	"l"
>>  #  define __PRIPTR_PREFIX	"l"
>>  #else
>> +#if defined(__U32_LONG_FMT__)
>> +#  define __PRI32_PREFIX        "l"
>> +#else
>> +#  define __PRI32_PREFIX
>> +#endif
>>  #  define __PRI64_PREFIX	"ll"
>>  #  define __PRIPTR_PREFIX
>>  #endif
>> +#define PRId32  __PRI32_PREFIX	"d"
>> +#define PRIu32  __PRI32_PREFIX	"u"
>> +#define PRIx32  __PRI32_PREFIX	"x"
>>  #define PRId64  __PRI64_PREFIX	"d"
>>  #define PRIu64  __PRI64_PREFIX	"u"
>>  #define PRIx64  __PRI64_PREFIX	"x"
>>


--
Alex Bennée
Andrew Jones Jan. 12, 2017, 4:56 p.m. UTC | #3
On Thu, Jan 12, 2017 at 01:29:24PM +0100, Paolo Bonzini wrote:
> 
> 
> On 11/01/2017 17:28, Alex Bennée wrote:
> > So we can have portable formatting of uint32_t types. However there is
> > a catch. Different compilers can use legally subtly different types
> > though so we need to probe the compiler defined intdef.h first.
> 
> Interesting, what platform has long uint32_t?  I thought the issue was
> whether 64-bit is long or "long long".
> 
> Paolo
> 
> > Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
> > ---
> >  Makefile       |  1 +
> >  configure      | 13 +++++++++++++
> >  lib/libcflat.h |  9 +++++++++
> >  3 files changed, 23 insertions(+)
> > 
> > diff --git a/Makefile b/Makefile
> > index a32333b..9822d9a 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -55,6 +55,7 @@ CFLAGS += $(fomit_frame_pointer)
> >  CFLAGS += $(fno_stack_protector)
> >  CFLAGS += $(fno_stack_protector_all)
> >  CFLAGS += $(wno_frame_address)
> > +CFLAGS += $(if $(U32_LONG_FMT),-D__U32_LONG_FMT__,)
> >  
> >  CXXFLAGS += $(CFLAGS)
> >  
> > diff --git a/configure b/configure
> > index 995c8fa..127868c 100755
> > --- a/configure
> > +++ b/configure
> > @@ -109,6 +109,18 @@ if [ -f $testdir/run ]; then
> >      ln -fs $testdir/run $testdir-run
> >  fi
> >  
> > +# check if uint32_t needs a long format modifier
> > +cat << EOF > lib_test.c
> > +#include <inttypes.h>
> > +EOF
> > +
> > +$cross_prefix$cc lib_test.c -E | grep "typedef" | grep "long" | grep "uint32_t" &> /dev/null

This won't work with cross compilers that don't have inttypes.h in their
path (like mine). How about something like

 cat << EOF > lib_test.c
 __UINT32_TYPE__
 EOF
 u32_long="`aarch64-linux-gnu-gcc lib_test.c -E | awk '/unsigned/ && $2 == "long"'`"

Although I feel there should be a compiler macro way to do this without
a need for configure/makefile trickery at all...

Thanks,
drew


> > +exit=$?
> > +if [ $exit -eq 0 ]; then
> > +    u32_long=true
> > +fi
> > +rm -f lib_test.c
> > +
> >  # check for dependent 32 bit libraries
> >  if [ "$arch" != "arm" ]; then
> >  cat << EOF > lib_test.c
> > @@ -155,4 +167,5 @@ TEST_DIR=$testdir
> >  FIRMWARE=$firmware
> >  ENDIAN=$endian
> >  PRETTY_PRINT_STACKS=$pretty_print_stacks
> > +U32_LONG_FMT=$u32_long
> >  EOF
> > diff --git a/lib/libcflat.h b/lib/libcflat.h
> > index 380395f..e80fc50 100644
> > --- a/lib/libcflat.h
> > +++ b/lib/libcflat.h
> > @@ -58,12 +58,21 @@ typedef _Bool		bool;
> >  #define true  1
> >  
> >  #if __SIZEOF_LONG__ == 8
> > +#  define __PRI32_PREFIX
> >  #  define __PRI64_PREFIX	"l"
> >  #  define __PRIPTR_PREFIX	"l"
> >  #else
> > +#if defined(__U32_LONG_FMT__)
> > +#  define __PRI32_PREFIX        "l"
> > +#else
> > +#  define __PRI32_PREFIX
> > +#endif
> >  #  define __PRI64_PREFIX	"ll"
> >  #  define __PRIPTR_PREFIX
> >  #endif
> > +#define PRId32  __PRI32_PREFIX	"d"
> > +#define PRIu32  __PRI32_PREFIX	"u"
> > +#define PRIx32  __PRI32_PREFIX	"x"
> >  #define PRId64  __PRI64_PREFIX	"d"
> >  #define PRIu64  __PRI64_PREFIX	"u"
> >  #define PRIx64  __PRI64_PREFIX	"x"
> > 
> --
> To unsubscribe from this list: send the line "unsubscribe kvm" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
Alex Bennée Jan. 12, 2017, 5:18 p.m. UTC | #4
Andrew Jones <drjones@redhat.com> writes:

> On Thu, Jan 12, 2017 at 01:29:24PM +0100, Paolo Bonzini wrote:
>>
>>
>> On 11/01/2017 17:28, Alex Bennée wrote:
>> > So we can have portable formatting of uint32_t types. However there is
>> > a catch. Different compilers can use legally subtly different types
>> > though so we need to probe the compiler defined intdef.h first.
>>
>> Interesting, what platform has long uint32_t?  I thought the issue was
>> whether 64-bit is long or "long long".
>>
>> Paolo
>>
>> > Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
>> > ---
>> >  Makefile       |  1 +
>> >  configure      | 13 +++++++++++++
>> >  lib/libcflat.h |  9 +++++++++
>> >  3 files changed, 23 insertions(+)
>> >
>> > diff --git a/Makefile b/Makefile
>> > index a32333b..9822d9a 100644
>> > --- a/Makefile
>> > +++ b/Makefile
>> > @@ -55,6 +55,7 @@ CFLAGS += $(fomit_frame_pointer)
>> >  CFLAGS += $(fno_stack_protector)
>> >  CFLAGS += $(fno_stack_protector_all)
>> >  CFLAGS += $(wno_frame_address)
>> > +CFLAGS += $(if $(U32_LONG_FMT),-D__U32_LONG_FMT__,)
>> >
>> >  CXXFLAGS += $(CFLAGS)
>> >
>> > diff --git a/configure b/configure
>> > index 995c8fa..127868c 100755
>> > --- a/configure
>> > +++ b/configure
>> > @@ -109,6 +109,18 @@ if [ -f $testdir/run ]; then
>> >      ln -fs $testdir/run $testdir-run
>> >  fi
>> >
>> > +# check if uint32_t needs a long format modifier
>> > +cat << EOF > lib_test.c
>> > +#include <inttypes.h>
>> > +EOF
>> > +
>> > +$cross_prefix$cc lib_test.c -E | grep "typedef" | grep "long" | grep "uint32_t" &> /dev/null
>
> This won't work with cross compilers that don't have inttypes.h in their
> path (like mine). How about something like

Hmm good point, in my case inttypes.h came from the libnewlib-dev package.

>
>  cat << EOF > lib_test.c
>  __UINT32_TYPE__
>  EOF
>  u32_long="`aarch64-linux-gnu-gcc lib_test.c -E | awk '/unsigned/ && $2 == "long"'`"

Hmm it is not clear from the docs if this is a GCCism. If it is do we
care? Also the docs do say:

"Some of these macros may not be defined on particular systems if GCC does not provide a stdint.h header on those systems. "

> Although I feel there should be a compiler macro way to do this without
> a need for configure/makefile trickery at all...

I did ask our toolchain bods. They started going on about potential
solutions using _Generic but I fear that might be worse in this case!

>
> Thanks,
> drew
>
>
>> > +exit=$?
>> > +if [ $exit -eq 0 ]; then
>> > +    u32_long=true
>> > +fi
>> > +rm -f lib_test.c
>> > +
>> >  # check for dependent 32 bit libraries
>> >  if [ "$arch" != "arm" ]; then
>> >  cat << EOF > lib_test.c
>> > @@ -155,4 +167,5 @@ TEST_DIR=$testdir
>> >  FIRMWARE=$firmware
>> >  ENDIAN=$endian
>> >  PRETTY_PRINT_STACKS=$pretty_print_stacks
>> > +U32_LONG_FMT=$u32_long
>> >  EOF
>> > diff --git a/lib/libcflat.h b/lib/libcflat.h
>> > index 380395f..e80fc50 100644
>> > --- a/lib/libcflat.h
>> > +++ b/lib/libcflat.h
>> > @@ -58,12 +58,21 @@ typedef _Bool		bool;
>> >  #define true  1
>> >
>> >  #if __SIZEOF_LONG__ == 8
>> > +#  define __PRI32_PREFIX
>> >  #  define __PRI64_PREFIX	"l"
>> >  #  define __PRIPTR_PREFIX	"l"
>> >  #else
>> > +#if defined(__U32_LONG_FMT__)
>> > +#  define __PRI32_PREFIX        "l"
>> > +#else
>> > +#  define __PRI32_PREFIX
>> > +#endif
>> >  #  define __PRI64_PREFIX	"ll"
>> >  #  define __PRIPTR_PREFIX
>> >  #endif
>> > +#define PRId32  __PRI32_PREFIX	"d"
>> > +#define PRIu32  __PRI32_PREFIX	"u"
>> > +#define PRIx32  __PRI32_PREFIX	"x"
>> >  #define PRId64  __PRI64_PREFIX	"d"
>> >  #define PRIu64  __PRI64_PREFIX	"u"
>> >  #define PRIx64  __PRI64_PREFIX	"x"
>> >
>> --
>> To unsubscribe from this list: send the line "unsubscribe kvm" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at  http://vger.kernel.org/majordomo-info.html


--
Alex Bennée
Andrew Jones Jan. 12, 2017, 5:43 p.m. UTC | #5
On Thu, Jan 12, 2017 at 05:18:25PM +0000, Alex Bennée wrote:
> 
> Andrew Jones <drjones@redhat.com> writes:
> 
> > On Thu, Jan 12, 2017 at 01:29:24PM +0100, Paolo Bonzini wrote:
> >>
> >>
> >> On 11/01/2017 17:28, Alex Bennée wrote:
> >> > So we can have portable formatting of uint32_t types. However there is
> >> > a catch. Different compilers can use legally subtly different types
> >> > though so we need to probe the compiler defined intdef.h first.
> >>
> >> Interesting, what platform has long uint32_t?  I thought the issue was
> >> whether 64-bit is long or "long long".
> >>
> >> Paolo
> >>
> >> > Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
> >> > ---
> >> >  Makefile       |  1 +
> >> >  configure      | 13 +++++++++++++
> >> >  lib/libcflat.h |  9 +++++++++
> >> >  3 files changed, 23 insertions(+)
> >> >
> >> > diff --git a/Makefile b/Makefile
> >> > index a32333b..9822d9a 100644
> >> > --- a/Makefile
> >> > +++ b/Makefile
> >> > @@ -55,6 +55,7 @@ CFLAGS += $(fomit_frame_pointer)
> >> >  CFLAGS += $(fno_stack_protector)
> >> >  CFLAGS += $(fno_stack_protector_all)
> >> >  CFLAGS += $(wno_frame_address)
> >> > +CFLAGS += $(if $(U32_LONG_FMT),-D__U32_LONG_FMT__,)
> >> >
> >> >  CXXFLAGS += $(CFLAGS)
> >> >
> >> > diff --git a/configure b/configure
> >> > index 995c8fa..127868c 100755
> >> > --- a/configure
> >> > +++ b/configure
> >> > @@ -109,6 +109,18 @@ if [ -f $testdir/run ]; then
> >> >      ln -fs $testdir/run $testdir-run
> >> >  fi
> >> >
> >> > +# check if uint32_t needs a long format modifier
> >> > +cat << EOF > lib_test.c
> >> > +#include <inttypes.h>
> >> > +EOF
> >> > +
> >> > +$cross_prefix$cc lib_test.c -E | grep "typedef" | grep "long" | grep "uint32_t" &> /dev/null
> >
> > This won't work with cross compilers that don't have inttypes.h in their
> > path (like mine). How about something like
> 
> Hmm good point, in my case inttypes.h came from the libnewlib-dev package.
> 
> >
> >  cat << EOF > lib_test.c
> >  __UINT32_TYPE__
> >  EOF
> >  u32_long="`aarch64-linux-gnu-gcc lib_test.c -E | awk '/unsigned/ && $2 == "long"'`"
> 
> Hmm it is not clear from the docs if this is a GCCism. If it is do we
> care? Also the docs do say:

I think we're pretty bound to gcc already. I guess until somebody picks
up the effort to get clang, or whatever, compiling runnable
kvm-unit-tests, and commits to maintaining it, then I wouldn't worry too
much about it...

> 
> "Some of these macros may not be defined on particular systems if GCC does not provide a stdint.h header on those systems. "

We already expect stdint.h, we include it from libcflat.h.

> 
> > Although I feel there should be a compiler macro way to do this without
> > a need for configure/makefile trickery at all...
> 
> I did ask our toolchain bods. They started going on about potential
> solutions using _Generic but I fear that might be worse in this case!

Yikes :-)

drew

> 
> >
> > Thanks,
> > drew
> >
> >
> >> > +exit=$?
> >> > +if [ $exit -eq 0 ]; then
> >> > +    u32_long=true
> >> > +fi
> >> > +rm -f lib_test.c
> >> > +
> >> >  # check for dependent 32 bit libraries
> >> >  if [ "$arch" != "arm" ]; then
> >> >  cat << EOF > lib_test.c
> >> > @@ -155,4 +167,5 @@ TEST_DIR=$testdir
> >> >  FIRMWARE=$firmware
> >> >  ENDIAN=$endian
> >> >  PRETTY_PRINT_STACKS=$pretty_print_stacks
> >> > +U32_LONG_FMT=$u32_long
> >> >  EOF
> >> > diff --git a/lib/libcflat.h b/lib/libcflat.h
> >> > index 380395f..e80fc50 100644
> >> > --- a/lib/libcflat.h
> >> > +++ b/lib/libcflat.h
> >> > @@ -58,12 +58,21 @@ typedef _Bool		bool;
> >> >  #define true  1
> >> >
> >> >  #if __SIZEOF_LONG__ == 8
> >> > +#  define __PRI32_PREFIX
> >> >  #  define __PRI64_PREFIX	"l"
> >> >  #  define __PRIPTR_PREFIX	"l"
> >> >  #else
> >> > +#if defined(__U32_LONG_FMT__)
> >> > +#  define __PRI32_PREFIX        "l"
> >> > +#else
> >> > +#  define __PRI32_PREFIX
> >> > +#endif
> >> >  #  define __PRI64_PREFIX	"ll"
> >> >  #  define __PRIPTR_PREFIX
> >> >  #endif
> >> > +#define PRId32  __PRI32_PREFIX	"d"
> >> > +#define PRIu32  __PRI32_PREFIX	"u"
> >> > +#define PRIx32  __PRI32_PREFIX	"x"
> >> >  #define PRId64  __PRI64_PREFIX	"d"
> >> >  #define PRIu64  __PRI64_PREFIX	"u"
> >> >  #define PRIx64  __PRI64_PREFIX	"x"
> >> >
> >> --
> >> To unsubscribe from this list: send the line "unsubscribe kvm" in
> >> the body of a message to majordomo@vger.kernel.org
> >> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 
> 
> --
> Alex Bennée
Paolo Bonzini Jan. 12, 2017, 5:44 p.m. UTC | #6
On 12/01/2017 18:18, Alex Bennée wrote:
>> Although I feel there should be a compiler macro way to do this without
>> a need for configure/makefile trickery at all...
> 
> I did ask our toolchain bods. They started going on about potential
> solutions using _Generic but I fear that might be worse in this case!

I don't think _Generic can do string concatenation, can it?

inttypes.h is not part of the set of freestanding headers, only stdint.h
is.  Who is providing stdint.h in your case?

Paolo
Alex Bennée Jan. 12, 2017, 6:01 p.m. UTC | #7
Paolo Bonzini <pbonzini@redhat.com> writes:

> On 12/01/2017 18:18, Alex Bennée wrote:
>>> Although I feel there should be a compiler macro way to do this without
>>> a need for configure/makefile trickery at all...
>>
>> I did ask our toolchain bods. They started going on about potential
>> solutions using _Generic but I fear that might be worse in this case!
>
> I don't think _Generic can do string concatenation, can it?
>
> inttypes.h is not part of the set of freestanding headers, only stdint.h
> is.  Who is providing stdint.h in your case?

/usr/lib/gcc/arm-none-eabi/5.4.1/include/stdint.h

is part of the compiler package although it can just include the lib
stdint.h if it is there:

  #ifndef _GCC_WRAP_STDINT_H
  #if __STDC_HOSTED__
  # if defined __cplusplus && __cplusplus >= 201103L
  #  undef __STDC_LIMIT_MACROS
  #  define __STDC_LIMIT_MACROS
  #  undef __STDC_CONSTANT_MACROS
  #  define __STDC_CONSTANT_MACROS
  # endif
  # include_next <stdint.h>
  #else
  # include "stdint-gcc.h"
  #endif
  #define _GCC_WRAP_STDINT_H
  #endif

So using inttypes we would get:

>arm-none-eabi-gcc ./test.c -E | grep typedef | grep uint32
typedef long unsigned int __uint32_t;
typedef __uint32_t uint32_t ;


--
Alex Bennée
Andrew Jones Jan. 13, 2017, 6:03 p.m. UTC | #8
On Thu, Jan 12, 2017 at 05:56:29PM +0100, Andrew Jones wrote:
> On Thu, Jan 12, 2017 at 01:29:24PM +0100, Paolo Bonzini wrote:
> > 
> > 
> > On 11/01/2017 17:28, Alex Bennée wrote:
> > > So we can have portable formatting of uint32_t types. However there is
> > > a catch. Different compilers can use legally subtly different types
> > > though so we need to probe the compiler defined intdef.h first.
> > 
> > Interesting, what platform has long uint32_t?  I thought the issue was
> > whether 64-bit is long or "long long".
> > 
> > Paolo
> > 
> > > Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
> > > ---
> > >  Makefile       |  1 +
> > >  configure      | 13 +++++++++++++
> > >  lib/libcflat.h |  9 +++++++++
> > >  3 files changed, 23 insertions(+)
> > > 
> > > diff --git a/Makefile b/Makefile
> > > index a32333b..9822d9a 100644
> > > --- a/Makefile
> > > +++ b/Makefile
> > > @@ -55,6 +55,7 @@ CFLAGS += $(fomit_frame_pointer)
> > >  CFLAGS += $(fno_stack_protector)
> > >  CFLAGS += $(fno_stack_protector_all)
> > >  CFLAGS += $(wno_frame_address)
> > > +CFLAGS += $(if $(U32_LONG_FMT),-D__U32_LONG_FMT__,)
> > >  
> > >  CXXFLAGS += $(CFLAGS)
> > >  
> > > diff --git a/configure b/configure
> > > index 995c8fa..127868c 100755
> > > --- a/configure
> > > +++ b/configure
> > > @@ -109,6 +109,18 @@ if [ -f $testdir/run ]; then
> > >      ln -fs $testdir/run $testdir-run
> > >  fi
> > >  
> > > +# check if uint32_t needs a long format modifier
> > > +cat << EOF > lib_test.c
> > > +#include <inttypes.h>
> > > +EOF
> > > +
> > > +$cross_prefix$cc lib_test.c -E | grep "typedef" | grep "long" | grep "uint32_t" &> /dev/null
> 
> This won't work with cross compilers that don't have inttypes.h in their
> path (like mine). How about something like

Paolo,

I see you merged the above. As I stated in this mail, that doesn't work
for my environment, neither arm nor powerpc cross-compile for me now...

Please replace with something like I proposed below. We can probably
replace the awk with a grep though.

Thanks,
drew


> 
>  cat << EOF > lib_test.c
>  __UINT32_TYPE__
>  EOF
>  u32_long="`aarch64-linux-gnu-gcc lib_test.c -E | awk '/unsigned/ && $2 == "long"'`"
> 
> Although I feel there should be a compiler macro way to do this without
> a need for configure/makefile trickery at all...
> 
> Thanks,
> drew
> 
> 
> > > +exit=$?
> > > +if [ $exit -eq 0 ]; then
> > > +    u32_long=true
> > > +fi
> > > +rm -f lib_test.c
> > > +
> > >  # check for dependent 32 bit libraries
> > >  if [ "$arch" != "arm" ]; then
> > >  cat << EOF > lib_test.c
> > > @@ -155,4 +167,5 @@ TEST_DIR=$testdir
> > >  FIRMWARE=$firmware
> > >  ENDIAN=$endian
> > >  PRETTY_PRINT_STACKS=$pretty_print_stacks
> > > +U32_LONG_FMT=$u32_long
> > >  EOF
> > > diff --git a/lib/libcflat.h b/lib/libcflat.h
> > > index 380395f..e80fc50 100644
> > > --- a/lib/libcflat.h
> > > +++ b/lib/libcflat.h
> > > @@ -58,12 +58,21 @@ typedef _Bool		bool;
> > >  #define true  1
> > >  
> > >  #if __SIZEOF_LONG__ == 8
> > > +#  define __PRI32_PREFIX
> > >  #  define __PRI64_PREFIX	"l"
> > >  #  define __PRIPTR_PREFIX	"l"
> > >  #else
> > > +#if defined(__U32_LONG_FMT__)
> > > +#  define __PRI32_PREFIX        "l"
> > > +#else
> > > +#  define __PRI32_PREFIX
> > > +#endif
> > >  #  define __PRI64_PREFIX	"ll"
> > >  #  define __PRIPTR_PREFIX
> > >  #endif
> > > +#define PRId32  __PRI32_PREFIX	"d"
> > > +#define PRIu32  __PRI32_PREFIX	"u"
> > > +#define PRIx32  __PRI32_PREFIX	"x"
> > >  #define PRId64  __PRI64_PREFIX	"d"
> > >  #define PRIu64  __PRI64_PREFIX	"u"
> > >  #define PRIx64  __PRI64_PREFIX	"x"
> > > 
> > --
> > To unsubscribe from this list: send the line "unsubscribe kvm" in
> > the body of a message to majordomo@vger.kernel.org
> > More majordomo info at  http://vger.kernel.org/majordomo-info.html
> --
> To unsubscribe from this list: send the line "unsubscribe kvm" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/Makefile b/Makefile
index a32333b..9822d9a 100644
--- a/Makefile
+++ b/Makefile
@@ -55,6 +55,7 @@  CFLAGS += $(fomit_frame_pointer)
 CFLAGS += $(fno_stack_protector)
 CFLAGS += $(fno_stack_protector_all)
 CFLAGS += $(wno_frame_address)
+CFLAGS += $(if $(U32_LONG_FMT),-D__U32_LONG_FMT__,)
 
 CXXFLAGS += $(CFLAGS)
 
diff --git a/configure b/configure
index 995c8fa..127868c 100755
--- a/configure
+++ b/configure
@@ -109,6 +109,18 @@  if [ -f $testdir/run ]; then
     ln -fs $testdir/run $testdir-run
 fi
 
+# check if uint32_t needs a long format modifier
+cat << EOF > lib_test.c
+#include <inttypes.h>
+EOF
+
+$cross_prefix$cc lib_test.c -E | grep "typedef" | grep "long" | grep "uint32_t" &> /dev/null
+exit=$?
+if [ $exit -eq 0 ]; then
+    u32_long=true
+fi
+rm -f lib_test.c
+
 # check for dependent 32 bit libraries
 if [ "$arch" != "arm" ]; then
 cat << EOF > lib_test.c
@@ -155,4 +167,5 @@  TEST_DIR=$testdir
 FIRMWARE=$firmware
 ENDIAN=$endian
 PRETTY_PRINT_STACKS=$pretty_print_stacks
+U32_LONG_FMT=$u32_long
 EOF
diff --git a/lib/libcflat.h b/lib/libcflat.h
index 380395f..e80fc50 100644
--- a/lib/libcflat.h
+++ b/lib/libcflat.h
@@ -58,12 +58,21 @@  typedef _Bool		bool;
 #define true  1
 
 #if __SIZEOF_LONG__ == 8
+#  define __PRI32_PREFIX
 #  define __PRI64_PREFIX	"l"
 #  define __PRIPTR_PREFIX	"l"
 #else
+#if defined(__U32_LONG_FMT__)
+#  define __PRI32_PREFIX        "l"
+#else
+#  define __PRI32_PREFIX
+#endif
 #  define __PRI64_PREFIX	"ll"
 #  define __PRIPTR_PREFIX
 #endif
+#define PRId32  __PRI32_PREFIX	"d"
+#define PRIu32  __PRI32_PREFIX	"u"
+#define PRIx32  __PRI32_PREFIX	"x"
 #define PRId64  __PRI64_PREFIX	"d"
 #define PRIu64  __PRI64_PREFIX	"u"
 #define PRIx64  __PRI64_PREFIX	"x"