functions-rpm.sh 5.19 KB
Newer Older
Markus Frosch's avatar
Markus Frosch committed
1 2
#!/bin/bash

3 4 5
# shellcheck source=functions-common.sh
. "$(dirname "$(readlink -f "$0")")"/functions-common.sh

Markus Frosch's avatar
Markus Frosch committed
6
detect_project() {
7
  IFS=" " read -r -a specs <<< "$(ls ./*.spec)"
Markus Frosch's avatar
Markus Frosch committed
8 9 10 11 12 13 14 15

  if [ "${#specs[@]}" -eq 0 ]; then
    echo "No Spec file found!" >&2
    exit 1
  elif [ "${#specs[@]}" -gt 1 ]; then
    echo "More than one spec file found!" >&2
    exit 1
  else
16
    basename "${specs[0]}" .spec
Markus Frosch's avatar
Markus Frosch committed
17 18 19 20
  fi
}

detect_os() {
21 22
  if [ -f /etc/os-release ]; then
    os=$(awk -F"=" '/^ID=/ {print $2}' /etc/os-release | sed -e 's/^"//' -e 's/"$//')
Markus Frosch's avatar
Markus Frosch committed
23

24 25 26 27 28 29 30 31 32
    if [ -n "$os" ]; then
      echo "$os"
      return
    else
      echo "Could not detect os (ID) from /etc/os-release" >&2
      exit 1
    fi
  elif [ -f /etc/redhat-release ]; then
    # before el7
33
    os=$(awk '{ print tolower($1) }' /etc/redhat-release)
34 35 36 37 38 39 40 41

    if [ -n "$os" ]; then
      echo "$os"
      return
    else
      echo "Could not detect os from /etc/redhat-release" >&2
      exit 1
    fi
Markus Frosch's avatar
Markus Frosch committed
42
  else
43
    echo "Could not detect os, looking in /etc/os-release /etc/redhat-release" >&2
Markus Frosch's avatar
Markus Frosch committed
44 45 46 47 48 49
    exit 1
  fi
}

detect_dist() {
  # TODO: verify for SUSE
50 51
  if [ -f /etc/os-release ]; then
    dist=$(awk -F= '/^VERSION_ID=/ {print $2}' /etc/os-release | sed -e 's/^"//' -e 's/"$//')
Markus Frosch's avatar
Markus Frosch committed
52

53 54 55 56 57 58 59 60 61
    if [ -n "$dist" ]; then
      echo "$dist"
      return
    else
      echo "Could not detect dist (VERSION_ID) from /etc/os-release" >&2
      exit 1
    fi
  elif [ -f /etc/redhat-release ]; then
    # before el7
62
    dist=$(grep -Po '(?<=\s)(\d+(\.\d+)+)(?=\s)' /etc/redhat-release | cut -d. -f1)
63 64 65 66 67 68 69 70

    if [ -n "$dist" ]; then
      echo "$dist"
      return
    else
      echo "Could not detect dist version from /etc/redhat-release" >&2
      exit 1
    fi
Markus Frosch's avatar
Markus Frosch committed
71
  else
72
    echo "Could not detect dist, looking in /etc/os-release /etc/redhat-release" >&2
Markus Frosch's avatar
Markus Frosch committed
73 74 75 76 77 78
    exit 1
  fi
}

detect_arch() {
  # TODO: verify for SUSE
79
  IFS=" " read -r -a arches <<< "$(rpm -qa rpm "*-release" --qf "%{arch}\\n"  | sort -u | grep -v noarch)"
Markus Frosch's avatar
Markus Frosch committed
80 81

  if [ "${#arches[@]}" -eq 0 ]; then
82
    echo "No basearch found while looking for *-release or rpm packages!" >&2
Markus Frosch's avatar
Markus Frosch committed
83 84
    exit 1
  elif [ "${#arches[@]}" -gt 1 ]; then
85
    echo "More than one base arch found with *-release or rpm packages!" >&2
Markus Frosch's avatar
Markus Frosch committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    exit 1
  else
    echo "${arches[0]}"
  fi
}

require_var() {
  err=0
  for var in "$@"; do
    if [ -z "${!var}" ]; then
      echo "Variable $var is not set!" >&2
      err+=1
    fi
  done
  [ "$err" -eq 0 ] || exit 1
  echo
}

