3 ################################################################################
5 # The MIT License (MIT)
7 # Copyright (c) 2016 Stéphane Desneux <sdx@iot.bzh>
8 # (c) 2016 Jan-Simon Möller <jsmoeller@linuxfoundation.org>
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:
17 # The above copyright notice and this permission notice shall be included in
18 # all copies or substantial portions of the Software.
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
28 ################################################################################
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
35 DEFAULT_MACHINE=qemux86-64
36 DEFAULT_BUILDDIR=./build
40 #SCRIPT=$(basename $BASH_SOURCE)
42 SCRIPTDIR=$(cd $(dirname $BASH_SOURCE) && pwd -P)
43 METADIR=$(cd $(dirname $BASH_SOURCE)/../.. && pwd -P)
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;}
51 info "------------ $SCRIPT: Starting"
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)
56 function list_machines() {
58 for y in $(ls -d $METADIR/$x/templates/machine/* 2>/dev/null); do
64 function list_all_machines() {
65 for x in $AGL_REPOSITORIES; do
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
77 error "Multiple machine templates are not allowed"
79 debug "Machines list has no duplicate."
82 function list_features() {
84 for y in $(ls -d $METADIR/$x/templates/feature/* 2>/dev/null); do
90 function list_all_features() {
91 for x in $AGL_REPOSITORIES; do
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
103 error "Multiple feature templates are not allowed"
105 debug "Features list has no duplicate."
108 function find_machine_dir() {
110 for x in $AGL_REPOSITORIES; do
111 dir=$METADIR/$x/templates/machine/$machine
112 [[ -d $dir ]] && { echo $dir; return 0; }
117 function find_feature_dir() {
119 for x in $AGL_REPOSITORIES; do
120 dir=$METADIR/$x/templates/feature/$feature
121 [[ -d $dir ]] && { echo $dir; return 0; }
128 Usage: . $SCRIPT [options] [feature [feature [... ]]]
131 Compatibility: bash, zsh, ksh
134 -m|--machine <machine>
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)
144 flag to force overwriting any existing configuration
158 echo "Available machines:" >&2
159 for x in $AGL_REPOSITORIES; do
160 buf=$(list_machines $x)
161 [[ -z "$buf" ]] && continue
164 [[ $y == $DEFAULT_MACHINE ]] && def="* " || def=" "
170 echo "Available features:" >&2
171 for x in $AGL_REPOSITORIES; do
172 buf=$(list_features $x)
173 [[ -z "$buf" ]] && continue
182 function append_fragment() {
183 basefile=$1; shift # output file
184 f=$1; shift # input file
187 debug "adding fragment to $basefile: $f"
189 echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >>$basefile
190 echo "# fragment { " >>$basefile
191 [[ -f $f ]] && echo "# $f" >>$basefile || true
193 [[ -n "$label" ]] && echo "$label" >>$basefile
194 [[ -f $f ]] && cat $f >>$basefile || true
196 echo "# }" >>$basefile
197 echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >>$basefile
198 [[ -f $f ]] && echo $f >>$BUILDDIR/conf/fragments.log || true
201 function execute_setup() {
203 debug "Executing script $script"
205 [[ $DEBUG == 1 ]] && opts="$opts -x"
206 pushd $BUILDDIR &>/dev/null
207 $BASH $opts $script \
209 || { rc=$?; error "Script $script failed"; }
214 # process all fragments
215 FRAGMENTS_BBLAYERS=""
216 FRAGMENTS_LOCALCONF=""
218 function process_fragments() {
220 debug "processing fragments in dir $dir"
222 verbose " Searching fragments: $dir"
224 # lookup for files with priorities specified: something like xx_bblayers.conf.yyyyy.inc
225 for x in $(ls $dir/??[._]bblayers.conf*.inc 2>/dev/null); do
226 FRAGMENTS_BBLAYERS="$FRAGMENTS_BBLAYERS $(basename $x):$x"
227 verbose " priority $(basename $x | cut -c1-2): $(basename $x)"
230 # same for local.conf
231 for x in $(ls $dir/??[._]local.conf*.inc 2>/dev/null); do
232 FRAGMENTS_LOCALCONF="$FRAGMENTS_LOCALCONF $(basename $x):$x"
233 verbose " priority $(basename $x | cut -c1-2): $(basename $x)"
237 for x in $(ls $dir/??[._]setup*.sh 2>/dev/null); do
238 FRAGMENTS_SETUP="$FRAGMENTS_SETUP $(basename $x):$x"
239 verbose " priority $(basename $x | cut -c1-2): $(basename $x)"
245 debug "Parsing arguments: $@"
246 TEMP=$(getopt -o m:b:s:fvdh --long machine:,builddir:,script:,force,verbose,debug,help -n $SCRIPT -- "$@")
247 [[ $? != 0 ]] && { usage; exit 1; }
252 ### default options values
253 MACHINE=$DEFAULT_MACHINE
254 BUILDDIR=$DEFAULT_BUILDDIR
260 -m|--machine) MACHINE=$2; shift 2;;
261 -b|--builddir) BUILDDIR=$2; shift 2;;
262 -s|--setupscript) SETUPSCRIPT=$2; shift 2;;
263 -f|--force) FORCE=1; shift;;
264 -v|--verbose) VERBOSE=1; shift;;
265 -d|--debug) VERBOSE=1; DEBUG=1; shift;;
266 -h|--help) HELP=1; shift;;
268 *) error "Arguments parsing error"; exit 1;;
272 [[ "$HELP" == 1 ]] && { usage; exit 0; }
274 verbose "Command line arguments: ${GLOBAL_ARGS[@]}"
276 # the remaining args are the features
279 # validate the machine list
280 debug "validating machines list"
283 # validate the machine
284 debug "validating machine $MACHINE"
285 find_machine_dir $MACHINE >/dev/null || error "Machine '$MACHINE' not found in [ $(list_all_machines)]"
287 # validate the features list
288 debug "validating features list"
291 if $(echo "$FEATURES" | grep -q 'agl-all-features' 2>&1 ) ; then
292 featuredir=$(find_feature_dir agl-all-features)
293 tmpfeatures="$FEATURES $(cat $featuredir/included.inc)"
294 tmpfeatures_uniq="$(echo $tmpfeatures | sed -e 's/agl-all-features//g' -e 's/ / /g' | sort -u )"
295 export FEATURES=$tmpfeatures_uniq
296 echo "Features used: $FEATURES"
299 # validate the features
300 for f in $FEATURES; do
301 debug "validating feature $f"
302 find_feature_dir $f >/dev/null || error "Feature '$f' not found in [ $(list_all_features)]"
306 debug "validating builddir $BUILDDIR"
307 BUILDDIR=$(mkdir -p $BUILDDIR && cd $BUILDDIR && pwd -P)
309 ###########################################################################################
310 function dump_log() {
311 info " ------------ $(basename $1) -----------------"
313 info " ----------------------------------------"
316 function genconfig() {
317 info "Generating configuration files:"
318 info " Build dir: $BUILDDIR"
319 info " Machine: $MACHINE"
320 info " Features: $FEATURES"
322 # step 1: run usual OE setup to generate conf dir
323 export TEMPLATECONF=$(cd $SCRIPTDIR/../templates/base && pwd -P)
324 debug "running oe-init-build-env with TEMPLATECONF=$TEMPLATECONF"
325 info " Running $METADIR/poky/oe-init-build-env"
326 info " Templates dir: $TEMPLATECONF"
329 . $METADIR/poky/oe-init-build-env $BUILDDIR >/dev/null
332 # step 2: concatenate other remaining fragments coming from base
333 process_fragments $TEMPLATECONF
335 # step 3: fragments for machine
336 process_fragments $(find_machine_dir $MACHINE)
338 # step 4: fragments for features
339 for feature in $FEATURES; do
340 process_fragments $(find_feature_dir $feature)
343 # step 5: sort fragments and append them in destination files
344 FRAGMENTS_BBLAYERS=$(sed 's/ /\n/g' <<<$FRAGMENTS_BBLAYERS | sort)
345 debug "bblayer fragments: $FRAGMENTS_BBLAYERS"
346 info " Config: $BUILDDIR/conf/bblayers.conf"
347 for x in $FRAGMENTS_BBLAYERS; do
349 append_fragment $BUILDDIR/conf/bblayers.conf $file
353 FRAGMENTS_LOCALCONF=$(sed 's/ /\n/g' <<<$FRAGMENTS_LOCALCONF | sort)
354 debug "localconf fragments: $FRAGMENTS_LOCALCONF"
355 info " Config: $BUILDDIR/conf/local.conf"
356 for x in $FRAGMENTS_LOCALCONF; do
358 append_fragment $BUILDDIR/conf/local.conf $file
362 FRAGMENTS_SETUP=$(sed 's/ /\n/g' <<<$FRAGMENTS_SETUP | sort)
363 debug "setup fragments: $FRAGMENTS_SETUP"
364 cat <<EOF >$BUILDDIR/conf/setup.sh
367 # this script has been generated by $BASH_SOURCE
369 export MACHINE="$MACHINE"
370 export FEATURES="$FEATURES"
371 export BUILDDIR="$BUILDDIR"
372 export METADIR="$METADIR"
374 echo "--- beginning of setup script"
376 info " Setup script: $BUILDDIR/conf/setup.sh"
377 for x in $FRAGMENTS_SETUP; do
379 append_fragment $BUILDDIR/conf/setup.sh $file "echo '--- fragment $file'"
382 append_fragment $BUILDDIR/conf/setup.sh "" "echo '--- end of setup script'"
384 infon " Executing setup script ... "
385 execute_setup $BUILDDIR/conf/setup.sh 2>&1 | tee $BUILDDIR/conf/setup.log
386 [[ ${PIPESTATUS[0]} == 0 ]] && {
388 [[ $VERBOSE == 1 ]] && dump_log $BUILDDIR/conf/setup.log
389 rm $BUILDDIR/conf/setup.sh
392 info "FAIL: please check $BUILDDIR/conf/setup.log"
393 dump_log $BUILDDIR/conf/setup.log
396 # NOTE: the setup.sh script is removed if execution succeeded (only the log remains)
399 ###########################################################################################
401 # check for overwrite
402 [[ $FORCE -eq 1 ]] && rm -f \
403 $BUILDDIR/conf/local.conf \
404 $BUILDDIR/conf/bblayers.conf \
405 $BUILDDIR/conf/setup.* \
408 if [[ -f $BUILDDIR/conf/local.conf || -f $BUILDDIR/conf/bblayers.conf ]]; then
409 info "Configuration files already exist:"
410 for x in $BUILDDIR/conf/local.conf $BUILDDIR/conf/bblayers.conf; do
411 [[ -f $x ]] && info " - $x"
413 info "Skipping configuration files generation."
414 info "Use option -f|--force to overwrite existing configuration."
419 # always generate setup script in builddir: it can be sourced later manually without re-running the setup
420 infon "Generating setup file: $BUILDDIR/agl-init-build-env ... "
421 cat <<EOF >$BUILDDIR/agl-init-build-env
422 . $METADIR/poky/oe-init-build-env $BUILDDIR
423 if [ -n "\$DL_DIR" ]; then
424 BB_ENV_EXTRAWHITE="\$BB_ENV_EXTRAWHITE DL_DIR"
426 if [ -n "\$SSTATE_DIR" ]; then
427 BB_ENV_EXTRAWHITE="\$BB_ENV_EXTRAWHITE SSTATE_DIR"
429 export BB_ENV_EXTRAWHITE
434 # finally, generate output script if requested by caller
435 if [[ -n "$SETUPSCRIPT" ]]; then
436 debug "generating setupscript in $SETUPSCRIPT"
437 cat <<EOF >$SETUPSCRIPT
438 . $BUILDDIR/agl-init-build-env
442 info "------------ $SCRIPT: Done"