1 /* $Id: endian_check.cpp 23595 2011-12-19 17:48:04Z rubidium $ */
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/>.
11 * @file endian_check.cpp
12 * This pretty simple file checks if the system is LITTLE_ENDIAN or BIG_ENDIAN
13 * it does that by putting a 1 and a 0 in an array, and read it out as one
14 * number. If it is 1, it is LITTLE_ENDIAN, if it is 256, it is BIG_ENDIAN
16 * After that it outputs the contents of an include files (endian.h)
17 * that says or TTD_LITTLE_ENDIAN, or TTD_BIG_ENDIAN. Makefile takes
18 * care of the real writing to the file.
24 /** Supported endian types */
26 ENDIAN_LITTLE
, ///< little endian
27 ENDIAN_BIG
, ///< big endian
31 * Shortcut to printf("#define TTD_ENDIAN TTD_*_ENDIAN")
32 * @param endian endian type to define
34 static inline void printf_endian(Endian endian
)
36 printf("#define TTD_ENDIAN %s\n", endian
== ENDIAN_LITTLE
? "TTD_LITTLE_ENDIAN" : "TTD_BIG_ENDIAN");
40 * Main call of the endian_check program
41 * @param argc argument count
42 * @param argv arguments themselves
45 int main (int argc
, char *argv
[])
47 unsigned char endian_test
[2] = { 1, 0 };
48 int force_BE
= 0, force_LE
= 0, force_PREPROCESSOR
= 0;
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;
54 printf("#ifndef ENDIAN_H\n#define ENDIAN_H\n");
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
65 printf("#ifdef __BIG_ENDIAN__\n");
66 printf_endian(ENDIAN_BIG
);
68 printf_endian(ENDIAN_LITTLE
);
70 } else if (*(short*)endian_test
== 1 ) {
71 printf_endian(ENDIAN_LITTLE
);
73 printf_endian(ENDIAN_BIG
);