2023-11-01 06:11:15 +08:00
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
#[=======================================================================[.rst:
T e s t B i g E n d i a n
- - - - - - - - - - - - -
2023-11-01 08:44:24 +08:00
. . d e p r e c a t e d : : 3 . 2 0
2023-11-01 06:11:15 +08:00
2023-11-01 08:44:24 +08:00
S u p s e r s e d e d b y t h e : v a r i a b l e : ` C M A K E _ < L A N G > _ B Y T E _ O R D E R ` v a r i a b l e .
2023-11-01 06:11:15 +08:00
2023-11-01 08:44:24 +08:00
C h e c k i f t h e t a r g e t a r c h i t e c t u r e i s b i g e n d i a n o r l i t t l e e n d i a n .
. . c o m m a n d : : t e s t _ b i g _ e n d i a n
. . c o d e - b l o c k : : c m a k e
test_big_endian ( <var> )
S t o r e s i n v a r i a b l e ` ` < v a r > ` ` e i t h e r 1 o r 0 i n d i c a t i n g w h e t h e r t h e
t a r g e t a r c h i t e c t u r e i s b i g o r l i t t l e e n d i a n .
2023-11-01 06:11:15 +08:00
#]=======================================================================]
2023-11-01 08:44:24 +08:00
include_guard ( )
2023-11-01 06:11:15 +08:00
include ( CheckTypeSize )
2023-11-01 08:44:24 +08: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 )
2023-11-01 06:11:15 +08:00
if ( NOT DEFINED HAVE_ ${ VARIABLE } )
2023-11-01 06:34:31 +08:00
message ( CHECK_START "Check if the system is big endian" )
message ( CHECK_START "Searching 16 bit integer" )
2023-11-01 06:11:15 +08: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 } )
if ( CMAKE_SIZEOF_UNSIGNED_SHORT EQUAL 2 )
2023-11-01 06:34:31 +08:00
message ( CHECK_PASS "Using unsigned short" )
2023-11-01 06:11:15 +08:00
set ( CMAKE_16BIT_TYPE "unsigned short" )
else ( )
CHECK_TYPE_SIZE ( "unsigned int" CMAKE_SIZEOF_UNSIGNED_INT LANGUAGE ${ _test_language } )
if ( CMAKE_SIZEOF_UNSIGNED_INT )
2023-11-01 06:34:31 +08:00
message ( CHECK_PASS "Using unsigned int" )
2023-11-01 06:11:15 +08:00
set ( CMAKE_16BIT_TYPE "unsigned int" )
else ( )
CHECK_TYPE_SIZE ( "unsigned long" CMAKE_SIZEOF_UNSIGNED_LONG LANGUAGE ${ _test_language } )
if ( CMAKE_SIZEOF_UNSIGNED_LONG )
2023-11-01 06:34:31 +08:00
message ( CHECK_PASS "Using unsigned long" )
2023-11-01 06:11:15 +08:00
set ( CMAKE_16BIT_TYPE "unsigned long" )
else ( )
message ( FATAL_ERROR "no suitable type found" )
endif ( )
endif ( )
endif ( )
if ( _test_language STREQUAL "CXX" )
2023-11-01 08:44:24 +08:00
set ( _test_file TestEndianess.cpp )
2023-11-01 06:11:15 +08:00
else ( )
2023-11-01 08:44:24 +08:00
set ( _test_file TestEndianess.c )
2023-11-01 06:11:15 +08:00
endif ( )
2023-11-01 08:44:24 +08:00
file ( READ "${CMAKE_ROOT}/Modules/TestEndianess.c.in" TEST_ENDIANESS_FILE_CONTENT )
string ( CONFIGURE "${TEST_ENDIANESS_FILE_CONTENT}" TEST_ENDIANESS_FILE_CONTENT @ONLY )
2023-11-01 06:11:15 +08:00
try_compile ( HAVE_ ${ VARIABLE }
2023-11-01 08:44:24 +08:00
S O U R C E _ F R O M _ V A R " $ { _ t e s t _ f i l e } " T E S T _ E N D I A N E S S _ F I L E _ C O N T E N T
2023-11-01 06:11:15 +08:00
C O P Y _ F I L E " $ { C M A K E _ B I N A R Y _ D I R } $ { C M A K E _ F I L E S _ D I R E C T O R Y } / T e s t E n d i a n e s s . b i n " )
if ( HAVE_ ${ VARIABLE } )
file ( STRINGS "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestEndianess.bin"
C M A K E _ T E S T _ E N D I A N E S S _ S T R I N G S _ L E L I M I T _ C O U N T 1 R E G E X " T H I S I S L I T T L E E N D I A N " )
file ( STRINGS "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestEndianess.bin"
C M A K E _ T E S T _ E N D I A N E S S _ S T R I N G S _ B E L I M I T _ C O U N T 1 R E G E X " T H I S I S B I G E N D I A N " )
# on mac, if there are universal binaries built both will be true
# return the result depending on the machine on which cmake runs
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 )
2023-11-01 06:34:31 +08:00
message ( CHECK_PASS "little endian" )
2023-11-01 06:11:15 +08:00
endif ( )
if ( CMAKE_TEST_ENDIANESS_STRINGS_BE )
set ( ${ VARIABLE } 1 CACHE INTERNAL "Result of TEST_BIG_ENDIAN" FORCE )
2023-11-01 06:34:31 +08:00
message ( CHECK_PASS "big endian" )
2023-11-01 06:11:15 +08:00
endif ( )
if ( NOT CMAKE_TEST_ENDIANESS_STRINGS_BE AND NOT CMAKE_TEST_ENDIANESS_STRINGS_LE )
2023-11-01 06:34:31 +08:00
message ( CHECK_FAIL "TEST_BIG_ENDIAN found no result!" )
2023-11-01 06:11:15 +08:00
message ( SEND_ERROR "TEST_BIG_ENDIAN found no result!" )
endif ( )
else ( )
2023-11-01 06:34:31 +08:00
message ( CHECK_FAIL "failed" )
2023-11-01 06:11:15 +08:00
set ( ${ VARIABLE } )
endif ( )
endif ( )
endmacro ( )