Monday, April 13, 2009

CMake: Detecting Platform/Operating Systems, Compiler Information

I was searching for this information from last three days. Finally I found the CMake syntax to write the platform specific code inside CMakeLists.txt.

Detecting the Operating System:

CMake actually defines several variables to identify the platform information. These variables will be assigned with the values based on the platform, operating system etc.

For example, In CMake version 2.6 following Variables are present to identify the Operating System Type.

UNIX
     is TRUE on all UNIX-like OS's, including Apple OS X and CygWin
WIN32
     is TRUE on Windows, including CygWin
APPLE
     is TRUE on Apple systems. Note: Having this variable value set to TRUE does not necessarily mean that Operating System is Mac OS X. It only means that in the C/C++ header file __APPLE__ Macro is defined. Use the alternate method of detecting Operating System type, mentioned below instead.

For GNU/Linux: There is NO Variable for GNU/Linux. ie; IF(LINUX) does NOT work. Use the alternative method mentioned below instead.

Alternative method to detect to Operating System:

CMake version 2.6 defines 2 different variables, which can be used to detect the Operating System preciously. They are,

CMAKE_SYSTEM

     the complete system name, e.g. "Linux-2.4.22", "FreeBSD-5.4-RELEASE" or "Windows 5.1"
CMAKE_SYSTEM_NAME
     the short system name, e.g. "Linux", "FreeBSD" or "Windows"

These variables can be used to detect the Operating System.

Example:

IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
# Mac OS X specific code
     SET(OperatingSystem "Mac OS X")
ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")


for detecting Mac OS X

and for Linux

IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
# Linux specific code
     SET(OperatingSystem "Linux")
ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")


Detecting the compiler:

Following variables can be used to detect the compilers.

MINGW
     is TRUE when using the MinGW compiler in Windows
MSYS
     is TRUE when using the MSYS developer environment in Windows
BORLAND
     is TRUE on Windows when using a Borland compiler
WATCOM
     is TRUE on Windows when using the Open Watcom compiler
MSVC, MSVC_IDE, MSVC60, MSVC70, MSVC71, MSVC80, CMAKE_COMPILER_2005, MSVC90
     Microsoft compiler
CMAKE_COMPILER_IS_GNUCC
     is TRUE if the compiler is a variant of gcc
CMAKE_COMPILER_IS_GNUCXX
     is TRUE if the compiler is a variant of g++
CYGWIN
     is TRUE on Windows when using the CygWin version of cmake

Detecting the System Processor:

Following variable can be used to detect the processor family.

CMAKE_SYSTEM_PROCESSOR
     the processor name (e.g. "Intel(R) Pentium(R) M processor 2.00GHz")

Example usage:

IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
     # Mac OS X specific code
     SET(OperatingSystem "Mac OS X")

     IF(${CMAKE_SYSTEM_PROCESSOR } MATCHES "Intel")
         # Intel Mac OS X specific code
         SET(Processor "Intel")
     ENDIF(${CMAKE_SYSTEM_PROCESSOR } MATCHES "Intel")

ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")


Links:

0 comments: