Convert feature.inc to feature.dep
[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 usage() {
127     cat <<EOF >&2
128 Usage: . $SCRIPT [options] [feature [feature [... ]]]
129
130 Version: $VERSION
131 Compatibility: bash, zsh, ksh
132
133 Options:
134    -m|--machine <machine>
135       what machine to use
136       default: '$DEFAULT_MACHINE'
137    -b|--build <directory>
138       build directory to use
139       default: '$DEFAULT_BUILDDIR'
140    -s|--script <filename>
141       file where setup script is generated
142       default: none (no script)
143    -f|--force
144       flag to force overwriting any existing configuration
145       default: false
146    -v|--verbose
147       verbose mode
148       default: false
149    -d|--debug
150       debug mode
151       default: false
152    -h|--help
153       get some help
154
155 EOF
156         local buf
157         
158         echo "Available machines:" >&2
159         for x in $AGL_REPOSITORIES; do
160                 buf=$(list_machines $x)
161                 [[ -z "$buf" ]] && continue
162                 echo "   [$x]"
163                 for y in $buf; do 
164                         [[ $y == $DEFAULT_MACHINE ]] && def="* " || def="  "
165                         echo "     $def$y"
166                 done
167         done
168         echo >&2
169
170         echo "Available features:" >&2
171         for x in $AGL_REPOSITORIES; do
172                 buf=$(list_features $x)
173                 [[ -z "$buf" ]] && continue
174                 echo "   [$x]"
175                 for feature in $buf; do
176                         print_feature="$feature"
177                         featuredir=$(find_feature_dir $feature)
178                         if [ -e $featuredir/included.dep ];then
179                                 print_feature="$print_feature :($(find_feature_dependency $feature $feature))"
180                         fi;
181                         echo "       $print_feature"
182                 done
183         done
184         echo >&2
185 }
186
187 function append_fragment() {
188         basefile=$1; shift # output file
189         f=$1; shift # input file
190         label=$(echo "$@")
191
192         debug "adding fragment to $basefile: $f"
193         echo >>$basefile
194         echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >>$basefile
195         echo "# fragment { " >>$basefile
196         [[ -f $f ]] && echo "# $f" >>$basefile || true
197         echo "#" >>$basefile
198         [[ -n "$label" ]] && echo "$label" >>$basefile
199         [[ -f $f ]] && cat $f >>$basefile || true
200         echo "#" >>$basefile
201         echo "# }" >>$basefile
202         echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >>$basefile
203         [[ -f $f ]] && echo $f >>$BUILDDIR/conf/fragments.log || true
204 }
205
206 function execute_setup() {
207         script=$1
208         debug "Executing script $script"
209         opts=
210         [[ $DEBUG == 1 ]] && opts="$opts -x"
211         pushd $BUILDDIR &>/dev/null
212                 $BASH $opts $script \
213                         && rc=0 \
214                         || { rc=$?; error "Script $script failed"; }
215         popd &>/dev/null
216         return $rc
217 }
218
219
220 # process all fragments
221 FRAGMENTS_BBLAYERS=""
222 FRAGMENTS_LOCALCONF=""
223 FRAGMENTS_SETUP=""
224 function process_fragments() {
225         for dir in "$@"; do
226                 debug "processing fragments in dir $dir"
227
228                 verbose "   Searching fragments: $dir"
229
230                 # lookup for files with priorities specified: something like xx_bblayers.conf.yyyyy.inc
231                 for x in $(ls $dir/??[._]bblayers.conf*.inc 2>/dev/null); do
232                         FRAGMENTS_BBLAYERS="$FRAGMENTS_BBLAYERS $(basename $x):$x"
233                         verbose "      priority $(basename $x | cut -c1-2): $(basename $x)"
234                 done
235
236                 # same for local.conf
237                 for x in $(ls $dir/??[._]local.conf*.inc 2>/dev/null); do
238                         FRAGMENTS_LOCALCONF="$FRAGMENTS_LOCALCONF $(basename $x):$x"
239                         verbose "      priority $(basename $x | cut -c1-2): $(basename $x)"
240                 done
241
242                 # same fot setup.sh
243                 for x in $(ls $dir/??[._]setup*.sh 2>/dev/null); do
244                         FRAGMENTS_SETUP="$FRAGMENTS_SETUP $(basename $x):$x"
245                         verbose "      priority $(basename $x | cut -c1-2): $(basename $x)"
246                 done
247         done
248 }
249
250 function containsFeature () {
251   for feature in $1; do
252     [[ "$feature" == "$2" ]] && return 1;
253   done;
254   return 0;
255 }
256
257 function find_feature_dependency() {
258         res_dep_features=""
259         featuredir=$(find_feature_dir $1)
260         full_feature=$2;
261         if [ -e $featuredir/included.dep ]; then
262                 dep_features="$(cat $featuredir/included.dep)"
263                 for dep_feature in $dep_features; do
264                         full_feature="$full_feature $res_dep_features"
265                         res_dep_features="$res_dep_features $dep_feature"
266                         if  containsFeature $dep_feature $full_feature ; then
267                                 res_dep_features="$res_dep_features $(find_feature_dependency $dep_feature $full_feature)"
268                         fi;
269                 done;
270         fi;
271         echo "$res_dep_features";
272         return 0;
273 }
274
275 GLOBAL_ARGS=( "$@" )
276 debug "Parsing arguments: $@"
277 TEMP=$(getopt -o m:b:s:fvdh --long machine:,builddir:,script:,force,verbose,debug,help -n $SCRIPT -- "$@")
278 [[ $? != 0 ]] && { usage; exit 1; }
279 eval set -- "$TEMP"
280
281 set -e
282
283 ### default options values
284 MACHINE=$DEFAULT_MACHINE
285 BUILDDIR=$DEFAULT_BUILDDIR
286 SETUPSCRIPT=
287 FORCE=
288
289 while true; do
290         case "$1" in
291                 -m|--machine)  MACHINE=$2; shift 2;;
292                 -b|--builddir) BUILDDIR=$2; shift 2;;
293                 -s|--setupscript) SETUPSCRIPT=$2; shift 2;;
294                 -f|--force) FORCE=1; shift;;
295                 -v|--verbose) VERBOSE=1; shift;;
296                 -d|--debug) VERBOSE=1; DEBUG=1; shift;;
297                 -h|--help)     HELP=1; shift;;
298                 --)            shift; break;;
299                 *) error "Arguments parsing error"; exit 1;;
300         esac
301 done
302
303 [[ "$HELP" == 1 ]] && { usage; exit 0; }
304
305 verbose "Command line arguments: ${GLOBAL_ARGS[@]}"
306
307 # the remaining args are the features
308 FEATURES="$@"
309
310 # validate the machine list
311 debug "validating machines list"
312 validate_machines
313
314 # validate the machine
315 debug "validating machine $MACHINE"
316 find_machine_dir $MACHINE >/dev/null || error "Machine '$MACHINE' not found in [ $(list_all_machines)]"
317
318 # validate the features list
319 debug "validating features list"
320 validate_features
321
322 TMP_FEATURES="";
323 for FEATURE in $FEATURES;do
324     TMP_FEATURES="$TMP_FEATURES $FEATURE"
325     TMP_FEATURES="$TMP_FEATURES $(find_feature_dependency $FEATURE $TMP_FEATURES)"
326 done
327 FEATURES=$TMP_FEATURES
328 echo "Features used: $FEATURES"
329
330 # validate the features
331 for f in $FEATURES; do
332         debug "validating feature $f"
333         find_feature_dir $f >/dev/null || error "Feature '$f' not found in [ $(list_all_features)]"
334 done
335
336 # validate build dir
337 debug "validating builddir $BUILDDIR"
338 BUILDDIR=$(mkdir -p $BUILDDIR && cd $BUILDDIR && pwd -P)
339
340 ###########################################################################################
341 function dump_log() {
342         info "    ------------ $(basename $1) -----------------"
343         sed 's/^/   | /g' $1
344         info "    ----------------------------------------"
345 }
346
347 function genconfig() {
348         info "Generating configuration files:"
349         info "   Build dir: $BUILDDIR"
350         info "   Machine: $MACHINE"
351         info "   Features: $FEATURES"
352
353         # step 1: run usual OE setup to generate conf dir
354         export TEMPLATECONF=$(cd $SCRIPTDIR/../templates/base && pwd -P)
355         debug "running oe-init-build-env with TEMPLATECONF=$TEMPLATECONF"
356         info "   Running $METADIR/poky/oe-init-build-env"
357         info "   Templates dir: $TEMPLATECONF"
358
359         CURDIR=$(pwd -P)
360         . $METADIR/poky/oe-init-build-env $BUILDDIR >/dev/null
361         cd $CURDIR
362
363         # step 2: concatenate other remaining fragments coming from base
364         process_fragments $TEMPLATECONF
365
366         # step 3: fragments for machine
367         process_fragments $(find_machine_dir $MACHINE)
368
369         # step 4: fragments for features
370         for feature in $FEATURES; do
371                 process_fragments $(find_feature_dir $feature)
372         done
373
374         # step 5: sort fragments and append them in destination files
375         FRAGMENTS_BBLAYERS=$(sed 's/ /\n/g' <<<$FRAGMENTS_BBLAYERS | sort)
376         debug "bblayer fragments: $FRAGMENTS_BBLAYERS"
377         info "   Config: $BUILDDIR/conf/bblayers.conf"
378         for x in $FRAGMENTS_BBLAYERS; do
379                 file=${x/#*:/}
380                 append_fragment $BUILDDIR/conf/bblayers.conf $file
381                 verbose "      + $file"
382         done
383
384         FRAGMENTS_LOCALCONF=$(sed 's/ /\n/g' <<<$FRAGMENTS_LOCALCONF | sort)
385         debug "localconf fragments: $FRAGMENTS_LOCALCONF"
386         info "   Config: $BUILDDIR/conf/local.conf"
387         for x in $FRAGMENTS_LOCALCONF; do
388                 file=${x/#*:/}
389                 append_fragment $BUILDDIR/conf/local.conf $file
390                 verbose "      + $file"
391         done
392
393         FRAGMENTS_SETUP=$(sed 's/ /\n/g' <<<$FRAGMENTS_SETUP | sort)
394         debug "setup fragments: $FRAGMENTS_SETUP"
395         cat <<EOF >$BUILDDIR/conf/setup.sh
396 #!/bin/bash
397
398 # this script has been generated by $BASH_SOURCE
399
400 export MACHINE="$MACHINE"
401 export FEATURES="$FEATURES"
402 export BUILDDIR="$BUILDDIR"
403 export METADIR="$METADIR"
404
405 echo "--- beginning of setup script"
406 EOF
407         info "   Setup script: $BUILDDIR/conf/setup.sh"
408         for x in $FRAGMENTS_SETUP; do
409                 file=${x/#*:/}
410                 append_fragment $BUILDDIR/conf/setup.sh $file "echo '--- fragment $file'"
411                 verbose "      + $file"
412         done
413         append_fragment $BUILDDIR/conf/setup.sh "" "echo '--- end of setup script'"
414
415         infon "   Executing setup script ... "
416         execute_setup $BUILDDIR/conf/setup.sh 2>&1 | tee $BUILDDIR/conf/setup.log
417         [[ ${PIPESTATUS[0]} == 0 ]] && {
418                 info "OK"
419                 [[ $VERBOSE == 1 ]] && dump_log $BUILDDIR/conf/setup.log
420                 rm $BUILDDIR/conf/setup.sh
421         } \
422         || {
423                 info "FAIL: please check $BUILDDIR/conf/setup.log"
424                 dump_log $BUILDDIR/conf/setup.log
425                 return 1
426         }
427         # NOTE: the setup.sh script is removed if execution succeeded (only the log remains)
428 }
429
430 ###########################################################################################
431
432 # check for overwrite
433 [[ $FORCE -eq 1 ]] && rm -f \
434         $BUILDDIR/conf/local.conf \
435         $BUILDDIR/conf/bblayers.conf \
436         $BUILDDIR/conf/setup.* \
437         $BUILDDIR/conf/*.log
438
439 if [[ -f $BUILDDIR/conf/local.conf || -f $BUILDDIR/conf/bblayers.conf ]]; then
440         info "Configuration files already exist:"
441         for x in $BUILDDIR/conf/local.conf $BUILDDIR/conf/bblayers.conf; do
442                 [[ -f $x ]] && info "   - $x" 
443         done
444         info "Skipping configuration files generation."
445         info "Use option -f|--force to overwrite existing configuration."
446 else
447         genconfig
448 fi
449
450 # always generate setup script in builddir: it can be sourced later manually without re-running the setup
451 infon "Generating setup file: $BUILDDIR/agl-init-build-env ... "
452 cat <<EOF >$BUILDDIR/agl-init-build-env
453 . $METADIR/poky/oe-init-build-env $BUILDDIR
454 if [ -n "\$DL_DIR" ]; then
455         BB_ENV_EXTRAWHITE="\$BB_ENV_EXTRAWHITE DL_DIR"
456 fi
457 if [ -n "\$SSTATE_DIR" ]; then
458          BB_ENV_EXTRAWHITE="\$BB_ENV_EXTRAWHITE SSTATE_DIR" 
459 fi
460 export BB_ENV_EXTRAWHITE
461 unset TEMPLATECONF
462 EOF
463 info "OK"
464
465 # finally, generate output script if requested by caller
466 if [[ -n "$SETUPSCRIPT" ]]; then
467         debug "generating setupscript in $SETUPSCRIPT"
468         cat <<EOF >$SETUPSCRIPT
469 . $BUILDDIR/agl-init-build-env
470 EOF
471 fi
472
473 info "------------ $SCRIPT: Done"