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 APPLE 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.

Alternate method to detect to Operating System:

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

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.
Variable Value Meaning
MINGW TRUE MinGW compiler in Windows.
MSYS TRUE MSYS developer environment in Windows.
BORLAND TRUE Borland compiler in Windows.
WATCOM TRUE Open Watcom compiler in Windows.
MSVC, MSVC_IDE, MSVC60, MSVC70, MSVC71, MSVC80, CMAKE_COMPILER_2005, MSVC90 TRUE Microsoft compiler.
CMAKE_COMPILER_IS_GNUCC TRUE compiler is a variant of GCC.
CMAKE_COMPILER_IS_GNUCXX TRUE compiler is a variant of g++.
CYGWIN TRUE 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:

Technorati tags: , .

5 comments:

Anonymous said...

Thank you for this post. It helped me :)

Anonymous said...

Thank you, this was helpful.

Bernhard Kornberger said...

Thank you very much!

I'd like to add a hint: At least in CMake 2.8.3 the system variables are empty if used before the the project("...") command.

thoni56 said...

Note that since 2.8.4 Cmake does not define WIN32 on Cygwin.

khyox said...

Thanks!

Another hint: while the CMAKE_SYSTEM, etc... variables are empty if used before the PROJECT command (as Bernhard posted -thanks!), the variables like APPLE are available for you to use.