TestBigEndian.cmake 4.53 KB
Newer Older
1 2 3 4 5 6
#.rst:
# TestBigEndian
# -------------
#
# Define macro to determine endian type
#
Andy Cedilnik's avatar
Andy Cedilnik committed
7 8
# Check if the system is big endian or little endian
#
9 10 11 12
# ::
#
#   TEST_BIG_ENDIAN(VARIABLE)
#   VARIABLE - variable to store the result to
Andy Cedilnik's avatar
Andy Cedilnik committed
13

14 15 16 17 18 19 20 21 22 23
#=============================================================================
# Copyright 2002-2009 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
24
# (To distribute this file outside of CMake, substitute the full
25 26
#  License text for the above reference.)

27
macro(TEST_BIG_ENDIAN VARIABLE)
28
  if(NOT DEFINED HAVE_${VARIABLE})
29 30
    message(STATUS "Check if the system is big endian")
    message(STATUS "Searching 16 bit integer")
31

32
    include(CheckTypeSize)
33 34

    CHECK_TYPE_SIZE("unsigned short" CMAKE_SIZEOF_UNSIGNED_SHORT)
35 36 37
    if(CMAKE_SIZEOF_UNSIGNED_SHORT EQUAL 2)
      message(STATUS "Using unsigned short")
      set(CMAKE_16BIT_TYPE "unsigned short")
38
    else()
39
      CHECK_TYPE_SIZE("unsigned int"   CMAKE_SIZEOF_UNSIGNED_INT)
40 41 42
      if(CMAKE_SIZEOF_UNSIGNED_INT)
        message(STATUS "Using unsigned int")
        set(CMAKE_16BIT_TYPE "unsigned int")
43

44
      else()
45

46
        CHECK_TYPE_SIZE("unsigned long"  CMAKE_SIZEOF_UNSIGNED_LONG)
47 48 49
        if(CMAKE_SIZEOF_UNSIGNED_LONG)
          message(STATUS "Using unsigned long")
          set(CMAKE_16BIT_TYPE "unsigned long")
50
        else()
51
          message(FATAL_ERROR "no suitable type found")
52
        endif()
53

54
      endif()
55

56
    endif()
57

58

59
    configure_file("${CMAKE_ROOT}/Modules/TestEndianess.c.in"
60
                   "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/TestEndianess.c"
61
                   @ONLY)
62

63
     file(READ "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/TestEndianess.c"
64
          TEST_ENDIANESS_FILE_CONTENT)
65

66
     try_compile(HAVE_${VARIABLE}
67 68 69 70 71
      "${CMAKE_BINARY_DIR}"
      "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/TestEndianess.c"
      OUTPUT_VARIABLE OUTPUT
      COPY_FILE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestEndianess.bin" )

72
      if(HAVE_${VARIABLE})
73

74
        file(STRINGS "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestEndianess.bin"
75
            CMAKE_TEST_ENDIANESS_STRINGS_LE LIMIT_COUNT 1 REGEX "THIS IS LITTLE ENDIAN")
76

77
        file(STRINGS "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestEndianess.bin"
78
            CMAKE_TEST_ENDIANESS_STRINGS_BE LIMIT_COUNT 1 REGEX "THIS IS BIG ENDIAN")
79 80 81

        # on mac, if there are universal binaries built both will be true
        # return the result depending on the machine on which cmake runs
82 83 84 85
        if(CMAKE_TEST_ENDIANESS_STRINGS_BE  AND  CMAKE_TEST_ENDIANESS_STRINGS_LE)
          if(CMAKE_SYSTEM_PROCESSOR MATCHES powerpc)
            set(CMAKE_TEST_ENDIANESS_STRINGS_BE TRUE)
            set(CMAKE_TEST_ENDIANESS_STRINGS_LE FALSE)
86
          else()
87 88
            set(CMAKE_TEST_ENDIANESS_STRINGS_BE FALSE)
            set(CMAKE_TEST_ENDIANESS_STRINGS_LE TRUE)
89
          endif()
90
          message(STATUS "TEST_BIG_ENDIAN found different results, consider setting CMAKE_OSX_ARCHITECTURES or CMAKE_TRY_COMPILE_OSX_ARCHITECTURES to one or no architecture !")
91
        endif()
92 93 94 95

        if(CMAKE_TEST_ENDIANESS_STRINGS_LE)
          set(${VARIABLE} 0 CACHE INTERNAL "Result of TEST_BIG_ENDIAN" FORCE)
          message(STATUS "Check if the system is big endian - little endian")
96
        endif()
97 98 99 100

        if(CMAKE_TEST_ENDIANESS_STRINGS_BE)
          set(${VARIABLE} 1 CACHE INTERNAL "Result of TEST_BIG_ENDIAN" FORCE)
          message(STATUS "Check if the system is big endian - big endian")
101
        endif()
102 103 104

        if(NOT CMAKE_TEST_ENDIANESS_STRINGS_BE  AND  NOT CMAKE_TEST_ENDIANESS_STRINGS_LE)
          message(SEND_ERROR "TEST_BIG_ENDIAN found no result!")
105
        endif()
106

107
        file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
108
          "Determining if the system is big endian passed with the following output:\n${OUTPUT}\nTestEndianess.c:\n${TEST_ENDIANESS_FILE_CONTENT}\n\n")
109

110
      else()
111 112
        message(STATUS "Check if the system is big endian - failed")
        file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
113
          "Determining if the system is big endian failed with the following output:\n${OUTPUT}\nTestEndianess.c:\n${TEST_ENDIANESS_FILE_CONTENT}\n\n")
114
        set(${VARIABLE})
115 116 117
      endif()
  endif()
endmacro()
118 119