2005-12-14 21:51:08 +03:00
# - Define macro to determine endian type
2002-10-01 23:56:18 +04:00
# Check if the system is big endian or little endian
2005-12-14 21:51:08 +03:00
# TEST_BIG_ENDIAN(VARIABLE)
# VARIABLE - variable to store the result to
2002-10-01 23:56:18 +04:00
#
2009-09-28 19:46:51 +04:00
#=============================================================================
# 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.
#=============================================================================
2010-08-07 04:48:47 +04:00
# (To distribute this file outside of CMake, substitute the full
2009-09-28 19:46:51 +04:00
# License text for the above reference.)
2012-08-13 21:47:32 +04:00
macro ( TEST_BIG_ENDIAN VARIABLE )
if ( "HAVE_${VARIABLE}" MATCHES "^HAVE_${VARIABLE}$" )
message ( STATUS "Check if the system is big endian" )
message ( STATUS "Searching 16 bit integer" )
2012-08-13 21:42:58 +04:00
2012-08-13 21:47:32 +04:00
include ( CheckTypeSize )
2007-08-10 21:14:00 +04:00
CHECK_TYPE_SIZE ( "unsigned short" CMAKE_SIZEOF_UNSIGNED_SHORT )
2012-08-13 21:47:32 +04:00
if ( CMAKE_SIZEOF_UNSIGNED_SHORT EQUAL 2 )
message ( STATUS "Using unsigned short" )
set ( CMAKE_16BIT_TYPE "unsigned short" )
2012-08-13 21:50:14 +04:00
else ( )
2007-08-10 21:14:00 +04:00
CHECK_TYPE_SIZE ( "unsigned int" CMAKE_SIZEOF_UNSIGNED_INT )
2012-08-13 21:47:32 +04:00
if ( CMAKE_SIZEOF_UNSIGNED_INT )
message ( STATUS "Using unsigned int" )
set ( CMAKE_16BIT_TYPE "unsigned int" )
2007-08-10 21:14:00 +04:00
2012-08-13 21:50:14 +04:00
else ( )
2012-08-13 21:42:58 +04:00
2007-08-10 21:14:00 +04:00
CHECK_TYPE_SIZE ( "unsigned long" CMAKE_SIZEOF_UNSIGNED_LONG )
2012-08-13 21:47:32 +04:00
if ( CMAKE_SIZEOF_UNSIGNED_LONG )
message ( STATUS "Using unsigned long" )
set ( CMAKE_16BIT_TYPE "unsigned long" )
2012-08-13 21:50:14 +04:00
else ( )
2012-08-13 21:47:32 +04:00
message ( FATAL_ERROR "no suitable type found" )
2012-08-13 21:50:14 +04:00
endif ( )
2012-08-13 21:42:58 +04:00
2012-08-13 21:50:14 +04:00
endif ( )
2012-08-13 21:42:58 +04:00
2012-08-13 21:50:14 +04:00
endif ( )
2007-08-10 21:14:00 +04:00
2012-08-13 21:42:58 +04:00
2012-08-13 21:47:32 +04:00
configure_file ( "${CMAKE_ROOT}/Modules/TestEndianess.c.in"
2007-08-10 21:14:00 +04:00
" $ { 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 } / C M a k e T m p / T e s t E n d i a n e s s . c "
I M M E D I A T E @ O N L Y )
2012-08-13 21:42:58 +04:00
2012-08-13 21:47:32 +04:00
file ( READ "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/TestEndianess.c"
2007-08-10 21:14:00 +04:00
T E S T _ E N D I A N E S S _ F I L E _ C O N T E N T )
2007-08-13 19:04:19 +04:00
2012-08-13 21:47:32 +04:00
try_compile ( HAVE_ ${ VARIABLE }
2007-08-10 21:14:00 +04:00
" $ { C M A K E _ B I N A R Y _ D I R } "
" $ { 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 } / C M a k e T m p / T e s t E n d i a n e s s . c "
O U T P U T _ V A R I A B L E O U T P U T
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 " )
2012-08-13 21:47:32 +04:00
if ( HAVE_ ${ VARIABLE } )
2007-08-10 21:14:00 +04:00
2012-08-13 21:47:32 +04:00
file ( STRINGS "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestEndianess.bin"
2007-08-10 21:14:00 +04:00
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 " )
2007-08-13 19:04:19 +04:00
2012-08-13 21:47:32 +04:00
file ( STRINGS "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestEndianess.bin"
2007-08-10 21:14:00 +04:00
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 " )
2007-08-13 19:04:19 +04:00
# on mac, if there are universal binaries built both will be true
# return the result depending on the machine on which cmake runs
2012-08-13 21:47:32 +04: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 )
2012-08-13 21:50:14 +04:00
else ( )
2012-08-13 21:47:32 +04:00
set ( CMAKE_TEST_ENDIANESS_STRINGS_BE FALSE )
set ( CMAKE_TEST_ENDIANESS_STRINGS_LE TRUE )
2012-08-13 21:50:14 +04:00
endif ( )
2012-08-13 21:47:32 +04:00
message ( STATUS "TEST_BIG_ENDIAN found different results, consider setting CMAKE_OSX_ARCHITECTURES or CMAKE_TRY_COMPILE_OSX_ARCHITECTURES to one or no architecture !" )
2012-08-13 21:50:14 +04:00
endif ( )
2012-08-13 21:47:32 +04:00
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" )
2012-08-13 21:50:14 +04:00
endif ( )
2012-08-13 21:47:32 +04:00
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" )
2012-08-13 21:50:14 +04:00
endif ( )
2012-08-13 21:47:32 +04:00
if ( NOT CMAKE_TEST_ENDIANESS_STRINGS_BE AND NOT CMAKE_TEST_ENDIANESS_STRINGS_LE )
message ( SEND_ERROR "TEST_BIG_ENDIAN found no result!" )
2012-08-13 21:50:14 +04:00
endif ( )
2012-08-13 21:47:32 +04:00
file ( APPEND ${ CMAKE_BINARY_DIR } ${ CMAKE_FILES_DIRECTORY } /CMakeError.log
2007-08-10 21:14:00 +04:00
" D e t e r m i n i n g i f t h e s y s t e m i s b i g e n d i a n p a s s e d w i t h t h e f o l l o w i n g o u t p u t : \ n $ { O U T P U T } \ n T e s t E n d i a n e s s . c : \ n $ { T E S T _ E N D I A N E S S _ F I L E _ C O N T E N T } \ n \ n " )
2012-08-13 21:42:58 +04:00
2012-08-13 21:50:14 +04:00
else ( )
2012-08-13 21:47:32 +04:00
message ( STATUS "Check if the system is big endian - failed" )
file ( APPEND ${ CMAKE_BINARY_DIR } ${ CMAKE_FILES_DIRECTORY } /CMakeError.log
2007-08-10 21:14:00 +04:00
" D e t e r m i n i n g i f t h e s y s t e m i s b i g e n d i a n f a i l e d w i t h t h e f o l l o w i n g o u t p u t : \ n $ { O U T P U T } \ n T e s t E n d i a n e s s . c : \ n $ { T E S T _ E N D I A N E S S _ F I L E _ C O N T E N T } \ n \ n " )
2012-08-13 21:47:32 +04:00
set ( ${ VARIABLE } )
2012-08-13 21:50:14 +04:00
endif ( )
endif ( )
endmacro ( )
2007-08-10 21:14:00 +04:00