【发布时间】:2019-08-05 13:10:10
【问题描述】:
我需要在 CentOS 而不是 Fedora 上构建。我见过以前的post,但它只检查它是 Windows、Linux 还是其他平台。
【问题讨论】:
标签: cmake
我需要在 CentOS 而不是 Fedora 上构建。我见过以前的post,但它只检查它是 Windows、Linux 还是其他平台。
【问题讨论】:
标签: cmake
检查/etc/os-release 的内容是否有效?
https://www.freedesktop.org/software/systemd/man/os-release.html
您可以直接读入文件,它会包含类似NAME="Ubuntu" 的行。
使用file() 解析它并获取NAME 字段。
file(STRINGS /etc/os-release distro REGEX "^NAME=")
string(REGEX REPLACE "NAME=\"(.*)\"" "\\1" distro "${distro}")
file(STRINGS /etc/os-release disversion REGEX "^VERSION_ID=")
string(REGEX REPLACE "VERSION_ID=\"(.*)\"" "\\1" disversion "${disversion}")
message("found ${distro}.${disversion}")
输出
found Ubuntu.20.04
【讨论】:
file(STRINGS /etc/os-release distro REGEX "^NAME=") string(REGEX REPLACE "NAME=\"(.*)\"" "\\1" distro "${distro}") if( ${distro} STREQUAL "CentOS Linux" ) ... BUILD INSTRUCTIONS ... endif()