Cmake test if linux

Detect underlying platform/flavour in Cmake

Does anybody know any cmake variable or hook or something which can give me underlying platform name/flavour name on which it is getting executed ? e.g. Linux-CentOs Linux-Ubuntu Linux-SLES I know cmake has «CMAKE_SYSTEM» variable but that doesn’t help differentiating flavours of linux for e.g. Any help is appreciated. edit : I just read that it can be done using lsb_release command ?

7 Answers 7

The following snippet populates the LSB_RELEASE_ID_SHORT cmake variable with information about the underlying Linux system:

find_program(LSB_RELEASE_EXEC lsb_release) execute_process(COMMAND $ -is OUTPUT_VARIABLE LSB_RELEASE_ID_SHORT OUTPUT_STRIP_TRAILING_WHITESPACE ) 

On Ubuntu, for example, it yields Ubuntu .

Different distros put the same things in different places. This was the perfect way to include libraries using those distro-specific locations.

Do you have the lsb_release binary installed in your system? On Arch, for example, its package is called lsb-release .

Slightly less convoluted than checking files on the filesystem is to deduce the best you can from the available CMAKE_SYSTEM vars. For instance a CMakeLists.txt file containing lines like this:

message("-- CMAKE_SYSTEM_INFO_FILE: $") message("-- CMAKE_SYSTEM_NAME: $") message("-- CMAKE_SYSTEM_PROCESSOR: $") message("-- CMAKE_SYSTEM: $") string (REGEX MATCH "\\.el3" os_version_suffix $) message("-- os_version_suffix: $") 

outputs this when I ran cmake . :

-- CMAKE_SYSTEM_INFO_FILE: Platform/Linux -- CMAKE_SYSTEM_NAME: Linux -- CMAKE_SYSTEM_PROCESSOR: x86_64 -- CMAKE_SYSTEM: Linux-2.6.32-573.7.1.el6.x86_64 -- os_version_suffix: .el6 

And for my situation, the .el6 was enough to differentiate.

However depending on your actual needs the test may be quite complex. For example Ubuntu as a Debian-based OS always has /etc/debian_version and many RPM-based OSes traditionally have /etc/redhat-release . There’s a file /etc/os-release in the Linux Standard Base (LSB) specification, but for example on the localhost this file is empty for an unknown reason 🙂

Читайте также:  Настройка динамической маршрутизации linux

yeah the problem is quite complex and I am thinking of manually setting up the variable but just wanted to see if theree are any solutions out there.

Well, one of the possible implementation I’ve given above. For SuSE-based you may also look at /etc/SuSE-release Another possible solution is to detect «main flavours» (e.g. whether its RPM -, DEB — or -based and then call a detected package manager and look for a specific package as described here

I know this is an old question, but as of now, there is still no cmake built-in function to find this information in good detail. I’ve implemented a small utility function that uses lsb_release on Linux to find a number of relevant system details:

function(get_linux_lsb_release_information) find_program(LSB_RELEASE_EXEC lsb_release) if(NOT LSB_RELEASE_EXEC) message(FATAL_ERROR "Could not detect lsb_release executable, can not gather required information") endif() execute_process(COMMAND "$" --short --id OUTPUT_VARIABLE LSB_RELEASE_ID_SHORT OUTPUT_STRIP_TRAILING_WHITESPACE) execute_process(COMMAND "$" --short --release OUTPUT_VARIABLE LSB_RELEASE_VERSION_SHORT OUTPUT_STRIP_TRAILING_WHITESPACE) execute_process(COMMAND "$" --short --codename OUTPUT_VARIABLE LSB_RELEASE_CODENAME_SHORT OUTPUT_STRIP_TRAILING_WHITESPACE) set(LSB_RELEASE_ID_SHORT "$" PARENT_SCOPE) set(LSB_RELEASE_VERSION_SHORT "$" PARENT_SCOPE) set(LSB_RELEASE_CODENAME_SHORT "$" PARENT_SCOPE) endfunction() 

Add it to your CMakeLists.txt and use it like this:

if(CMAKE_SYSTEM_NAME MATCHES "Linux") get_linux_lsb_release_information() message(STATUS "Linux $ $ $") endif() 

If you need further details, check what else lsb_release can provide with lsb_release -a .

Note that not every Linux has lsb_release installed. Most systems provide it, but its not mandatory. On newer Ubuntu, for example, its the default on desktop installs, and required by ubuntu-minimal . If it should be missing on your machine, you can install it with sudo apt install lsb-release .

Источник

OpenGuru Weblog

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.

Читайте также:  Usb linux installer universal usb installer

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.

IF($ MATCHES "Darwin") # Mac OS X specific code SET(OperatingSystem "Mac OS X") ENDIF($ MATCHES "Darwin")
IF($ MATCHES "Linux") # Linux specific code SET(OperatingSystem "Linux") ENDIF($ 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.
Читайте также:  Wsl windows subsystem linux

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»)

IF($ MATCHES "Darwin") # Mac OS X specific code SET(OperatingSystem "Mac OS X") IF($ MATCHES "Intel") # Intel Mac OS X specific code SET(Processor "Intel") ENDIF($ MATCHES "Intel") ENDIF($ MATCHES "Darwin")

Источник

Оцените статью
Adblock
detector