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 [[ -n $f ]] && echo "# $f" >>$basefile || true
228 [[ -n "$label" ]] && echo "$label" >>$basefile
229 [[ -n $f ]] && cat $f >>$basefile || true
232 echo "# }" >>$basefile
233 echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >>$basefile
234 [[ -n $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/../meta-agl-core/conf/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 FRAGMENTS=$(cd $SCRIPTDIR/../templates/base && pwd -P)
421 process_fragments $FRAGMENTS
423 # step 3: fragments for machine
424 process_fragments $(find_machine_dir $MACHINE)
426 # step 4: fragments for features
427 for feature in $FEATURES; do
428 process_fragments $(find_feature_dir $feature)
431 # step 5: sort fragments and append them in destination files
432 FRAGMENTS_BBLAYERS=$(sed 's/ /\n/g' <<<$FRAGMENTS_BBLAYERS | sort)
433 debug "bblayer fragments: $FRAGMENTS_BBLAYERS"
434 info " Config: $BUILDDIR/conf/bblayers.conf"
435 for x in $FRAGMENTS_BBLAYERS; do
437 append_fragment $BUILDDIR/conf/bblayers.conf $file
441 FRAGMENTS_LOCALCONF=$(sed 's/ /\n/g' <<<$FRAGMENTS_LOCALCONF | sort)
442 debug "localconf fragments: $FRAGMENTS_LOCALCONF"
443 info " Config: $BUILDDIR/conf/local.conf"
444 for x in $FRAGMENTS_LOCALCONF; do
446 append_fragment $BUILDDIR/conf/local.conf $file
449 # special fragment to call distro-manifest-generator.sh from
450 # meta-agl-profile-core/recipes-core/distro-build-manifest/distro-build-manifest.bb
451 append_fragment $BUILDDIR/conf/local.conf /dev/stdin "# generated by $(realpath $BASH_SOURCE)" <<-EOF
452 DISTRO_SETUP_MANIFEST = "$(realpath -Ls $BUILDDIR)/$SETUP_MANIFEST"
453 DISTRO_MANIFEST_GENERATOR = "$(dirname $(realpath $BASH_SOURCE))/distro-manifest-generator.sh"
456 FRAGMENTS_SETUP=$(sed 's/ /\n/g' <<<$FRAGMENTS_SETUP | sort)
457 debug "setup fragments: $FRAGMENTS_SETUP"
458 cat <<EOF >$BUILDDIR/conf/setup.sh
461 # this script has been generated by $BASH_SOURCE
463 export MACHINE="$MACHINE"
464 export FEATURES="$FEATURES"
465 export BUILDDIR="$BUILDDIR"
466 export METADIR="$METADIR"
467 export RPMREVISION="$RPMREVISION"
468 export LOCALCONF="$BUILDDIR/conf/local.conf"
470 echo "--- beginning of setup script"
472 info " Setup script: $BUILDDIR/conf/setup.sh"
473 for x in $FRAGMENTS_SETUP; do
475 append_fragment $BUILDDIR/conf/setup.sh $file "echo '--- fragment $file'"
478 append_fragment $BUILDDIR/conf/setup.sh "" "echo '--- end of setup script'"
480 infon " Executing setup script ... "
481 execute_setup $BUILDDIR/conf/setup.sh 2>&1 | tee $BUILDDIR/conf/setup.log
482 [[ ${PIPESTATUS[0]} == 0 ]] && {
484 [[ $VERBOSE == 1 ]] && dump_log $BUILDDIR/conf/setup.log
485 rm $BUILDDIR/conf/setup.sh
488 info "FAIL: please check $BUILDDIR/conf/setup.log"
489 dump_log $BUILDDIR/conf/setup.log
494 ###########################################################################################
496 # check for overwrite
497 [[ $FORCE -eq 1 ]] && rm -f \
498 $BUILDDIR/conf/local.conf \
499 $BUILDDIR/conf/bblayers.conf \
500 $BUILDDIR/conf/templateconf.cfg \
501 $BUILDDIR/conf/setup.* \
504 ####### step 1: generate configuration file #######
506 if [[ -f $BUILDDIR/conf/local.conf || -f $BUILDDIR/conf/bblayers.conf ]]; then
507 info "Configuration files already exist:"
508 for x in $BUILDDIR/conf/local.conf $BUILDDIR/conf/bblayers.conf; do
509 [[ -f $x ]] && info " - $x"
511 info "Skipping configuration files generation."
512 info "Use option -f|--force to overwrite existing configuration."
517 ####### step 2: generate aglsetup.manifest #######
519 infon "Generating setup manifest: $BUILDDIR/$SETUP_MANIFEST ... "
520 for x in /etc/os-release /usr/lib/os-release; do
523 FEATURES_md5=$(echo $FEATURES|md5sum -|awk '{print $1;}')
524 cat <<EOF >$BUILDDIR/$SETUP_MANIFEST
525 # ----------------------------------------------
526 # This fragment has been generated by $SCRIPT at setup time
529 DIST_DISTRO_NAME="AGL"
531 # target machine as passed to $SCRIPT
532 DIST_MACHINE="$MACHINE"
534 # features as resolved by $SCRIPT
535 DIST_FEATURES="$FEATURES"
536 DIST_FEATURES_MD5="${FEATURES_md5}"
538 # build host information deduced from os-release
539 DIST_BUILD_HOST="$(id -un)@$(hostname -f || hostname || hostname -s)"
540 DIST_BUILD_OS="${PRETTY_NAME:-${NAME} ${VERSION} [COMPUTED]}"
543 DIST_METADIR="$METADIR"
546 DIST_SETUP_TS="$(date -u +%Y%m%d_%H%M%S_%Z)"
549 DIST_SETUP_TOPIC="$TOPIC"
551 # ------------ end of $SCRIPT fragment --------
555 ####### step 3: generate agl-init-build-env #######
557 # always generate setup script in builddir: it can be sourced later manually without re-running the setup
558 infon "Generating setup file: $BUILDDIR/agl-init-build-env ... "
560 cat <<EOF >$BUILDDIR/agl-init-build-env
561 export TEMPLATECONF=${METADIR}/meta-agl/meta-agl-core/conf/templates/base
562 . $METADIR/external/poky/oe-init-build-env $BUILDDIR
563 if [ -n "\$DL_DIR" ]; then
564 BB_ENV_PASSTHROUGH_ADDITIONS="\$BB_ENV_PASSTHROUGH_ADDITIONS DL_DIR"
566 if [ -n "\$SSTATE_DIR" ]; then
567 BB_ENV_PASSTHROUGH_ADDITIONS="\$BB_ENV_PASSTHROUGH_ADDITIONS SSTATE_DIR"
569 export BB_ENV_PASSTHROUGH_ADDITIONS
574 ####### step 4: generate output script #######
576 # finally, generate output script if requested by caller
577 if [[ -n "$SETUPSCRIPT" ]]; then
578 debug "generating setupscript in $SETUPSCRIPT"
579 cat <<EOF >$SETUPSCRIPT
580 . $BUILDDIR/agl-init-build-env
584 info "------------ $SCRIPT: Done"