Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows: Improve check for endianness when using Visual Studio. #998

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,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