|
| 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 | +if(NOT DEFINED _os_release_info) |
| 23 | +set(_os_release_info TRUE) |
| 24 | + |
| 25 | + |
| 26 | +# os_release_info.cmake - Function to dump OS name and version |
| 27 | + |
| 28 | +# This file has no dependencies on other files (e.g., functions or definitions) |
| 29 | +# of the local cmake environment. |
| 30 | + |
| 31 | +# Set cmake policies for at least this level: |
| 32 | +cmake_minimum_required(VERSION 2.8.12) |
| 33 | + |
| 34 | + |
| 35 | +# Function get_os_release_info - Determine and return OS name and version |
| 36 | +# |
| 37 | +# Args: |
| 38 | +# 1. the name of a variable to receive os_name |
| 39 | +# 2. the name of a variable to receive os_version |
| 40 | +# |
| 41 | +# Return values: (Quotation marks are always stripped). |
| 42 | +# Upon failure, return values are null strings. |
| 43 | +# |
| 44 | +# Examples: |
| 45 | +# os_name os_version |
| 46 | +# -------------- ------- |
| 47 | +# clear-linux-os 21180 (Changes twice daily) |
| 48 | +# ubuntu 12.04 16.04 17.10 18.04 |
| 49 | +# fedora 27 |
| 50 | +# centos 6.9 7.4.1708 |
| 51 | +# |
| 52 | +# Potential sources are tried (in order of preference) until a |
| 53 | +# suitable one is found. |
| 54 | + |
| 55 | +# Implementation documentation: |
| 56 | +# |
| 57 | +# The potential sources, in order, are as follows. |
| 58 | +# - /etc/centos-release |
| 59 | +# Centos 7 also has /etc/os-release. File /etc/os-release is less |
| 60 | +# precise about the Centos version (e.g., "7" instead of "7.4.1708"). |
| 61 | +# For that reason, this file is checked first. |
| 62 | +# Examples: |
| 63 | +# CentOS release 6.9 (Final) |
| 64 | +# CentOS Linux release 7.4.1708 (Core) |
| 65 | +# - /usr/lib/os-release |
| 66 | +# Present for Clear Linux, modern Fedora, and Ubuntu since some time |
| 67 | +# between 14.04 and 16.04. The ID and VERSION_ID values are used. |
| 68 | +# Examples: |
| 69 | +# ID=clear-linux-os VERSION_ID=21180 |
| 70 | +# ID=fedora VERSION_ID=27 |
| 71 | +# ID=ubuntu VERSION_ID="14.04" |
| 72 | +# ID=ubuntu VERSION_ID="16.04" |
| 73 | +# ID="ubuntu" VERSION_ID="17.10" |
| 74 | +# - /etc/os-release - Same form as (sometimes a link to) /usr/lib/os-release |
| 75 | +# ID="Ubuntu" VERSION_ID="12.04" |
| 76 | +# ID="Ubuntu" VERSION_ID="14.04" |
| 77 | +# with a symbolic link: /etc/os-release -> ../usr/lib/os-release |
| 78 | +# ID="CentOS Linux" VERSION_ID="7" Also: ID_LIKE="rhel fedora" |
| 79 | +# - /etc/lsb-release |
| 80 | +# For Centos, not too meaningful. |
| 81 | +# Other "OS"s are more reasonable: |
| 82 | +# DISTRIB_ID=Ubuntu DISTRIB_RELEASE=12.04 |
| 83 | +# DISTRIB_ID=Ubuntu DISTRIB_RELEASE=14.04 |
| 84 | +# DISTRIB_ID=Ubuntu DISTRIB_RELEASE=17.10 |
| 85 | + |
| 86 | + |
| 87 | +function(get_os_release_info _vn_id _vn_version_id) |
| 88 | + set(_var_id "") |
| 89 | + set(_var_version_id "") |
| 90 | + |
| 91 | + if("${_var_id}" STREQUAL "") |
| 92 | + set(file_path "/etc/centos-release") |
| 93 | + if(EXISTS "${file_path}") |
| 94 | + # Example: CentOS release 6.9 (Final) |
| 95 | + file(STRINGS "${file_path}" file_list LIMIT_COUNT 1) |
| 96 | + list(GET file_list 0 file_line) |
| 97 | + |
| 98 | + # Remove all parenthesized items. |
| 99 | + string(REGEX REPLACE "\\([^)]+\\)" "" file_line "${file_line}") |
| 100 | + |
| 101 | + # Extract start and end, discard optional "version" or "release" |
| 102 | + string(REGEX MATCH "^([A-Za-z0-9_]+)( +(version|release))? +(.*)$" _dummy "${file_line}") |
| 103 | + # 1 2 3 4 |
| 104 | + |
| 105 | + set(_var_id "${CMAKE_MATCH_1}") |
| 106 | + set(_var_version_id "${CMAKE_MATCH_4}") |
| 107 | + endif() |
| 108 | + endif() |
| 109 | + |
| 110 | + if("${_var_id}" STREQUAL "") |
| 111 | + if(EXISTS "/usr/lib/os-release") |
| 112 | + set(file_path "/usr/lib/os-release") |
| 113 | + elseif(EXISTS "/etc/os-release") |
| 114 | + set(file_path "/etc/os-release") |
| 115 | + else() |
| 116 | + set(file_path "") |
| 117 | + endif() |
| 118 | + |
| 119 | + if(NOT "${file_path}" STREQUAL "") |
| 120 | + file(STRINGS "${file_path}" data_list REGEX "^(ID|VERSION_ID)=") |
| 121 | + |
| 122 | + # Look for lines like "ID="..." and VERSION_ID="..." |
| 123 | + foreach(_var ${data_list}) |
| 124 | + if("${_var}" MATCHES "^(ID)=(.*)$") |
| 125 | + set(_var_id "${CMAKE_MATCH_2}") |
| 126 | + elseif("${_var}" MATCHES "^(VERSION_ID)=(.*)$") |
| 127 | + set(_var_version_id "${CMAKE_MATCH_2}") |
| 128 | + endif() |
| 129 | + endforeach() |
| 130 | + endif() |
| 131 | + endif() |
| 132 | + |
| 133 | + if("${_var_id}" STREQUAL "") |
| 134 | + set(file_path "/etc/lsb-release") |
| 135 | + if(EXISTS "${file_path}") |
| 136 | + file(STRINGS "${file_path}" data_list REGEX "^(DISTRIB_ID|DISTRIB_RELEASE)=") |
| 137 | + |
| 138 | + # Look for lines like "DISTRIB_ID="..." and DISTRIB_RELEASE="..." |
| 139 | + foreach(_var ${data_list}) |
| 140 | + if("${_var}" MATCHES "^(DISTRIB_ID)=(.*)$") |
| 141 | + set(_var_id "${CMAKE_MATCH_2}") |
| 142 | + elseif("${_var}" MATCHES "^(DISTRIB_RELEASE)=(.*)$") |
| 143 | + set(_var_version_id "${CMAKE_MATCH_2}") |
| 144 | + endif() |
| 145 | + endforeach() |
| 146 | + endif() |
| 147 | + endif() |
| 148 | + |
| 149 | + string(TOLOWER "${_var_id}" "_var_id") |
| 150 | + |
| 151 | + string(STRIP "${_var_id}" _var_id) |
| 152 | + string(STRIP "${_var_version_id}" _var_version_id) |
| 153 | + |
| 154 | + # Remove any enclosing quotation marks |
| 155 | + string(REGEX REPLACE "^\"(.*)\"$" "\\1" _var_id "${_var_id}") |
| 156 | + string(REGEX REPLACE "^\"(.*)\"$" "\\1" _var_version_id "${_var_version_id}") |
| 157 | + |
| 158 | + if(NOT "${_vn_id}" STREQUAL "") |
| 159 | + set(${_vn_id} "${_var_id}" PARENT_SCOPE) |
| 160 | + endif() |
| 161 | + |
| 162 | + if(NOT "${_vn_version_id}" STREQUAL "") |
| 163 | + set(${_vn_version_id} "${_var_version_id}" PARENT_SCOPE) |
| 164 | + endif() |
| 165 | + |
| 166 | +endfunction() |
| 167 | + |
| 168 | + |
| 169 | +endif(NOT DEFINED _os_release_info) |
0 commit comments