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/*/templates/{machine,feature}); do echo $(basename $(dirname $(dirname $x))); done | sort -u)
57 function list_machines() {
59 for y in $(ls -d $METADIR/$x/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); 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); 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 dir=$METADIR/$x/templates/machine/$machine
120 [[ -d $dir ]] && { echo $dir; return 0; }
125 function find_feature_dir() {
127 for x in $AGL_REPOSITORIES; do
128 dir=$METADIR/$x/templates/feature/$feature
129 [[ -d $dir ]] && { echo $dir; return 0; }
136 Usage: . $SCRIPT [options] [feature [feature [... ]]]
139 Compatibility: bash, zsh, ksh
142 -m|--machine <machine>
144 default: '$DEFAULT_MACHINE'
145 -b|--build <directory>
146 build directory to use
147 default: '$DEFAULT_BUILDDIR'
148 -s|--script <filename>
149 file where setup script is generated
150 default: none (no script)
152 flag to force overwriting any existing configuration
154 -r|--rpm-revision <schema>
155 Specify how to handle RPM packages revisions
157 'prservice[:<address>]' : Use a PR service daemon.
158 if <address> is not specified, the default value 'localhost:0'
159 is used (shortcut for a PR service started by bitbake)
160 'timestamp' : Use a generated time stamp (UTC).
161 'value:<revision>' : Use <revision> explicitly.
164 Specify an optional topic for this setup.
165 If specified, the topic will be propagated in build manifests:
166 - in deployment dir: tmp/deploy/images/*/build-info
167 - in target image: /etc/platform-info/build
168 - in SDK: tmp/deploy/sdk/*.build-info
173 display version, set AGLSETUP_VERSION variable with version value and exit
176 for early debug, set env variable DEBUG.
178 DEBUG=true source aglsetup.sh -V
186 echo "Available machines:" >&2
187 for x in $AGL_REPOSITORIES; do
188 buf=$(list_machines $x)
189 [[ -z "$buf" ]] && continue
192 [[ $y == $DEFAULT_MACHINE ]] && def="* " || def=" "
198 echo "Available features:" >&2
199 for x in $AGL_REPOSITORIES; do
200 buf=$(list_features $x)
201 [[ -z "$buf" ]] && continue
203 for feature in $buf; do
204 print_feature="$feature"
205 featuredir=$(find_feature_dir $feature)
206 if [ -e $featuredir/included.dep ];then
207 print_feature="$print_feature :($(find_feature_dependency $feature $feature))"
209 echo " $print_feature"
215 function append_fragment() {
216 basefile=$1; shift # output file
217 f=$1; shift # input file
220 debug "adding fragment to $basefile: $f"
222 echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >>$basefile
223 echo "# fragment { " >>$basefile
224 [[ -f $f ]] && echo "# $f" >>$basefile || true
226 [[ -n "$label" ]] && echo "$label" >>$basefile
227 [[ -f $f ]] && cat $f >>$basefile || true
230 echo "# }" >>$basefile
231 echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >>$basefile
232 [[ -f $f ]] && echo $f >>$BUILDDIR/conf/fragments.log || true
235 function execute_setup() {
237 debug "Executing script $script"
239 $DEBUG && opts="$opts -x"
240 pushd $BUILDDIR &>/dev/null
241 $BASH $opts $script \
243 || { rc=$?; error "Script $script failed"; }
249 # process all fragments
250 FRAGMENTS_BBLAYERS=""
251 FRAGMENTS_LOCALCONF=""
253 function process_fragments() {
255 debug "processing fragments in dir $dir"
257 verbose " Searching fragments: $dir"
259 # lookup for files with priorities specified: something like xx_bblayers.conf.yyyyy.inc
260 for x in $(ls $dir/??[._]bblayers.conf*.inc 2>/dev/null); do
261 FRAGMENTS_BBLAYERS="$FRAGMENTS_BBLAYERS $(basename $x):$x"
262 verbose " priority $(basename $x | cut -c1-2): $(basename $x)"
265 # same for local.conf
266 for x in $(ls $dir/??[._]local.conf*.inc 2>/dev/null); do
267 FRAGMENTS_LOCALCONF="$FRAGMENTS_LOCALCONF $(basename $x):$x"
268 verbose " priority $(basename $x | cut -c1-2): $(basename $x)"
272 for x in $(ls $dir/??[._]setup*.sh 2>/dev/null); do
273 FRAGMENTS_SETUP="$FRAGMENTS_SETUP $(basename $x):$x"
274 verbose " priority $(basename $x | cut -c1-2): $(basename $x)"
279 function containsFeature () {
280 for feature in $1; do
281 [[ "$feature" == "$2" ]] && return 1;
286 function find_feature_dependency() {
288 featuredir=$(find_feature_dir $1)
290 if [ -e $featuredir/included.dep ]; then
291 dep_features="$(cat $featuredir/included.dep)"
292 for dep_feature in $dep_features; do
293 full_feature="$full_feature $res_dep_features"
294 res_dep_features="$res_dep_features $dep_feature"
295 if containsFeature $dep_feature $full_feature ; then
296 res_dep_features="$res_dep_features $(find_feature_dependency $dep_feature $full_feature)"
300 echo "$res_dep_features";
305 debug "Parsing arguments: $@"
306 TEMP=$(getopt -o m:b:r:t:s:fvVdh --long machine:,builddir:,rpm-revision:,topic:,script:,force,verbose,version,debug,help -n $SCRIPT -- "$@")
307 [[ $? != 0 ]] && { usage; exit 1; }
312 ### default options values
313 MACHINE=$DEFAULT_MACHINE
314 BUILDDIR=$DEFAULT_BUILDDIR
319 SETUP_MANIFEST=aglsetup.manifest
323 -m|--machine) MACHINE=$2; shift 2;;
324 -b|--builddir) BUILDDIR=$2; shift 2;;
325 -s|--setupscript) SETUPSCRIPT=$2; shift 2;;
326 -f|--force) FORCE=1; shift;;
327 -r|--rpm-revision) RPMREVISION=$2; shift 2;;
328 -t|--topic) TOPIC=$2; shift 2;;
329 -v|--verbose) VERBOSE=1; shift;;
330 -V|--version) SHOWVERSION=1; shift;;
331 -d|--debug) VERBOSE=1; DEBUG=true; shift;;
332 -h|--help) HELP=1; shift;;
334 *) error "Arguments parsing error"; exit 1;;
338 [[ "$HELP" == 1 ]] && { usage; exit 0; }
340 if [[ "$SHOWVERSION" == 1 ]]; then
341 # display version on stdout
344 # generate output script if requested by caller
345 if [[ -n "$SETUPSCRIPT" ]]; then
346 cat <<EOF >$SETUPSCRIPT
347 AGLSETUP_VERSION=$VERSION
351 # IMPORTANT: exit successfully
352 # older aglsetup scripts with version <1.2.0 will fail with option --version
356 info "------------ $SCRIPT: Starting"
358 verbose "Command line arguments: ${GLOBAL_ARGS[@]}"
360 # the remaining args are the features
363 # validate the machine list
364 debug "validating machines list"
367 # validate the machine
368 debug "validating machine $MACHINE"
369 find_machine_dir $MACHINE >/dev/null || error "Machine '$MACHINE' not found in [ $(list_all_machines)]"
371 # validate the features list
372 debug "validating features list"
376 for FEATURE in $FEATURES;do
377 TMP_FEATURES="$TMP_FEATURES $FEATURE"
378 TMP_FEATURES="$TMP_FEATURES $(find_feature_dependency $FEATURE $TMP_FEATURES)"
380 # remove duplicate features if any
381 FEATURES=$(for x in $TMP_FEATURES; do echo $x; done | sort -u | awk '{printf("%s ",$1);}')
383 # validate the features
384 for f in $FEATURES; do
385 debug "validating feature $f"
386 find_feature_dir $f >/dev/null || error "Feature '$f' not found in [ $(list_all_features)]"
390 debug "validating builddir $BUILDDIR"
391 BUILDDIR=$(mkdir -p "$BUILDDIR" && cd "$BUILDDIR" && pwd -P)
394 ###########################################################################################
395 function dump_log() {
396 info " ------------ $(basename $1) -----------------"
398 info " ----------------------------------------"
401 function genconfig() {
402 info "Generating configuration files:"
403 info " Build dir: $BUILDDIR"
404 info " Machine: $MACHINE"
405 info " Features: $FEATURES"
407 # step 1: run usual OE setup to generate conf dir
408 export TEMPLATECONF=$(cd $SCRIPTDIR/../templates/base && pwd -P)
409 debug "running oe-init-build-env with TEMPLATECONF=$TEMPLATECONF"
410 info " Running $METADIR/poky/oe-init-build-env"
411 info " Templates dir: $TEMPLATECONF"
414 . $METADIR/poky/oe-init-build-env $BUILDDIR >/dev/null
417 # step 2: concatenate other remaining fragments coming from base
418 process_fragments $TEMPLATECONF
420 # step 3: fragments for machine
421 process_fragments $(find_machine_dir $MACHINE)
423 # step 4: fragments for features
424 for feature in $FEATURES; do
425 process_fragments $(find_feature_dir $feature)
428 # step 5: sort fragments and append them in destination files
429 FRAGMENTS_BBLAYERS=$(sed 's/ /\n/g' <<<$FRAGMENTS_BBLAYERS | sort)
430 debug "bblayer fragments: $FRAGMENTS_BBLAYERS"
431 info " Config: $BUILDDIR/conf/bblayers.conf"
432 for x in $FRAGMENTS_BBLAYERS; do
434 append_fragment $BUILDDIR/conf/bblayers.conf $file
438 FRAGMENTS_LOCALCONF=$(sed 's/ /\n/g' <<<$FRAGMENTS_LOCALCONF | sort)
439 debug "localconf fragments: $FRAGMENTS_LOCALCONF"
440 info " Config: $BUILDDIR/conf/local.conf"
441 for x in $FRAGMENTS_LOCALCONF; do
443 append_fragment $BUILDDIR/conf/local.conf $file
446 # special fragment to call distro-manifest-generator.sh from
447 # meta-agl-profile-core/recipes-core/distro-build-manifest/distro-build-manifest.bb
448 append_fragment $BUILDDIR/conf/local.conf /dev/stdin "# generated by $(realpath $BASH_SOURCE)" <<-EOF
449 DISTRO_SETUP_MANIFEST = "$(realpath -Ls $BUILDDIR)/$SETUP_MANIFEST"
450 DISTRO_MANIFEST_GENERATOR = "$(dirname $(realpath $BASH_SOURCE))/distro-manifest-generator.sh"
453 FRAGMENTS_SETUP=$(sed 's/ /\n/g' <<<$FRAGMENTS_SETUP | sort)
454 debug "setup fragments: $FRAGMENTS_SETUP"
455 cat <<EOF >$BUILDDIR/conf/setup.sh
458 # this script has been generated by $BASH_SOURCE
460 export MACHINE="$MACHINE"
461 export FEATURES="$FEATURES"
462 export BUILDDIR="$BUILDDIR"
463 export METADIR="$METADIR"
464 export RPMREVISION="$RPMREVISION"
465 export LOCALCONF="$BUILDDIR/conf/local.conf"
467 echo "--- beginning of setup script"
469 info " Setup script: $BUILDDIR/conf/setup.sh"
470 for x in $FRAGMENTS_SETUP; do
472 append_fragment $BUILDDIR/conf/setup.sh $file "echo '--- fragment $file'"
475 append_fragment $BUILDDIR/conf/setup.sh "" "echo '--- end of setup script'"
477 infon " Executing setup script ... "
478 execute_setup $BUILDDIR/conf/setup.sh 2>&1 | tee $BUILDDIR/conf/setup.log
479 [[ ${PIPESTATUS[0]} == 0 ]] && {
481 [[ $VERBOSE == 1 ]] && dump_log $BUILDDIR/conf/setup.log
482 rm $BUILDDIR/conf/setup.sh
485 info "FAIL: please check $BUILDDIR/conf/setup.log"
486 dump_log $BUILDDIR/conf/setup.log
491 ###########################################################################################
493 # check for overwrite
494 [[ $FORCE -eq 1 ]] && rm -f \
495 $BUILDDIR/conf/local.conf \
496 $BUILDDIR/conf/bblayers.conf \
497 $BUILDDIR/conf/setup.* \
500 ####### step 1: generate configuration file #######
502 if [[ -f $BUILDDIR/conf/local.conf || -f $BUILDDIR/conf/bblayers.conf ]]; then
503 info "Configuration files already exist:"
504 for x in $BUILDDIR/conf/local.conf $BUILDDIR/conf/bblayers.conf; do
505 [[ -f $x ]] && info " - $x"
507 info "Skipping configuration files generation."
508 info "Use option -f|--force to overwrite existing configuration."
513 ####### step 2: generate aglsetup.manifest #######
515 infon "Generating setup manifest: $BUILDDIR/$SETUP_MANIFEST ... "
516 for x in /etc/os-release /usr/lib/os-release; do
519 FEATURES_md5=$(echo $FEATURES|md5sum -|awk '{print $1;}')
520 cat <<EOF >$BUILDDIR/$SETUP_MANIFEST
521 # ----------------------------------------------
522 # This fragment has been generated by $SCRIPT at setup time
525 DIST_DISTRO_NAME="AGL"
527 # target machine as passed to $SCRIPT
528 DIST_MACHINE="$MACHINE"
530 # features as resolved by $SCRIPT
531 DIST_FEATURES="$FEATURES"
532 DIST_FEATURES_MD5="${FEATURES_md5}"
534 # build host information deduced from os-release
535 DIST_BUILD_HOST="$(id -un)@$(hostname -f || hostname || hostname -s)"
536 DIST_BUILD_OS="${PRETTY_NAME:-${NAME} ${VERSION} [COMPUTED]}"
539 DIST_METADIR="$METADIR"
542 DIST_SETUP_TS="$(date -u +%Y%m%d_%H%M%S_%Z)"
545 DIST_SETUP_TOPIC="$TOPIC"
547 # ------------ end of $SCRIPT fragment --------
551 ####### step 3: generate agl-init-build-env #######
553 # always generate setup script in builddir: it can be sourced later manually without re-running the setup
554 infon "Generating setup file: $BUILDDIR/agl-init-build-env ... "
556 cat <<EOF >$BUILDDIR/agl-init-build-env
557 . $METADIR/poky/oe-init-build-env $BUILDDIR
558 if [ -n "\$DL_DIR" ]; then
559 BB_ENV_EXTRAWHITE="\$BB_ENV_EXTRAWHITE DL_DIR"
561 if [ -n "\$SSTATE_DIR" ]; then
562 BB_ENV_EXTRAWHITE="\$BB_ENV_EXTRAWHITE SSTATE_DIR"
564 export BB_ENV_EXTRAWHITE
569 ####### step 4: generate output script #######
571 # finally, generate output script if requested by caller
572 if [[ -n "$SETUPSCRIPT" ]]; then
573 debug "generating setupscript in $SETUPSCRIPT"
574 cat <<EOF >$SETUPSCRIPT
575 . $BUILDDIR/agl-init-build-env
579 info "------------ $SCRIPT: Done"