Skip to content

Commit

Permalink
FreeBSD support in endian.h (dmlc#476)
Browse files Browse the repository at this point in the history
* FreeBSD support in endian.h

* Better endian detection logic

* Fix typo

* Add attribution to Boost Library for detection logic

* Fix typo

* Fix style error

* Simplify detection logic; offload corner cases to CMake
  • Loading branch information
mallniya authored and ruslo committed Mar 23, 2019
1 parent c1683d2 commit 8f3a737
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 2 additions & 0 deletions cmake/build_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@

#cmakedefine DMLC_NANOSLEEP_PRESENT

#cmakedefine DMLC_CMAKE_LITTLE_ENDIAN ${DMLC_CMAKE_LITTLE_ENDIAN}

#endif // DMLC_BUILD_CONFIG_H_
18 changes: 14 additions & 4 deletions include/dmlc/endian.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <endian.h>
#define DMLC_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
#if defined(__APPLE__) || defined(_WIN32)
#define DMLC_LITTLE_ENDIAN 1
#elif defined(__GLIBC__)
#include <endian.h>
#define DMLC_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
#elif defined(__FreeBSD__)
#include <sys/endian.h>
#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 */
Expand Down

0 comments on commit 8f3a737

Please sign in to comment.