3 ################################################################################
5 # The MIT License (MIT)
7 # Copyright (c) 2016 Stéphane Desneux <sdx@iot.bzh>
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:
16 # The above copyright notice and this permission notice shall be included in
17 # all copies or substantial portions of the Software.
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
27 ################################################################################
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
34 DEFAULT_MACHINE=qemux86-64
35 DEFAULT_BUILDDIR=./build
39 #SCRIPT=$(basename $BASH_SOURCE)
41 SCRIPTDIR=$(cd $(dirname $BASH_SOURCE) && pwd -P)
42 METADIR=$(cd $(dirname $BASH_SOURCE)/../.. && pwd -P)
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;}
50 info "------------ $SCRIPT: Starting"
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)
55 function list_machines() {
57 for y in $(ls -d $METADIR/$x/templates/machine/* 2>/dev/null); do
63 function list_all_machines() {
64 for x in $AGL_REPOSITORIES; do
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
76 error "Multiple machine templates are not allowed"
78 debug "Machines list has no duplicate."
81 function list_features() {
83 for y in $(ls -d $METADIR/$x/templates/feature/* 2>/dev/null); do
89 function list_all_features() {
90 for x in $AGL_REPOSITORIES; do
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
102 error "Multiple feature templates are not allowed"
104 debug "Features list has no duplicate."
107 function find_machine_dir() {
109 for x in $AGL_REPOSITORIES; do
110 dir=$METADIR/$x/templates/machine/$machine
111 [[ -d $dir ]] && { echo $dir; return 0; }
116 function find_feature_dir() {
118 for x in $AGL_REPOSITORIES; do
119 dir=$METADIR/$x/templates/feature/$feature
120 [[ -d $dir ]] && { echo $dir; return 0; }
127 Usage: . $SCRIPT [options] [feature [feature [... ]]]
130 Compatibility: bash, zsh, ksh
133 -m|--machine <machine>
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)
143 flag to force overwriting any existing configuration
157 echo "Available machines:" >&2
158 for x in $AGL_REPOSITORIES; do
159 buf=$(list_machines $x)
160 [[ -z "$buf" ]] && continue
163 [[ $y == $DEFAULT_MACHINE ]] && def="* " || def=" "
169 echo "Available features:" >&2
170 for x in $AGL_REPOSITORIES; do
171 buf=$(list_features $x)
172 [[ -z "$buf" ]] && continue
181 function append_fragment() {
182 basefile=$1; shift # output file
183 f=$1; shift # input file
186 debug "adding fragment to $basefile: $f"
188 echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >>$basefile
189 echo "# fragment { " >>$basefile
190 [[ -f $f ]] && echo "# $f" >>$basefile || true
192 [[ -n "$label" ]] && echo "$label" >>$basefile
193 [[ -f $f ]] && cat $f >>$basefile || true
195 echo "# }" >>$basefile
196 echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >>$basefile
197 [[ -f $f ]] && echo $f >>$BUILDDIR/conf/fragments.log || true
200 function execute_setup() {
202 debug "Executing script $script"
204 [[ $DEBUG == 1 ]] && opts="$opts -x"
205 pushd $BUILDDIR &>/dev/null
206 $BASH $opts $script \
208 || { rc=$?; error "Script $script failed"; }
213 # process all fragments
214 FRAGMENTS_BBLAYERS=""
215 FRAGMENTS_LOCALCONF=""
217 function process_fragments() {
219 debug "processing fragments in dir $dir"
221 verbose " Searching fragments: $dir"
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)"
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)"
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)"
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; }
251 ### default options values
252 MACHINE=$DEFAULT_MACHINE
253 BUILDDIR=$DEFAULT_BUILDDIR
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;;
267 *) error "Arguments parsing error"; exit 1;;
271 [[ "$HELP" == 1 ]] && { usage; exit 0; }
273 verbose "Command line arguments: ${GLOBAL_ARGS[@]}"
275 # the remaining args are the features
278 # validate the machine list
279 debug "validating machines list"
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)]"
286 # validate the features list
287 debug "validating features list"
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)]"
297 debug "validating builddir $BUILDDIR"
298 BUILDDIR=$(mkdir -p $BUILDDIR && cd $BUILDDIR && pwd -P)
300 ###########################################################################################
301 function dump_log() {
302 info " ------------ $(basename $1) -----------------"
304 info " ----------------------------------------"
307 function genconfig() {
308 info "Generating configuration files:"
309 info " Build dir: $BUILDDIR"
310 info " Machine: $MACHINE"
311 info " Features: $FEATURES"
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"
320 . $METADIR/poky/oe-init-build-env $BUILDDIR >/dev/null
323 # step 2: concatenate other remaining fragments coming from base
324 process_fragments $TEMPLATECONF
326 # step 3: fragments for machine
327 process_fragments $(find_machine_dir $MACHINE)
329 # step 4: fragments for features
330 for feature in $FEATURES; do
331 process_fragments $(find_feature_dir $feature)
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
340 append_fragment $BUILDDIR/conf/bblayers.conf $file
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
349 append_fragment $BUILDDIR/conf/local.conf $file
353 FRAGMENTS_SETUP=$(sed 's/ /\n/g' <<<$FRAGMENTS_SETUP | sort)
354 debug "setup fragments: $FRAGMENTS_SETUP"
355 cat <<EOF >$BUILDDIR/conf/setup.sh
358 # this script has been generated by $BASH_SOURCE
360 export MACHINE="$MACHINE"
361 export FEATURES="$FEATURES"
362 export BUILDDIR="$BUILDDIR"
363 export METADIR="$METADIR"
365 echo "--- beginning of setup script"
367 info " Setup script: $BUILDDIR/conf/setup.sh"
368 for x in $FRAGMENTS_SETUP; do
370 append_fragment $BUILDDIR/conf/setup.sh $file "echo '--- fragment $file'"
373 append_fragment $BUILDDIR/conf/setup.sh "" "echo '--- end of setup script'"
375 infon " Executing setup script ... "
376 execute_setup $BUILDDIR/conf/setup.sh 2>&1 | tee $BUILDDIR/conf/setup.log \
379 [[ $VERBOSE == 1 ]] && dump_log $BUILDDIR/conf/setup.log
380 rm $BUILDDIR/conf/setup.sh
383 info "FAIL: please check $BUILDDIR/conf/setup.log"
384 dump_log $BUILDDIR/conf/setup.log
387 # NOTE: the setup.sh script is removed if execution succeeded (only the log remains)
390 ###########################################################################################
392 # check for overwrite
393 [[ $FORCE -eq 1 ]] && rm -f \
394 $BUILDDIR/conf/local.conf \
395 $BUILDDIR/conf/bblayers.conf \
396 $BUILDDIR/conf/setup.* \
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"
404 info "Skipping configuration files generation."
405 info "Use option -f|--force to overwrite existing configuration."
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"
417 if [ -n "\$SSTATE_DIR" ]; then
418 BB_ENV_EXTRAWHITE="\$BB_ENV_EXTRAWHITE SSTATE_DIR"
420 export BB_ENV_EXTRAWHITE
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
433 info "------------ $SCRIPT: Done"