toolchain: Add global check for endianness preprocessor definitions.
This commit adds a global check for __BYTE_ORDER__,
__ORDER_LITTLE_ENDIAN__ and __ORDER_BIG_ENDIAN__ preprocessor
definitions that are used throughout the Zephyr codebase.
These preprocessor definitions being not defined can easily
go unnoticed and cause unexpected behaviours, as detailed in
PR #18922.
Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
diff --git a/include/toolchain.h b/include/toolchain.h
index 88cedb0..adc8c2c 100644
--- a/include/toolchain.h
+++ b/include/toolchain.h
@@ -41,4 +41,18 @@
#include <toolchain/other.h>
#endif
+/*
+ * Ensure that __BYTE_ORDER__ and related preprocessor definitions are defined,
+ * as these are often used without checking for definition and doing so can
+ * cause unexpected behaviours.
+ */
+#ifndef _LINKER
+#if !defined(__BYTE_ORDER__) || !defined(__ORDER_BIG_ENDIAN__) || \
+ !defined(__ORDER_LITTLE_ENDIAN__)
+
+#error "__BYTE_ORDER__ is not defined"
+
+#endif
+#endif /* !_LINKER */
+
#endif /* ZEPHYR_INCLUDE_TOOLCHAIN_H_ */