| 1 |
dnl Written by Zack Weinberg <zackw at panix.com> in 2018. |
| 2 |
dnl To the extent possible under law, Zack Weinberg has waived all |
| 3 |
dnl copyright and related or neighboring rights to this work. |
| 4 |
dnl |
| 5 |
dnl See https://creativecommons.org/publicdomain/zero/1.0/ for further |
| 6 |
dnl details. |
| 7 |
dnl |
| 8 |
dnl This macro is like AC_C_BIGENDIAN, but it doesn't try to determine |
| 9 |
dnl a final value at configure time. Instead, it probes for a set of |
| 10 |
dnl headers and compile-time macros that may or may not be available, |
| 11 |
dnl and uses them to guide preprocessor logic that makes the final |
| 12 |
dnl determination. This works better with MacOS "universal binaries", |
| 13 |
dnl which may involve compiling the program twice under opposite |
| 14 |
dnl endiannesses; no fixed byte-order macro is correct in that case, |
| 15 |
dnl but the compiler's built-ins will be. This approach is also |
| 16 |
dnl friendlier to cross-compilation. |
| 17 |
dnl |
| 18 |
dnl This is the preprocessor logic that should be put in an appropriate |
| 19 |
dnl location, after including config.h: |
| 20 |
dnl |
| 21 |
dnl #include <limits.h> |
| 22 |
dnl #ifdef HAVE_ENDIAN_H |
| 23 |
dnl #include <endian.h> |
| 24 |
dnl #endif |
| 25 |
dnl #ifdef HAVE_SYS_ENDIAN_H |
| 26 |
dnl #include <sys/endian.h> |
| 27 |
dnl #endif |
| 28 |
dnl #ifdef HAVE_SYS_PARAM_H |
| 29 |
dnl #include <sys/param.h> |
| 30 |
dnl #endif |
| 31 |
dnl |
| 32 |
dnl #if ENDIANNESS_IS_BIG |
| 33 |
dnl # define ENDIAN_BIG |
| 34 |
dnl #elif ENDIANNESS_IS_LITTLE |
| 35 |
dnl # define ENDIAN_LITTLE |
| 36 |
dnl #elif ENDIANNESS_IS_PDP |
| 37 |
dnl # define ENDIAN_PDP |
| 38 |
dnl #else |
| 39 |
dnl # error "Unable to determine byte order" |
| 40 |
dnl #endif |
| 41 |
dnl ------------------------------------------------ |
| 42 |
|
| 43 |
dnl There's no good way to implement this macro as a _shell_ loop, but we |
| 44 |
dnl can reasonably implement it as an _m4_ loop that expands to a sequence |
| 45 |
dnl of conditionals. Actually two sequences of conditionals, one inside |
| 46 |
dnl AC_CACHE_CHECK and one outside. |
| 47 |
|
| 48 |
m4_define([zw_C_ENDIANNESS_options], [ |
| 49 |
[ [BYTE_ORDER and xxx_ENDIAN], |
| 50 |
[defined BYTE_ORDER && defined BIG_ENDIAN && defined LITTLE_ENDIAN && BIG_ENDIAN != LITTLE_ENDIAN], |
| 51 |
[BYTE_ORDER == BIG_ENDIAN], |
| 52 |
[BYTE_ORDER == LITTLE_ENDIAN], |
| 53 |
[BYTE_ORDER == PDP_ENDIAN], |
| 54 |
], |
| 55 |
[ [__BYTE_ORDER and __xxx_ENDIAN], |
| 56 |
[defined __BYTE_ORDER && defined __BIG_ENDIAN && defined __LITTLE_ENDIAN && __BIG_ENDIAN != __LITTLE_ENDIAN], |
| 57 |
[__BYTE_ORDER == __BIG_ENDIAN], |
| 58 |
[__BYTE_ORDER == __LITTLE_ENDIAN], |
| 59 |
[__BYTE_ORDER == __PDP_ENDIAN], |
| 60 |
], |
| 61 |
[ [__BYTE_ORDER__ and __xxx_ENDIAN__], |
| 62 |
[defined __BYTE_ORDER__ && defined __BIG_ENDIAN__ && defined __LITTLE_ENDIAN__ && __BIG_ENDIAN__ != __LITTLE_ENDIAN__], |
| 63 |
[__BYTE_ORDER__ == __BIG_ENDIAN__], |
| 64 |
[__BYTE_ORDER__ == __LITTLE_ENDIAN__], |
| 65 |
[__BYTE_ORDER__ == __PDP_ENDIAN__], |
| 66 |
], |
| 67 |
[ [__BYTE_ORDER__ and __ORDER_xxx_ENDIAN__], |
| 68 |
[defined __BYTE_ORDER__ && defined __ORDER_BIG_ENDIAN__ && defined __ORDER_LITTLE_ENDIAN__ && __ORDER_BIG_ENDIAN__ != __ORDER_LITTLE_ENDIAN__], |
| 69 |
[__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__], |
| 70 |
[__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__], |
| 71 |
[__BYTE_ORDER__ == __ORDER_PDP_ENDIAN__], |
| 72 |
], |
| 73 |
[ [_BIG_ENDIAN and _LITTLE_ENDIAN], |
| 74 |
[(defined _BIG_ENDIAN) != (defined _LITTLE_ENDIAN)], |
| 75 |
[defined _BIG_ENDIAN], [defined _LITTLE_ENDIAN], [0], |
| 76 |
], |
| 77 |
[ [__BIG_ENDIAN__ and __LITTLE_ENDIAN__], |
| 78 |
[(defined __BIG_ENDIAN__) != (defined __LITTLE_ENDIAN__)], |
| 79 |
[defined __BIG_ENDIAN__], [defined __LITTLE_ENDIAN__], [0], |
| 80 |
], |
| 81 |
[ [__ARMEB__ and __ARMEL__], |
| 82 |
[(defined __ARMEB__) != (defined __ARMEL__)], |
| 83 |
[defined __ARMEB__], [defined __ARMEL__], [0], |
| 84 |
], |
| 85 |
[ [__THUMBEB__ and __THUMBEL__], |
| 86 |
[(defined __THUMBEB__) != (defined __THUMBEL__)], |
| 87 |
[defined __THUMBEB__], [defined __THUMBEL__], [0], |
| 88 |
], |
| 89 |
[ [__AARCH64EB__ and __AARCH64EL__], |
| 90 |
[(defined __AARCH64EB__) != (defined __AARCH64EL__)], |
| 91 |
[defined __AARCH64EB__], [defined __AARCH64EL__], [0], |
| 92 |
], |
| 93 |
[ [__MIPSEB__ and __MIPSEL__], |
| 94 |
[(defined __MIPSEB__) != (defined __MIPSEL__)], |
| 95 |
[defined __MIPSEB__], [defined __MIPSEL__], [0], |
| 96 |
], |
| 97 |
[ [__MIPSEB and __MIPSEL], |
| 98 |
[(defined __MIPSEB) != (defined __MIPSEL)], |
| 99 |
[defined __MIPSEB], [defined __MIPSEL], [0], |
| 100 |
], |
| 101 |
[ [_MIPSEB and _MIPSEL], |
| 102 |
[(defined _MIPSEB) != (defined _MIPSEL)], |
| 103 |
[defined _MIPSEB], [defined _MIPSEL], [0], |
| 104 |
] |
| 105 |
]) |
| 106 |
|
| 107 |
m4_define([zw_C_ENDIANNESS_test_one], [ |
| 108 |
if test "$[]ac_cv_c_byte_order_macros" = unknown; then |
| 109 |
AC_PREPROC_IFELSE([AC_LANG_PROGRAM([[ |
| 110 |
#include <limits.h> |
| 111 |
#ifdef HAVE_ENDIAN_H |
| 112 |
#include <endian.h> |
| 113 |
#endif |
| 114 |
#ifdef HAVE_SYS_ENDIAN_H |
| 115 |
#include <sys/endian.h> |
| 116 |
#endif |
| 117 |
#ifdef HAVE_SYS_PARAM_H |
| 118 |
#include <sys/param.h> |
| 119 |
#endif |
| 120 |
#if !($2) |
| 121 |
#error fail |
| 122 |
#endif |
| 123 |
]])], |
| 124 |
[ac_cv_c_byte_order_macros="$1"]) |
| 125 |
fi]) |
| 126 |
|
| 127 |
m4_define([zw_C_ENDIANNESS_case_one], |
| 128 |
[["$1"], [ |
| 129 |
zw_C_ENDIANNESS_IS_BIG="($3)" |
| 130 |
zw_C_ENDIANNESS_IS_LIT="($4)" |
| 131 |
zw_C_ENDIANNESS_IS_PDP="($5)"]]) |
| 132 |
|
| 133 |
AC_DEFUN([zw_C_ENDIANNESS], [ |
| 134 |
AC_REQUIRE([AC_PROG_CC]) |
| 135 |
AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS]) |
| 136 |
AC_CHECK_HEADERS_ONCE([endian.h sys/endian.h sys/param.h]) |
| 137 |
AC_CACHE_CHECK([for byte order macros], |
| 138 |
[ac_cv_c_byte_order_macros], [ |
| 139 |
ac_cv_c_byte_order_macros=unknown |
| 140 |
m4_map([zw_C_ENDIANNESS_test_one], [zw_C_ENDIANNESS_options])]) |
| 141 |
|
| 142 |
AS_CASE([$ac_cv_c_byte_order_macros], |
| 143 |
m4_map_sep([zw_C_ENDIANNESS_case_one], [,], [zw_C_ENDIANNESS_options]), |
| 144 |
[AC_MSG_ERROR([unable to determine byte order at compile time])]) |
| 145 |
|
| 146 |
AC_DEFINE_UNQUOTED([ENDIANNESS_IS_BIG], [$zw_C_ENDIANNESS_IS_BIG], |
| 147 |
[Define as an @%:@if expression that is true when compiling for a big-endian CPU.]) |
| 148 |
AC_DEFINE_UNQUOTED([ENDIANNESS_IS_LITTLE], [$zw_C_ENDIANNESS_IS_LIT], |
| 149 |
[Define as an @%:@if expression that is true when compiling for a little-endian CPU.]) |
| 150 |
AC_DEFINE_UNQUOTED([ENDIANNESS_IS_PDP], [$zw_C_ENDIANNESS_IS_PDP], |
| 151 |
[Define as an @%:@if expression that is true when compiling for a PDP-endian CPU.]) |
| 152 |
]) |