OpenTTD
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 {
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) {
58  } else if (force_BE == 1) {
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");
67  printf("#else\n");
69  printf("#endif\n");
70  } else if (*(short*)endian_test == 1 ) {
72  } else {
74  }
75  printf("#endif\n");
76 
77  return 0;
78 }
static void printf_endian(Endian endian)
Shortcut to printf("#define TTD_ENDIAN TTD_*_ENDIAN")
big endian
Endian
Supported endian types.
little endian
int main(int argc, char *argv[])
Main call of the endian_check program.