TestBigEndian.cmake 4.5 KB

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