diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b8c1aaa6f..ebf68d3bc9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -186,6 +186,14 @@ if(MSVC) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -MP -W4 ${MSVC_DISABLED_WARNINGS_STR}") endif() +include(TestBigEndian) +TEST_BIG_ENDIAN(IS_BIG_ENDIAN) +if(IS_BIG_ENDIAN) + add_definitions(-DHAVE_BIG_ENDIAN) +else() + add_definitions(-DHAVE_LITTLE_ENDIAN) +endif() + check_function_exists(asprintf HAVE_ASPRINTF) if(HAVE_ASPRINTF) add_definitions(-DHAVE_ASPRINTF) diff --git a/include/compat/endian.h b/include/compat/endian.h index d0dcfe3251..5114599f02 100644 --- a/include/compat/endian.h +++ b/include/compat/endian.h @@ -13,13 +13,21 @@ #define PDP_ENDIAN 3412 /* - * Use GCC and Visual Studio compiler defines to determine endian. + * Use compiler and build system defines to determine endianness. */ +#if defined(__BYTE_ORDER__) #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ #define BYTE_ORDER LITTLE_ENDIAN #else #define BYTE_ORDER BIG_ENDIAN #endif +#elif defined(HAVE_LITTLE_ENDIAN) +#define BYTE_ORDER LITTLE_ENDIAN +#elif defined(HAVE_BIG_ENDIAN) +#define BYTE_ORDER BIG_ENDIAN +#else +#error "Could not detect endianness." +#endif #elif defined(HAVE_ENDIAN_H) #include_next