Fix previous commit.
[pwmd.git] / m4 / ax_c_bigendian_cross.m4
blobe6a1840d200836782e4905004d85dc9c859c76ad
1 # ===========================================================================
2 #   http://www.gnu.org/software/autoconf-archive/ax_c_bigendian_cross.html
3 # ===========================================================================
5 # SYNOPSIS
7 #   AX_C_BIGENDIAN_CROSS
9 # DESCRIPTION
11 #   Check endianess even when crosscompiling (partially based on the
12 #   original AC_C_BIGENDIAN).
14 #   The implementation will create a binary, but instead of running the
15 #   binary it will be grep'ed for some symbols that differ for different
16 #   endianess of the binary.
18 #   NOTE: The upcoming autoconf 2.53 does include the idea of this macro,
19 #   what makes it superfluous by then.
21 # LICENSE
23 #   Copyright (c) 2008 Guido U. Draheim <guidod@gmx.de>
25 #   This program is free software; you can redistribute it and/or modify it
26 #   under the terms of the GNU General Public License as published by the
27 #   Free Software Foundation; either version 3 of the License, or (at your
28 #   option) any later version.
30 #   This program is distributed in the hope that it will be useful, but
31 #   WITHOUT ANY WARRANTY; without even the implied warranty of
32 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
33 #   Public License for more details.
35 #   You should have received a copy of the GNU General Public License along
36 #   with this program. If not, see <http://www.gnu.org/licenses/>.
38 #   As a special exception, the respective Autoconf Macro's copyright owner
39 #   gives unlimited permission to copy, distribute and modify the configure
40 #   scripts that are the output of Autoconf when processing the Macro. You
41 #   need not follow the terms of the GNU General Public License when using
42 #   or distributing such scripts, even though portions of the text of the
43 #   Macro appear in them. The GNU General Public License (GPL) does govern
44 #   all other use of the material that constitutes the Autoconf Macro.
46 #   This special exception to the GPL applies to versions of the Autoconf
47 #   Macro released by the Autoconf Archive. When you make and distribute a
48 #   modified version of the Autoconf Macro, you may extend this special
49 #   exception to the GPL to apply to your modified version as well.
51 #serial 5
53 AU_ALIAS([AC_C_BIGENDIAN_CROSS], [AX_C_BIGENDIAN_CROSS])
54 AC_DEFUN([AX_C_BIGENDIAN_CROSS],
55 [AC_CACHE_CHECK(whether byte ordering is bigendian, ac_cv_c_bigendian,
56 [ac_cv_c_bigendian=unknown
57 # See if sys/param.h defines the BYTE_ORDER macro.
58 AC_TRY_COMPILE([#include <sys/types.h>
59 #include <sys/param.h>], [
60 #if !BYTE_ORDER || !BIG_ENDIAN || !LITTLE_ENDIAN
61  bogus endian macros
62 #endif], [# It does; now see whether it defined to BIG_ENDIAN or not.
63 AC_TRY_COMPILE([#include <sys/types.h>
64 #include <sys/param.h>], [
65 #if BYTE_ORDER != BIG_ENDIAN
66  not big endian
67 #endif], ac_cv_c_bigendian=yes, ac_cv_c_bigendian=no)])
68 if test $ac_cv_c_bigendian = unknown; then
69 AC_TRY_RUN([main () {
70   /* Are we little or big endian?  From Harbison&Steele.  */
71   union
72   {
73     long l;
74     char c[sizeof (long)];
75   } u;
76   u.l = 1;
77   exit (u.c[sizeof (long) - 1] == 1);
78 }], ac_cv_c_bigendian=no, ac_cv_c_bigendian=yes,
79 [ echo $ac_n "cross-compiling... " 2>&AC_FD_MSG ])
80 fi])
81 if test $ac_cv_c_bigendian = unknown; then
82 AC_MSG_CHECKING(to probe for byte ordering)
84 cat >conftest.c <<EOF
85 short ascii_mm[] = { 0x4249, 0x4765, 0x6E44, 0x6961, 0x6E53, 0x7953, 0 };
86 short ascii_ii[] = { 0x694C, 0x5454, 0x656C, 0x6E45, 0x6944, 0x6E61, 0 };
87 void _ascii() { char* s = (char*) ascii_mm; s = (char*) ascii_ii; }
88 short ebcdic_ii[] = { 0x89D3, 0xE3E3, 0x8593, 0x95C5, 0x89C4, 0x9581, 0 };
89 short ebcdic_mm[] = { 0xC2C9, 0xC785, 0x95C4, 0x8981, 0x95E2, 0xA8E2, 0 };
90 void _ebcdic() { char* s = (char*) ebcdic_mm; s = (char*) ebcdic_ii; }
91 int main() { _ascii (); _ebcdic (); return 0; }
92 EOF
93 ] if test -f conftest.c ; then
94      if ${CC-cc} -c conftest.c -o conftest.o && test -f conftest.o ; then
95         if test `grep -l BIGenDianSyS conftest.o` ; then
96            echo $ac_n ' big endian probe OK, ' 1>&AC_FD_MSG
97            ac_cv_c_bigendian=yes
98         fi
99         if test `grep -l LiTTleEnDian conftest.o` ; then
100            echo $ac_n ' little endian probe OK, ' 1>&AC_FD_MSG
101            if test $ac_cv_c_bigendian = yes ; then
102             ac_cv_c_bigendian=unknown;
103            else
104             ac_cv_c_bigendian=no
105            fi
106         fi
107         echo $ac_n 'guessing bigendian ...  ' >&AC_FD_MSG
108      fi
109   fi
110 AC_MSG_RESULT($ac_cv_c_bigendian)
112 if test $ac_cv_c_bigendian = yes; then
113   AC_DEFINE(WORDS_BIGENDIAN, 1, [whether byteorder is bigendian])
114   BYTEORDER=4321
115 else
116   BYTEORDER=1234
118 AC_DEFINE_UNQUOTED(BYTEORDER, $BYTEORDER, [1234 = LIL_ENDIAN, 4321 = BIGENDIAN])
119 if test $ac_cv_c_bigendian = unknown; then
120   AC_MSG_ERROR(unknown endianess - sorry, please pre-set ac_cv_c_bigendian)