Fix aglsetup always succeeded regardless of whether errors or not
[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 #
9 # Permission is hereby granted, free of charge, to any person obtaining a copy
10 # of this software and associated documentation files (the "Software"), to deal
11 # in the Software without restriction, including without limitation the rights
12 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 # copies of the Software, and to permit persons to whom the Software is
14 # furnished to do so, subject to the following conditions:
15 #
16 # The above copyright notice and this permission notice shall be included in
17 # all copies or substantial portions of the Software.
18 #
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 # SOFTWARE.
26 #
27 ################################################################################
28
29 # this script shouldn't be called directly, but through aglsetup.sh that will in 
30 # turn execute (source) generated instructions back in the parent shell, 
31 # whether it's bash, zsh, or any other supported shell
32
33 VERSION=1.1.0
34 DEFAULT_MACHINE=qemux86-64
35 DEFAULT_BUILDDIR=./build
36 VERBOSE=0
37 DEBUG=0
38
39 #SCRIPT=$(basename $BASH_SOURCE)
40 SCRIPT=aglsetup.sh
41 SCRIPTDIR=$(cd $(dirname $BASH_SOURCE) && pwd -P)
42 METADIR=$(cd $(dirname $BASH_SOURCE)/../.. && pwd -P)
43
44 function info() { echo "$@" >&2; }
45 function infon() { echo -n "$@" >&2; }
46 function error() { echo "ERROR: $@" >&2; return 1; }
47 function verbose() { [[ $VERBOSE == 1 ]] && echo "$@" >&2; return 0; }
48 function debug() { [[ $DEBUG == 1 ]] && echo "DEBUG: $@" >&2; return 0;}
49
50 info "------------ $SCRIPT: Starting"
51
52 #compute AGL_REPOSITORIES
53 AGL_REPOSITORIES=$(for x in $(ls -d $METADIR/*/templates/{machine,feature}); do echo $(basename $(dirname $(dirname $x))); done | sort -u)
54
55 function list_machines() {
56         for x in $@; do
57                 for y in $(ls -d $METADIR/$x/templates/machine/* 2>/dev/null); do
58                         echo $(basename $y)
59                 done
60         done
61 }
62
63 function list_all_machines() {
64         for x in $AGL_REPOSITORIES; do
65                 list_machines $x
66         done
67 }
68
69 function validate_machines() {
70         list_all_machines | sort | uniq -c | while read cnt machine; do
71                 [[ $cnt == 1 ]] && continue
72                 info "Machine $machine found in the following repositories:"
73                 for x in $(ls -d $METADIR/*/templates/machine/$machine); do
74                         info "   - $x"
75                 done
76                 error "Multiple machine templates are not allowed"
77         done
78         debug "Machines list has no duplicate."
79 }
80
81 function list_features() {
82         for x in $@; do
83                 for y in $(ls -d $METADIR/$x/templates/feature/* 2>/dev/null); do
84                         echo $(basename $y)
85                 done
86         done
87 }
88
89 function list_all_features() {
90         for x in $AGL_REPOSITORIES; do
91                 list_features $x
92         done
93 }
94
95 function validate_features() {
96         list_all_features | sort | uniq -c | while read cnt feature; do
97                 [[ $cnt == 1 ]] && continue;
98                 info "Feature $feature found in the following repositories:"
99                 for x in $(ls -d $METADIR/*/templates/feature/$feature); do
100                         info "   - $x"
101                 done
102                 error "Multiple feature templates are not allowed"
103         done
104         debug "Features list has no duplicate."
105 }
106
107 function find_machine_dir() {
108         machine=$1
109         for x in $AGL_REPOSITORIES; do
110                 dir=$METADIR/$x/templates/machine/$machine
111                 [[ -d $dir ]] && { echo $dir; return 0; }
112         done
113         return 1
114 }
115
116 function find_feature_dir() {
117         feature=$1
118         for x in $AGL_REPOSITORIES; do
119                 dir=$METADIR/$x/templates/feature/$feature
120                 [[ -d $dir ]] && { echo $dir; return 0; }
121         done
122         return 1
123 }
124
125 function usage() {
126     cat <<EOF >&2
127 Usage: . $SCRIPT [options] [feature [feature [... ]]]
128
129 Version: $VERSION
130 Compatibility: bash, zsh, ksh
131
132 Options:
133    -m|--machine <machine>
134       what machine to use
135       default: '$DEFAULT_MACHINE'
136    -b|--build <directory>
137       build directory to use
138       default: '$DEFAULT_BUILDDIR'
139    -s|--script <filename>
140       file where setup script is generated
141       default: none (no script)
142    -f|--force
143       flag to force overwriting any existing configuration
144       default: false
145    -v|--verbose
146       verbose mode
147       default: false
148    -d|--debug
149       debug mode
150       default: false
151    -h|--help
152       get some help
153
154 EOF
155         local buf
156         
157         echo "Available machines:" >&2
158         for x in $AGL_REPOSITORIES; do
159                 buf=$(list_machines $x)
160                 [[ -z "$buf" ]] && continue
161                 echo "   [$x]"
162                 for y in $buf; do 
163                         [[ $y == $DEFAULT_MACHINE ]] && def="* " || def="  "
164                         echo "     $def$y"
165                 done
166         done
167         echo >&2
168
169         echo "Available features:" >&2
170         for x in $AGL_REPOSITORIES; do
171                 buf=$(list_features $x)
172                 [[ -z "$buf" ]] && continue
173                 echo "   [$x]"
174                 for y in $buf; do
175                         echo "       $y"
176                 done
177         done
178         echo >&2
179 }
180
181 function append_fragment() {
182         basefile=$1; shift # output file
183         f=$1; shift # input file
184         label=$(echo "$@")
185
186         debug "adding fragment to $basefile: $f"
187         echo >>$basefile
188         echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >>$basefile
189         echo "# fragment { " >>$basefile
190         [[ -f $f ]] && echo "# $f" >>$basefile || true
191         echo "#" >>$basefile
192         [[ -n "$label" ]] && echo "$label" >>$basefile
193         [[ -f $f ]] && cat $f >>$basefile || true
194         echo "#" >>$basefile
195         echo "# }" >>$basefile
196         echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >>$basefile
197         [[ -f $f ]] && echo $f >>$BUILDDIR/conf/fragments.log || true
198 }
199
200 function execute_setup() {
201         script=$1
202         debug "Executing script $script"
203         opts=
204         [[ $DEBUG == 1 ]] && opts="$opts -x"
205         pushd $BUILDDIR &>/dev/null
206                 $BASH $opts $script \
207                         && rc=0 \
208                         || { rc=$?; error "Script $script failed"; }
209         popd &>/dev/null
210         return $rc
211 }
212
213 # process all fragments
214 FRAGMENTS_BBLAYERS=""
215 FRAGMENTS_LOCALCONF=""
216 FRAGMENTS_SETUP=""
217 function process_fragments() {
218         for dir in "$@"; do
219                 debug "processing fragments in dir $dir"
220
221                 verbose "   Searching fragments: $dir"
222
223                 # lookup for files with priorities specified: something like xx_bblayers.conf.yyyyy.inc
224                 for x in $(ls $dir/??[._]bblayers.conf*.inc 2>/dev/null); do
225                         FRAGMENTS_BBLAYERS="$FRAGMENTS_BBLAYERS $(basename $x):$x"
226                         verbose "      priority $(basename $x | cut -c1-2): $(basename $x)"
227                 done
228
229                 # same for local.conf
230                 for x in $(ls $dir/??[._]local.conf*.inc 2>/dev/null); do
231                         FRAGMENTS_LOCALCONF="$FRAGMENTS_LOCALCONF $(basename $x):$x"
232                         verbose "      priority $(basename $x | cut -c1-2): $(basename $x)"
233                 done
234
235                 # same fot setup.sh
236                 for x in $(ls $dir/??[._]setup*.sh 2>/dev/null); do
237                         FRAGMENTS_SETUP="$FRAGMENTS_SETUP $(basename $x):$x"
238                         verbose "      priority $(basename $x | cut -c1-2): $(basename $x)"
239                 done
240         done
241 }
242
243 GLOBAL_ARGS=( "$@" )
244 debug "Parsing arguments: $@"
245 TEMP=$(getopt -o m:b:s:fvdh --long machine:,builddir:,script:,force,verbose,debug,help -n $SCRIPT -- "$@")
246 [[ $? != 0 ]] && { usage; exit 1; }
247 eval set -- "$TEMP"
248
249 set -e
250
251 ### default options values
252 MACHINE=$DEFAULT_MACHINE
253 BUILDDIR=$DEFAULT_BUILDDIR
254 SETUPSCRIPT=
255 FORCE=
256
257 while true; do
258         case "$1" in
259                 -m|--machine)  MACHINE=$2; shift 2;;
260                 -b|--builddir) BUILDDIR=$2; shift 2;;
261                 -s|--setupscript) SETUPSCRIPT=$2; shift 2;;
262                 -f|--force) FORCE=1; shift;;
263                 -v|--verbose) VERBOSE=1; shift;;
264                 -d|--debug) VERBOSE=1; DEBUG=1; shift;;
265                 -h|--help)     HELP=1; shift;;
266                 --)            shift; break;;
267                 *) error "Arguments parsing error"; exit 1;;
268         esac
269 done
270
271 [[ "$HELP" == 1 ]] && { usage; exit 0; }
272
273 verbose "Command line arguments: ${GLOBAL_ARGS[@]}"
274
275 # the remaining args are the features
276 FEATURES="$@"
277
278 # validate the machine list
279 debug "validating machines list"
280 validate_machines
281
282 # validate the machine
283 debug "validating machine $MACHINE"
284 find_machine_dir $MACHINE >/dev/null || error "Machine '$MACHINE' not found in [ $(list_all_machines)]"
285
286 # validate the features list
287 debug "validating features list"
288 validate_features
289
290 # validate the features
291 for f in $FEATURES; do
292         debug "validating feature $f"
293         find_feature_dir $f >/dev/null || error "Feature '$f' not found in [ $(list_all_features)]"
294 done
295
296 # validate build dir
297 debug "validating builddir $BUILDDIR"
298 BUILDDIR=$(mkdir -p $BUILDDIR && cd $BUILDDIR && pwd -P)
299
300 ###########################################################################################
301 function dump_log() {
302         info "    ------------ $(basename $1) -----------------"
303         sed 's/^/   | /g' $1
304         info "    ----------------------------------------"
305 }
306
307 function genconfig() {
308         info "Generating configuration files:"
309         info "   Build dir: $BUILDDIR"
310         info "   Machine: $MACHINE"
311         info "   Features: $FEATURES"
312
313         # step 1: run usual OE setup to generate conf dir
314         export TEMPLATECONF=$(cd $SCRIPTDIR/../templates/base && pwd -P)
315         debug "running oe-init-build-env with TEMPLATECONF=$TEMPLATECONF"
316         info "   Running $METADIR/poky/oe-init-build-env"
317         info "   Templates dir: $TEMPLATECONF"
318
319         CURDIR=$(pwd -P)
320         . $METADIR/poky/oe-init-build-env $BUILDDIR >/dev/null
321         cd $CURDIR
322
323         # step 2: concatenate other remaining fragments coming from base
324         process_fragments $TEMPLATECONF
325
326         # step 3: fragments for machine
327         process_fragments $(find_machine_dir $MACHINE)
328
329         # step 4: fragments for features
330         for feature in $FEATURES; do
331                 process_fragments $(find_feature_dir $feature)
332         done
333
334         # step 5: sort fragments and append them in destination files
335         FRAGMENTS_BBLAYERS=$(sed 's/ /\n/g' <<<$FRAGMENTS_BBLAYERS | sort)
336         debug "bblayer fragments: $FRAGMENTS_BBLAYERS"
337         info "   Config: $BUILDDIR/conf/bblayers.conf"
338         for x in $FRAGMENTS_BBLAYERS; do
339                 file=${x/#*:/}
340                 append_fragment $BUILDDIR/conf/bblayers.conf $file
341                 verbose "      + $file"
342         done
343
344         FRAGMENTS_LOCALCONF=$(sed 's/ /\n/g' <<<$FRAGMENTS_LOCALCONF | sort)
345         debug "localconf fragments: $FRAGMENTS_LOCALCONF"
346         info "   Config: $BUILDDIR/conf/local.conf"
347         for x in $FRAGMENTS_LOCALCONF; do
348                 file=${x/#*:/}
349                 append_fragment $BUILDDIR/conf/local.conf $file
350                 verbose "      + $file"
351         done
352
353         FRAGMENTS_SETUP=$(sed 's/ /\n/g' <<<$FRAGMENTS_SETUP | sort)
354         debug "setup fragments: $FRAGMENTS_SETUP"
355         cat <<EOF >$BUILDDIR/conf/setup.sh
356 #!/bin/bash
357
358 # this script has been generated by $BASH_SOURCE
359
360 export MACHINE="$MACHINE"
361 export FEATURES="$FEATURES"
362 export BUILDDIR="$BUILDDIR"
363 export METADIR="$METADIR"
364
365 echo "--- beginning of setup script"
366 EOF
367         info "   Setup script: $BUILDDIR/conf/setup.sh"
368         for x in $FRAGMENTS_SETUP; do
369                 file=${x/#*:/}
370                 append_fragment $BUILDDIR/conf/setup.sh $file "echo '--- fragment $file'"
371                 verbose "      + $file"
372         done
373         append_fragment $BUILDDIR/conf/setup.sh "" "echo '--- end of setup script'"
374
375         infon "   Executing setup script ... "
376         execute_setup $BUILDDIR/conf/setup.sh 2>&1 | tee $BUILDDIR/conf/setup.log
377         [[ ${PIPESTATUS[0]} == 0 ]] && {
378                 info "OK"
379                 [[ $VERBOSE == 1 ]] && dump_log $BUILDDIR/conf/setup.log
380                 rm $BUILDDIR/conf/setup.sh
381         } \
382         || {
383                 info "FAIL: please check $BUILDDIR/conf/setup.log"
384                 dump_log $BUILDDIR/conf/setup.log
385                 return 1
386         }
387         # NOTE: the setup.sh script is removed if execution succeeded (only the log remains)
388 }
389
390 ###########################################################################################
391
392 # check for overwrite
393 [[ $FORCE -eq 1 ]] && rm -f \
394         $BUILDDIR/conf/local.conf \
395         $BUILDDIR/conf/bblayers.conf \
396         $BUILDDIR/conf/setup.* \
397         $BUILDDIR/conf/*.log
398
399 if [[ -f $BUILDDIR/conf/local.conf || -f $BUILDDIR/conf/bblayers.conf ]]; then
400         info "Configuration files already exist:"
401         for x in $BUILDDIR/conf/local.conf $BUILDDIR/conf/bblayers.conf; do
402                 [[ -f $x ]] && info "   - $x" 
403         done
404         info "Skipping configuration files generation."
405         info "Use option -f|--force to overwrite existing configuration."
406 else
407         genconfig
408 fi
409
410 # always generate setup script in builddir: it can be sourced later manually without re-running the setup
411 infon "Generating setup file: $BUILDDIR/agl-init-build-env ... "
412 cat <<EOF >$BUILDDIR/agl-init-build-env
413 . $METADIR/poky/oe-init-build-env $BUILDDIR
414 if [ -n "\$DL_DIR" ]; then
415         BB_ENV_EXTRAWHITE="\$BB_ENV_EXTRAWHITE DL_DIR"
416 fi
417 if [ -n "\$SSTATE_DIR" ]; then
418          BB_ENV_EXTRAWHITE="\$BB_ENV_EXTRAWHITE SSTATE_DIR" 
419 fi
420 export BB_ENV_EXTRAWHITE
421 unset TEMPLATECONF
422 EOF
423 info "OK"
424
425 # finally, generate output script if requested by caller
426 if [[ -n "$SETUPSCRIPT" ]]; then
427         debug "generating setupscript in $SETUPSCRIPT"
428         cat <<EOF >$SETUPSCRIPT
429 . $BUILDDIR/agl-init-build-env
430 EOF
431 fi
432
433 info "------------ $SCRIPT: Done"