Append a new empty line to features' local template file when setting up AGL
[AGL/meta-agl.git] / scripts / .aglsetup_genconfig.bash
1 #!/bin/bash
2
3 ################################################################################
4 #
5 # The MIT License (MIT)
6 #
7 # Copyright (c) 2016 Stéphane Desneux <sdx@iot.bzh>
8 #           (c) 2016 Jan-Simon Möller <jsmoeller@linuxfoundation.org>
9 #
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:
16 #
17 # The above copyright notice and this permission notice shall be included in
18 # all copies or substantial portions of the Software.
19 #
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
26 # SOFTWARE.
27 #
28 ################################################################################
29
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
33
34 VERSION=1.1.0
35 DEFAULT_MACHINE=qemux86-64
36 DEFAULT_BUILDDIR=./build
37 VERBOSE=0
38 DEBUG=0
39
40 #SCRIPT=$(basename $BASH_SOURCE)
41 SCRIPT=aglsetup.sh
42 SCRIPTDIR=$(cd $(dirname $BASH_SOURCE) && pwd -P)
43 METADIR=$(cd $(dirname $BASH_SOURCE)/../.. && pwd -P)
44
45 function info() { echo "$@" >&2; }
46 function infon() { echo -n "$@" >&2; }
47 function error() { echo "ERROR: $@" >&2; return 1; }
48 function verbose() { [[ $VERBOSE == 1 ]] && echo "$@" >&2; return 0; }
49 function debug() { [[ $DEBUG == 1 ]] && echo "DEBUG: $@" >&2; return 0;}
50
51 info "------------ $SCRIPT: Starting"
52
53 #compute AGL_REPOSITORIES
54 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)
55
56 function list_machines() {
57         for a in $@; do
58                 for y in $(ls -d $METADIR/{.,bsp}/$a/templates/machine/* 2>/dev/null); do
59                         echo $(basename $y)
60                 done
61         done
62 }
63
64 function list_all_machines() {
65         for x in $AGL_REPOSITORIES; do
66                 list_machines $x
67         done
68 }
69
70 function validate_builddir() {
71         if [[ "$BUILDDIR" =~ [[:space:]] ]]; then
72                 error "Build dir '$BUILDDIR' shouldn't contain any space"
73         fi
74         debug "Build dir is valid"
75 }
76
77 function validate_machines() {
78         list_all_machines | sort | uniq -c | while read cnt machine; do
79                 [[ $cnt == 1 ]] && continue
80                 info "Machine $machine found in the following repositories:"
81                 for x in $(ls -d $METADIR/*/templates/machine/$machine $METADIR/bsp/*/templates/machine/$machine 2>/dev/null); do
82                         info "   - $x"
83                 done
84                 error "Multiple machine templates are not allowed"
85         done
86         debug "Machines list has no duplicate."
87 }
88
89 function list_features() {
90         for x in $@; do
91                 for y in $(ls -d $METADIR/$x/templates/feature/* 2>/dev/null); do
92                         echo $(basename $y)
93                 done
94         done
95 }
96
97 function list_all_features() {
98         for x in $AGL_REPOSITORIES; do
99                 list_features $x
100         done
101 }
102
103 function validate_features() {
104         list_all_features | sort | uniq -c | while read cnt feature; do
105                 [[ $cnt == 1 ]] && continue;
106                 info "Feature $feature found in the following repositories:"
107                 for x in $(ls -d $METADIR/*/templates/feature/$feature 2>/dev/null); do
108                         info "   - $x"
109                 done
110                 error "Multiple feature templates are not allowed"
111         done
112         debug "Features list has no duplicate."
113 }
114
115 function find_machine_dir() {
116         machine=$1
117         for x in $AGL_REPOSITORIES; do
118                 dirs=$(ls -d $METADIR/{.,bsp}/$x/templates/machine/$machine 2>/dev/null)
119                 for dir in $dirs; do
120                     [[ -d $dir ]] && { echo $dir; return 0; }
121                 done
122         done
123         return 1
124 }
125
126 function find_feature_dir() {
127         feature=$1
128         for x in $AGL_REPOSITORIES; do
129                 dir=$METADIR/$x/templates/feature/$feature
130                 [[ -d $dir ]] && { echo $dir; return 0; }
131         done
132         return 1
133 }
134
135 function usage() {
136     cat <<EOF >&2
137 Usage: . $SCRIPT [options] [feature [feature [... ]]]
138
139 Version: $VERSION
140 Compatibility: bash, zsh, ksh
141
142 Options:
143    -m|--machine <machine>
144       what machine to use
145       default: '$DEFAULT_MACHINE'
146    -b|--build <directory>
147       build directory to use
148       default: '$DEFAULT_BUILDDIR'
149    -s|--script <filename>
150       file where setup script is generated
151       default: none (no script)
152    -f|--force
153       flag to force overwriting any existing configuration
154       default: false
155    -r|--rpm-revision <schema>
156       Specify how to handle RPM packages revisions
157       <schema> can be:
158           'prservice[:<address>]' : Use a PR service daemon.
159               if <address> is not specified, the default value 'localhost:0'
160               is used (shortcut for a PR service started by bitbake)
161           'timestamp' : Use a generated time stamp (UTC).
162           'value:<revision>' : Use <revision> explicitly.
163           'none' : Do nothing.
164    -v|--verbose
165       verbose mode
166       default: false
167    -d|--debug
168       debug mode
169       default: false
170    -h|--help
171       get some help
172
173 EOF
174         local buf
175
176         echo "Available machines:" >&2
177         for x in $AGL_REPOSITORIES; do
178                 buf=$(list_machines $x)
179                 [[ -z "$buf" ]] && continue
180                 echo "   [$x]"
181                 for y in $buf; do
182                         [[ $y == $DEFAULT_MACHINE ]] && def="* " || def="  "
183                         echo "     $def$y"
184                 done
185         done
186         echo >&2
187
188         echo "Available features:" >&2
189         for x in $AGL_REPOSITORIES; do
190                 buf=$(list_features $x)
191                 [[ -z "$buf" ]] && continue
192                 echo "   [$x]"
193                 for feature in $buf; do
194                         print_feature="$feature"
195                         featuredir=$(find_feature_dir $feature)
196                         if [ -e $featuredir/included.dep ];then
197                                 print_feature="$print_feature :($(find_feature_dependency $feature $feature))"
198                         fi;
199                         echo "       $print_feature"
200                 done
201         done
202         echo >&2
203 }
204
205 function append_fragment() {
206         basefile=$1; shift # output file
207         f=$1; shift # input file
208         label=$(echo "$@")
209
210         debug "adding fragment to $basefile: $f"
211         echo >>$basefile
212         echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >>$basefile
213         echo "# fragment { " >>$basefile
214         [[ -f $f ]] && echo "# $f" >>$basefile || true
215         echo "#" >>$basefile
216         [[ -n "$label" ]] && echo "$label" >>$basefile
217         [[ -f $f ]] && cat $f >>$basefile || true
218         echo "" >>$basefile
219         echo "#" >>$basefile
220         echo "# }" >>$basefile
221         echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >>$basefile
222         [[ -f $f ]] && echo $f >>$BUILDDIR/conf/fragments.log || true
223 }
224
225 function execute_setup() {
226         script=$1
227         debug "Executing script $script"
228         opts="-e"
229         [[ $DEBUG == 1 ]] && opts="$opts -x"
230         pushd $BUILDDIR &>/dev/null
231                 $BASH $opts $script \
232                         && rc=0 \
233                         || { rc=$?; error "Script $script failed"; }
234         popd &>/dev/null
235         return $rc
236 }
237
238
239 # process all fragments
240 FRAGMENTS_BBLAYERS=""
241 FRAGMENTS_LOCALCONF=""
242 FRAGMENTS_SETUP=""
243 function process_fragments() {
244         for dir in "$@"; do
245                 debug "processing fragments in dir $dir"
246
247                 verbose "   Searching fragments: $dir"
248
249                 # lookup for files with priorities specified: something like xx_bblayers.conf.yyyyy.inc
250                 for x in $(ls $dir/??[._]bblayers.conf*.inc 2>/dev/null); do
251                         FRAGMENTS_BBLAYERS="$FRAGMENTS_BBLAYERS $(basename $x):$x"
252                         verbose "      priority $(basename $x | cut -c1-2): $(basename $x)"
253                 done
254
255                 # same for local.conf
256                 for x in $(ls $dir/??[._]local.conf*.inc 2>/dev/null); do
257                         FRAGMENTS_LOCALCONF="$FRAGMENTS_LOCALCONF $(basename $x):$x"
258                         verbose "      priority $(basename $x | cut -c1-2): $(basename $x)"
259                 done
260
261                 # same fot setup.sh
262                 for x in $(ls $dir/??[._]setup*.sh 2>/dev/null); do
263                         FRAGMENTS_SETUP="$FRAGMENTS_SETUP $(basename $x):$x"
264                         verbose "      priority $(basename $x | cut -c1-2): $(basename $x)"
265                 done
266         done
267 }
268
269 function containsFeature () {
270   for feature in $1; do
271     [[ "$feature" == "$2" ]] && return 1;
272   done;
273   return 0;
274 }
275
276 function find_feature_dependency() {
277         res_dep_features=""
278         featuredir=$(find_feature_dir $1)
279         full_feature=$2;
280         if [ -e $featuredir/included.dep ]; then
281                 dep_features="$(cat $featuredir/included.dep)"
282                 for dep_feature in $dep_features; do
283                         full_feature="$full_feature $res_dep_features"
284                         res_dep_features="$res_dep_features $dep_feature"
285                         if  containsFeature $dep_feature $full_feature ; then
286                                 res_dep_features="$res_dep_features $(find_feature_dependency $dep_feature $full_feature)"
287                         fi;
288                 done;
289         fi;
290         echo "$res_dep_features";
291         return 0;
292 }
293
294 GLOBAL_ARGS=( "$@" )
295 debug "Parsing arguments: $@"
296 TEMP=$(getopt -o m:b:r:s:fvdh --long machine:,builddir:,rpm-revision:,script:,force,verbose,debug,help -n $SCRIPT -- "$@")
297 [[ $? != 0 ]] && { usage; exit 1; }
298 eval set -- "$TEMP"
299
300 set -e
301
302 ### default options values
303 MACHINE=$DEFAULT_MACHINE
304 BUILDDIR=$DEFAULT_BUILDDIR
305 SETUPSCRIPT=
306 FORCE=
307 RPMREVISION=
308 SETUP_MANIFEST=aglsetup.manifest
309
310 while true; do
311         case "$1" in
312                 -m|--machine)      MACHINE=$2; shift 2;;
313                 -b|--builddir)     BUILDDIR=$2; shift 2;;
314                 -s|--setupscript)  SETUPSCRIPT=$2; shift 2;;
315                 -f|--force)        FORCE=1; shift;;
316                 -r|--rpm-revision) RPMREVISION=$2; shift 2;;
317                 -v|--verbose)      VERBOSE=1; shift;;
318                 -d|--debug)        VERBOSE=1; DEBUG=1; shift;;
319                 -h|--help)         HELP=1; shift;;
320                 --)                shift; break;;
321                 *) error "Arguments parsing error"; exit 1;;
322         esac
323 done
324
325 [[ "$HELP" == 1 ]] && { usage; exit 0; }
326
327 verbose "Command line arguments: ${GLOBAL_ARGS[@]}"
328
329 # the remaining args are the features
330 FEATURES="$@"
331
332 # validate the machine list
333 debug "validating machines list"
334 validate_machines
335
336 # validate the machine
337 debug "validating machine $MACHINE"
338 find_machine_dir $MACHINE >/dev/null || error "Machine '$MACHINE' not found in [ $(list_all_machines)]"
339
340 # validate the features list
341 debug "validating features list"
342 validate_features
343
344 TMP_FEATURES="";
345 for FEATURE in $FEATURES;do
346     TMP_FEATURES="$TMP_FEATURES $FEATURE"
347     TMP_FEATURES="$TMP_FEATURES $(find_feature_dependency $FEATURE $TMP_FEATURES)"
348 done
349 # remove duplicate features if any
350 FEATURES=$(for x in $TMP_FEATURES; do echo $x; done | sort -u | awk '{printf("%s ",$1);}')
351
352 # validate the features
353 for f in $FEATURES; do
354         debug "validating feature $f"
355         find_feature_dir $f >/dev/null || error "Feature '$f' not found in [ $(list_all_features)]"
356 done
357
358 # validate build dir
359 debug "validating builddir $BUILDDIR"
360 BUILDDIR=$(mkdir -p "$BUILDDIR" && cd "$BUILDDIR" && pwd -P)
361 validate_builddir
362
363 ###########################################################################################
364 function dump_log() {
365         info "    ------------ $(basename $1) -----------------"
366         sed 's/^/   | /g' $1
367         info "    ----------------------------------------"
368 }
369
370 function genconfig() {
371         info "Generating configuration files:"
372         info "   Build dir: $BUILDDIR"
373         info "   Machine: $MACHINE"
374         info "   Features: $FEATURES"
375
376         # step 1: run usual OE setup to generate conf dir
377         export TEMPLATECONF=$(cd $SCRIPTDIR/../templates/base && pwd -P)
378         debug "running oe-init-build-env with TEMPLATECONF=$TEMPLATECONF"
379         info "   Running $METADIR/external/poky/oe-init-build-env"
380         info "   Templates dir: $TEMPLATECONF"
381
382         CURDIR=$(pwd -P)
383         . $METADIR/external/poky/oe-init-build-env $BUILDDIR >/dev/null
384         cd $CURDIR
385
386         # step 2: concatenate other remaining fragments coming from base
387         process_fragments $TEMPLATECONF
388
389         # step 3: fragments for machine
390         process_fragments $(find_machine_dir $MACHINE)
391
392         # step 4: fragments for features
393         for feature in $FEATURES; do
394                 process_fragments $(find_feature_dir $feature)
395         done
396
397         # step 5: sort fragments and append them in destination files
398         FRAGMENTS_BBLAYERS=$(sed 's/ /\n/g' <<<$FRAGMENTS_BBLAYERS | sort)
399         debug "bblayer fragments: $FRAGMENTS_BBLAYERS"
400         info "   Config: $BUILDDIR/conf/bblayers.conf"
401         for x in $FRAGMENTS_BBLAYERS; do
402                 file=${x/#*:/}
403                 append_fragment $BUILDDIR/conf/bblayers.conf $file
404                 verbose "      + $file"
405         done
406
407         FRAGMENTS_LOCALCONF=$(sed 's/ /\n/g' <<<$FRAGMENTS_LOCALCONF | sort)
408         debug "localconf fragments: $FRAGMENTS_LOCALCONF"
409         info "   Config: $BUILDDIR/conf/local.conf"
410         for x in $FRAGMENTS_LOCALCONF; do
411                 file=${x/#*:/}
412                 append_fragment $BUILDDIR/conf/local.conf $file
413                 verbose "      + $file"
414         done
415         # special fragment to call distro-manifest-generator.sh from 
416         # meta-agl-profile-core/recipes-core/distro-build-manifest/distro-build-manifest.bb
417         append_fragment $BUILDDIR/conf/local.conf /dev/stdin "# generated by $(realpath $BASH_SOURCE)" <<-EOF
418                 DISTRO_SETUP_MANIFEST = "$(realpath -Ls $BUILDDIR)/$SETUP_MANIFEST"
419                 DISTRO_MANIFEST_GENERATOR = "$(dirname $(realpath $BASH_SOURCE))/distro-manifest-generator.sh"
420         EOF
421
422         FRAGMENTS_SETUP=$(sed 's/ /\n/g' <<<$FRAGMENTS_SETUP | sort)
423         debug "setup fragments: $FRAGMENTS_SETUP"
424         cat <<EOF >$BUILDDIR/conf/setup.sh
425 #!/bin/bash
426
427 # this script has been generated by $BASH_SOURCE
428
429 export MACHINE="$MACHINE"
430 export FEATURES="$FEATURES"
431 export BUILDDIR="$BUILDDIR"
432 export METADIR="$METADIR"
433 export RPMREVISION="$RPMREVISION"
434 export LOCALCONF="$BUILDDIR/conf/local.conf"
435
436 echo "--- beginning of setup script"
437 EOF
438         info "   Setup script: $BUILDDIR/conf/setup.sh"
439         for x in $FRAGMENTS_SETUP; do
440                 file=${x/#*:/}
441                 append_fragment $BUILDDIR/conf/setup.sh $file "echo '--- fragment $file'"
442                 verbose "      + $file"
443         done
444         append_fragment $BUILDDIR/conf/setup.sh "" "echo '--- end of setup script'"
445
446         infon "   Executing setup script ... "
447         execute_setup $BUILDDIR/conf/setup.sh 2>&1 | tee $BUILDDIR/conf/setup.log
448         [[ ${PIPESTATUS[0]} == 0 ]] && {
449                 info "OK"
450                 [[ $VERBOSE == 1 ]] && dump_log $BUILDDIR/conf/setup.log
451                 rm $BUILDDIR/conf/setup.sh
452         } \
453         || {
454                 info "FAIL: please check $BUILDDIR/conf/setup.log"
455                 dump_log $BUILDDIR/conf/setup.log
456                 return 1
457         }
458 }
459
460 ###########################################################################################
461
462 # check for overwrite
463 [[ $FORCE -eq 1 ]] && rm -f \
464         $BUILDDIR/conf/local.conf \
465         $BUILDDIR/conf/bblayers.conf \
466         $BUILDDIR/conf/setup.* \
467         $BUILDDIR/conf/*.log
468
469 ####### step 1: generate configuration file #######
470
471 if [[ -f $BUILDDIR/conf/local.conf || -f $BUILDDIR/conf/bblayers.conf ]]; then
472         info "Configuration files already exist:"
473         for x in $BUILDDIR/conf/local.conf $BUILDDIR/conf/bblayers.conf; do
474                 [[ -f $x ]] && info "   - $x"
475         done
476         info "Skipping configuration files generation."
477         info "Use option -f|--force to overwrite existing configuration."
478 else
479         genconfig
480 fi
481
482 ####### step 2: generate aglsetup.manifest #######
483
484 infon "Generating setup manifest: $BUILDDIR/$SETUP_MANIFEST ... "
485 for x in /etc/os-release /usr/lib/os-release; do
486         [[ -f $x ]] && . $x
487 done
488 FEATURES_md5=$(echo $FEATURES|md5sum -|awk '{print $1;}')
489 cat <<EOF >$BUILDDIR/$SETUP_MANIFEST
490 # ----------------------------------------------
491 # This fragment has been generated by $SCRIPT at setup time
492
493 # distro name
494 DIST_DISTRO_NAME="AGL"
495
496 # target machine as passed to $SCRIPT
497 DIST_MACHINE="$MACHINE"
498
499 # features as resolved by $SCRIPT
500 DIST_FEATURES="$FEATURES"
501 DIST_FEATURES_MD5="${FEATURES_md5}"
502
503 # build host information deduced from os-release
504 DIST_BUILD_HOST="$(id -un)@$(hostname -f || hostname || hostname -s)"
505 DIST_BUILD_OS="${PRETTY_NAME:-${NAME} ${VERSION} [COMPUTED]}"
506
507 # meta directory
508 DIST_METADIR="$METADIR"
509
510 # timestamp
511 DIST_SETUP_TS="$(date -u +%Y%m%d_%H%M%S_%Z)"
512
513 # ------------ end of $SCRIPT fragment --------
514 EOF
515 info "OK"
516
517 ####### step 3: generate agl-init-build-env #######
518
519 # always generate setup script in builddir: it can be sourced later manually without re-running the setup
520 infon "Generating setup file: $BUILDDIR/agl-init-build-env ... "
521
522 cat <<EOF >$BUILDDIR/agl-init-build-env
523 . $METADIR/external/poky/oe-init-build-env $BUILDDIR
524 if [ -n "\$DL_DIR" ]; then
525         BB_ENV_EXTRAWHITE="\$BB_ENV_EXTRAWHITE DL_DIR"
526 fi
527 if [ -n "\$SSTATE_DIR" ]; then
528         BB_ENV_EXTRAWHITE="\$BB_ENV_EXTRAWHITE SSTATE_DIR"
529 fi
530 export BB_ENV_EXTRAWHITE
531 unset TEMPLATECONF
532 EOF
533 info "OK"
534
535 ####### step 4: generate output script #######
536
537 # finally, generate output script if requested by caller
538 if [[ -n "$SETUPSCRIPT" ]]; then
539         debug "generating setupscript in $SETUPSCRIPT"
540         cat <<EOF >$SETUPSCRIPT
541 . $BUILDDIR/agl-init-build-env
542 EOF
543 fi
544
545 info "------------ $SCRIPT: Done"