File: environment.cmake

package info (click to toggle)
cmake-format 0.6.13-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,436 kB
  • sloc: python: 16,990; makefile: 14
file content (119 lines) | stat: -rw-r--r-- 3,508 bytes parent folder | download | duplicates (4)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Detect whether or not the source tree for this build is the upstream
# monorepository or a sparse export of it. Returns (via `<varname>`) the slug of
# the sparse export, if detected, or `NONE` if this is the monorepo.
#
# Usage:
# ~~~
# detect_sparse_export(<varname>)
# ~~~
function(detect_sparse_export varname)
  if("${TANGENT_BUILD_CONTEXT}" STREQUAL DEBIAN_PACKAGE)
    set(${varname}
        NONE
        PARENT_SCOPE)
    return()
  endif()

  if(EXISTS ${CMAKE_SOURCE_DIR}/.sparse-export)
    file(STRINGS ${CMAKE_SOURCE_DIR}/.sparse-export _sparse_export)
    set(${varname}
        "${_sparse_export}"
        PARENT_SCOPE)
    return()
  endif()

  set(exportsdir ${CMAKE_SOURCE_DIR}/tangent/tooling/sparse-exports/)
  if(EXISTS ${exportsdir}/iamgroot.txt)
    set(${varname}
        "groot"
        PARENT_SCOPE)
    return()
  endif()

  file(
    GLOB _children
    RELATIVE ${exportsdir}
    ${exportsdir}/*)
  list(LENGTH _children num_children)

  if("${num_children}" EQUAL 0)
    message(FATAL_ERROR " Invalid sparse export. ${exportsdir} is empty"
                        " and .sparse-export is  missing")
    return()
  endif()

  if("${num_children}" GREATER 1)
    string(REPLACE ";" "\n  " _children "${_children}")
    message(FATAL_ERROR " Invalid sparse export. ${exportsdir} contains"
                        " too many children:\n  ${_children}")
  endif()

  list(GET _children 0 this_export)
  set(${varname}
      ${this_export}
      PARENT_SCOPE)
endfunction()

# Detect the build environment that is running cmake. Returns in the calling
# scope the following variables:
#
# * IS_TRAVIS_CI - TRUE if this is on travis
# * IS_PULL_REQUEST - TRUE if this is a travis pull request build
# * TANGENT_IS_DEBIAN_BUILD - TRUE if this is a pbuilder/pbuild or launchpad
#   execution
# * TANGENT_SPARSE_EXPORT - set to the slug of the sparse export or `NONE` if
#   this is the monorepo
#
# Usage:
# ~~~
# include(environment.cmake)
# ~~~
set(IS_TRAVIS_CI FALSE)
if("$ENV{CI}" STREQUAL "true"
   AND "$ENV{TRAVIS}" STREQUAL "true")
  set(IS_TRAVIS_CI TRUE CACHE BOOL "This build is on travis" FORCE)
endif()

set(IS_PULL_REQUEST FALSE)
if(DEFINED ENV{TRAVIS_PULL_REQUEST} AND "$ENV{TRAVIS_PULL_REQUEST}" STREQUAL
                                        "true")
  set(IS_PULL_REQUEST TRUE)
endif()

set(TANGENT_IS_DEBIAN_BUILD FALSE)
if("${TANGENT_BUILD_CONTEXT}" STREQUAL DEBIAN_PACKAGE)
  set(TANGENT_IS_DEBIAN_BUILD TRUE)
endif()

detect_sparse_export(_sparse_export)
set(TANGENT_SPARSE_EXPORT "${_sparse_export}")

foreach(key ID RELEASE CODENAME DESCRIPTION)
  unset("BUILDENV_DISTRIB_${key}")
endforeach()
if(EXISTS "/etc/lsb-release")
  file(STRINGS "/etc/lsb-release" _lines)
  foreach(line ${_lines})
    if("${line}" MATCHES "^([^=]+)=(.+)")
      # cmake-lint: disable=C0103
      set("BUILDENV_${CMAKE_MATCH_1}" "${CMAKE_MATCH_2}")
      set_property(GLOBAL PROPERTY "BUILDENV_${CMAKE_MATCH_1}"
                                   "${CMAKE_MATCH_2}")
    endif()
  endforeach()
else()
  message(WARNING "Can't query lsb-release")
endif()

unset(BUILDENV_DPKG_ARCHITECTURE)
execute_process(
  COMMAND dpkg --print-architecture
  RESULT_VARIABLE _returncode
  OUTPUT_VARIABLE _this_arch
  ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
if(_returncode EQUAL 0)
  set(BUILDENV_DPKG_ARCHITECTURE "${_this_arch}")
  set_property(GLOBAL PROPERTY BUILDENV_DPKG_ARCHITECTURE "${_this_arch}")
else()
  message(WARNING "Failed to query current distribution architecture")
endif()