Skip to content
Snippets Groups Projects
setup-environment 15.1 KiB
Newer Older
#!/bin/bash
# -*- mode: shell-script; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
#
# Copyright (C) 2012, 2013, 2016 O.S. Systems Software LTDA.
# Authored-by:  Otavio Salvador <otavio@ossystems.com.br>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Add options for the script
# Copyright (C) 2013 Freescale Semiconductor, Inc.

CWD=$(pwd)
PROGNAME="setup-environment"
PACKAGE_CLASSES=${PACKAGE_CLASSES:-package_rpm}

SUPPORTED_MACHINES="$(find sources/*/conf/machine -name "*.conf" \
                            -exec basename -s ".conf" {} \; | sort )"
DEFAULT_MACHINE="$(echo "$SUPPORTED_MACHINES" | head -n 1)"

GF_ARCHS=($(find sources/meta-seconorth*/conf/machine -name "*.conf" \
GF_DISTROS="$(find sources/meta-seconorth-distro/conf/distro -name "*.conf" \
                    -exec basename -s ".conf" {} \; | sort )"

POKY_DISTROS="$(find sources/poky/meta-poky/conf/distro -name "*.conf" \
                    -exec basename -s ".conf" {} \; | sort )"

DEFAULT_DISTRO="$(echo "$GF_DISTROS" | head -n 1)"

GF_IMAGES="$(find sources/meta-seconorth-distro/recipes-bsp/images/ -iname "*.bb" \
                -exec basename -s ".bb" {} \;)"

SUPPORTED_MULTICONFIGS="$(find sources/*/conf/multiconfig -name "*.conf" \
                        -exec basename -s ".conf" {} \; 2>/dev/null || true )"
SUPPORTED_MULTICONFIGS_SINGLE_LINE=${SUPPORTED_MULTICONFIGS//$'\n'/ }
if [ -z "$SUPPORTED_MULTICONFIGS_SINGLE_LINE" ]; then
    MULTICONFIG="false"
fi

DL_DIR_GLOBAL="/usr/src/packages/"
SSTATE_DIR="/var/cache/yocto/sstate_cache"
SSTATE_MIRROR="/var/cache/yocto/sstate_cache"

MESSAGES=""
message()
{
    MESSAGES="$MESSAGES$1\n"
}

usage()
{
    echo  "
Usage: MACHINE=<machine> DISTRO=<distro> {BBLAYERS=<bblayers>} {MULTICONFIG=true} source $PROGNAME <build-dir>
Usage:                                                                            source $PROGNAME <build-dir>
    <machine>    machine name
    <distro>     distro name
    <bblayers>   file with layer list (optional)
    <build-dir>  build directory
    MULTICONFIG  set to true, to enable multiconfig configuration

The first usage is for creating a new build directory. In this case, the
script creates the build directory <build-dir>, configures it for the
specified <machine> and <distro>, and prepares the calling shell for running
bitbake on the build directory.

The second usage is for using an existing build directory. In this case,
the script prepares the calling shell for running bitbake on the build
directory <build-dir>. The build directory configuration is unchanged.
"

        echo "Supported machines:"
        echo "$SUPPORTED_MACHINES" | xargs -I% echo -e "\t%"
        echo
        echo "Available SECO Northern Europe distros:"
        echo "$GF_DISTROS" | xargs -I% echo -e "\t%"
        echo
        echo "Available Poky distros:"
        echo "$POKY_DISTROS" | xargs -I% echo -e "\t%"
        echo
        echo "Available multiconfigs:"
        echo "$SUPPORTED_MULTICONFIGS" | xargs -I% echo -e "\t%"
echo "
Examples:

- To create a new Yocto build directory:
  $ MACHINE=$DEFAULT_MACHINE DISTRO=$DEFAULT_DISTRO source $PROGNAME build

- To create a new Yocto build directory with multiconfig configuration:
  $ MACHINE=$DEFAULT_MACHINE DISTRO=$DEFAULT_DISTRO MULTICONFIG=true source $PROGNAME build

- To use an existing Yocto build directory:
  $ source $PROGNAME build
"
}

clean_up()
{
   unset EULA
   unset CWD TEMPLATES SHORTOPTS LONGOPTS ARGS PROGNAME
   unset generated_config updated
   unset MACHINE SDKMACHINE DISTRO OEROOT MULTICONFIG
   unset BBLAYERS
# copied from https://stackoverflow.com/a/28776166
is_sourced() {
    if [ -n "$ZSH_VERSION" ]; then
        case $ZSH_EVAL_CONTEXT in *:file:*) return 0;; esac
    else  # Add additional POSIX-compatible shell names here, if needed.
        case ${0##*/} in dash|-dash|bash|-bash|ksh|-ksh|sh|-sh) return 0;; esac
    fi
    return 1  # NOT sourced.
}

if [ "$(whoami)" = "root" ]; then
    echo "ERROR: Do not use the BSP as root. Exiting ..."
# get command line options
SHORTOPTS="h"
LONGOPTS="help"

ARGS=$(getopt --options $SHORTOPTS  \
  --longoptions $LONGOPTS --name $PROGNAME -- "$@" )
# Print the usage menu if invalid options are specified
if [ $? != 0 ] || [ $# -lt 0 ]; then
   usage && clean_up
   echo No args
fi

eval set -- "$ARGS"
while true;
do
    case $1 in
        -h|--help)
           usage clean_up
           ;;
        --)
           shift
           break
           ;;
    esac
done

distro_default=false
machine_default=false
BUILDDIR="$1"
if [ -z "$BUILDDIR" ];then
    build_dir_setup_enabled="true"
else
    build_dir_setup_enabled="false"
fi

if "$build_dir_setup_enabled" && [ -z "$MACHINE" ]; then
    MACHINE="$DEFAULT_MACHINE"
    machine_default=true
fi

if "$build_dir_setup_enabled" && [ -z "$DISTRO" ]; then
    DISTRO="$DEFAULT_DISTRO"
    distro_default=true
fi

if [ -z "$BUILDDIR" ];then
    BUILDDIR="build-$MACHINE"
fi

if [ ! -e "$BUILDDIR/conf/local.conf.sample" ]; then
    build_dir_setup_enabled="true"
    if $machine_default;then message  "No Machine specified, using default: $MACHINE"; fi
    if $distro_default;then message "No Distro specified, using default: $DISTRO"; fi
else
    build_dir_setup_enabled="false"
fi

if [ -z "$SDKMACHINE" ]; then
    SDKMACHINE='i686'
fi

OEROOT=$PWD/sources/poky
if [ -e "$PWD/sources/oe-core" ]; then
    OEROOT=$PWD/sources/oe-core
fi

# shellcheck source=../sources/poky/oe-init-build-env
. "$OEROOT/oe-init-build-env" "$CWD/$BUILDDIR" > /dev/null

# if conf/local.conf not generated, no need to go further
if [ ! -e conf/local.conf ]; then
    clean_up
    is_sourced && return || exit 1
fi

# Clean up PATH, because if it includes tokens to current directories somehow,
# wrong binaries can be used instead of the expected ones during task execution
PATH="$( echo "$PATH" | sed 's/\(:.\|:\)*:/:/g;s/^.\?://;s/:.\?$//')"
export PATH

generated_config=
if "$build_dir_setup_enabled"; then

    # Some customer projects provide their own bblayers.conf.
    # The custom configurations are located inside the respective
    # manifest repositories. If such a custom config exists, we use it
    # otherwise we fallback to the default lookup.
    if [ -f "$CWD/.repo/manifests/bblayers.conf" ]; then
        BBLAYERS_PATH="$CWD/.repo/manifests/bblayers.conf"
    else
        # If a bblayers name is provided via the command line
        # parameter, we skip the arch based find mechanism.
        if [ -z "$BBLAYERS" ]; then
            # Map the MACHINE to a default bblayers file
            case "$MACHINE" in
                seco-mx8mm|seco-mx8mp|seco-mx6-fsl)
                    BBLAYERS=bblayers_fsl.conf
                ;;
                seco-genio700|seco-genio510)
                    BBLAYERS=bblayers_mtk.conf
                ;;
                seco-mx6|seco-mx6ull|*) # Also default
                    BBLAYERS=bblayers.conf
                ;;
            esac
        fi

        BBLAYERS_PATH="$CWD/.conf/${BBLAYERS}"
    # Copy bblayers to config folder
    if [ -f "$BBLAYERS_PATH" ]; then
        cp "$BBLAYERS_PATH" conf/bblayers.conf
        message "Using bblayers.conf from $BBLAYERS_PATH"
    else
      echo "No $BBLAYERS file found in the config repo!"
      echo "Bitbake will fall back to the poky default bblayers.conf"
    fi

    if [ -z "${DISTRO/*fngsystem*}" ] || [ -z "${DISTRO/*no-gplv3*}" ] \
       || [ "$MULTICONFIG" = "true" ]; then
        # Some custom projects already include the NoGPLv3 or GPLv2 layer.
        # If these layers are already present, the Bitbake parsing fails.
        # As a precaution, check if the layers are already present and
        # only add them if not.
        if ! grep -q "/sources/meta-seconorth-nogplv3" conf/bblayers.conf; then
            echo "BBLAYERS += \"\${BSPDIR}/sources/meta-seconorth-nogplv3\"" >> conf/bblayers.conf
        fi

        if ! grep -q "/sources/meta-gplv2" conf/bblayers.conf; then
            echo "BBLAYERS += \"\${BSPDIR}/sources/meta-gplv2\"" >> conf/bblayers.conf
        fi
    # Backup default local.conf
    mv conf/local.conf conf/local.conf.sample

    {
        # Generate the local.conf based on the Yocto defaults
        grep -v '^#\|^$' conf/local.conf.sample

        # Add manifest revision
        echo
        echo 'require ${BSPDIR}/.conf/git-describe.inc'

        # shellcheck disable=SC2016
        echo 'MANIFEST_VERSION := "${@git_describe(d.getVar("BSPDIR") + "/.repo/manifests", d.getVar("DISTRO"))}"'
        echo

        # Add source revisions of subprojects
        find "${CWD}/.repo/manifests" -maxdepth 1 -type f,l -name 'SRCREV*.conf' \
            -printf "require \${BSPDIR}/.repo/manifests/%f\n"
        # Add SECO North typical download dir as default settings
        if [ -d "$DL_DIR_GLOBAL" ] && [ -w "$DL_DIR_GLOBAL" ];then
            if [ "$USE_DL_MIRROR" = "true" ];then
                echo 'INHERIT += "own-mirrors"'
                echo "SOURCE_MIRROR_URL = \"$DL_DIR_GLOBAL\""
                message "Using $DL_DIR_GLOBAL as local mirror."
                echo "DL_DIR ?= \"/home/$USER/yocto_downloads\""
                message "Using /home/$USER/yocto_downloads for downloads."
            else
                echo "DL_DIR ?= \"$DL_DIR_GLOBAL\""
                message "Using $DL_DIR_GLOBAL for downloads."
            fi

            echo 'BB_GENERATE_MIRROR_TARBALLS = "1"'
            message "Generating mirror tarballs from downloaded repos."
        else
            echo "DL_DIR ?= \"$CWD/downloads\""
        fi
        # Add SECO North typical sstate cache dir as default settings
        if [ "$CI" = "true" ] && [ -d "$SSTATE_DIR" ] && [ -w "$SSTATE_DIR" ];then
            message "Using $SSTATE_DIR as sstate cache directory"
            echo "SSTATE_DIR = \"$SSTATE_DIR\""
        elif [ -d "$SSTATE_MIRROR" ] && [ -r "$SSTATE_MIRROR" ];then
            message "Using $SSTATE_MIRROR as sstate cache mirror"
            echo "SSTATE_MIRRORS = \"file://.* file://$SSTATE_MIRROR/PATH\""
        fi

        # Increase niceness to make the build server more responsive
        echo
        echo 'BB_NICE_LEVEL = "1"'
        # Enable the cpu pressure monitoring
        echo 'BB_PRESSURE_MAX_CPU = "10000"'

        # If a recipe source isn't available upstream anymore, we need
        # to fall back to our mirror server. The MIRROR variable is
        # evaluated last, after the local copy, the PREMIRRORS, and
        # the upstream version (also see the Yocto Glossary for more
        # details).
        echo
        echo "PREMIRRORS:prepend = \" \\"
        echo "    ftp://.*/.* https://support.garz-fricke.com/mirror/ \\"
        echo "    git://.*/.* https://support.garz-fricke.com/mirror/ \\ "
        echo "    gitsm://.*/.* https://support.garz-fricke.com/mirror/ \\ "
        echo "    http://.*/.* https://support.garz-fricke.com/mirror/ \\ "
        echo "    https://.*/.* https://support.garz-fricke.com/mirror/ \\ "
        echo "\""
        if [ "$MULTICONFIG" = "true" ]; then
            echo
            echo "BBMULTICONFIG = \"$SUPPORTED_MULTICONFIGS_SINGLE_LINE\""
        fi


    # Change settings according environment
    sed -e "s,MACHINE ??=.*,MACHINE ??= '$MACHINE',g" \
        -e "s,SDKMACHINE ??=.*,SDKMACHINE ??= '$SDKMACHINE',g" \
        -e "s,DISTRO ?=.*,DISTRO ?= '$DISTRO',g" \
        -e "s,PACKAGE_CLASSES ?=.*,PACKAGE_CLASSES ?= '$PACKAGE_CLASSES',g" \
        -i conf/local.conf
    # MediTek Genio 700 specific settings
    if [ "$MACHINE" = "seco-genio700" ]; then
        {
        echo
        echo 'LICENSE_FLAGS_ACCEPTED = "commercial"'
        echo 'SKIP_META_SECURITY_SANITY_CHECK = "1"'
        echo
        } >> conf/local.conf
    fi

    for s in $HOME/.oe $HOME/.yocto; do
        if [ -e "$s/site.conf" ]; then
            echo "Linking $s/site.conf to conf/site.conf"
            ln -s "$s/site.conf" conf
        fi
    done

    generated_config=1
fi

# Handle EULA setting
if grep -q "meta-freescale" conf/bblayers.conf; then

    EULA_ACCEPTED=

    # EULA has been accepted already (ACCEPT_FSL_EULA is set in local.conf)
    if grep -q '^\s*ACCEPT_FSL_EULA\s*=\s*["'\'']..*["'\'']' conf/local.conf; then
        EULA_ACCEPTED=1
    fi

    if [ -z "$EULA_ACCEPTED" ] && [ -n "$EULA" ]; then
        # The FSL EULA is not set as accepted in local.conf, but the EULA
        # variable is set in the environment, so we just configure
        # ACCEPT_FSL_EULA in local.conf according to $EULA.
        echo "ACCEPT_FSL_EULA = \"$EULA\"" >> conf/local.conf
    elif [ -n "$EULA_ACCEPTED" ]; then
        # The FSL EULA has been accepted once, so ACCEPT_FSL_EULA is set
        # in local.conf.  No need to do anything.
        :
    else
        # THE FSL EULA is not set as accepted in local.conf, and EULA is
        # not set in the environment, so we need to ask user if he/she
        # accepts the FSL EULA:
        cat <<EOF

    Some BSPs depend on libraries and packages which are covered by Freescale's
    End User License Agreement (EULA). To have the right to use these binaries in
    your images, you need to read and accept the following...

EOF

        sleep 4

        more -d $CWD/sources/meta-freescale/EULA
        echo
        REPLY=
        while [ -z "$REPLY" ]; do
            echo -n "Do you accept the EULA you just read? (y/n) "
            read REPLY
            case "$REPLY" in
                y|Y)
                echo "EULA has been accepted."
                echo "ACCEPT_FSL_EULA = \"1\"" >> conf/local.conf
                            EULA_ACCEPTED=1
                ;;
                n|N)
                echo "EULA has not been accepted."
                ;;
                *)
                REPLY=
                ;;
            esac
        done
    fi
fi

# Print welcome message
cat <<EOF

Welcome to SECO Northern Europe BSP

The Yocto Project has extensive documentation about OE including a
reference manual which can be found at:
    https://docs.yoctoproject.org/

For more information about OpenEmbedded see their website:
    https://www.openembedded.org/

cat <<EOF

You can now run 'bitbake <target>'

Common targets are:
EOF
    echo "$GF_IMAGES " | xargs -I% echo -e "\t%"

if [ -n "$generated_config" ]; then
    cat <<EOF

Your build environment has been configured with:

        MACHINE=$MACHINE
        SDKMACHINE=$SDKMACHINE
        DISTRO=$DISTRO
        BBLAYERS=$BBLAYERS
EOF
else
    echo "Your configuration files at $BUILDDIR have not been touched."
fi

clean_up