问题
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 ?
回答1:
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
.
回答2:
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.
回答3:
Likely, you have to write such a test yourself. Here's one of the possible examples, 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 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 :)
回答4:
on my machine
CMAKE_SYSTEM_INFO_FILE == "Platform/Linux"
CMAKE_SYSTEM_NAME == "Linux"
CMAKE_SYSTEM == "Linux-<kernel version>"
obtained with cmake --system-information
, I know of people that use said macros in their own CMakeLists.txt
files so they work as expected, probably CMAKE_SYSTEM_NAME
is what you really want but here you go, you get this 3 and the command to inspect the properties of your machine as far as cmake
is concerned .
回答5:
No. As of 2015, CMake apparently still can't even tell you if it's running on Ubuntu.
CMake has given us a standard language in which to write garbage procedural platform-specific build specifications, but in my opinion it has failed at all of it's goals beyond that. It can't even package ~itself properly for Ubuntu; some poor maintainer is apparently doing that by hand.
回答6:
Based on thiagowfx answer, If you want to get the codename of the distro (if it is available):
execute_process(COMMAND lsb_release -cs
OUTPUT_VARIABLE RELEASE_CODENAME
OUTPUT_STRIP_TRAILING_WHITESPACE
)
E.g. in Ubuntu 14.04 the variable RELEASE_CODENAME
will hold trusty
.
来源:https://stackoverflow.com/questions/26919334/detect-underlying-platform-flavour-in-cmake