ENH: fix for Vista
[cmake.git] / Modules / TestBigEndian.cmake
blobffc3a0cda4340fabc9c5708ce05613c060d8f5a3
1 # - Define macro to determine endian type
2 # Check if the system is big endian or little endian
3 #  TEST_BIG_ENDIAN(VARIABLE)
4 #  VARIABLE - variable to store the result to
7 MACRO(TEST_BIG_ENDIAN VARIABLE)
8   IF("HAVE_${VARIABLE}" MATCHES "^HAVE_${VARIABLE}$")
9     MESSAGE(STATUS "Check if the system is big endian")
10     MESSAGE(STATUS "Searching 16 bit integer")
11   
12     INCLUDE(CheckTypeSize)
14     CHECK_TYPE_SIZE("unsigned short" CMAKE_SIZEOF_UNSIGNED_SHORT)
15     IF(CMAKE_SIZEOF_UNSIGNED_SHORT EQUAL 2)
16       MESSAGE(STATUS "Using unsigned short")
17       SET(CMAKE_16BIT_TYPE "unsigned short")
18     ELSE(CMAKE_SIZEOF_UNSIGNED_SHORT EQUAL 2)
19       CHECK_TYPE_SIZE("unsigned int"   CMAKE_SIZEOF_UNSIGNED_INT)
20       IF(CMAKE_SIZEOF_UNSIGNED_INT)
21         MESSAGE(STATUS "Using unsigned int")
22         SET(CMAKE_16BIT_TYPE "unsigned int")
24       ELSE(CMAKE_SIZEOF_UNSIGNED_INT)
25   
26         CHECK_TYPE_SIZE("unsigned long"  CMAKE_SIZEOF_UNSIGNED_LONG)
27         IF(CMAKE_SIZEOF_UNSIGNED_LONG)
28           MESSAGE(STATUS "Using unsigned long")
29           SET(CMAKE_16BIT_TYPE "unsigned long")
30         ELSE(CMAKE_SIZEOF_UNSIGNED_LONG)
31           MESSAGE(FATAL_ERROR "no suitable type found")
32         ENDIF(CMAKE_SIZEOF_UNSIGNED_LONG)
33     
34       ENDIF(CMAKE_SIZEOF_UNSIGNED_INT)
35     
36     ENDIF(CMAKE_SIZEOF_UNSIGNED_SHORT EQUAL 2)
38   
39     CONFIGURE_FILE("${CMAKE_ROOT}/Modules/TestEndianess.c.in" 
40                    "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/TestEndianess.c"
41                     IMMEDIATE @ONLY)
42   
43      FILE(READ "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/TestEndianess.c"
44           TEST_ENDIANESS_FILE_CONTENT)
46      TRY_COMPILE(HAVE_${VARIABLE}
47       "${CMAKE_BINARY_DIR}"
48       "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/TestEndianess.c"
49       OUTPUT_VARIABLE OUTPUT
50       COPY_FILE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestEndianess.bin" )
52       IF(HAVE_${VARIABLE})
54         FILE(STRINGS "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestEndianess.bin"
55             CMAKE_TEST_ENDIANESS_STRINGS_LE LIMIT_COUNT 1 REGEX "THIS IS LITTLE ENDIAN")
57         FILE(STRINGS "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestEndianess.bin"
58             CMAKE_TEST_ENDIANESS_STRINGS_BE LIMIT_COUNT 1 REGEX "THIS IS BIG ENDIAN")
60         # on mac, if there are universal binaries built both will be true
61         # return the result depending on the machine on which cmake runs
62         IF(CMAKE_TEST_ENDIANESS_STRINGS_BE  AND  CMAKE_TEST_ENDIANESS_STRINGS_LE)
63           IF(CMAKE_SYSTEM_PROCESSOR MATCHES powerpc)
64             SET(CMAKE_TEST_ENDIANESS_STRINGS_BE TRUE)
65             SET(CMAKE_TEST_ENDIANESS_STRINGS_LE FALSE)
66           ELSE(CMAKE_SYSTEM_PROCESSOR MATCHES powerpc)
67             SET(CMAKE_TEST_ENDIANESS_STRINGS_BE FALSE)
68             SET(CMAKE_TEST_ENDIANESS_STRINGS_LE TRUE)
69           ENDIF(CMAKE_SYSTEM_PROCESSOR MATCHES powerpc)
70           MESSAGE(STATUS "TEST_BIG_ENDIAN found different results, consider setting CMAKE_OSX_ARCHITECTURES or CMAKE_TRY_COMPILE_OSX_ARCHITECTURES to one or no architecture !")
71         ENDIF(CMAKE_TEST_ENDIANESS_STRINGS_BE  AND  CMAKE_TEST_ENDIANESS_STRINGS_LE)
73         IF(CMAKE_TEST_ENDIANESS_STRINGS_LE)
74           SET(${VARIABLE} 0 CACHE INTERNAL "Result of TEST_BIG_ENDIAN" FORCE)
75           MESSAGE(STATUS "Check if the system is big endian - little endian")
76         ENDIF(CMAKE_TEST_ENDIANESS_STRINGS_LE)
78         IF(CMAKE_TEST_ENDIANESS_STRINGS_BE)
79           SET(${VARIABLE} 1 CACHE INTERNAL "Result of TEST_BIG_ENDIAN" FORCE)
80           MESSAGE(STATUS "Check if the system is big endian - big endian")
81         ENDIF(CMAKE_TEST_ENDIANESS_STRINGS_BE)
83         IF(NOT CMAKE_TEST_ENDIANESS_STRINGS_BE  AND  NOT CMAKE_TEST_ENDIANESS_STRINGS_LE)
84           MESSAGE(SEND_ERROR "TEST_BIG_ENDIAN found no result!")
85         ENDIF(NOT CMAKE_TEST_ENDIANESS_STRINGS_BE  AND  NOT CMAKE_TEST_ENDIANESS_STRINGS_LE)
87         FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
88           "Determining if the system is big endian passed with the following output:\n${OUTPUT}\nTestEndianess.c:\n${TEST_ENDIANESS_FILE_CONTENT}\n\n")
90       ELSE(HAVE_${VARIABLE})
91         MESSAGE(STATUS "Check if the system is big endian - failed")
92         FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
93           "Determining if the system is big endian failed with the following output:\n${OUTPUT}\nTestEndianess.c:\n${TEST_ENDIANESS_FILE_CONTENT}\n\n")
94         SET(${VARIABLE})
95       ENDIF(HAVE_${VARIABLE})
96   ENDIF("HAVE_${VARIABLE}" MATCHES "^HAVE_${VARIABLE}$")
97 ENDMACRO(TEST_BIG_ENDIAN)