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

Bill Hoffman bill.hoffman at kitware.com
Wed Nov 22 14:14:18 EST 2006


Sean McBride wrote:
> On 2006-11-22 19:00, Eric Noulard said:
>
>   
>> Sorry for my dummy answer:
>>
>> 1) There is a TestBigEndian.cmake already shipped with CMake (as of 2.4.3)
>> 2) I should have checked ENDIANESS_RESULT and not
>>    ENDIANESS_OUT
>>     
>
> At the risk of repeating myself... :)
>
> Remember that using TRY_COMPILE/TRY_RUN or any method that runs code on
> the build machine breaks cross-compilation.  Notably, if you want to
> build a Universal Binary for Mac OS X, you guarantee that it will not
> work on one architecture.  Maybe it's not important for some people, but
> it's good to be aware of it.
>
> Since a compiler knows what CPU it is compiling for (by definition), I
> think a better solution, in general, is to query the compiler.  gcc for
> example always #defines __BIG_ENDIAN__ or __LITTLE_ENDIAN__ as
> appropriate.  Other compilers probably do likewise, but I am not aware
> of a compiler independent way to determine this, which is very unfortunate.
>
> Also, with posix, there are APIs like ntohl() that always do the right
> thing.  On some systems, sys/param.h indicates endianness.
>
>   

The test itself is not so bad, and autoconf does tests like this as 
well.   The problem is that gcc may define
this stuff, but if you are going to work with any compiler, there is no 
standard for it.   When cross compiling all
try-runs should be invalidated, and the build system should get the 
answer some other way.    However,
TRY_COMPILE is always safe.  It either compiles or it does not, it does 
not break anything with cross compiling.
It is when you run things that you can get into trouble.   For the mac, 
I would recommend a try_run, but then
adding a special bit of ifdef MAC stuff to the configured .h file.

#cmakedefine CMAKE_WORDS_BIGENDIAN
#ifdef MAC
#ifndef __BIG_ENDIAN__
#undef CMAKE_WORDS_BIGENDIAN
#endif  // end if big endian
#endif  // end if mac




More information about the CMake mailing list