File: LinuxDistributionInfo.cmake

package info (click to toggle)
projecteur 0.10-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,664 kB
  • sloc: cpp: 11,301; xml: 33; ansic: 11; makefile: 3
file content (64 lines) | stat: -rw-r--r-- 2,206 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# This file is part of Projecteur - https://github.com/jahnf/projecteur - See LICENSE.md and README.md
cmake_minimum_required(VERSION 3.6)

# Try to get the Linux distribution and version as a string (host system)
# When cross compiling this function won't work to get the target distribution.
function(get_linux_distribution VAR_DIST_NAME VAR_DIST_VERSION)
  # Set fallback defaults
  set(DIST_NAME "linux")
  set(DIST_NAME_SET 0)
  set(DIST_VERSION "unknown")
  set(DIST_VERSION_SET 0)

  # Read os- lsb-... release files
  file(GLOB rel_info_files "/etc/*-release")
  foreach(info_file IN LISTS rel_info_files)
    file(STRINGS "${info_file}" file_lines LIMIT_COUNT 128)
    list(APPEND rel_info_all "${file_lines}")
  endforeach()

  # Get distribution id/name - try different keys
  foreach(var ID DISTRIB_ID NAME)
    foreach(line IN LISTS rel_info_all)
      if( "${line}" MATCHES "^${var}=[\"]?([^ \"]*)")
        string(STRIP "${CMAKE_MATCH_1}" DIST_NAME)
        string(TOLOWER "${DIST_NAME}" DIST_NAME)
        string(REPLACE "\\" "_" DIST_NAME "${DIST_NAME}")
        string(REPLACE "/" "_" DIST_NAME "${DIST_NAME}")
        set(DIST_NAME_SET 1)
        break()
      endif()
    endforeach()
    if(DIST_NAME_SET)
      break()
    endif()
  endforeach()

  # Get distribution version/release - try different keys
  foreach(var VERSION_ID DISTRIB_RELEASE VERSION)
    foreach(line IN LISTS rel_info_all)
      if( "${line}" MATCHES "^${var}=[\"]?([^ \"]*)")
        string(STRIP "${CMAKE_MATCH_1}" DIST_VERSION)
        string(TOLOWER "${DIST_VERSION}" DIST_VERSION)
        string(REPLACE "\\" "_" DIST_VERSION "${DIST_VERSION}")
        string(REPLACE "/" "_" DIST_VERSION "${DIST_VERSION}")
        set(DIST_VERSION_SET 1)
        break()
      endif()
    endforeach()
    if(DIST_VERSION_SET)
      break()
    endif()
  endforeach()

  if(NOT DIST_NAME_SET)
    message(STATUS "Could not get linux distribution id, defaulting to 'linux'")
  endif()

  if(NOT DIST_VERSION_SET)
    message(STATUS "Could not get linux version, defaulting to 'unknown'")
  endif()

  set(${VAR_DIST_NAME} "${DIST_NAME}" PARENT_SCOPE)
  set(${VAR_DIST_VERSION} "${DIST_VERSION}" PARENT_SCOPE)
endfunction()