The preprocessor definitions __BYTE_ORDER__, __ORDER_BIG_ENDIAN__, and __ORDER_LITTLE_ENDIAN__ are gcc extensions (also supported by clang), but msvc (and maybe others) do not define them. As a result BYTE_ORDER and ORDER_BIG_ENDIAN both evaluate to 0 by the prepreprocess, and __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ , the first #if condition to 1, assuming the wrong byte order in x86(_64).
This patch instead uses CMake's TestBigEndian module to determine target architecture's endianness at configure time.
Note this also uses the same mechanism for the runtime. If compiling flang as a cross-compiler, the runtime for the compile-target must be built separately (Flang does not support the LLVM_ENABLE_RUNTIMES mechanism).
Fixes llvm.org/PR51597