TestBigEndian.cmake 4.5 KB

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