Add m4/endian.m4 and use it in configure.ac.
[platform/upstream/flac.git] / m4 / endian.m4
1 dnl Copyright (C) 2012  Xiph.org Foundation
2 dnl
3 dnl Redistribution and use in source and binary forms, with or without
4 dnl modification, are permitted provided that the following conditions
5 dnl are met:
6 dnl
7 dnl - Redistributions of source code must retain the above copyright
8 dnl notice, this list of conditions and the following disclaimer.
9 dnl
10 dnl - Redistributions in binary form must reproduce the above copyright
11 dnl notice, this list of conditions and the following disclaimer in the
12 dnl documentation and/or other materials provided with the distribution.
13 dnl
14 dnl - Neither the name of the Xiph.org Foundation nor the names of its
15 dnl contributors may be used to endorse or promote products derived from
16 dnl this software without specific prior written permission.
17 dnl
18 dnl THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 dnl ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 dnl LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 dnl A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
22 dnl CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 dnl EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 dnl PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 dnl PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 dnl LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 dnl NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 dnl SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30
31 dnl @synopsis XIPH_C_FIND_ENDIAN
32 dnl
33 dnl Determine endian-ness of target processor.
34 dnl @version 1.1        Mar 03 2002
35 dnl @author Erik de Castro Lopo <erikd@mega-nerd.com>
36 dnl
37 dnl Majority written from scratch to replace the standard autoconf macro
38 dnl AC_C_BIGENDIAN. Only part remaining from the original is the invocation
39 dnl of the AC_TRY_RUN macro.
40 dnl
41 dnl Find endian-ness in the following way:
42 dnl    1) Look in <endian.h>.
43 dnl    2) If 1) fails, look in <sys/types.h> and <sys/param.h>.
44 dnl    3) If 1) and 2) fails and not cross compiling run a test program.
45 dnl    4) If 1) and 2) fails and cross compiling then guess based on target.
46
47 AC_DEFUN([XIPH_C_FIND_ENDIAN],
48 [AC_CACHE_CHECK(processor byte ordering,
49         ac_cv_c_byte_order,
50
51 # Initialize to unknown
52 ac_cv_c_byte_order=unknown
53
54 if test x$ac_cv_header_endian_h = xyes ; then
55
56         # First try <endian.h> which should set BYTE_ORDER.
57
58         [AC_TRY_LINK([
59                 #include <endian.h>
60                 #if BYTE_ORDER != LITTLE_ENDIAN
61                         not big endian
62                 #endif
63                 ], return 0 ;,
64                         ac_cv_c_byte_order=little
65                 )]
66
67         [AC_TRY_LINK([
68                 #include <endian.h>
69                 #if BYTE_ORDER != BIG_ENDIAN
70                         not big endian
71                 #endif
72                 ], return 0 ;,
73                         ac_cv_c_byte_order=big
74                 )]
75
76         fi
77
78 if test $ac_cv_c_byte_order = unknown ; then
79
80         [AC_TRY_LINK([
81                 #include <sys/types.h>
82                 #include <sys/param.h>
83                 #if !BYTE_ORDER || !BIG_ENDIAN || !LITTLE_ENDIAN
84                         bogus endian macros
85                 #endif
86                 ], return 0 ;,
87
88                 [AC_TRY_LINK([
89                         #include <sys/types.h>
90                         #include <sys/param.h>
91                         #if BYTE_ORDER != LITTLE_ENDIAN
92                                 not big endian
93                         #endif
94                         ], return 0 ;,
95                                 ac_cv_c_byte_order=little
96                         )]
97
98                 [AC_TRY_LINK([
99                         #include <sys/types.h>
100                         #include <sys/param.h>
101                         #if BYTE_ORDER != LITTLE_ENDIAN
102                                 not big endian
103                         #endif
104                         ], return 0 ;,
105                                 ac_cv_c_byte_order=little
106                         )]
107
108                 )]
109
110         fi
111
112 if test $ac_cv_c_byte_order = unknown ; then
113         if test $cross_compiling = yes ; then
114                 # This is the last resort. Try to guess the target processor endian-ness
115                 # by looking at the target CPU type.
116                 [
117                 case "$target_cpu" in
118                         alpha* | i?86* | mipsel* | ia64*)
119                                 ac_cv_c_byte_order=little
120                                 ;;
121
122                         m68* | mips* | powerpc* | hppa* | sparc*)
123                                 ac_cv_c_byte_order=big
124                                 ;;
125
126                         esac
127                 ]
128         else
129                 AC_TRY_RUN(
130                 [[
131                 int main (void)
132                 {       /* Are we little or big endian?  From Harbison&Steele.  */
133                         union
134                         {       long l ;
135                                 char c [sizeof (long)] ;
136                         } u ;
137                         u.l = 1 ;
138                         return (u.c [sizeof (long) - 1] == 1);
139                         }
140                         ]], , ac_cv_c_byte_order=big,
141                         )
142
143                 AC_TRY_RUN(
144                 [[int main (void)
145                 {       /* Are we little or big endian?  From Harbison&Steele.  */
146                         union
147                         {       long l ;
148                                 char c [sizeof (long)] ;
149                         } u ;
150                         u.l = 1 ;
151                         return (u.c [0] == 1);
152                         }]], , ac_cv_c_byte_order=little,
153                         )
154                 fi
155         fi
156
157 )
158
159 if test $ac_cv_c_byte_order = big ; then
160         ac_cv_c_big_endian=1
161         ac_cv_c_little_endian=0
162 elif test $ac_cv_c_byte_order = little ; then
163         ac_cv_c_big_endian=0
164         ac_cv_c_little_endian=1
165 else
166         ac_cv_c_big_endian=0
167         ac_cv_c_little_endian=0
168
169         AC_MSG_WARN([[*****************************************************************]])
170         AC_MSG_WARN([[*** Not able to determine endian-ness of target processor.       ]])
171         AC_MSG_WARN([[*** The constants CPU_IS_BIG_ENDIAN and CPU_IS_LITTLE_ENDIAN in  ]])
172         AC_MSG_WARN([[*** config.h may need to be hand editied.                        ]])
173         AC_MSG_WARN([[*****************************************************************]])
174         fi
175
176 ]
177 )# XIPH_C_FIND_ENDIAN