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 #=============================================================================
8 # Copyright 2002-2009 Kitware, Inc.
10 # Distributed under the OSI-approved BSD License (the "License");
11 # see accompanying file Copyright.txt for details.
13 # This software is distributed WITHOUT ANY WARRANTY; without even the
14 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 # See the License for more information.
16 #=============================================================================
17 # (To distribute this file outside of CMake, substitute the full
18 # License text for the above reference.)
20 macro(TEST_BIG_ENDIAN VARIABLE)
21 if("HAVE_${VARIABLE}" MATCHES "^HAVE_${VARIABLE}$")
22 message(STATUS "Check if the system is big endian")
23 message(STATUS "Searching 16 bit integer")
25 include(CheckTypeSize)
27 CHECK_TYPE_SIZE("unsigned short" CMAKE_SIZEOF_UNSIGNED_SHORT)
28 if(CMAKE_SIZEOF_UNSIGNED_SHORT EQUAL 2)
29 message(STATUS "Using unsigned short")
30 set(CMAKE_16BIT_TYPE "unsigned short")
32 CHECK_TYPE_SIZE("unsigned int" CMAKE_SIZEOF_UNSIGNED_INT)
33 if(CMAKE_SIZEOF_UNSIGNED_INT)
34 message(STATUS "Using unsigned int")
35 set(CMAKE_16BIT_TYPE "unsigned int")
39 CHECK_TYPE_SIZE("unsigned long" CMAKE_SIZEOF_UNSIGNED_LONG)
40 if(CMAKE_SIZEOF_UNSIGNED_LONG)
41 message(STATUS "Using unsigned long")
42 set(CMAKE_16BIT_TYPE "unsigned long")
44 message(FATAL_ERROR "no suitable type found")
52 configure_file("${CMAKE_ROOT}/Modules/TestEndianess.c.in"
53 "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/TestEndianess.c"
56 file(READ "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/TestEndianess.c"
57 TEST_ENDIANESS_FILE_CONTENT)
59 try_compile(HAVE_${VARIABLE}
61 "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/TestEndianess.c"
62 OUTPUT_VARIABLE OUTPUT
63 COPY_FILE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestEndianess.bin" )
67 file(STRINGS "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestEndianess.bin"
68 CMAKE_TEST_ENDIANESS_STRINGS_LE LIMIT_COUNT 1 REGEX "THIS IS LITTLE ENDIAN")
70 file(STRINGS "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestEndianess.bin"
71 CMAKE_TEST_ENDIANESS_STRINGS_BE LIMIT_COUNT 1 REGEX "THIS IS BIG ENDIAN")
73 # on mac, if there are universal binaries built both will be true
74 # return the result depending on the machine on which cmake runs
75 if(CMAKE_TEST_ENDIANESS_STRINGS_BE AND CMAKE_TEST_ENDIANESS_STRINGS_LE)
76 if(CMAKE_SYSTEM_PROCESSOR MATCHES powerpc)
77 set(CMAKE_TEST_ENDIANESS_STRINGS_BE TRUE)
78 set(CMAKE_TEST_ENDIANESS_STRINGS_LE FALSE)
80 set(CMAKE_TEST_ENDIANESS_STRINGS_BE FALSE)
81 set(CMAKE_TEST_ENDIANESS_STRINGS_LE TRUE)
83 message(STATUS "TEST_BIG_ENDIAN found different results, consider setting CMAKE_OSX_ARCHITECTURES or CMAKE_TRY_COMPILE_OSX_ARCHITECTURES to one or no architecture !")
86 if(CMAKE_TEST_ENDIANESS_STRINGS_LE)
87 set(${VARIABLE} 0 CACHE INTERNAL "Result of TEST_BIG_ENDIAN" FORCE)
88 message(STATUS "Check if the system is big endian - little endian")
91 if(CMAKE_TEST_ENDIANESS_STRINGS_BE)
92 set(${VARIABLE} 1 CACHE INTERNAL "Result of TEST_BIG_ENDIAN" FORCE)
93 message(STATUS "Check if the system is big endian - big endian")
96 if(NOT CMAKE_TEST_ENDIANESS_STRINGS_BE AND NOT CMAKE_TEST_ENDIANESS_STRINGS_LE)
97 message(SEND_ERROR "TEST_BIG_ENDIAN found no result!")
100 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
101 "Determining if the system is big endian passed with the following output:\n${OUTPUT}\nTestEndianess.c:\n${TEST_ENDIANESS_FILE_CONTENT}\n\n")
104 message(STATUS "Check if the system is big endian - failed")
105 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
106 "Determining if the system is big endian failed with the following output:\n${OUTPUT}\nTestEndianess.c:\n${TEST_ENDIANESS_FILE_CONTENT}\n\n")