build_helper 6.19 KB
################################################################################
# Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The OpenAirInterface Software Alliance licenses this file to You under
# the OAI Public License, Version 1.1  (the "License"); you may not use this file
# except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.openairinterface.org/?page_id=698
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------
# For more information about the OpenAirInterface (OAI) Software Alliance:
#      contact@openairinterface.org
################################################################################

# file build_helper
# brief
# author Laurent Thomas, Lionel GAUTHIER, Rohan KHARADE
#
#######################################
SUPPORTED_DISTRO="Ubuntu 18.04, Ubuntu 20.04"
#SUPPORTED_DISTRO="Ubuntu 18.04, Ubuntu 20.04, CentOS 7, RHEL 7"
#SUPPORTED_DISTRO="Ubuntu 18.04"
if [ ! -f /etc/os-release ]; then
  echo_fatal "No /etc/os-release file found. You're likely on an unsupported distro."
fi
OS_DISTRO=$(grep "^ID=" /etc/os-release | sed "s/ID=//" | sed "s/\"//g")
OS_RELEASE=$(grep "^VERSION_ID=" /etc/os-release | sed "s/VERSION_ID=//" | sed "s/\"//g")
case "$OS_DISTRO" in
  fedora) OS_BASEDISTRO="fedora"; INSTALLER="dnf"; CMAKE="cmake" ;;
  rhel)   OS_BASEDISTRO="fedora"; INSTALLER="yum"; CMAKE="cmake3" ;;
  centos) OS_BASEDISTRO="fedora"; INSTALLER="yum"; CMAKE="cmake3" ;;
  debian) OS_BASEDISTRO="debian"; INSTALLER="apt-get"; CMAKE="cmake" ;;
  ubuntu) OS_BASEDISTRO="debian"; INSTALLER="apt-get"; CMAKE="cmake" ;;
esac

IS_CONTAINER=`egrep -c "docker|kubepods" /proc/self/cgroup`

if [ $IS_CONTAINER -eq 0 ]
then
  SUDO='sudo -S -E'
else
  SUDO=''
fi

###############################
## echo and  family
###############################
black='\E[30m'
red='\E[31m'
green='\E[32m'
yellow='\E[33m'
blue='\E[1;34m'
magenta='\E[35m'
cyan='\E[36m'
white='\E[37m'
reset_color='\E[00m'
COLORIZE=1

#-------------------------------------------------------------------------------
cecho()  {
    # Color-echo
    # arg1 = message
    # arg2 = color
    local default_msg="No Message."
    message=${1:-$default_msg}
    color=${2:-$green}
    [ "$COLORIZE" = "1" ] && message="$color$message$reset_color"
    echo -e "$message"
    return
}

echo_error()   { cecho "$*" $red          ;}
echo_fatal()   { cecho "$*" $red; exit -1 ;}
echo_warning() { cecho "$*" $yellow       ;}
echo_success() { cecho "$*" $green        ;}
echo_info()    { cecho "$*" $blue         ;}

#-------------------------------------------------------------------------------
# From https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash
# arg1 is a dotted (or not) version number (ex 4.10.6.56-ubunutu) 
# arg2 is a dotted (or not) version number (ex 4.10.6.56-ubunutu)
# return 0 if $1 lower or equal $2, else 1
version_le() {
  [  "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
}

# From https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash
version_lt() {
  [ "$1" = "$2" ] && return 1 || version_le $1 $2
}

# From https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash
version_ge() {
  [  "$1" = "`echo -e "$1\n$2" | sort -V | tail -n1`" ]
}

# From https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash
version_gt() {
  [ "$1" = "$2" ] && return 1 || version_ge $1 $2
}

########################
# distribution helpers #
########################

#-------------------------------------------------------------------------------
# This function return a string to identify the distribution we are running
# If we can't check the distribution, it returns "Unknown"
# This function return always true as exit code by design
# Examples:
#   ubuntu18.04
#   debian8.5
get_distribution_release() {
    if [[ ! -z "$OS_DISTRO$OS_RELEASE" ]]; then
        echo -n "$OS_DISTRO$OS_RELEASE"
    else
        echo -n Unknown
    fi
}

check_supported_distribution() {
    echo_info "Detected OS release - $OS_DISTRO $OS_RELEASE"
    local distribution=$(get_distribution_release)
    case "$distribution" in
        "ubuntu18.04") return 0 ;;
        "ubuntu20.04") return 0 ;;
        #"rhel7")       return 0 ;;
        #"centos7")     return 0 ;;
    esac
    return 1
}


###################################
# Compilers
###################################

# From https://stackoverflow.com/a/20473191
# test if a list include item
# arg1 is list, ex "item1 item2 ..."
# arg2 is item
function list_include_item {
  local list="$1"
  local item="$2"
  if [[ $list =~ (^|[[:space:]])"$item"($|[[:space:]]) ]] ; then
    # yes, list include item
    result=0
  else
    result=1
  fi
  return $result
}

# arg 1 Build directory OPENAIR_DIR/build/?/build
# arg 2 Executable target name
# arg 3 Executable name (no path)
# arg 4 Verbose         (1 or 0)
compilations() {
  echo_info "Compilation log for $1 is here: $dlog/$2.txt"
  cd $OPENAIRCN_DIR/$1/
    {
      make build-release  | tee $dlog/$2.txt
      if grep -q "@@ Installing vpp @@" "$dlog/$2.txt" ; then
        echo_success "##### VPP Compiled Successfully #####"
      else
        echo_error "##### VPP not compiled #####"
      fi
    }
}

################################
# set_openair_env
###############################
#-------------------------------------------------------------------------------
set_openair_env(){
  fullpath=`readlink -f $BASH_SOURCE`
  [ -f "/.$fullpath" ] || fullpath=`readlink -f $PWD/$fullpath`
  openair_path=${fullpath%/build/*}
  openair_path=${openair_path%/scripts/*}
  openair_path=${openair_path%/docker*}
  export OPENAIRCN_DIR=$openair_path
}