aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/setup-mel-builddir
blob: 5c394533c7149ce1d0873485dd1c046bfd7d5ad7 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/bin/sh
#
# ---------------------------------------------------------------------------------------------------------------------
# SPDX-License-Identifier: GPL-2.0
# ---------------------------------------------------------------------------------------------------------------------
#
# Copyright 2022 Siemens
#
# This file is licensed under the terms of the GNU General Public License
# version 2.  This program  is licensed "as is" without any warranty of any
# kind, whether express or implied.

DISTRO="${DISTRO:-mel}"
CORELAYERS="${CORELAYERS:-}"
OPTIONALLAYERS="${OPTIONALLAYERS-}"
EXTRAMELLAYERS="${EXTRAMELLAYERS-}"
layer_priority_overrides="openembedded-layer=1"


usage () {
    cat >&2 <<END
Usage: $0 [OPTIONS...] [MACHINE]

When operating from a MEL install, this script will use the template config
files from the installed BSP. If multiple BSPs are installed, you must specify
MACHINE, and the script will use the templates from that BSP.

When no installed BSP is available, MACHINE is required, and this script will
set up the build directory with a base set of layers + all layers which
include the machine .conf file for this machine, sorted by layer recipe
priority.

If the build directory already exists and is configured, you can leave MACHINE
unspecified, and the script will set you up for that build directory without
changing its configuration.

All layers whose directory name is update-* or meta-mentor-cve, at toplevel or
a subdir, of the parent of meta-mentor, will be automatically included, as
a mechanism for delivering sets of updates for releases.

Options:

  -b BUILDDIR  Specify the build directory to create (default: 'build')
  -o MELDIR    Specify the root of the MEL install, where you can find bitbake
               and the layers
  -p PATHS     Specify colon separated list of paths, where you can find layers
               in addition to the root of the MEL install (default: empty).
               If any path in this list contains wildcards, the list must be quoted
               to avoid wildcard substitution by the shell
  -l LAYERS    Space-separated list of layer names for additional layers you
               want to be included in the configuration. This argument is
               cumulative. Both '-l "meta-foo meta-bar"' and
               '-l meta-foo -l meta-bar' are valid and include both layers.
  -f           Force overwrite of existing configuration files in the build
               directory. This includes local.conf and bblayers.conf, but not
               site.conf
  -h           Show this usage information

END
}

prompt_choice () {
    non_interactive=0
    while getopts n opt; do
        case "$opt" in
            n)
                non_interactive=1
                ;;
        esac
    done
    shift $(expr $OPTIND - 1)

    prompt_message="$1"
    prompt_default="${2:-}"

    prompt_options="$tmpdir/prompt-choice"
    cat >"$prompt_options"

    prompt_count="$(wc -l <"$prompt_options")"
    case $prompt_count in
        0)
            # No choices provided
            return 1
            ;;
        1)
            # Only one choice
            cat "$prompt_options"
            return 0
            ;;
    esac

    if [ $non_interactive -eq 1 ]; then
        printf >&2 'Error: unable to prompt for `%s` interactively\n' "$prompt_message"
        return 1
    fi

    while [ -z "$prompt_value" ]; do
        prompt_num=0
        while read choice; do
            prompt_num=$(expr $prompt_num + 1)
            eval prompt_value_${prompt_num}="$choice"
            printf >&2 "%s) %s\n" "$prompt_num" "$choice"
        done <"$prompt_options"
        printf >&2 "%s: " "$prompt_message"
        read selection </dev/tty
        prompt_value="$(eval echo \${prompt_value_$selection})"
    done
    printf "%s\n" "$prompt_value"
}

abspath () {
    readlink -f "$1"
}

