Skip to content

Commit 7ec118f

Browse files
[ADT] Use CMake to detect host endianness (NFC)
This patch replaces the manual endian detection logic in ADT/bit.h with CMake's CMAKE_CXX_BYTE_ORDER, freeing ourselves from the work that the build system is already capable of. The public interface, llvm::endianness, remains unchanged.
1 parent a3557c3 commit 7ec118f

File tree

3 files changed

+14
-27
lines changed

3 files changed

+14
-27
lines changed

llvm/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,15 @@ if(NOT LLVM_ENABLE_NEW_PASS_MANAGER)
10791079
" no longer supported.")
10801080
endif()
10811081

1082+
# Determine host endianness.
1083+
if(CMAKE_CXX_BYTE_ORDER STREQUAL "BIG_ENDIAN")
1084+
set(LLVM_HOST_IS_BIG_ENDIAN 1)
1085+
elseif(CMAKE_CXX_BYTE_ORDER STREQUAL "LITTLE_ENDIAN")
1086+
set(LLVM_HOST_IS_BIG_ENDIAN 0)
1087+
else()
1088+
message(FATAL_ERROR "Unsupported CMAKE_CXX_BYTE_ORDER: ${CMAKE_CXX_BYTE_ORDER}")
1089+
endif()
1090+
10821091
include(HandleLLVMOptions)
10831092

10841093
######

llvm/include/llvm/ADT/bit.h

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef LLVM_ADT_BIT_H
1515
#define LLVM_ADT_BIT_H
1616

17+
#include "llvm/Config/config.h"
1718
#include "llvm/Support/Compiler.h"
1819
#include <cstddef> // for std::size_t
1920
#include <cstdint>
@@ -28,32 +29,6 @@
2829
#include <cstdlib> // for _byteswap_{ushort,ulong,uint64}
2930
#endif
3031

31-
#if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__) || \
32-
defined(__Fuchsia__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__) || \
33-
defined(__OpenBSD__) || defined(__DragonFly__) || defined(__managarm__)
34-
#include <endian.h>
35-
#elif defined(_AIX)
36-
#include <sys/machine.h>
37-
#elif defined(__sun)
38-
/* Solaris provides _BIG_ENDIAN/_LITTLE_ENDIAN selector in sys/types.h */
39-
#include <sys/types.h>
40-
#define BIG_ENDIAN 4321
41-
#define LITTLE_ENDIAN 1234
42-
#if defined(_BIG_ENDIAN)
43-
#define BYTE_ORDER BIG_ENDIAN
44-
#else
45-
#define BYTE_ORDER LITTLE_ENDIAN
46-
#endif
47-
#elif defined(__MVS__)
48-
#define BIG_ENDIAN 4321
49-
#define LITTLE_ENDIAN 1234
50-
#define BYTE_ORDER BIG_ENDIAN
51-
#else
52-
#if !defined(BYTE_ORDER) && !defined(_WIN32)
53-
#include <machine/endian.h>
54-
#endif
55-
#endif
56-
5732
#ifdef _MSC_VER
5833
// Declare these intrinsics manually rather including intrin.h. It's very
5934
// expensive, and bit.h is popular via MathExtras.h.
@@ -71,7 +46,7 @@ namespace llvm {
7146
enum class endianness {
7247
big,
7348
little,
74-
#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
49+
#if LLVM_HOST_IS_BIG_ENDIAN
7550
native = big
7651
#else
7752
native = little

llvm/include/llvm/Config/config.h.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
/* Bug report URL. */
1111
#define BUG_REPORT_URL "${BUG_REPORT_URL}"
1212

13+
/* Define to 1 if the host is a big endian machine, and to 0 otherwise. */
14+
#cmakedefine01 LLVM_HOST_IS_BIG_ENDIAN
15+
1316
/* Define to 1 to enable backtraces, and to 0 otherwise. */
1417
#cmakedefine01 ENABLE_BACKTRACES
1518

0 commit comments

Comments
 (0)