1# Copyright (c) 2018, Intel Corporation 2# 3# Permission is hereby granted, free of charge, to any person obtaining a 4# copy of this software and associated documentation files (the "Software"), 5# to deal in the Software without restriction, including without limitation 6# the rights to use, copy, modify, merge, publish, distribute, sublicense, 7# and/or sell copies of the Software, and to permit persons to whom the 8# Software is furnished to do so, subject to the following conditions: 9# 10# The above copyright notice and this permission notice shall be included 11# in all copies or substantial portions of the Software. 12# 13# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 17# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 18# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 19# OTHER DEALINGS IN THE SOFTWARE. 20 21 22# os_release_info.cmake - Function to dump OS name and version 23 24# This file has no dependencies on other files (e.g., functions or definitions) 25# of the local cmake environment. 26 27if(NOT DEFINED _os_release_info) 28set(_os_release_info TRUE) 29 30# Set cmake policies for at least this level: 31cmake_minimum_required(VERSION 2.8.12) 32 33if(POLICY CMP0054) 34 cmake_policy(SET CMP0054 NEW) 35endif() 36 37 38# Function get_os_release_info - Determine and return OS name and version 39# 40# Args: 41# 1. the name of a variable to receive os_name 42# 2. the name of a variable to receive os_version 43# 44# Return values: (Quotation marks are always stripped). 45# Upon failure, return values are null strings. 46# 47# Examples: 48# os_name os_version 49# -------------- ------- 50# clear-linux-os 21180 (Changes twice daily) 51# ubuntu 12.04 16.04 17.10 18.04 52# fedora 27 53# centos 6.9 7.4.1708 54# 55# Potential sources are tried (in order of preference) until a 56# suitable one is found. 57 58# Implementation documentation: 59# 60# The potential sources, in order, are as follows. 61# - /etc/centos-release 62# Centos 7 also has /etc/os-release. File /etc/os-release is less 63# precise about the Centos version (e.g., "7" instead of "7.4.1708"). 64# For that reason, this file is checked first. 65# Examples: 66# CentOS release 6.9 (Final) 67# CentOS Linux release 7.4.1708 (Core) 68# - /usr/lib/os-release 69# Present for Clear Linux, modern Fedora, and Ubuntu since some time 70# between 14.04 and 16.04. The ID and VERSION_ID values are used. 71# Examples: 72# ID=clear-linux-os VERSION_ID=21180 73# ID=fedora VERSION_ID=27 74# ID=ubuntu VERSION_ID="14.04" 75# ID=ubuntu VERSION_ID="16.04" 76# ID="ubuntu" VERSION_ID="17.10" 77# - /etc/os-release - Same form as (sometimes a link to) /usr/lib/os-release 78# ID="Ubuntu" VERSION_ID="12.04" 79# ID="Ubuntu" VERSION_ID="14.04" 80# with a symbolic link: /etc/os-release -> ../usr/lib/os-release 81# ID="CentOS Linux" VERSION_ID="7" Also: ID_LIKE="rhel fedora" 82# - /etc/lsb-release 83# For Centos, not too meaningful. 84# Other "OS"s are more reasonable: 85# DISTRIB_ID=Ubuntu DISTRIB_RELEASE=12.04 86# DISTRIB_ID=Ubuntu DISTRIB_RELEASE=14.04 87# DISTRIB_ID=Ubuntu DISTRIB_RELEASE=17.10 88 89 90function(get_os_release_info _vn_id _vn_version_id) 91 set(_var_id "") 92 set(_var_version_id "") 93 94 if("${_var_id}" STREQUAL "") 95 set(file_path "/etc/centos-release") 96 if(EXISTS "${file_path}") 97 # Example: CentOS release 6.9 (Final) 98 file(STRINGS "${file_path}" file_list LIMIT_COUNT 1) 99 list(GET file_list 0 file_line) 100 101 # Remove all parenthesized items. 102 string(REGEX REPLACE "\\([^)]+\\)" "" file_line "${file_line}") 103 104 # Extract start and end, discard optional "version" or "release" 105 string(REGEX MATCH "^([A-Za-z0-9_]+)( +(version|release))? +(.*)$" _dummy "${file_line}") 106 # 1 2 3 4 107 108 set(_var_id "${CMAKE_MATCH_1}") 109 set(_var_version_id "${CMAKE_MATCH_4}") 110 endif() 111 endif() 112 113 if("${_var_id}" STREQUAL "") 114 if(EXISTS "/usr/lib/os-release") 115 set(file_path "/usr/lib/os-release") 116 elseif(EXISTS "/etc/os-release") 117 set(file_path "/etc/os-release") 118 else() 119 set(file_path "") 120 endif() 121 122 if(NOT "${file_path}" STREQUAL "") 123 file(STRINGS "${file_path}" data_list REGEX "^(ID|VERSION_ID)=") 124 125 # Look for lines like "ID="..." and VERSION_ID="..." 126 foreach(_var ${data_list}) 127 if("${_var}" MATCHES "^(ID)=(.*)$") 128 set(_var_id "${CMAKE_MATCH_2}") 129 elseif("${_var}" MATCHES "^(VERSION_ID)=(.*)$") 130 set(_var_version_id "${CMAKE_MATCH_2}") 131 endif() 132 endforeach() 133 endif() 134 endif() 135 136 if("${_var_id}" STREQUAL "") 137 set(file_path "/etc/lsb-release") 138 if(EXISTS "${file_path}") 139 file(STRINGS "${file_path}" data_list REGEX "^(DISTRIB_ID|DISTRIB_RELEASE)=") 140 141 # Look for lines like "DISTRIB_ID="..." and DISTRIB_RELEASE="..." 142 foreach(_var ${data_list}) 143 if("${_var}" MATCHES "^(DISTRIB_ID)=(.*)$") 144 set(_var_id "${CMAKE_MATCH_2}") 145 elseif("${_var}" MATCHES "^(DISTRIB_RELEASE)=(.*)$") 146 set(_var_version_id "${CMAKE_MATCH_2}") 147 endif() 148 endforeach() 149 endif() 150 endif() 151 152 string(TOLOWER "${_var_id}" "_var_id") 153 154 string(STRIP "${_var_id}" _var_id) 155 string(STRIP "${_var_version_id}" _var_version_id) 156 157 # Remove any enclosing quotation marks 158 string(REGEX REPLACE "^\"(.*)\"$" "\\1" _var_id "${_var_id}") 159 string(REGEX REPLACE "^\"(.*)\"$" "\\1" _var_version_id "${_var_version_id}") 160 161 if(NOT "${_vn_id}" STREQUAL "") 162 set(${_vn_id} "${_var_id}" PARENT_SCOPE) 163 endif() 164 165 if(NOT "${_vn_version_id}" STREQUAL "") 166 set(${_vn_version_id} "${_var_version_id}" PARENT_SCOPE) 167 endif() 168 169endfunction() 170 171 172endif(NOT DEFINED _os_release_info) 173