diff --git a/CMakeLists.txt b/CMakeLists.txt index 174ad01456..d133a12467 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,6 +96,15 @@ endif() check_symbol_exists(nanosleep time.h DMLC_NANOSLEEP_PRESENT) +# Check endianness +include(TestBigEndian) +test_big_endian(BIG_ENDIAN) +if(BIG_ENDIAN) + set(DMLC_CMAKE_LITTLE_ENDIAN 0) +else() + set(DMLC_CMAKE_LITTLE_ENDIAN 1) +endif() + message(STATUS "${CMAKE_LOCAL}/build_config.h.in -> ${INCLUDE_DMLC_DIR}/build_config.h") configure_file("${CMAKE_LOCAL}/build_config.h.in" "${INCLUDE_DMLC_DIR}/build_config.h") diff --git a/cmake/build_config.h.in b/cmake/build_config.h.in index 6c03ba3680..37db8877a6 100644 --- a/cmake/build_config.h.in +++ b/cmake/build_config.h.in @@ -25,4 +25,6 @@ #cmakedefine DMLC_NANOSLEEP_PRESENT +#cmakedefine DMLC_CMAKE_LITTLE_ENDIAN ${DMLC_CMAKE_LITTLE_ENDIAN} + #endif // DMLC_BUILD_CONFIG_H_ diff --git a/include/dmlc/endian.h b/include/dmlc/endian.h index e7deeaa490..bb1a503658 100644 --- a/include/dmlc/endian.h +++ b/include/dmlc/endian.h @@ -8,11 +8,21 @@ #include "./base.h" -#if defined(__APPLE__) || defined(_WIN32) -#define DMLC_LITTLE_ENDIAN 1 +#ifdef DMLC_CMAKE_LITTLE_ENDIAN + // If compiled with CMake, use CMake's endian detection logic + #define DMLC_LITTLE_ENDIAN DMLC_CMAKE_LITTLE_ENDIAN #else -#include -#define DMLC_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN) + #if defined(__APPLE__) || defined(_WIN32) + #define DMLC_LITTLE_ENDIAN 1 + #elif defined(__GLIBC__) + #include + #define DMLC_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN) + #elif defined(__FreeBSD__) + #include + #define DMLC_LITTLE_ENDIAN (_BYTE_ORDER == _LITTLE_ENDIAN) + #else + #error "Unable to determine endianness of your machine; use CMake to compile" + #endif #endif /*! \brief whether serialize using little endian */