Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 764
voidMainReturn Avatar asked Nov 13 '14 22:11

voidMainReturn


2 Answers

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 ${LSB_RELEASE_EXEC} -is
    OUTPUT_VARIABLE LSB_RELEASE_ID_SHORT
    OUTPUT_STRIP_TRAILING_WHITESPACE
)

On Ubuntu, for example, it yields Ubuntu.

like image 71
thiagowfx Avatar answered Nov 18 '22 22:11

thiagowfx


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: ${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}")

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.

like image 10
MarkHu Avatar answered Nov 19 '22 00:11

MarkHu