Add feature shortcuts matching ci system
[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/*/templates/{machine,feature}); do echo $(basename $(dirname $(dirname $x))); done | sort -u)
55
56 function list_machines() {
57         for x in $@; do
58                 for y in $(ls -d $METADIR/$x/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_machines() {
71         list_all_machines | sort | uniq -c | while read cnt machine; do
72                 [[ $cnt == 1 ]] && continue
73                 info "Machine $machine found in the following repositories:"
74                 for x in $(ls -d $METADIR/*/templates/machine/$machine); do
75                         info "   - $x"
76                 done
77                 error "Multiple machine templates are not allowed"
78         done
79         debug "Machines list has no duplicate."
80 }
81
82 function list_features() {
83         for x in $@; do
84                 for y in $(ls -d $METADIR/$x/templates/feature/* 2>/dev/null); do
85                         echo $(basename $y)
86                 done
87         done
88 }
89
90 function list_all_features() {
91         for x in $AGL_REPOSITORIES; do
92                 list_features $x
93         done
94 }
95
96 function validate_features() {
97         list_all_features | sort | uniq -c | while read cnt feature; do
98                 [[ $cnt == 1 ]] && continue;
99                 info "Feature $feature found in the following repositories:"
100                 for x in $(ls -d $METADIR/*/templates/feature/$feature); do
101                         info "   - $x"
102                 done
103                 error "Multiple feature templates are not allowed"
104         done
105         debug "Features list has no duplicate."
106 }
107
108 function find_machine_dir() {
109         machine=$1
110         for x in $AGL_REPOSITORIES; do
111                 dir=$METADIR/$x/templates/machine/$machine
112                 [[ -d $dir ]] && { echo $dir; return 0; }
113         done
114         return 1
115 }
116
117 function find_feature_dir() {
118         feature=$1
119         for x in $AGL_REPOSITORIES; do
120                 dir=$METADIR/$x/templates/feature/$feature
121                 [[ -d $dir ]] && { echo $dir; return 0; }
122         done
123         return 1
124 }
125
126 function process-feature-shortcuts() {
127     TOCHECK=$1
128     debug "processing feature shortcut $TOCHECK"
129     if $(echo "$FEATURES" | grep -q "$TOCHECK" 2>&1 ) ; then
130         featuredir=$(find_feature_dir $TOCHECK)
131         if test -d $featuredir; then
132             tmpfeatures="$FEATURES $(cat $featuredir/included.inc)"
133             tmpfeatures_uniq=$(echo $tmpfeatures | sed -e "s/$TOCHECK//g" -e 's/  / /g' | tr "[:blank:]" "\n" | sort -u )
134             export FEATURES=$tmpfeatures_uniq
135             debug "Features used: $FEATURES"
136         else
137             error "No feature directory with this name: $TOCHECK"
138             exit 1
139         fi
140     fi
141 }
142
143
144 function usage() {
145     cat <<EOF >&2
146 Usage: . $SCRIPT [options] [feature [feature [... ]]]
147
148 Version: $VERSION
149 Compatibility: bash, zsh, ksh
150
151 Options:
152    -m|--machine <machine>
153       what machine to use
154       default: '$DEFAULT_MACHINE'
155    -b|--build <directory>
156       build directory to use
157       default: '$DEFAULT_BUILDDIR'
158    -s|--script <filename>
159       file where setup script is generated
160       default: none (no script)
161    -f|--force
162       flag to force overwriting any existing configuration
163       default: false
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         [[ -f $f ]] && echo $f >>$BUILDDIR/conf/fragments.log || true
222 }
223
224 function execute_setup() {
225         script=$1
226         debug "Executing script $script"
227         opts=
228         [[ $DEBUG == 1 ]] && opts="$opts -x"
229         pushd $BUILDDIR &>/dev/null
230                 $BASH $opts $script \
231                         && rc=0 \
232                         || { rc=$?; error "Script $script failed"; }
233         popd &>/dev/null
234         return $rc
235 }
236
237
238 # process all fragments
239 FRAGMENTS_BBLAYERS=""
240 FRAGMENTS_LOCALCONF=""
241 FRAGMENTS_SETUP=""
242 function process_fragments() {
243         for dir in "$@"; do
244                 debug "processing fragments in dir $dir"
245
246                 verbose "   Searching fragments: $dir"
247
248                 # lookup for files with priorities specified: something like xx_bblayers.conf.yyyyy.inc
249                 for x in $(ls $dir/??[._]bblayers.conf*.inc 2>/dev/null); do
250                         FRAGMENTS_BBLAYERS="$FRAGMENTS_BBLAYERS $(basename $x):$x"
251                         verbose "      priority $(basename $x | cut -c1-2): $(basename $x)"
252                 done
253
254                 # same for local.conf
255                 for x in $(ls $dir/??[._]local.conf*.inc 2>/dev/null); do
256                         FRAGMENTS_LOCALCONF="$FRAGMENTS_LOCALCONF $(basename $x):$x"
257                         verbose "      priority $(basename $x | cut -c1-2): $(basename $x)"
258                 done
259
260                 # same fot setup.sh
261                 for x in $(ls $dir/??[._]setup*.sh 2>/dev/null); do
262                         FRAGMENTS_SETUP="$FRAGMENTS_SETUP $(basename $x):$x"
263                         verbose "      priority $(basename $x | cut -c1-2): $(basename $x)"
264                 done
265         done
266 }
267
268 function containsFeature () {
269   for feature in $1; do
270     [[ "$feature" == "$2" ]] && return 1;
271   done;
272   return 0;
273 }
274
275 function find_feature_dependency() {
276         res_dep_features=""
277         featuredir=$(find_feature_dir $1)
278         full_feature=$2;
279         if [ -e $featuredir/included.dep ]; then
280                 dep_features="$(cat $featuredir/included.dep)"
281                 for dep_feature in $dep_features; do
282                         full_feature="$full_feature $res_dep_features"
283                         res_dep_features="$res_dep_features $dep_feature"
284                         if  containsFeature $dep_feature $full_feature ; then
285                                 res_dep_features="$res_dep_features $(find_feature_dependency $dep_feature $full_feature)"
286                         fi;
287                 done;
288         fi;
289         echo "$res_dep_features";
290         return 0;
291 }
292
293 GLOBAL_ARGS=( "$@" )
294 debug "Parsing arguments: $@"
295 TEMP=$(getopt -o m:b:s:fvdh --long machine:,builddir:,script:,force,verbose,debug,help -n $SCRIPT -- "$@")
296 [[ $? != 0 ]] && { usage; exit 1; }
297 eval set -- "$TEMP"
298
299 set -e
300
301 ### default options values
302 MACHINE=$DEFAULT_MACHINE
303 BUILDDIR=$DEFAULT_BUILDDIR
304 SETUPSCRIPT=
305 FORCE=
306
307 while true; do
308         case "$1" in
309                 -m|--machine)  MACHINE=$2; shift 2;;
310                 -b|--builddir) BUILDDIR=$2; shift 2;;
311                 -s|--setupscript) SETUPSCRIPT=$2; shift 2;;
312                 -f|--force) FORCE=1; shift;;
313                 -v|--verbose) VERBOSE=1; shift;;
314                 -d|--debug) VERBOSE=1; DEBUG=1; shift;;
315                 -h|--help)     HELP=1; shift;;
316                 --)            shift; break;;
317                 *) error "Arguments parsing error"; exit 1;;
318         esac
319 done
320
321 [[ "$HELP" == 1 ]] && { usage; exit 0; }
322
323 verbose "Command line arguments: ${GLOBAL_ARGS[@]}"
324
325 # the remaining args are the features
326 FEATURES="$@"
327 process-feature-shortcuts agl-all-features
328 process-feature-shortcuts agl-ci-change-features
329 process-feature-shortcuts agl-ci-snapshot-features
330
331 # validate the machine list
332 debug "validating machines list"
333 validate_machines
334
335 # validate the machine
336 debug "validating machine $MACHINE"
337 find_machine_dir $MACHINE >/dev/null || error "Machine '$MACHINE' not found in [ $(list_all_machines)]"
338
339 # validate the features list
340 debug "validating features list"
341 validate_features
342
343 TMP_FEATURES="";
344 for FEATURE in $FEATURES;do
345     TMP_FEATURES="$TMP_FEATURES $FEATURE"
346     TMP_FEATURES="$TMP_FEATURES $(find_feature_dependency $FEATURE $TMP_FEATURES)"
347 done
348 FEATURES=$TMP_FEATURES
349 echo "Features used: $FEATURES"
350
351 # validate the features
352 for f in $FEATURES; do
353         debug "validating feature $f"
354         find_feature_dir $f >/dev/null || error "Feature '$f' not found in [ $(list_all_features)]"
355 done
356
357 # validate build dir
358 debug "validating builddir $BUILDDIR"
359 BUILDDIR=$(mkdir -p $BUILDDIR && cd $BUILDDIR && pwd -P)
360
361 ###########################################################################################
362 function dump_log() {
363         info "    ------------ $(basename $1) -----------------"
364         sed 's/^/   | /g' $1
365         info "    ----------------------------------------"
366 }
367
368 function genconfig() {
369         info "Generating configuration files:"
370         info "   Build dir: $BUILDDIR"
371         info "   Machine: $MACHINE"
372         info "   Features: $FEATURES"
373
374         # step 1: run usual OE setup to generate conf dir
375         export TEMPLATECONF=$(cd $SCRIPTDIR/../templates/base && pwd -P)
376         debug "running oe-init-build-env with TEMPLATECONF=$TEMPLATECONF"
377         info "   Running $METADIR/poky/oe-init-build-env"
378         info "   Templates dir: $TEMPLATECONF"
379
380         CURDIR=$(pwd -P)
381         . $METADIR/poky/oe-init-build-env $BUILDDIR >/dev/null
382         cd $CURDIR
383
384         # step 2: concatenate other remaining fragments coming from base
385         process_fragments $TEMPLATECONF
386
387         # step 3: fragments for machine
388         process_fragments $(find_machine_dir $MACHINE)
389
390         # step 4: fragments for features
391         for feature in $FEATURES; do
392                 process_fragments $(find_feature_dir $feature)
393         done
394
395         # step 5: sort fragments and append them in destination files
396         FRAGMENTS_BBLAYERS=$(sed 's/ /\n/g' <<<$FRAGMENTS_BBLAYERS | sort)
397         debug "bblayer fragments: $FRAGMENTS_BBLAYERS"
398         info "   Config: $BUILDDIR/conf/bblayers.conf"
399         for x in $FRAGMENTS_BBLAYERS; do
400                 file=${x/#*:/}
401                 append_fragment $BUILDDIR/conf/bblayers.conf $file
402                 verbose "      + $file"
403         done
404
405         FRAGMENTS_LOCALCONF=$(sed 's/ /\n/g' <<<$FRAGMENTS_LOCALCONF | sort)
406         debug "localconf fragments: $FRAGMENTS_LOCALCONF"
407         info "   Config: $BUILDDIR/conf/local.conf"
408         for x in $FRAGMENTS_LOCALCONF; do
409                 file=${x/#*:/}
410                 append_fragment $BUILDDIR/conf/local.conf $file
411                 verbose "      + $file"
412         done
413
414         FRAGMENTS_SETUP=$(sed 's/ /\n/g' <<<$FRAGMENTS_SETUP | sort)
415         debug "setup fragments: $FRAGMENTS_SETUP"
416         cat <<EOF >$BUILDDIR/conf/setup.sh
417 #!/bin/bash
418
419 # this script has been generated by $BASH_SOURCE
420
421 export MACHINE="$MACHINE"
422 export FEATURES="$FEATURES"
423 export BUILDDIR="$BUILDDIR"
424 export METADIR="$METADIR"
425
426 echo "--- beginning of setup script"
427 EOF
428         info "   Setup script: $BUILDDIR/conf/setup.sh"
429         for x in $FRAGMENTS_SETUP; do
430                 file=${x/#*:/}
431                 append_fragment $BUILDDIR/conf/setup.sh $file "echo '--- fragment $file'"
432                 verbose "      + $file"
433         done
434         append_fragment $BUILDDIR/conf/setup.sh "" "echo '--- end of setup script'"
435
436         infon "   Executing setup script ... "
437         execute_setup $BUILDDIR/conf/setup.sh 2>&1 | tee $BUILDDIR/conf/setup.log
438         [[ ${PIPESTATUS[0]} == 0 ]] && {
439                 info "OK"
440                 [[ $VERBOSE == 1 ]] && dump_log $BUILDDIR/conf/setup.log
441                 rm $BUILDDIR/conf/setup.sh
442         } \
443         || {
444                 info "FAIL: please check $BUILDDIR/conf/setup.log"
445                 dump_log $BUILDDIR/conf/setup.log
446                 return 1
447         }
448         # NOTE: the setup.sh script is removed if execution succeeded (only the log remains)
449 }
450
451 ###########################################################################################
452
453 # check for overwrite
454 [[ $FORCE -eq 1 ]] && rm -f \
455         $BUILDDIR/conf/local.conf \
456         $BUILDDIR/conf/bblayers.conf \
457         $BUILDDIR/conf/setup.* \
458         $BUILDDIR/conf/*.log
459
460 if [[ -f $BUILDDIR/conf/local.conf || -f $BUILDDIR/conf/bblayers.conf ]]; then
461         info "Configuration files already exist:"
462         for x in $BUILDDIR/conf/local.conf $BUILDDIR/conf/bblayers.conf; do
463                 [[ -f $x ]] && info "   - $x" 
464         done
465         info "Skipping configuration files generation."
466         info "Use option -f|--force to overwrite existing configuration."
467 else
468         genconfig
469 fi
470
471 # always generate setup script in builddir: it can be sourced later manually without re-running the setup
472 infon "Generating setup file: $BUILDDIR/agl-init-build-env ... "
473 cat <<EOF >$BUILDDIR/agl-init-build-env
474 . $METADIR/poky/oe-init-build-env $BUILDDIR
475 if [ -n "\$DL_DIR" ]; then
476         BB_ENV_EXTRAWHITE="\$BB_ENV_EXTRAWHITE DL_DIR"
477 fi
478 if [ -n "\$SSTATE_DIR" ]; then
479          BB_ENV_EXTRAWHITE="\$BB_ENV_EXTRAWHITE SSTATE_DIR" 
480 fi
481 export BB_ENV_EXTRAWHITE
482 unset TEMPLATECONF
483 EOF
484 info "OK"
485
486 # finally, generate output script if requested by caller
487 if [[ -n "$SETUPSCRIPT" ]]; then
488         debug "generating setupscript in $SETUPSCRIPT"
489         cat <<EOF >$SETUPSCRIPT
490 . $BUILDDIR/agl-init-build-env
491 EOF
492 fi
493
494 info "------------ $SCRIPT: Done"