Base platform / flavor detection in Cmake

Does anyone know any cmake or hook variable or something that can give me the base name of the name / flavor of the platform on which it runs? e.g. Linux-CentOS Linux-Ubuntu Linux-SLES

I know that cmake has the variable "CMAKE_SYSTEM", but that does not help differentiate linux flavors, for example. Any help is appreciated.

edit: I just read that this can be done with the lsb_release command?

+5
source share
6 answers

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

 find_program(LSB_RELEASE lsb_release) execute_process(COMMAND ${LSB_RELEASE} -is OUTPUT_VARIABLE LSB_RELEASE_ID_SHORT OUTPUT_STRIP_TRAILING_WHITESPACE ) 

In Ubuntu, for example, it gives Ubuntu .

+10
source

You probably need to write such a test yourself. Here is one possible example, just googled: https://htcondor-wiki.cs.wisc.edu/index.cgi/fileview?f=build/cmake/FindLinuxPlatform.cmake&v=4592599fecc08e5588c4244e2b0ceb7d32363a56

However, depending on your actual needs, the test can be quite complicated. For example, Ubuntu, as a Debian-based operating system, always has /etc/debian_version , and many RPM-based OSs traditionally have /etc/redhat-release . There the /etc/os-release file is in the Linux Standard Base Specification (LSB), but, for example, on localhost this file is empty for some unknown reason :)

+3
source

Slightly less confusing than checking files in the file system is to get everything out of the available CMAKE_SYSTEM vars. For example, a CMakeLists.txt file containing the following lines:

 message("-- CMAKE_SYSTEM_INFO_FILE: ${CMAKE_SYSTEM_INFO_FILE}") message("-- CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}") message("-- CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}") message("-- CMAKE_SYSTEM: ${CMAKE_SYSTEM}") string (REGEX MATCH "\\.el[1-9]" os_version_suffix ${CMAKE_SYSTEM}) message("-- os_version_suffix: ${os_version_suffix}") 

prints 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 .el6 was enough to distinguish.

+2
source

on my car

 CMAKE_SYSTEM_INFO_FILE == "Platform/Linux" CMAKE_SYSTEM_NAME == "Linux" CMAKE_SYSTEM == "Linux-<kernel version>" 

obtained with cmake --system-information , I know people who use the specified macros in their CMakeLists.txt files, so they work as expected, maybe CMAKE_SYSTEM_NAME is what you really want, but here you go, you get this is 3 and to check the properties of your device before cmake .

+1
source

Not. As of 2015, CMake apparently can't even tell you if it works on Ubuntu.

CMake provided us with a standard language for writing assembly specifications for garbage procedures, but, in my opinion, it did not cope with all the objectives arising from this. It can't even properly pack ~ for Ubuntu; some poor attendant seems to be doing this manually.

0
source

Based on thiagowfx answer , if you want to get the distribution code name (if available):

 execute_process(COMMAND lsb_release -cs OUTPUT_VARIABLE RELEASE_CODENAME OUTPUT_STRIP_TRAILING_WHITESPACE ) 

eg. in Ubuntu 14.04, the RELEASE_CODENAME variable will contain trusty .

0
source

Source: https://habr.com/ru/post/1206841/


All Articles