new configuration templates based on fragments
[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.0.0
34 AGL_REPOSITORIES="meta-agl meta-agl-extra meta-agl-contrib"
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 function list_machines() {
54         for x in $@; do
55                 for y in $(ls -d $METADIR/$x/templates/machine/* 2>/dev/null); do
56                         echo -n "$(basename $y) "
57                 done
58         done
59 }
60
61 function list_all_machines() {
62         for x in $AGL_REPOSITORIES; do
63                 [[ ! -d $METADIR/$x ]] && continue
64                 list_machines $x
65         done
66 }
67
68 function list_features() {
69         for x in $@; do
70                 for y in $(ls -d $METADIR/$x/templates/feature/* 2>/dev/null); do
71                         echo -n "$(basename $y) "
72                 done
73         done
74 }
75
76 function list_all_features() {
77         for x in $AGL_REPOSITORIES; do
78                 [[ ! -d $METADIR/$x ]] && continue
79                 list_features $x
80         done
81 }
82
83 function find_machine_dir() {
84         machine=$1
85         for x in $AGL_REPOSITORIES; do
86                 [[ ! -d $METADIR/$x ]] && continue
87                 dir=$METADIR/$x/templates/machine/$machine
88                 [[ -d $dir ]] && { echo $dir; return 0; }
89         done
90         return 1
91 }
92
93 function find_feature_dir() {
94         feature=$1
95         for x in $AGL_REPOSITORIES; do
96                 [[ ! -d $METADIR/$x ]] && continue
97                 dir=$METADIR/$x/templates/feature/$feature
98                 [[ -d $dir ]] && { echo $dir; return 0; }
99         done
100         return 1
101 }
102
103 function usage() {
104     cat <<EOF >&2
105 Usage: . $SCRIPT [options] [feature [feature [... ]]]
106
107 Version: $VERSION
108 Compatibility: bash, zsh, ksh
109
110 Options:
111    -m|--machine <machine>
112       what machine to use
113       default: '$DEFAULT_MACHINE'
114    -b|--build <directory>
115       build directory to use
116       default: '$DEFAULT_BUILDDIR'
117    -s|--script <filename>
118       file where setup script is generated
119       default: none (no script)
120    -f|--force
121       flag to force overwriting any existing configuration
122       default: false
123    -v|--verbose
124       verbose mode
125       default: false
126    -d|--debug
127       debug mode
128       default: false
129    -h|--help
130       get some help
131
132 EOF
133         
134         echo "Available machines:" >&2
135         for x in $AGL_REPOSITORIES; do
136                 [[ ! -d $METADIR/$x ]] && continue
137                 echo "   [$x]"
138                 for y in $(list_machines $x); do 
139                         [[ $y == $DEFAULT_MACHINE ]] && def="* " || def="  "
140                         echo "     $def$y"
141                 done
142         done
143         echo >&2
144
145         echo "Available features:" >&2
146         for x in $AGL_REPOSITORIES; do
147                 [[ ! -d $METADIR/$x ]] && continue
148                 echo "   [$x]"
149                 for y in $(list_features $x); do
150                         echo "       $y"
151                 done
152         done
153         echo >&2
154 }
155
156 function append_fragment() {
157         basefile=$1; shift # output file
158         f=$1; shift # input file
159         label=$(echo "$@")
160
161         debug "adding fragment to $basefile: $f"
162         echo >>$basefile
163         echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >>$basefile
164         echo "# fragment { " >>$basefile
165         [[ -f $f ]] && echo "# $f" >>$basefile || true
166         echo "#" >>$basefile
167         [[ -n "$label" ]] && echo "$label" >>$basefile
168         [[ -f $f ]] && cat $f >>$basefile || true
169         echo "#" >>$basefile
170         echo "# }" >>$basefile
171         echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >>$basefile
172         [[ -f $f ]] && echo $f >>$BUILDDIR/conf/fragments.log || true
173 }
174
175 function execute_setup() {
176         script=$1
177         debug "Executing script $script"
178         opts=
179         [[ $DEBUG == 1 ]] && opts="$opts -x"
180         pushd $BUILDDIR &>/dev/null
181                 $BASH $opts $script \
182                         && rc=0 \
183                         || { rc=$?; error "Script $script failed"; }
184         popd &>/dev/null
185         return $rc
186 }
187
188 # process all fragments
189 FRAGMENTS_BBLAYERS=""
190 FRAGMENTS_LOCALCONF=""
191 FRAGMENTS_SETUP=""
192 function process_fragments() {
193         for dir in "$@"; do
194                 debug "processing fragments in dir $dir"
195
196                 verbose "   Searching fragments: $dir"
197
198                 # lookup for files with priorities specified: something like xx_bblayers.conf.yyyyy.inc
199                 for x in $(ls $dir/??[._]bblayers.conf*.inc 2>/dev/null); do
200                         FRAGMENTS_BBLAYERS="$FRAGMENTS_BBLAYERS $(basename $x):$x"
201                         verbose "      priority $(basename $x | cut -c1-2): $(basename $x)"
202                 done
203
204                 # same for local.conf
205                 for x in $(ls $dir/??[._]local.conf*.inc 2>/dev/null); do
206                         FRAGMENTS_LOCALCONF="$FRAGMENTS_LOCALCONF $(basename $x):$x"
207                         verbose "      priority $(basename $x | cut -c1-2): $(basename $x)"
208                 done
209
210                 # same fot setup.sh
211                 for x in $(ls $dir/??[._]setup*.sh 2>/dev/null); do
212                         FRAGMENTS_SETUP="$FRAGMENTS_SETUP $(basename $x):$x"
213                         verbose "      priority $(basename $x | cut -c1-2): $(basename $x)"
214                 done
215         done
216 }
217
218 GLOBAL_ARGS=( "$@" )
219 debug "Parsing arguments: $@"
220 TEMP=$(getopt -o m:b:s:fvdh --long machine:,builddir:,script:,force,verbose,debug,help -n $SCRIPT -- "$@")
221 [[ $? != 0 ]] && { usage; exit 1; }
222 eval set -- "$TEMP"
223
224 set -e
225
226 ### default options values
227 MACHINE=$DEFAULT_MACHINE
228 BUILDDIR=$DEFAULT_BUILDDIR
229 SETUPSCRIPT=
230 FORCE=
231
232 while true; do
233         case "$1" in
234                 -m|--machine)  MACHINE=$2; shift 2;;
235                 -b|--builddir) BUILDDIR=$2; shift 2;;
236                 -s|--setupscript) SETUPSCRIPT=$2; shift 2;;
237                 -f|--force) FORCE=1; shift;;
238                 -v|--verbose) VERBOSE=1; shift;;
239                 -d|--debug) VERBOSE=1; DEBUG=1; shift;;
240                 -h|--help)     HELP=1; shift;;
241                 --)            shift; break;;
242                 *) error "Arguments parsing error"; exit 1;;
243         esac
244 done
245
246 [[ "$HELP" == 1 ]] && { usage; exit 0; }
247
248 verbose "Command line arguments: ${GLOBAL_ARGS[@]}"
249
250 # the remaining args are the features
251 FEATURES="$@"
252
253 # validate the machine
254 debug "validating machine $MACHINE"
255 find_machine_dir $MACHINE >/dev/null || error "Machine '$MACHINE' not found in [ $(list_all_machines)]"
256
257 # validate the features
258 for f in $FEATURES; do
259         debug "validating feature $f"
260         find_feature_dir $f >/dev/null || error "Feature '$f' not found in [ $(list_all_features)]"
261 done
262
263 # validate build dir
264 debug "validating builddir $BUILDDIR"
265 BUILDDIR=$(mkdir -p $BUILDDIR && cd $BUILDDIR && pwd -P)
266
267 ###########################################################################################
268 function dump_log() {
269         info "    ------------ $(basename $1) -----------------"
270         sed 's/^/   | /g' $1
271         info "    ----------------------------------------"
272 }
273
274 function genconfig() {
275         info "Generating configuration files:"
276         info "   Build dir: $BUILDDIR"
277         info "   Machine: $MACHINE"
278         info "   Features: $FEATURES"
279
280         # step 1: run usual OE setup to generate conf dir
281         export TEMPLATECONF=$(cd $SCRIPTDIR/../templates/base && pwd -P)
282         debug "running oe-init-build-env with TEMPLATECONF=$TEMPLATECONF"
283         info "   Running $METADIR/poky/oe-init-build-env"
284         info "   Templates dir: $TEMPLATECONF"
285
286         CURDIR=$(pwd -P)
287         . $METADIR/poky/oe-init-build-env $BUILDDIR >/dev/null
288         cd $CURDIR
289
290         # step 2: concatenate other remaining fragments coming from base
291         process_fragments $TEMPLATECONF
292
293         # step 3: fragments for machine
294         process_fragments $(find_machine_dir $MACHINE)
295
296         # step 4: fragments for features
297         for feature in $FEATURES; do
298                 process_fragments $(find_feature_dir $feature)
299         done
300
301         # step 5: sort fragments and append them in destination files
302         FRAGMENTS_BBLAYERS=$(sed 's/ /\n/g' <<<$FRAGMENTS_BBLAYERS | sort)
303         debug "bblayer fragments: $FRAGMENTS_BBLAYERS"
304         info "   Config: $BUILDDIR/conf/bblayers.conf"
305         for x in $FRAGMENTS_BBLAYERS; do
306                 file=${x/#*:/}
307                 append_fragment $BUILDDIR/conf/bblayers.conf $file
308                 verbose "      + $file"
309         done
310
311         FRAGMENTS_LOCALCONF=$(sed 's/ /\n/g' <<<$FRAGMENTS_LOCALCONF | sort)
312         debug "localconf fragments: $FRAGMENTS_LOCALCONF"
313         info "   Config: $BUILDDIR/conf/local.conf"
314         for x in $FRAGMENTS_LOCALCONF; do
315                 file=${x/#*:/}
316                 append_fragment $BUILDDIR/conf/local.conf $file
317                 verbose "      + $file"
318         done
319
320         FRAGMENTS_SETUP=$(sed 's/ /\n/g' <<<$FRAGMENTS_SETUP | sort)
321         debug "setup fragments: $FRAGMENTS_SETUP"
322         cat <<EOF >$BUILDDIR/conf/setup.sh
323 #!/bin/bash
324
325 # this script has been generated by $BASH_SOURCE
326
327 export MACHINE="$MACHINE"
328 export FEATURES="$FEATURES"
329 export BUILDDIR="$BUILDDIR"
330 export METADIR="$METADIR"
331
332 echo "--- beginning of setup script"
333 EOF
334         info "   Setup script: $BUILDDIR/conf/setup.sh"
335         for x in $FRAGMENTS_SETUP; do
336                 file=${x/#*:/}
337                 append_fragment $BUILDDIR/conf/setup.sh $file "echo '--- fragment $file'"
338                 verbose "      + $file"
339         done
340         append_fragment $BUILDDIR/conf/setup.sh "" "echo '--- end of setup script'"
341
342         infon "   Executing setup script ... "
343         execute_setup $BUILDDIR/conf/setup.sh >$BUILDDIR/conf/setup.log 2>&1 \
344                 && { 
345                         info "OK"
346                         [[ $VERBOSE == 1 ]] && dump_log $BUILDDIR/conf/setup.log
347                         rm $BUILDDIR/conf/setup.sh
348                 } \
349                 || { 
350                         info "FAIL: please check $BUILDDIR/conf/setup.log"
351                         dump_log $BUILDDIR/conf/setup.log
352                         return 1
353                 }
354         # NOTE: the setup.sh script is removed if execution succeeded (only the log remains)
355 }
356
357 ###########################################################################################
358
359 # check for overwrite
360 [[ $FORCE -eq 1 ]] && rm -f \
361         $BUILDDIR/conf/local.conf \
362         $BUILDDIR/conf/bblayers.conf \
363         $BUILDDIR/conf/setup.* \
364         $BUILDDIR/conf/*.log
365
366 if [[ -f $BUILDDIR/conf/local.conf || -f $BUILDDIR/conf/bblayers.conf ]]; then
367         info "Configuration files already exist:"
368         for x in $BUILDDIR/conf/local.conf $BUILDDIR/conf/bblayers.conf; do
369                 [[ -f $x ]] && info "   - $x" 
370         done
371         info "Skipping configuration files generation."
372         info "Use option -f|--force to overwrite existing configuration."
373 else
374         genconfig
375 fi
376
377 # always generate setup script in builddir: it can be sourced later manually without re-running the setup
378 infon "Generating setup file: $BUILDDIR/agl-init-build-env ... "
379 cat <<EOF >$BUILDDIR/agl-init-build-env
380 . $METADIR/poky/oe-init-build-env $BUILDDIR
381 if [ -n "\$DL_DIR" ]; then
382         BB_ENV_EXTRAWHITE="\$BB_ENV_EXTRAWHITE DL_DIR"
383 fi
384 if [ -n "\$SSTATE_DIR" ]; then
385          BB_ENV_EXTRAWHITE="\$BB_ENV_EXTRAWHITE SSTATE_DIR" 
386 fi
387 export BB_ENV_EXTRAWHITE
388 unset TEMPLATECONF
389 EOF
390 info "OK"
391
392 # finally, generate output script if requested by caller
393 if [[ -n "$SETUPSCRIPT" ]]; then
394         debug "generating setupscript in $SETUPSCRIPT"
395         cat <<EOF >$SETUPSCRIPT
396 . $BUILDDIR/agl-init-build-env
397 EOF
398 fi
399
400 info "------------ $SCRIPT: Done"