configure_builddir () {
    if [ -n "$EXCLUDEDLAYERS" ]; then
        set -- "$@" -x "$EXCLUDEDLAYERS"
    fi
    if [ "$verbose" -eq 1 ]; then
        set -- "$@" -v
    fi


    sed -i -e"s/^#*MACHINE *?*=.*/MACHINE ??= \"$MACHINE\"/" $BUILDDIR/conf/local.conf
    sed -n -i -e "s|##COREBASE##|$OEROOT|g" -e '/^BBLAYERS /{ :start; /\\$/{n; b start}; d; }; p' -e "/BBFILE_PRIORITY/d" $BUILDDIR/conf/bblayers.conf

    echo 'BBLAYERS ?= "\' >> $BUILDDIR/conf/bblayers.conf
    layersfile="$tmpdir/layers"

    # Convert layer paths to layer names for bb-determine-layers
    extralayernames=
    for layer in $extralayers; do
        if [ -d "$layer" ]; then
            layerpaths="$layerpaths:$layer"
            layer="$(PATH="$OEROOT/scripts:$OEROOT/bitbake/bin:$PATH" $(dirname "$0")/bb-print-layer-data 2>/dev/null "$layer" | cut -d: -f1)"
        fi
        extralayernames="$extralayernames $layer"
    done

    updatelayernames=
    for layer in $updatelayers; do
        if [ -d "$layer" ]; then
            layer="$(PATH="$OEROOT/scripts:$OEROOT/bitbake/bin:$PATH" $(dirname "$0")/bb-print-layer-data 2>/dev/null "$layer" | cut -d: -f1)"
            layerpaths="$layerpaths:$layer"
        fi
        updatelayernames="$updatelayernames $layer"
    done

    $layerdir/scripts/bb-determine-layers -g "$layerpaths" -l "$CORELAYERS $extralayernames" -o "$EXTRAMELLAYERS $OPTIONALLAYERS" -e "$updatelayernames" -O "$layer_priority_overrides" -m "$MACHINE" -d "$DISTRO" "$@" >$layersfile
    if [ $? -ne 0 ]; then
        echo >&2 "Error in execution of bb-determine-layers, aborting"
        return 1
    fi
    sed -e "s#^$BUILDDIR/#\${TOPDIR}/#g;" -e 's,^,    ,; s,$, \\,' <$layersfile >>$BUILDDIR/conf/bblayers.conf
    echo '"' >> $BUILDDIR/conf/bblayers.conf
    configured_layers="$(PATH="$OEROOT/scripts:$BITBAKEDIR/bin:$PATH" $(dirname "$0")/bb-print-layer-data $(cat $layersfile) 2>/dev/null | cut -d: -f1 | xargs)"

    echo "$layer_priority_overrides" | tr " " "\n" | \
        while IFS== read -r layer priority; do
            case " $configured_layers " in
                *\ $layer\ *)
                    printf 'BBFILE_PRIORITY_%s_%s = "%d"\n' "$layer" "$DISTRO" "$priority" >> $BUILDDIR/conf/bblayers.conf
                    ;;
            esac
        done
}

process_arguments () {
    BUILDDIR=
    MELDIR=
    OEROOT=
    extrasearchpaths=
    extralayers=
    force_overwrite=0
    verbose=0
    while getopts b:o:p:l:fvh opt; do
        case "$opt" in
            b)
                BUILDDIR="$(abspath $OPTARG)"
                ;;
            o)
                MELDIR="$(abspath $OPTARG)"
                ;;
            p)
                extrasearchpaths="$OPTARG"
                ;;
            l)
                extralayers="$extralayers $OPTARG"
                ;;
            f)
                force_overwrite=1
                ;;
            v)
                verbose=1
                ;;
            \?|h)
                usage
                return 1
                ;;
        esac
    done

    if [ -z $BUILDDIR ]; then
        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
    fi

    if [ -z "$MELDIR" ]; then
        MELDIR=$(dirname $layerdir)
    fi

    updatelayers=
    for lconf in $MELDIR/update-*/conf/layer.conf \
                 $MELDIR/*/update-*/conf/layer.conf \
                 $MELDIR/meta-mentor-cve/conf/layer.conf; do
        updatelayer="${lconf%/conf/layer.conf}"
        if [ -e "$updatelayer" ]; then
            updatelayers="$updatelayers $updatelayer"
        fi
    done

    layerpaths="*:*/*:meta*/*/*:$MELDIR/*:$MELDIR/*/*:$MELDIR/meta*/*/*${extrasearchpaths:+:}$extrasearchpaths"
}