get_rpmbuild() {
105 106
    local RPMBUILD dist

107
    dist="$(rpm -E '%{?dist}' | sed 's/\(\.centos\)\?$/.'"$ICINGA_BUILD_BRANDING_TAG"'/')"
108 109 110 111 112 113
    # TODO: target_arch needed?
    #local setarch=''
    #if [ -n "$target_arch" ]; then
    #  setarch="setarch ${target_arch}"
    #fi
    #  ${setarch} \
Markus Frosch's avatar
Markus Frosch committed
114 115
    RPMBUILD=(
        /usr/bin/rpmbuild \
116
        --define "vendor $ICINGA_BUILD_BRANDING_VENDOR" \
Markus Frosch's avatar
Markus Frosch committed
117
        --define "dist $dist" \
118
        --define "_topdir ${WORKDIR}/build" \
119
        --define "buildroot %{_buildrootdir}/%{name}" \
Markus Frosch's avatar
Markus Frosch committed
120 121 122 123 124 125 126 127 128 129 130 131 132
        "$@"
    )
    declare -p RPMBUILD
}

rpmbuild() {
    local RPMBUILD
    eval "$(get_rpmbuild "$@")"
    "${RPMBUILD[@]}"
}

find_compilers() {
  local location=${1:-/usr/bin}
133
  cd "$location" || return 1
Markus Frosch's avatar
Markus Frosch committed
134 135 136
  ls {cc,cpp,[gc]++,gcc}{,-*} 2>/dev/null || true
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
patch_scl_ccache() {
  local name="$1"
  local cache_path="$2"

  echo "Ensuring SCL ${name} supports ccache..."

  # This is the only good way to re-add ccache to top of PATH
  # scl enable (inside icinga2.spec) will set its own path first
  local enable_file="/opt/rh/${name}/enable"
  local line="PATH=\"${cache_path}:\${PATH}\""
  if ! grep -qF "${line}" "${enable_file}"; then
    echo "Adding '${line}' to ${enable_file}"
    sudo sh -ec "echo '${line}' >>'${enable_file}'"
  fi
}

Markus Frosch's avatar
Markus Frosch committed
153 154
# repair/prepare ccache (needed on some distros like CentOS 5 + 6, SUSE, OpenSUSE)
preconfigure_ccache() {
155 156
  CCACHE_LINKS="$(rpm -E %_libdir)"/ccache
  IFS=" " read -r -a compilers <<< "$(find_compilers)"
Markus Frosch's avatar
Markus Frosch committed
157

158 159
  devtoolsets=''
  if [ -d /opt/rh ]; then
160 161 162
    if devtoolsets="$(cd /opt/rh && ls -d devtoolset-*)"; then
      for devtoolset in $devtoolsets; do
        patch_scl_ccache "$devtoolset" "${CCACHE_LINKS}"
163

164 165 166 167
        IFS=" " read -r -a extra_compilers <<< "$(find_compilers "/opt/rh/${devtoolset}/root/usr/bin")"
        compilers+=("${extra_compilers[@]}")
      done
    fi
Markus Frosch's avatar
Markus Frosch committed
168
  fi
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

  echo 'Preparing/Repairing ccache symlinks...'
  (
    set -e
    test -d "${CCACHE_LINKS}" || sudo mkdir "${CCACHE_LINKS}"
    cd "${CCACHE_LINKS}"

    for comp in "${compilers[@]}"; do
      [ ! -e "${comp}" ] || continue
      sudo ln -svf /usr/bin/ccache "${comp}"
    done
  )

  # Enable ccache as a default wrapper for compilers
  PATH="${CCACHE_LINKS}:${PATH}"
Markus Frosch's avatar
Markus Frosch committed
184 185
}

186 187 188 189 190 191 192
: "${ICINGA_BUILD_PROJECT:="$(detect_project)"}"
: "${ICINGA_BUILD_OS:="$(detect_os)"}"
: "${ICINGA_BUILD_DIST:="$(detect_dist)"}"
: "${ICINGA_BUILD_ARCH:="$(detect_arch)"}"
: "${ICINGA_BUILD_TYPE:="release"}"
: "${ICINGA_BUILD_UPSTREAM_BRANCH:="master"}"
: "${ICINGA_BUILD_IGNORE_LINT:=1}"
193 194
: "${ICINGA_BUILD_BRANDING_TAG:=icinga}"
: "${ICINGA_BUILD_BRANDING_VENDOR:=Icinga.com}"
Markus Frosch's avatar
Markus Frosch committed
195

Markus Frosch's avatar
Markus Frosch committed
196
[ -n "${ICINGA_NO_ENV}" ] || print_build_env
Markus Frosch's avatar
Markus Frosch committed
197 198

require_var ICINGA_BUILD_PROJECT ICINGA_BUILD_OS ICINGA_BUILD_DIST ICINGA_BUILD_ARCH ICINGA_BUILD_TYPE
199
export_build_env
Markus Frosch's avatar
Markus Frosch committed
200 201

export LANG=C
202
WORKDIR="$(pwd)"
203
BUILDDIR='build'
204
export WORKDIR BUILDDIR
Markus Frosch's avatar
Markus Frosch committed
205 206

# vi: ts=2 sw=2 expandtab