[libgenstext] Don't use CMake's TEST_BIG_ENDIAN() function.
authorDavid Korth <gerbilsoft@gerbilsoft.com>
Sat, 5 Sep 2015 15:43:58 +0000 (11:43 -0400)
committerDavid Korth <gerbilsoft@gerbilsoft.com>
Sat, 5 Sep 2015 15:43:58 +0000 (11:43 -0400)
Copied byteorder.h from libcompat. We're using a copy because we
don't want libgenstext to depend on libcompat.

Interestingly, I found this by attempting to build Gens/GS II with
MSVC 6.0 (lol). TEST_BIG_ENDIAN() failed because it couldn't find
a suitable type to test.

src/libgenstext/CMakeLists.txt
src/libgenstext/Encoding.cpp
src/libgenstext/byteorder.h [new file with mode: 0644]
src/libgenstext/byteorder.h.in [deleted file]

index db55d75..8183336 100644 (file)
@@ -38,18 +38,6 @@ ENDIF(NOT WIN32)
 # Write the config.h file.
 CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/config.libgenstext.h.in" "${CMAKE_CURRENT_BINARY_DIR}/config.libgenstext.h")
 
-# Test system byteorder.
-# TODO: Universal Binary support; detect PDP endian.
-INCLUDE(TestBigEndian)
-TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
-IF(IS_BIG_ENDIAN)
-       SET(GENSTEXT_BYTEORDER "GENSTEXT_BIG_ENDIAN")
-ELSE(IS_BIG_ENDIAN)
-       SET(GENSTEXT_BYTEORDER "GENSTEXT_LIL_ENDIAN")
-ENDIF(IS_BIG_ENDIAN)
-UNSET(IS_BIG_ENDIAN)
-CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/byteorder.h.in" "${CMAKE_CURRENT_BINARY_DIR}/byteorder.h")
-
 # Sources.
 SET(libgenstext_SRCS
        Encoding.cpp
index d3c0f30..6442a26 100644 (file)
@@ -29,9 +29,9 @@
 # include <windows.h>
 #elif defined(HAVE_ICONV)
 #  include <iconv.h>
-#  if GENSTEXT_BYTEORDER == GENSTEXT_BIG_ENDIAN
+#  if SYS_BYTEORDER == SYS_BIG_ENDIAN
 #    define UTF16_ENCODING "UTF-16BE"
-#  else /* GENSTEXT_BYTEORDER == GENSTEXT_LIL_ENDIAN */
+#  else /* SYS_BYTEORDER == SYS_LIL_ENDIAN */
 #    define UTF16_ENCODING "UTF-16LE"
 #  endif
 #endif
diff --git a/src/libgenstext/byteorder.h b/src/libgenstext/byteorder.h
new file mode 100644 (file)
index 0000000..23f552d
--- /dev/null
@@ -0,0 +1,80 @@
+/***************************************************************************
+ * libgenstext: Gens/GS II Text Manipulation Library.                      *
+ * byteorder.h: System byte order header.                                  *
+ *                                                                         *
+ * Copyright (c) 2011-2015 by David Korth.                                 *
+ *                                                                         *
+ * This program is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU General Public License as published by the   *
+ * Free Software Foundation; either version 2 of the License, or (at your  *
+ * option) any later version.                                              *
+ *                                                                         *
+ * This program is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of              *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
+ * GNU General Public License for more details.                            *
+ *                                                                         *
+ * You should have received a copy of the GNU General Public License along *
+ * with this program; if not, write to the Free Software Foundation, Inc., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.           *
+ ***************************************************************************/
+
+// NOTE: This is copied from libcompat.
+// We're using a copy so we don't have to depend
+// on libcompat in this library.
+
+#ifndef __LIBGENSTEXT_BYTEORDER_H__
+#define __LIBGENSTEXT_BYTEORDER_H__
+
+/**
+ * Check the system byte order.
+ *
+ * We're doing the test here instead of in CMake because:
+ * 1. CMake doesn't support testing multiple architectures
+ *    at the same time. (Mac OS X universal binary)
+ * 2. CMake can't test the byte order when cross-compiling to
+ *    a different CPU.
+ *
+ * Based on SDL_endian.h from SDL 1.2 and 2.0.
+ * Other references:
+ * - http://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros
+ * - http://sourceforge.net/p/predef/wiki/Architectures/
+ */
+
+#define SYS_LIL_ENDIAN 1234
+#define SYS_BIG_ENDIAN 4321
+#define SYS_PDP_ENDIAN 3412
+
+#if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) || \
+    defined(__hppa__) || defined(__HPPA__) || \
+    defined(__m68k__) || defined(__MC68K__) || defined(_M_M68K) || \
+    defined(mc68000) || defined(M68000) || \
+    ((defined(__mips) || defined(__mips__) || defined(__mips) || defined(__MIPS__)) && \
+     (defined(__mipseb) || defined(__mipseb__) || defined(__MIPSEB) || defined(__MIPSEB__))) || \
+    defined(__powerpc__) || defined(__POWERPC__) || \
+    defined(__ppc__) || defined(__PPC__) || defined(_M_PPC) || \
+    defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
+    defined(__sparc) || defined(__sparc__)
+
+/* System is big-endian. */
+#define SYS_BYTEORDER SYS_BIG_ENDIAN
+#define SYS_IS_BIG_ENDIAN 1
+#undef SYS_IS_LIL_ENDIAN
+#undef SYS_IS_PDP_ENDIAN
+
+#elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_PDP_ENDIAN__)
+
+/* System is PDP-endian. */
+#error Why are you trying to run a Genesis emulator on a PDP?
+
+#else
+
+/* System is little-endian. */
+#define SYS_BYTEORDER SYS_LIL_ENDIAN
+#define SYS_IS_LIL_ENDIAN 1
+#undef SYS_IS_BIG_ENDIAN
+#undef SYS_IS_PDP_ENDIAN
+
+#endif
+
+#endif /* __LIBGENSTEXT_BYTEORDER_H__ */
diff --git a/src/libgenstext/byteorder.h.in b/src/libgenstext/byteorder.h.in
deleted file mode 100644 (file)
index 84ad882..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-/***************************************************************************
- * libgenstext: Gens/GS II Text Manipulation Library.                      *
- * byteorder.h.in: Source file for byteorder.h.                            *
- * Indicates the system byteorder as detected by TEST_BIG_ENDIAN().        *
- *                                                                         *
- * Copyright (c) 2011 by David Korth.                                      *
- *                                                                         *
- * This program is free software; you can redistribute it and/or modify it *
- * under the terms of the GNU General Public License as published by the   *
- * Free Software Foundation; either version 2 of the License, or (at your  *
- * option) any later version.                                              *
- *                                                                         *
- * This program is distributed in the hope that it will be useful, but     *
- * WITHOUT ANY WARRANTY; without even the implied warranty of              *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
- * GNU General Public License for more details.                            *
- *                                                                         *
- * You should have received a copy of the GNU General Public License along *
- * with this program; if not, write to the Free Software Foundation, Inc., *
- * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.           *
- ***************************************************************************/
-
-#ifndef __LIBGENSTEXT_UTIL_BYTEORDER_H__
-#define __LIBGENSTEXT_UTIL_BYTEORDER_H__
-
-#define GENSTEXT_LIL_ENDIAN 1234
-#define GENSTEXT_BIG_ENDIAN 4321
-#define GENSTEXT_PDP_ENDIAN 3412
-
-/* System byteorder. */
-#cmakedefine GENSTEXT_BYTEORDER @GENSTEXT_BYTEORDER@
-
-#endif /* __LIBGENSTEXT_UTIL_BYTEORDER_H__ */