setup_builddir () {
    process_arguments "$@" || return $?
    shift $(expr $OPTIND - 1)

    OEROOT="$($layerdir/scripts/bb-determine-layers -g "$layerpaths" -l core 2>/dev/null)/.." || {
        echo >&2 "Error: unable to locate core layer (oe-core) in $MELDIR, aborting"
        return 3
    }

    if [ $# -gt 0 ]; then
        MACHINE="$1"
    fi

    if [ -z "$MACHINE" ]; then
        if [ -e "$MELDIR/.machine" ]; then
            MACHINE="$(cat "$MELDIR/.machine")"
        elif [ -e "$BUILDDIR/conf/local.conf" ]; then
            MACHINE=$(sed -n 's/^MACHINE ?*= "\([^"]*\)"/\1/p' $BUILDDIR/conf/local.conf)
        fi
    fi

    if [ -n "$MACHINE" ]; then
        if [ -e $MELDIR/$MACHINE/binary ]; then
            # User specified machine and we have the binary content for this BSP
            TEMPLATECONF=$MELDIR/$MACHINE/conf
        fi
    else
        if [ ! -e $BUILDDIR/conf/local.conf -o ! -e $BUILDDIR/conf/bblayers.conf ] || [ -z "$MACHINE" ]; then
            if ! [ -t 1 ] || ! [ -t 0 ]; then
                non_interactive=-n
            else
                non_interactive=
            fi
            ls -1d $MELDIR/*/binary 2>/dev/null | sed "s,^$MELDIR/,,; s,/binary$,," | \
                while read machine; do
                    if [ ! -e "$MELDIR/$machine/conf/local.conf.sample" ]; then
                        continue
                    fi
                    echo "$machine"
                done | prompt_choice $non_interactive "Select BSP to use" >$tmpdir/machine || {
                    echo >&2 "Error: unable to prompt for an installed MEL BSP, please specify MACHINE"
                    return 1
                }
            MACHINE="$(cat "$tmpdir/machine")"
            if [ -z "$MACHINE" ]; then
                echo >&2 "Error: unable to locate installed MEL BSP, please specify MACHINE"
                return 1
            else
                TEMPLATECONF=$MELDIR/$MACHINE/conf
            fi
        fi
    fi

    # Pick up qemu layers from meta-mentor-bsp as we do
    # not create separate machines for them
    case ${MACHINE} in
        qemu*)
            OPTIONALLAYERS="$OPTIONALLAYERS mentor-bsp-$MACHINE"
            ;;
    esac


    if [ $force_overwrite -eq 1 ]; then
        rm -f $BUILDDIR/conf/local.conf $BUILDDIR/conf/bblayers.conf
    fi

    if [ -d "$BUILDDIR" ]; then
        existing_builddir=1
    else
        existing_builddir=0
    fi

    mkdir -p $BUILDDIR/conf

    if [ -z "$TEMPLATECONF" ]; then
        if [ -f "$BUILDDIR/conf/templateconf.cfg" ]; then
            TEMPLATECONF="$(cat "$BUILDDIR/conf/templateconf.cfg")"
        elif [ -e "$layerdir/conf/local.conf.sample" ]; then
            TEMPLATECONF="$layerdir/conf"
        elif [ -e "$OEROOT/.templateconf" ]; then
            . "$OEROOT/.templateconf"
            # Convert to absolute path
            TEMPLATECONF="$(cd "$(dirname "$TEMPLATECONF")" && pwd -P)/$(basename "$TEMPLATECONF")"
        else
            echo >&2 "Unable to determine TEMPLATECONF"
            return 1
        fi
    fi

    if [ ! -f "$BUILDDIR/conf/templateconf.cfg" ]; then
        echo "$TEMPLATECONF" >"$BUILDDIR/conf/templateconf.cfg"
    fi

    if [ ! -e $BUILDDIR/conf/local.conf ]; then
        cp $TEMPLATECONF/local.conf.sample $BUILDDIR/conf/local.conf
        echo "You had no local.conf file. This configuration file has therefore been"
        echo "created for you with some default values."

        if [ -h "$MELDIR/toolchains" ]; then
            toolchains_path="$MELDIR/toolchains"
            sedexpr='${MELDIR}/toolchains'
        else
            toolchains_path="$MELDIR/../../toolchains"
            sedexpr='${MELDIR}/../../toolchains'
        fi
        if [ -d "$toolchains_path" ]; then
            sed -i -e "s,^#\?EXTERNAL_TOOLCHAIN.*,TOOLCHAINS_PATH ?= \"$toolchains_path\"," "$BUILDDIR/conf/local.conf"
        fi
    else
        if [ -n "$MACHINE" ]; then
            echo "Configuring existing local.conf for $MACHINE"
        fi
    fi

    if [ ! -e $BUILDDIR/conf/bblayers.conf ]; then
        echo "MELDIR = \"$MELDIR\"" >$BUILDDIR/conf/bblayers.conf
        echo >>$BUILDDIR/conf/bblayers.conf
        cat $TEMPLATECONF/bblayers.conf.sample >>$BUILDDIR/conf/bblayers.conf
        echo "You had no bblayers.conf file. This configuration file has therefore been"
        echo "created for you with some default values."
    else
        if [ -n "$MACHINE" ]; then
            echo "Configuring existing bblayers.conf for $MACHINE"
        fi
    fi

    if [ -d "$MELDIR/bitbake" ]; then
        BITBAKEDIR="$MELDIR/bitbake"
    else
        BITBAKEDIR="$OEROOT/bitbake"
    fi

    if [ -e "$layerdir/pre-setup.$MACHINE" ]; then
        . "$layerdir/pre-setup.$MACHINE"
    fi
    PATH="$OEROOT/scripts:$BITBAKEDIR/bin:$PATH" configure_builddir || return $?

    cat "$scriptsdir/build-setup-environment.in" \
        | sed -e "s#@OEROOT@#$OEROOT#g" \
              -e "s#@BITBAKEDIR@#$BITBAKEDIR#g" \
              -e "s#@TEMPLATECONF@#$TEMPLATECONF#g" >"$BUILDDIR/setup-environment"
}

scriptsdir="$(cd $(dirname $0) && pwd)"
scriptsdir="$(abspath $scriptsdir)"
layerdir="${scriptsdir%/*}"

tmpdir="$(mktemp -d -t "${0##*/}.XXXXX")" || exit 1
trap 'rm -rf "$tmpdir"' EXIT INT TERM

setup_builddir "$@" || {
    if [ "$existing_builddir" = 0 ]; then
        rm -rf "$BUILDDIR"
    fi
    exit $?
}
if [ -e $BUILDDIR/conf/local.conf ]; then
    sed -i -e "s/^DISTRO.*/DISTRO = \'$DISTRO\'/" $BUILDDIR/conf/local.conf
fi