Skip to content

Commit

Permalink
Land #998, Windows: Improve check for endianness when using Visual St…
Browse files Browse the repository at this point in the history
…udio
  • Loading branch information
busterb committed Feb 12, 2024
2 parents f63596f + 5bd332e commit db3ab16
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ endif()
if(MSVC)
add_definitions(-Dinline=__inline)
message(STATUS "Using [${CMAKE_C_COMPILER_ID}] compiler")

include(TestBigEndian)
TEST_BIG_ENDIAN(HAVE_BIG_ENDIAN)
if(HAVE_BIG_ENDIAN)
add_definitions(-DHAVE_BIG_ENDIAN)
else()
add_definitions(-DHAVE_LITTLE_ENDIAN)
endif()

if(CMAKE_C_COMPILER_ID MATCHES "MSVC" OR CMAKE_C_COMPILER_ID MATCHES "Clang")
set(MSVC_DISABLED_WARNINGS_LIST
"C4018" # 'expression' : signed/unsigned mismatch
Expand Down
17 changes: 16 additions & 1 deletion include/compat/endian.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,28 @@
#define PDP_ENDIAN 3412

/*
* Use GCC and Visual Studio compiler defines to determine endian.
* Use GCC compiler 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
#endif

/*
* Use build system defines to determine endianness.
*/
#if !defined(BYTE_ORDER)
#if 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
#endif

#elif defined(HAVE_ENDIAN_H)
#include_next <endian.h>
Expand Down

0 comments on commit db3ab16

Please sign in to comment.