Ticket #38795: TestBigEndian.cmake

File TestBigEndian.cmake, 4.5 KB (added by ozanyarman@…, 11 years ago)

The original location was an alias pointing to this file.

Line 
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
5#
6
7#=============================================================================
8# Copyright 2002-2009 Kitware, Inc.
9#
10# Distributed under the OSI-approved BSD License (the "License");
11# see accompanying file Copyright.txt for details.
12#
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.)
19
20macro(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")
24
25    include(CheckTypeSize)
26
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")
31    else()
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")
36
37      else()
38
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")
43        else()
44          message(FATAL_ERROR "no suitable type found")
45        endif()
46
47      endif()
48
49    endif()
50
51
52    configure_file("${CMAKE_ROOT}/Modules/TestEndianess.c.in"
53                   "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/TestEndianess.c"
54                    IMMEDIATE @ONLY)
55
56     file(READ "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/TestEndianess.c"
57          TEST_ENDIANESS_FILE_CONTENT)
58
59     try_compile(HAVE_${VARIABLE}
60      "${CMAKE_BINARY_DIR}"
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" )
64
65      if(HAVE_${VARIABLE})
66
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")
69
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")
72
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)
79          else()
80            set(CMAKE_TEST_ENDIANESS_STRINGS_BE FALSE)
81            set(CMAKE_TEST_ENDIANESS_STRINGS_LE TRUE)
82          endif()
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 !")
84        endif()
85
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")
89        endif()
90
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")
94        endif()
95
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!")
98        endif()
99
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")
102
103      else()
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")
107        set(${VARIABLE})
108      endif()
109  endif()
110endmacro()
111
112