aboutsummaryrefslogtreecommitdiffstats
path: root/setup-environment
blob: d805d050ebc9f1fd82570db78864f26abd378ec5 (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# ---------------------------------------------------------------------------------------------------------------------
# SPDX-License-Identifier: MIT
# ---------------------------------------------------------------------------------------------------------------------

if [ -z "$ZSH_NAME" ] && [ "x$0" = "x./setup-environment" ]; then
    echo >&2 "Error: This script needs to be sourced. Please run as '. ./setup-environment'"
else
    if [ -n "$BASH_SOURCE" ]; then
        layerdir="$(cd "$(dirname "$BASH_SOURCE")" && pwd -P)"
    elif [ -n "$ZSH_NAME" ]; then
        layerdir="$(cd "$(dirname "$0")" && pwd -P)"
    else
        layerdir="$(pwd -P)"
    fi

    export PYENV_ROOT="${PYENV_ROOT:-~/.pyenv}"
    PATH="$PYENV_ROOT/shims:$PATH"

    if [ -f conf/local.conf -o -f conf/bblayers.conf ]; then
        # Assuming we're already in the build dir
        BUILDDIR=$PWD
    else
        BUILDDIR=$PWD/build
    fi

    for i in $(seq $#); do
        setup_mel_arg="$(eval printf "%s" "\$$i")"
        case "$setup_mel_arg" in
            -b)
                BUILDDIR="$(eval printf "%s" "\$$(expr $i + 1)")"
                if [ -z "$BUILDDIR" ]; then
                    echo >&2 "-b requires an argument"
                fi
                BUILDDIR="$(readlink -f "$BUILDDIR")"
                ;;
        esac
    done

    (
        for layercheck in $layerdir . $layerdir/..; do
            if [ -e "$layercheck/setup-environment.conf" ]; then
                . "$layercheck/setup-environment.conf"
            fi
            if [ -e "$layercheck/.setup-environment.conf" ]; then
                . "$layercheck/.setup-environment.conf"
            fi
        done

        OPTIONALLAYERS="${OPTIONALLAYERS-mentor-private tracing-layer}"
        # Customer directory layers handling (e.g. <customername>-custom)
        for layercheck in . $layerdir/..; do
            if [ -e "$layercheck/customer.conf" ]; then
                while read -r _customer; do
                    for layercheck2 in . $layerdir/..; do
                        if [ -d "$layercheck2/$_customer-custom" ]; then
                            if [ -e "$layercheck2/$_customer-custom/custom.conf" ]; then
                                CUSTOMERLAYERS=$(cat $layercheck2/$_customer-custom/custom.conf | sed -e '/^[ 	]*#/d')
                                CUSTOMERLAYERS=$(echo $CUSTOMERLAYERS | sed -e 's/\n//g')
                                OPTIONALLAYERS="$OPTIONALLAYERS $CUSTOMERLAYERS"
                            fi
                            break
                        fi
                    done
                done <"$layercheck/customer.conf"
                break
            fi
        done

        # Hotfix layers handling
        if [ -e "$layerdir/../hotfixes/hotfix.conf" ]; then
            HOTFIXES=$(cat $layerdir/../hotfixes/hotfix.conf | sed -e '/^[ 	]*#/d')
            HOTFIXES=$(echo $HOTFIXES | sed -e 's/\n//g')
            OPTIONALLAYERS="$OPTIONALLAYERS $HOTFIXES"
        fi

        # Extra layers handling
        if [ -e "$layerdir/../xlayers.conf" ]; then
            EXTRALAYERS=$(cat $layerdir/../xlayers.conf | sed -e '/^[ 	]*#/d')
            EXTRALAYERS=$(echo $EXTRALAYERS | sed -e 's/\n//g')
            OPTIONALLAYERS="$OPTIONALLAYERS $EXTRALAYERS"
        fi

        export OPTIONALLAYERS EXTRAMELLAYERS EXCLUDEDLAYERS

        $layerdir/scripts/setup-mel-builddir "$@"
    )
    mel_setup_ret=$?
    if [ $mel_setup_ret -eq 0 ] && [ -n "$BUILDDIR" ] && [ -e "$BUILDDIR" ]; then
        . $BUILDDIR/setup-environment

        configured_layers() {
            tac $BUILDDIR/conf/bblayers.conf \
                | sed -n -e '/^"/,/^BBLAYERS = /{ /^BBLAYERS =/d; /^"/d; p;}' \
                | awk {'print $1'} | sed -e "s#\${TOPDIR}/#$BUILDDIR/#"
        }

        load_lconf_snippet() {
            if [ ! -e "$1/$2" ]; then
                return
            fi
            (
                lheadername="${1#${layerdir%/*}/}/$2"
                printf '\n## Begin %s\n\n' "$lheadername"
                cat "$1/$2"
                printf '\n## End %s\n' "$lheadername"
            ) >>conf/local.conf
        }

        if [ -e $layerdir/post-setup-environment ]; then
            . $layerdir/post-setup-environment
        fi

        if [ -e "$BUILDDIR/conf/local.conf" ] && [ -e "$BUILDDIR/conf/bblayers.conf" ]; then
            sed -i -n -e ":out; /^## Begin /{ :loop; /^## End /{ d; b out; }; n; b loop; }; p;" conf/local.conf
            SETUP_ENV_MACHINE="$(sed -n -e 's/^MACHINE *?*= *"\(.*\)"/\1/p' "$BUILDDIR/conf/local.conf")"

            load_lconf_snippet "$layerdir" "conf/local.conf.append"
            load_lconf_snippet "$layerdir" "conf/local.conf.append.$SETUP_ENV_MACHINE"
            configured_layers | grep -Fvx "$layerdir" | while read layer; do
                if [ -e $layer/post-setup-environment ]; then
                    . $layer/post-setup-environment
                fi
                load_lconf_snippet "$layer" "conf/local.conf.append"
                load_lconf_snippet "$layer" "conf/local.conf.append.$SETUP_ENV_MACHINE"
            done

            . $BUILDDIR/setup-environment >/dev/null 2>&1
        fi

        unset SETUP_ENV_MACHINE
        unset load_lconf_snippet
        unset configured_layers
    fi
    unset layerdir setup_mel_arg
    test $mel_setup_ret -eq 0
fi