CMake Cookbook
上QQ阅读APP看书,第一时间看更新

How it works

CMake defines the CMAKE_HOST_SYSTEM_PROCESSOR variable to contain the name of the processor it is currently running on. This can be set to "i386", "i686", "x86_64", "AMD64", and such like, depending, of course, on the CPU at hand. CMAKE_SIZEOF_VOID_P is defined to hold the size of a pointer to the void type. We can query both at the CMake level in order to modify targets or target compile definitions. Using preprocessor definitions, we can branch source code compilation based on the detected host processor architecture. As discussed in previous recipes, such customization should be avoided when writing new code, but sometimes it is useful when working with legacy code or when cross-compiling, which is the subject of Chapter 13, Alternative Generators and Cross-compilation.

Using CMAKE_SIZEOF_VOID_P is the only truly portable way of checking whether the CPU at hand has a 32- or 64-bit architecture.