OpenTTD: src/endian_check.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

endian_check.cpp
Go to the documentation of this file.
1 /* $Id: endian_check.cpp 23595 2011-12-19 17:48:04Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD 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, version 2.
6 * OpenTTD 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.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
21 #include <stdio.h>
22 #include <string.h>
23
25 enum Endian {
26 ENDIAN_LITTLE,
27 ENDIAN_BIG,
28 };
29
34 static inline void printf_endian(Endian endian)
35 {
36 printf("#define TTD_ENDIAN %s\n", endian == ENDIAN_LITTLE ? "TTD_LITTLE_ENDIAN" : "TTD_BIG_ENDIAN");
37 }
38
45 int main (int argc, char *argv[])
46 {
47 unsigned char endian_test[2] = { 1, 0 };
48 int force_BE = 0, force_LE = 0, force_PREPROCESSOR = 0;
49
50 if (argc > 1 && strcmp(argv[1], "BE") == 0) force_BE = 1;
51 if (argc > 1 && strcmp(argv[1], "LE") == 0) force_LE = 1;
52 if (argc > 1 && strcmp(argv[1], "PREPROCESSOR") == 0) force_PREPROCESSOR = 1;
53
54 printf("#ifndef ENDIAN_H\n#define ENDIAN_H\n");
55
56 if (force_LE == 1) {
57 printf_endian(ENDIAN_LITTLE);
58 } else if (force_BE == 1) {
59 printf_endian(ENDIAN_BIG);
60 } else if (force_PREPROCESSOR == 1) {
61 /* Support for universal binaries on OSX
62 * Universal binaries supports both PPC and x86
63 * If a compiler for OSX gets this setting, it will always pick the correct endian and no test is needed
64 */
65 printf("#ifdef __BIG_ENDIAN__\n");
66 printf_endian(ENDIAN_BIG);
67 printf("#else\n");
68 printf_endian(ENDIAN_LITTLE);
69 printf("#endif\n");
70 } else if (*(short*)endian_test == 1 ) {
71 printf_endian(ENDIAN_LITTLE);
72 } else {
73 printf_endian(ENDIAN_BIG);
74 }
75 printf("#endif\n");
76
77 return 0;
78 }