3 ################################################################################
5 # The MIT License (MIT)
7 # Copyright (c) 2016-2019 Stéphane Desneux <sdx@iot.bzh>
8 # (c) 2016 Jan-Simon Möller <jsmoeller@linuxfoundation.org>
10 # Permission is hereby granted, free of charge, to any person obtaining a copy
11 # of this software and associated documentation files (the "Software"), to deal
12 # in the Software without restriction, including without limitation the rights
13 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 # copies of the Software, and to permit persons to whom the Software is
15 # furnished to do so, subject to the following conditions:
17 # The above copyright notice and this permission notice shall be included in
18 # all copies or substantial portions of the Software.
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 ################################################################################
30 # this script shouldn't be called directly, but through aglsetup.sh that will in
31 # turn execute (source) generated instructions back in the parent shell,
32 # whether it's bash, zsh, or any other supported shell
35 DEFAULT_MACHINE=qemux86-64
36 DEFAULT_BUILDDIR=./build
41 #SCRIPT=$(basename $BASH_SOURCE)
43 SCRIPTDIR=$(cd $(dirname $BASH_SOURCE) && pwd -P)
44 METADIR=$(cd $(dirname $BASH_SOURCE)/../.. && pwd -P)
46 function info() { echo "$@" >&2; }
47 function infon() { echo -n "$@" >&2; }
48 function error() { echo "ERROR: $@" >&2; return 1; }
49 function verbose() { [[ $VERBOSE == 1 ]] && echo "$@" >&2; return 0; }
50 function debug() { $DEBUG && echo "DEBUG: $@" >&2; return 0;}
52 debug "------------ $SCRIPT: starting with command line arguments: $@"
54 #compute AGL_REPOSITORIES
55 AGL_REPOSITORIES=$(for x in $(ls -d $METADIR/meta-*/templates/{machine,feature} $METADIR/bsp/*/templates/machine 2>/dev/null); do echo $(basename $(dirname $(dirname $x))); done | sort -u)
57 function list_machines() {
59 for y in $(ls -d $METADIR/{.,bsp}/$a/templates/machine/* 2>/dev/null); do
65 function list_all_machines() {
66 for x in $AGL_REPOSITORIES; do
71 function validate_builddir() {
72 if [[ "$BUILDDIR" =~ [[:space:]] ]]; then
73 error "Build dir '$BUILDDIR' shouldn't contain any space"
75 debug "Build dir is valid"
78 function validate_machines() {
79 list_all_machines | sort | uniq -c | while read cnt machine; do
80 [[ $cnt == 1 ]] && continue
81 info "Machine $machine found in the following repositories:"
82 for x in $(ls -d $METADIR/*/templates/machine/$machine $METADIR/bsp/*/templates/machine/$machine 2>/dev/null); do
85 error "Multiple machine templates are not allowed"
87 debug "Machines list has no duplicate."
90 function list_features() {
92 for y in $(ls -d $METADIR/$x/templates/feature/* 2>/dev/null); do
98 function list_all_features() {
99 for x in $AGL_REPOSITORIES; do
104 function validate_features() {
105 list_all_features | sort | uniq -c | while read cnt feature; do
106 [[ $cnt == 1 ]] && continue;
107 info "Feature $feature found in the following repositories:"
108 for x in $(ls -d $METADIR/*/templates/feature/$feature 2>/dev/null); do
111 error "Multiple feature templates are not allowed"
113 debug "Features list has no duplicate."
116 function find_machine_dir() {
118 for x in $AGL_REPOSITORIES; do
119 dirs=$(ls -d $METADIR/{.,bsp}/$x/templates/machine/$machine 2>/dev/null)
121 [[ -d $dir ]] && { echo $dir; return 0; }
127 function find_feature_dir() {
129 for x in $AGL_REPOSITORIES; do
130 dir=$METADIR/$x/templates/feature/$feature
131 [[ -d $dir ]] && { echo $dir; return 0; }
138 Usage: . $SCRIPT [options] [feature [feature [... ]]]
141 Compatibility: bash, zsh, ksh
144 -m|--machine <machine>
146 default: '$DEFAULT_MACHINE'
147 -b|--build <directory>
148 build directory to use
149 default: '$DEFAULT_BUILDDIR'
150 -s|--script <filename>
151 file where setup script is generated
152 default: none (no script)
154 flag to force overwriting any existing configuration
156 -r|--rpm-revision <schema>
157 Specify how to handle RPM packages revisions
159 'prservice[:<address>]' : Use a PR service daemon.
160 if <address> is not specified, the default value 'localhost:0'
161 is used (shortcut for a PR service started by bitbake)
162 'timestamp' : Use a generated time stamp (UTC).
163 'value:<revision>' : Use <revision> explicitly.
166 Specify an optional topic for this setup.
167 If specified, the topic will be propagated in build manifests:
168 - in deployment dir: tmp/deploy/images/*/build-info
169 - in target image: /etc/platform-info/build
170 - in SDK: tmp/deploy/sdk/*.build-info
175 display version, set AGLSETUP_VERSION variable with version value and exit
178 for early debug, set env variable DEBUG.
180 DEBUG=true source aglsetup.sh -V
188 echo "Available machines:" >&2
189 for x in $AGL_REPOSITORIES; do
190 buf=$(list_machines $x)
191 [[ -z "$buf" ]] && continue
194 [[ $y == $DEFAULT_MACHINE ]] && def="* " || def=" "
200 echo "Available features:" >&2
201 for x in $AGL_REPOSITORIES; do
202 buf=$(list_features $x)
203 [[ -z "$buf" ]] && continue
205 for feature in $buf; do
206 print_feature="$feature"
207 featuredir=$(find_feature_dir $feature)
208 if [ -e $featuredir/included.dep ];then
209 print_feature="$print_feature :($(find_feature_dependency $feature $feature))"
211 echo " $print_feature"
217 function append_fragment() {
218 basefile=$1; shift # output file
219 f=$1; shift # input file
222 debug "adding fragment to $basefile: $f"
224 echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >>$basefile
225 echo "# fragment { " >>$basefile
226 [[ -f $f ]] && echo "# $f" >>$basefile || true
228 [[ -n "$label" ]] && echo "$label" >>$basefile
229 [[ -f $f ]] && cat $f >>$basefile || true
232 echo "# }" >>$basefile
233 echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >>$basefile
234 [[ -f $f ]] && echo $f >>$BUILDDIR/conf/fragments.log || true
237 function execute_setup() {
239 debug "Executing script $script"
241 $DEBUG && opts="$opts -x"
242 pushd $BUILDDIR &>/dev/null
243 $BASH $opts $script \
245 || { rc=$?; error "Script $script failed"; }
251 # process all fragments
252 FRAGMENTS_BBLAYERS=""
253 FRAGMENTS_LOCALCONF=""
255 function process_fragments() {
257 debug "processing fragments in dir $dir"
259 verbose " Searching fragments: $dir"
261 # lookup for files with priorities specified: something like xx_bblayers.conf.yyyyy.inc
262 for x in $(ls $dir/??[._]bblayers.conf*.inc 2>/dev/null); do
263 FRAGMENTS_BBLAYERS="$FRAGMENTS_BBLAYERS $(basename $x):$x"
264 verbose " priority $(basename $x | cut -c1-2): $(basename $x)"
267 # same for local.conf
268 for x in $(ls $dir/??[._]local.conf*.inc 2>/dev/null); do
269 FRAGMENTS_LOCALCONF="$FRAGMENTS_LOCALCONF $(basename $x):$x"
270 verbose " priority $(basename $x | cut -c1-2): $(basename $x)"
274 for x in $(ls $dir/??[._]setup*.sh 2>/dev/null); do
275 FRAGMENTS_SETUP="$FRAGMENTS_SETUP $(basename $x):$x"
276 verbose " priority $(basename $x | cut -c1-2): $(basename $x)"
281 function containsFeature () {
282 for feature in $1; do
283 [[ "$feature" == "$2" ]] && return 1;
288 function find_feature_dependency() {
290 featuredir=$(find_feature_dir $1)
292 if [ -e $featuredir/included.dep ]; then
293 dep_features="$(cat $featuredir/included.dep)"
294 for dep_feature in $dep_features; do
295 full_feature="$full_feature $res_dep_features"
296 res_dep_features="$res_dep_features $dep_feature"
297 if containsFeature $dep_feature $full_feature ; then
298 res_dep_features="$res_dep_features $(find_feature_dependency $dep_feature $full_feature)"
302 echo "$res_dep_features";
307 debug "Parsing arguments: $@"
308 TEMP=$(getopt -o m:b:r:t:s:fvVdh --long machine:,builddir:,rpm-revision:,topic:,script:,force,verbose,version,debug,help -n $SCRIPT -- "$@")
309 [[ $? != 0 ]] && { usage; exit 1; }
314 ### default options values
315 MACHINE=$DEFAULT_MACHINE
316 BUILDDIR=$DEFAULT_BUILDDIR
321 SETUP_MANIFEST=aglsetup.manifest
325 -m|--machine) MACHINE=$2; shift 2;;
326 -b|--builddir) BUILDDIR=$2; shift 2;;
327 -s|--setupscript) SETUPSCRIPT=$2; shift 2;;
328 -f|--force) FORCE=1; shift;;
329 -r|--rpm-revision) RPMREVISION=$2; shift 2;;
330 -t|--topic) TOPIC=$2; shift 2;;
331 -v|--verbose) VERBOSE=1; shift;;
332 -V|--version) SHOWVERSION=1; shift;;
333 -d|--debug) VERBOSE=1; DEBUG=true; shift;;
334 -h|--help) HELP=1; shift;;
336 *) error "Arguments parsing error"; exit 1;;
340 [[ "$HELP" == 1 ]] && { usage; exit 0; }
342 if [[ "$SHOWVERSION" == 1 ]]; then
343 # display version on stdout
346 # generate output script if requested by caller
347 if [[ -n "$SETUPSCRIPT" ]]; then
348 cat <<EOF >$SETUPSCRIPT
349 AGLSETUP_VERSION=$VERSION
353 # IMPORTANT: exit successfully
354 # older aglsetup scripts with version <1.2.0 will fail with option --version
358 info "------------ $SCRIPT: Starting"
360 verbose "Command line arguments: ${GLOBAL_ARGS[@]}"
362 # the remaining args are the features
365 # validate the machine list
366 debug "validating machines list"
369 # validate the machine
370 debug "validating machine $MACHINE"
371 find_machine_dir $MACHINE >/dev/null || error "Machine '$MACHINE' not found in [ $(list_all_machines)]"
373 # validate the features list
374 debug "validating features list"
378 for FEATURE in $FEATURES;do
379 TMP_FEATURES="$TMP_FEATURES $FEATURE"
380 TMP_FEATURES="$TMP_FEATURES $(find_feature_dependency $FEATURE $TMP_FEATURES)"
382 # remove duplicate features if any
383 FEATURES=$(for x in $TMP_FEATURES; do echo $x; done | sort -u | awk '{printf("%s ",$1);}')
385 # validate the features
386 for f in $FEATURES; do
387 debug "validating feature $f"
388 find_feature_dir $f >/dev/null || error "Feature '$f' not found in [ $(list_all_features)]"
392 debug "validating builddir $BUILDDIR"
393 BUILDDIR=$(mkdir -p "$BUILDDIR" && cd "$BUILDDIR" && pwd -P)
396 ###########################################################################################
397 function dump_log() {
398 info " ------------ $(basename $1) -----------------"
400 info " ----------------------------------------"
403 function genconfig() {
404 info "Generating configuration files:"
405 info " Build dir: $BUILDDIR"
406 info " Machine: $MACHINE"
407 info " Features: $FEATURES"
409 # step 1: run usual OE setup to generate conf dir
410 export TEMPLATECONF=$(cd $SCRIPTDIR/../templates/base && pwd -P)
411 debug "running oe-init-build-env with TEMPLATECONF=$TEMPLATECONF"
412 info " Running $METADIR/external/poky/oe-init-build-env"
413 info " Templates dir: $TEMPLATECONF"
416 . $METADIR/external/poky/oe-init-build-env $BUILDDIR >/dev/null
419 # step 2: concatenate other remaining fragments coming from base
420 process_fragments $TEMPLATECONF
422 # step 3: fragments for machine
423 process_fragments $(find_machine_dir $MACHINE)
425 # step 4: fragments for features
426 for feature in $FEATURES; do
427 process_fragments $(find_feature_dir $feature)
430 # step 5: sort fragments and append them in destination files
431 FRAGMENTS_BBLAYERS=$(sed 's/ /\n/g' <<<$FRAGMENTS_BBLAYERS | sort)
432 debug "bblayer fragments: $FRAGMENTS_BBLAYERS"
433 info " Config: $BUILDDIR/conf/bblayers.conf"
434 for x in $FRAGMENTS_BBLAYERS; do
436 append_fragment $BUILDDIR/conf/bblayers.conf $file
440 FRAGMENTS_LOCALCONF=$(sed 's/ /\n/g' <<<$FRAGMENTS_LOCALCONF | sort)
441 debug "localconf fragments: $FRAGMENTS_LOCALCONF"
442 info " Config: $BUILDDIR/conf/local.conf"
443 for x in $FRAGMENTS_LOCALCONF; do
445 append_fragment $BUILDDIR/conf/local.conf $file
448 # special fragment to call distro-manifest-generator.sh from
449 # meta-agl-profile-core/recipes-core/distro-build-manifest/distro-build-manifest.bb
450 append_fragment $BUILDDIR/conf/local.conf /dev/stdin "# generated by $(realpath $BASH_SOURCE)" <<-EOF
451 DISTRO_SETUP_MANIFEST = "$(realpath -Ls $BUILDDIR)/$SETUP_MANIFEST"
452 DISTRO_MANIFEST_GENERATOR = "$(dirname $(realpath $BASH_SOURCE))/distro-manifest-generator.sh"
455 FRAGMENTS_SETUP=$(sed 's/ /\n/g' <<<$FRAGMENTS_SETUP | sort)
456 debug "setup fragments: $FRAGMENTS_SETUP"
457 cat <<EOF >$BUILDDIR/conf/setup.sh
460 # this script has been generated by $BASH_SOURCE
462 export MACHINE="$MACHINE"
463 export FEATURES="$FEATURES"
464 export BUILDDIR="$BUILDDIR"
465 export METADIR="$METADIR"
466 export RPMREVISION="$RPMREVISION"
467 export LOCALCONF="$BUILDDIR/conf/local.conf"
469 echo "--- beginning of setup script"
471 info " Setup script: $BUILDDIR/conf/setup.sh"
472 for x in $FRAGMENTS_SETUP; do
474 append_fragment $BUILDDIR/conf/setup.sh $file "echo '--- fragment $file'"
477 append_fragment $BUILDDIR/conf/setup.sh "" "echo '--- end of setup script'"
479 infon " Executing setup script ... "
480 execute_setup $BUILDDIR/conf/setup.sh 2>&1 | tee $BUILDDIR/conf/setup.log
481 [[ ${PIPESTATUS[0]} == 0 ]] && {
483 [[ $VERBOSE == 1 ]] && dump_log $BUILDDIR/conf/setup.log
484 rm $BUILDDIR/conf/setup.sh
487 info "FAIL: please check $BUILDDIR/conf/setup.log"
488 dump_log $BUILDDIR/conf/setup.log
493 ###########################################################################################
495 # check for overwrite
496 [[ $FORCE -eq 1 ]] && rm -f \
497 $BUILDDIR/conf/local.conf \
498 $BUILDDIR/conf/bblayers.conf \
499 $BUILDDIR/conf/setup.* \
502 ####### step 1: generate configuration file #######
504 if [[ -f $BUILDDIR/conf/local.conf || -f $BUILDDIR/conf/bblayers.conf ]]; then
505 info "Configuration files already exist:"
506 for x in $BUILDDIR/conf/local.conf $BUILDDIR/conf/bblayers.conf; do
507 [[ -f $x ]] && info " - $x"
509 info "Skipping configuration files generation."
510 info "Use option -f|--force to overwrite existing configuration."
515 ####### step 2: generate aglsetup.manifest #######
517 infon "Generating setup manifest: $BUILDDIR/$SETUP_MANIFEST ... "
518 for x in /etc/os-release /usr/lib/os-release; do
521 FEATURES_md5=$(echo $FEATURES|md5sum -|awk '{print $1;}')
522 cat <<EOF >$BUILDDIR/$SETUP_MANIFEST
523 # ----------------------------------------------
524 # This fragment has been generated by $SCRIPT at setup time
527 DIST_DISTRO_NAME="AGL"
529 # target machine as passed to $SCRIPT
530 DIST_MACHINE="$MACHINE"
532 # features as resolved by $SCRIPT
533 DIST_FEATURES="$FEATURES"
534 DIST_FEATURES_MD5="${FEATURES_md5}"
536 # build host information deduced from os-release
537 DIST_BUILD_HOST="$(id -un)@$(hostname -f || hostname || hostname -s)"
538 DIST_BUILD_OS="${PRETTY_NAME:-${NAME} ${VERSION} [COMPUTED]}"
541 DIST_METADIR="$METADIR"
544 DIST_SETUP_TS="$(date -u +%Y%m%d_%H%M%S_%Z)"
547 DIST_SETUP_TOPIC="$TOPIC"
549 # ------------ end of $SCRIPT fragment --------
553 ####### step 3: generate agl-init-build-env #######
555 # always generate setup script in builddir: it can be sourced later manually without re-running the setup
556 infon "Generating setup file: $BUILDDIR/agl-init-build-env ... "
558 cat <<EOF >$BUILDDIR/agl-init-build-env
559 . $METADIR/external/poky/oe-init-build-env $BUILDDIR
560 if [ -n "\$DL_DIR" ]; then
561 BB_ENV_EXTRAWHITE="\$BB_ENV_EXTRAWHITE DL_DIR"
563 if [ -n "\$SSTATE_DIR" ]; then
564 BB_ENV_EXTRAWHITE="\$BB_ENV_EXTRAWHITE SSTATE_DIR"
566 export BB_ENV_EXTRAWHITE
571 ####### step 4: generate output script #######
573 # finally, generate output script if requested by caller
574 if [[ -n "$SETUPSCRIPT" ]]; then
575 debug "generating setupscript in $SETUPSCRIPT"
576 cat <<EOF >$SETUPSCRIPT
577 . $BUILDDIR/agl-init-build-env
581 info "------------ $SCRIPT: Done"