Files
meta-cyber-scle/scripts/envsetup.sh
2023-07-17 15:42:30 +00:00

275 lines
7.7 KiB
Bash

#!/bin/bash
#----------------------------------------------
# Make sure script has been sourced
#
if [ "$0" = "$BASH_SOURCE" ]; then
echo "###################################"
echo "ERROR: YOU MUST SOURCE the script"
echo "###################################"
exit 1
fi
BUILD_DIR=$1
# Init env var
SCLE_ROOT_DIR=`realpath $PWD`
SCLE_DIR="meta-cyber-scle"
if [ "a${SCLE_DL_DIR}" = "a" ]; then
SCLE_DL_DIR="$SCLE_ROOT_DIR/oe-downloads"
fi
if [ "a${SCLE_SSTATE_DIR}" = "a" ]; then
SCLE_SSTATE_DIR="${SCLE_ROOT_DIR}/sstate-cache"
fi
# Use utf-8 encoding
if ! echo $LANG | grep -q "en_US.UTF-8"
then
export LANG="en_US.UTF-8"
fi
if [ "a${DISTRO}" = "a" ]; then
DISTRO="pengwyn-distro"
fi
if [ "a${MACHINE}" = "a" ]; then
MACHINE="pengwyn"
fi
if [ "a${VERS}" = "a" ]; then
VERS="none"
fi
if [ "a${REV}" = "a" ]; then
REV="0"
fi
_TEMPLATECONF="$SCLE_ROOT_DIR/${SCLE_DIR}/conf/template/"
#----------------------------------------------
# Standard Openembedded init
#
echo -e "[source $SCLE_ROOT_DIR/poky/oe-init-build-env]"
TEMPLATECONF=${_TEMPLATECONF} source $SCLE_ROOT_DIR/poky/oe-init-build-env ${BUILD_DIR} > /dev/null 2> /dev/null
_FORMAT_PATTERN='::-::'
######################################################
# Make selection for <TARGET> requested from <LISTING> provided using shell or ui choice
#
_choice_shell() {
#format list to have display aligned on column with '-' separation between name and description
local options=$(echo "$2" | column -t -s "::")
if [ "z$ZSH_NAME" != "z" ]
then
# zsh don't split string as expected (see http://zsh.sourceforge.net/FAQ/zshfaq03.html)
eval "options=($options)"
fi
#change separator from 'space' to 'end of line' for 'select' command
old_IFS=$IFS
IFS=$'\n'
local i=1
unset LAUNCH_MENU_CHOICES
for opt in $options; do
printf "%3.3s. %s\n" $i $opt
LAUNCH_MENU_CHOICES=(${LAUNCH_MENU_CHOICES[@]} $opt)
i=$(($i+1))
done
IFS=$old_IFS
# Item selection from list
local selection=""
while [ -z "$selection" ]; do
echo -n "Please enter your choice of $1 (1-$(echo "$options" | wc -l)): "
read answer
if [[ $answer =~ ^[0-9]+$ ]]; then
if [ $answer -gt 0 ] && [ $answer -le ${#LAUNCH_MENU_CHOICES[@]} ]; then
if [ "z$ZSH_NAME" != "z" ]
then
selection=${LAUNCH_MENU_CHOICES[$(($answer))]}
else
selection=${LAUNCH_MENU_CHOICES[$(($answer-1))]}
fi
break
fi
fi
echo "Invalid choice: $answer"
done
eval $1=$(echo $selection | cut -d' ' -f1)
}
_choice_ui() {
local target=""
#change separator from 'space' to 'end of line' to get full line
old_IFS=$IFS
IFS=$'\n'
for ITEM in $2; do
local target_name=$(echo $ITEM | awk -F''"${_FORMAT_PATTERN}"'' '{print $1}')
local target_desc=$(echo $ITEM | awk -F''"${_FORMAT_PATTERN}"'' '{print $NF}')
TARGETTABLE+=($target_name "$target_desc" OFF)
done
IFS=$old_IFS
while [[ -z $target ]]
do
target=$(${UI_CMD} --title "Available $1" --radiolist "Please choose a $1" 0 0 0 "${TARGETTABLE[@]}" 3>&1 1>&2 2>&3)
test -z $target || break
#display dialog box to provide some help to user
${UI_CMD} --title "How to select $1" --msgbox "Keyboard usage:\n\n'ENTER' to validate\n'SPACE' to select\n 'TAB' to navigate" 0 0
done
unset TARGETTABLE
unset ITEM
eval $1=$target
}
choice() {
local __TARGET=$1
local choices="$2"
echo "[$__TARGET configuration]"
if [ $(echo "$choices" | wc -l) -eq 1 ]; then
eval $__TARGET=$(echo $choices | awk -F''"${_FORMAT_PATTERN}"'' '{print $1}')
else
if ! [[ -z $DISPLAY ]] && ! [[ -z ${UI_CMD} ]]; then
_choice_ui $__TARGET "$choices"
else
_choice_shell $__TARGET "$choices"
fi
fi
echo "Selected $__TARGET: $(eval echo \$$__TARGET)"
echo ""
}
######################################################
# Choose target machine
#
conf_machine()
{
local choices=$(find ${SCLE_ROOT_DIR}/${SCLE_DIR}/conf/machine/ -name "*.conf" 2>/dev/null | sort | uniq)
for ITEM in $choices
do
if [[ -z $(grep "#@DESCRIPTION" $ITEM) ]]; then
echo ""
echo "ERROR: No '#@DESCRIPTION' field available in $__CONGIG file:"
echo "$ITEM"
echo ""
return 1
fi
done
unset ITEM
if [ $(echo $choices | wc -l) -eq 1 ]; then
# return only file name (distro or machine)
echo "$(echo $choices | sed 's|^.*/\(.*\)\.conf|\1|')"
else
echo "$(echo $choices | xargs grep "#@DESCRIPTION" | sed 's|^.*/\(.*\)\.conf:#@DESCRIPTION:[ \t]*\(.*$\)|\1'"${_FORMAT_PATTERN}"'\2|')"
fi
}
######################################################
# Apply configuration to site.conf file
#
conf_siteconf()
{
_NCPU=$(grep '^processor' /proc/cpuinfo 2>/dev/null | wc -l)
# Sanity check that we have a valid number, if not then fallback to a safe default
[ "$_NCPU" -ge 1 ] 2>/dev/null || _NCPU=2
cat > conf/site.conf <<EOF
#
# local.conf covers user settings, site.conf covers site specific information
# such as proxy server addresses and optionally any shared download location
#
# SITE_CONF_VERSION is increased each time build/conf/site.conf
# changes incompatibly
SCONF_VERSION = "1"
BB_NUMBER_THREADS = "${_NCPU}"
PARALLEL_MAKE = "-j ${_NCPU}"
DL_DIR = "$SCLE_DL_DIR"
SSTATE_DIR = "$SCLE_SSTATE_DIR"
MACHINE = "$MACHINE"
DISTRO = "$DISTRO"
PACKAGE_CLASSES = "package_deb"
#LICENSE_FLAGS_WHITELIST += "commercial"
INHERIT += "rm_work"
EOF
}
######################################################
# Update bblayer.conf file
update_layerconf() {
cp $1/conf/template/bblayers.conf.sample conf/bblayers.conf
}
######################################################
# extract description for images provided
list_images_descr() {
list_images=$1
if [ "z$ZSH_NAME" != "z" ]
then
# zsh don't split string as expected (see http://zsh.sourceforge.net/FAQ/zshfaq03.html)
eval "list_images=($list_images)"
fi
for l in $list_images;
do
local image=`echo $l | sed -e 's#^.*/\([^/]*\).bb$#\1#'`
if [ ! -z "$(grep "^SUMMARY[ \t]*=" $l)" ]; then
local descr=`grep "^SUMMARY[ \t]*=" $l | sed -e 's/^.*"\(.*\)["\]$/\1/'`
else
local descr=`grep "^DESCRIPTION[ \t]*=" $l | sed -e 's/^.*"\(.*\)["\]$/\1/'`
fi
if [ -z "$descr" ] && [ "$2" == "ERR" ]; then
echo ""
echo "ERROR: No description available for image: $image"
echo "$l"
echo ""
return 1
else
printf " %-33s - $descr\n" $image
fi
done
}
######################################################
# alias function: list all images available
#
list_images() {
local metalayer=$1
if [[ -z $metalayer ]] || ! [[ -e "$SCLE_ROOT_DIR/$metalayer" ]]; then
echo ""
echo "ERROR: Cannot find layer '$metalayer' at $SCLE_ROOT_DIR/ root."
echo "Please use full meta layer path from baseline root:"
echo "eg: 'stoe_list_images openembedded-core'"
echo ""
return 1
fi
local LIST=`find $SCLE_ROOT_DIR/$metalayer/* -wholename "*/images/*.bb" | grep -v meta-skeleton | sort`
echo ""
echo "=================================================="
echo "Available images for '$metalayer' layer are:"
echo ""
list_images_descr "$LIST" "ERR"
echo ""
}
touch conf/local.conf
if [ -f conf/site.conf ]; then
echo "=====> !!!! [WARNING] site.conf already exists. Nothing done... !!!!"
else
conf_siteconf
fi
update_layerconf ${SCLE_ROOT_DIR}/${SCLE_DIR}
list_images ${SCLE_DIR}