[CMake] Finding out the Endianness of a system at CMake config time

Brandon J. Van Every bvanevery at gmail.com
Wed Nov 22 13:15:29 EST 2006


Eric Noulard wrote:
> May be you could use TRY_RUN to detect endianity
> with something like:
>
> TRY_RUN(
>  ENDIANESS_RESULT
>  ENDIANESS_COMPILE
>  /tmp/
>  ${CMAKE_CURRENT_SOURCE_DIR}/make/endianess.c
>  OUTPUT_VARIABLE ENDIANESS_OUT
>  )
>
>
> MESSAGE(STATUS "ENDIANITY is :${ENDIANESS_OUT}")
>
> endianess.c is attached.
>
> The trouble I have is that I expected to have
> my "endianess" program output in ${ENDIANESS_OUT}
> but I get too much including
> compilation process ouput.

The format is:

TRY_RUN(RUN_RESULT_VAR COMPILE_RESULT_VAR
bindir srcfile <CMAKE_FLAGS <Flags>>
<COMPILE_DEFINITIONS <flags>>
<OUTPUT_VARIABLE var>
<ARGS <arg1> <arg2>..


Write a test called IsBigEndian.c.  When run, have it return either 0 or 
1.  Get the result with RUN_RESULT_VAR.  Use it and be happy.  Caveat: 0 
is typically regarded as a *success* code when running a system tool, so 
go over your bit twiddling convention carefully.  I can't remember what 
I did in Chicken's StackGrowsDownward.c, only that Felix caught me doing 
it backwards and I was surprised.

Use COMPILER_RESULT_VAR to make sure your code actually worked, and 
throw errors if it doesn't.  Your code should always compile; this would 
be a strange / severe / exceptional error.

OUTPUT_VARIABLE isn't useful for this kind of problem.  It gives you all 
the stdout stuff and you don't want that.

A complete working example of this kind of code is available in the 
Chicken CMake build.  Search CMakeLists.txt for STACK_GROWS_DOWNWARD.  
http://www.call-with-current-continuation.org .  I think I'll document 
it more, so that the return code conventions are less confusing.


Cheers,
Brandon Van Every




More information about the CMake mailing list