This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: x-files must die: rather too much at once


On 09-Mar-2001, Zack Weinberg <zackw@stanford.edu> wrote:
> +dnl Host endianness probe.
> +dnl Extremely clever hack thanks to Richard Henderson;
> +dnl works when cross compiling, doesn't rely on sys/param.h.
...
> +struct {
> +  char h[2];
> +  short two;
> +  char e[2];
> +} s = { { 'a', 'b' },
> +        ('C' << CHAR_BIT) | 'D',
> +        { 'e', '\0' } };
> +changequote([,])dnl
> +EOF
> +if AC_TRY_EVAL(ac_compile); then
> +  if grep abCDe conftest.o >/dev/null; then
> +    gcc_cv_c_host_endianness=big
> +  elif grep abDCe conftest.o >/dev/null; then
> +    gcc_cv_c_host_endianness=little
> +  else
> +    AC_MSG_ERROR(endianness not recognized)
> +  fi

There is a small but non-zero chance that the grep command may get false hits,
due to the presence of other data in the object file that happens to have
that pattern (e.g. timestamps, for example).

There are several things that can be done to reduce the chance of this problem
biting us.  The simplest one is to add a longer hopefully-unique prefix
at the start of the string that you grep for:

	struct {
	  /* Use a hopefully unique prefix to avoid false hits.  */
	  char prefix[sizeof("gcc_host_endianness_test_") - 1];
	  char h[2];
	  short two;
	  char e[2];
	} s = { "gcc_host_endianness_test_",
	        { 'a', 'b' },
		('C' << CHAR_BIT) | 'D',
		{ 'e', '\0' } };
	changequote([,])dnl
	EOF
	if AC_TRY_EVAL(ac_compile); then
	  if grep gcc_host_endianness_test_abCDe conftest.o >/dev/null; then
	    gcc_cv_c_host_endianness=big
	  elif grep gcc_host_endianness_test_abDCe conftest.o >/dev/null; then
	    gcc_cv_c_host_endianness=little
	  else
	    AC_MSG_ERROR(endianness not recognized)
	  fi

-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]