cmake/Modules/TestBigEndian.cmake

144 lines
5.3 KiB
CMake
Raw Normal View History

2016-10-30 18:24:19 +01:00
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
2019-11-11 23:01:05 +01:00
#[=======================================================================[.rst:
TestBigEndian
-------------
2021-09-14 00:13:48 +02:00
.. deprecated:: 3.20
2019-11-11 23:01:05 +01:00
2021-09-14 00:13:48 +02:00
Supserseded by the :variable:`CMAKE_<LANG>_BYTE_ORDER` variable.
2019-11-11 23:01:05 +01:00
2021-09-14 00:13:48 +02:00
Check if the target architecture is big endian or little endian.
.. command:: test_big_endian
.. code-block:: cmake
test_big_endian(<var>)
Stores in variable ``<var>`` either 1 or 0 indicating whether the
target architecture is big or little endian.
2019-11-11 23:01:05 +01:00
#]=======================================================================]
2021-09-14 00:13:48 +02:00
include_guard()
2018-08-09 18:06:22 +02:00
include(CheckTypeSize)
2021-09-14 00:13:48 +02:00
function(TEST_BIG_ENDIAN VARIABLE)
if(";${CMAKE_C_BYTE_ORDER};${CMAKE_CXX_BYTE_ORDER};${CMAKE_CUDA_BYTE_ORDER};${CMAKE_OBJC_BYTE_ORDER};${CMAKE_OBJCXX_BYTE_ORDER};" MATCHES ";(BIG_ENDIAN|LITTLE_ENDIAN);")
set(order "${CMAKE_MATCH_1}")
if(order STREQUAL "BIG_ENDIAN")
set("${VARIABLE}" 1 PARENT_SCOPE)
else()
set("${VARIABLE}" 0 PARENT_SCOPE)
endif()
else()
__TEST_BIG_ENDIAN_LEGACY_IMPL(is_big)
set("${VARIABLE}" "${is_big}" PARENT_SCOPE)
endif()
endfunction()
macro(__TEST_BIG_ENDIAN_LEGACY_IMPL VARIABLE)
2015-04-27 22:25:09 +02:00
if(NOT DEFINED HAVE_${VARIABLE})
2020-08-30 11:54:41 +02:00
message(CHECK_START "Check if the system is big endian")
message(CHECK_START "Searching 16 bit integer")
2013-03-16 19:13:01 +02:00
2017-07-20 19:35:53 +02:00
if(CMAKE_C_COMPILER_LOADED)
set(_test_language "C")
elseif(CMAKE_CXX_COMPILER_LOADED)
set(_test_language "CXX")
else()
message(FATAL_ERROR "TEST_BIG_ENDIAN needs either C or CXX language enabled")
endif()
CHECK_TYPE_SIZE("unsigned short" CMAKE_SIZEOF_UNSIGNED_SHORT LANGUAGE ${_test_language})
2013-03-16 19:13:01 +02:00
if(CMAKE_SIZEOF_UNSIGNED_SHORT EQUAL 2)
2020-08-30 11:54:41 +02:00
message(CHECK_PASS "Using unsigned short")
2013-03-16 19:13:01 +02:00
set(CMAKE_16BIT_TYPE "unsigned short")
else()
2017-07-20 19:35:53 +02:00
CHECK_TYPE_SIZE("unsigned int" CMAKE_SIZEOF_UNSIGNED_INT LANGUAGE ${_test_language})
2013-03-16 19:13:01 +02:00
if(CMAKE_SIZEOF_UNSIGNED_INT)
2020-08-30 11:54:41 +02:00
message(CHECK_PASS "Using unsigned int")
2013-03-16 19:13:01 +02:00
set(CMAKE_16BIT_TYPE "unsigned int")
else()
2017-07-20 19:35:53 +02:00
CHECK_TYPE_SIZE("unsigned long" CMAKE_SIZEOF_UNSIGNED_LONG LANGUAGE ${_test_language})
2013-03-16 19:13:01 +02:00
if(CMAKE_SIZEOF_UNSIGNED_LONG)
2020-08-30 11:54:41 +02:00
message(CHECK_PASS "Using unsigned long")
2013-03-16 19:13:01 +02:00
set(CMAKE_16BIT_TYPE "unsigned long")
else()
message(FATAL_ERROR "no suitable type found")
endif()
endif()
endif()
2017-07-20 19:35:53 +02:00
if(_test_language STREQUAL "CXX")
set(_test_file "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/TestEndianess.cpp")
else()
set(_test_file "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/TestEndianess.c")
endif()
2013-03-16 19:13:01 +02:00
configure_file("${CMAKE_ROOT}/Modules/TestEndianess.c.in"
2017-07-20 19:35:53 +02:00
${_test_file}
2014-08-03 19:52:23 +02:00
@ONLY)
2013-03-16 19:13:01 +02:00
2017-07-20 19:35:53 +02:00
file(READ ${_test_file} TEST_ENDIANESS_FILE_CONTENT)
2013-03-16 19:13:01 +02:00
try_compile(HAVE_${VARIABLE}
"${CMAKE_BINARY_DIR}"
2017-07-20 19:35:53 +02:00
${_test_file}
OUTPUT_VARIABLE OUTPUT
COPY_FILE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestEndianess.bin" )
2013-03-16 19:13:01 +02:00
if(HAVE_${VARIABLE})
2013-03-16 19:13:01 +02:00
file(STRINGS "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestEndianess.bin"
CMAKE_TEST_ENDIANESS_STRINGS_LE LIMIT_COUNT 1 REGEX "THIS IS LITTLE ENDIAN")
2013-03-16 19:13:01 +02:00
file(STRINGS "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestEndianess.bin"
CMAKE_TEST_ENDIANESS_STRINGS_BE LIMIT_COUNT 1 REGEX "THIS IS BIG ENDIAN")
# on mac, if there are universal binaries built both will be true
# return the result depending on the machine on which cmake runs
2013-03-16 19:13:01 +02:00
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)
else()
set(CMAKE_TEST_ENDIANESS_STRINGS_BE FALSE)
set(CMAKE_TEST_ENDIANESS_STRINGS_LE TRUE)
endif()
message(STATUS "TEST_BIG_ENDIAN found different results, consider setting CMAKE_OSX_ARCHITECTURES or CMAKE_TRY_COMPILE_OSX_ARCHITECTURES to one or no architecture !")
endif()
if(CMAKE_TEST_ENDIANESS_STRINGS_LE)
set(${VARIABLE} 0 CACHE INTERNAL "Result of TEST_BIG_ENDIAN" FORCE)
2020-08-30 11:54:41 +02:00
message(CHECK_PASS "little endian")
2013-03-16 19:13:01 +02:00
endif()
if(CMAKE_TEST_ENDIANESS_STRINGS_BE)
set(${VARIABLE} 1 CACHE INTERNAL "Result of TEST_BIG_ENDIAN" FORCE)
2020-08-30 11:54:41 +02:00
message(CHECK_PASS "big endian")
2013-03-16 19:13:01 +02:00
endif()
if(NOT CMAKE_TEST_ENDIANESS_STRINGS_BE AND NOT CMAKE_TEST_ENDIANESS_STRINGS_LE)
2020-08-30 11:54:41 +02:00
message(CHECK_FAIL "TEST_BIG_ENDIAN found no result!")
2013-03-16 19:13:01 +02:00
message(SEND_ERROR "TEST_BIG_ENDIAN found no result!")
endif()
2016-07-09 11:21:54 +02:00
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
"Determining if the system is big endian passed with the following output:\n${OUTPUT}\nTestEndianess.c:\n${TEST_ENDIANESS_FILE_CONTENT}\n\n")
2013-03-16 19:13:01 +02:00
else()
2020-08-30 11:54:41 +02:00
message(CHECK_FAIL "failed")
2013-03-16 19:13:01 +02:00
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
"Determining if the system is big endian failed with the following output:\n${OUTPUT}\nTestEndianess.c:\n${TEST_ENDIANESS_FILE_CONTENT}\n\n")
2013-03-16 19:13:01 +02:00
set(${VARIABLE})
endif()
endif()
endmacro()