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 AGL_REPOSITORIES="meta-agl meta-agl-extra meta-agl-contrib"
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 function list_machines() {
55 for y in $(ls -d $METADIR/$x/templates/machine/* 2>/dev/null); do
56 echo -n "$(basename $y) "
61 function list_all_machines() {
62 for x in $AGL_REPOSITORIES; do
63 [[ ! -d $METADIR/$x ]] && continue
68 function list_features() {
70 for y in $(ls -d $METADIR/$x/templates/feature/* 2>/dev/null); do
71 echo -n "$(basename $y) "
76 function list_all_features() {
77 for x in $AGL_REPOSITORIES; do
78 [[ ! -d $METADIR/$x ]] && continue
83 function find_machine_dir() {
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; }
93 function find_feature_dir() {
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; }
105 Usage: . $SCRIPT [options] [feature [feature [... ]]]
108 Compatibility: bash, zsh, ksh
111 -m|--machine <machine>
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)
121 flag to force overwriting any existing configuration
134 echo "Available machines:" >&2
135 for x in $AGL_REPOSITORIES; do
136 [[ ! -d $METADIR/$x ]] && continue
138 for y in $(list_machines $x); do
139 [[ $y == $DEFAULT_MACHINE ]] && def="* " || def=" "
145 echo "Available features:" >&2
146 for x in $AGL_REPOSITORIES; do
147 [[ ! -d $METADIR/$x ]] && continue
149 for y in $(list_features $x); do
156 function append_fragment() {
157 basefile=$1; shift # output file
158 f=$1; shift # input file
161 debug "adding fragment to $basefile: $f"
163 echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >>$basefile
164 echo "# fragment { " >>$basefile
165 [[ -f $f ]] && echo "# $f" >>$basefile || true
167 [[ -n "$label" ]] && echo "$label" >>$basefile
168 [[ -f $f ]] && cat $f >>$basefile || true
170 echo "# }" >>$basefile
171 echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >>$basefile
172 [[ -f $f ]] && echo $f >>$BUILDDIR/conf/fragments.log || true
175 function execute_setup() {
177 debug "Executing script $script"
179 [[ $DEBUG == 1 ]] && opts="$opts -x"
180 pushd $BUILDDIR &>/dev/null
181 $BASH $opts $script \
183 || { rc=$?; error "Script $script failed"; }
188 # process all fragments
189 FRAGMENTS_BBLAYERS=""
190 FRAGMENTS_LOCALCONF=""
192 function process_fragments() {
194 debug "processing fragments in dir $dir"
196 verbose " Searching fragments: $dir"
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)"
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)"
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)"
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; }
226 ### default options values
227 MACHINE=$DEFAULT_MACHINE
228 BUILDDIR=$DEFAULT_BUILDDIR
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;;
242 *) error "Arguments parsing error"; exit 1;;
246 [[ "$HELP" == 1 ]] && { usage; exit 0; }
248 verbose "Command line arguments: ${GLOBAL_ARGS[@]}"
250 # the remaining args are the features
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)]"
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)]"
264 debug "validating builddir $BUILDDIR"
265 BUILDDIR=$(mkdir -p $BUILDDIR && cd $BUILDDIR && pwd -P)
267 ###########################################################################################
268 function dump_log() {
269 info " ------------ $(basename $1) -----------------"
271 info " ----------------------------------------"
274 function genconfig() {
275 info "Generating configuration files:"
276 info " Build dir: $BUILDDIR"
277 info " Machine: $MACHINE"
278 info " Features: $FEATURES"
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"
287 . $METADIR/poky/oe-init-build-env $BUILDDIR >/dev/null
290 # step 2: concatenate other remaining fragments coming from base
291 process_fragments $TEMPLATECONF
293 # step 3: fragments for machine
294 process_fragments $(find_machine_dir $MACHINE)
296 # step 4: fragments for features
297 for feature in $FEATURES; do
298 process_fragments $(find_feature_dir $feature)
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
307 append_fragment $BUILDDIR/conf/bblayers.conf $file
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
316 append_fragment $BUILDDIR/conf/local.conf $file
320 FRAGMENTS_SETUP=$(sed 's/ /\n/g' <<<$FRAGMENTS_SETUP | sort)
321 debug "setup fragments: $FRAGMENTS_SETUP"
322 cat <<EOF >$BUILDDIR/conf/setup.sh
325 # this script has been generated by $BASH_SOURCE
327 export MACHINE="$MACHINE"
328 export FEATURES="$FEATURES"
329 export BUILDDIR="$BUILDDIR"
330 export METADIR="$METADIR"
332 echo "--- beginning of setup script"
334 info " Setup script: $BUILDDIR/conf/setup.sh"
335 for x in $FRAGMENTS_SETUP; do
337 append_fragment $BUILDDIR/conf/setup.sh $file "echo '--- fragment $file'"
340 append_fragment $BUILDDIR/conf/setup.sh "" "echo '--- end of setup script'"
342 infon " Executing setup script ... "
343 execute_setup $BUILDDIR/conf/setup.sh 2>&1 | tee $BUILDDIR/conf/setup.log \
346 [[ $VERBOSE == 1 ]] && dump_log $BUILDDIR/conf/setup.log
347 rm $BUILDDIR/conf/setup.sh
350 info "FAIL: please check $BUILDDIR/conf/setup.log"
351 dump_log $BUILDDIR/conf/setup.log
354 # NOTE: the setup.sh script is removed if execution succeeded (only the log remains)
357 ###########################################################################################
359 # check for overwrite
360 [[ $FORCE -eq 1 ]] && rm -f \
361 $BUILDDIR/conf/local.conf \
362 $BUILDDIR/conf/bblayers.conf \
363 $BUILDDIR/conf/setup.* \
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"
371 info "Skipping configuration files generation."
372 info "Use option -f|--force to overwrite existing configuration."
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"
384 if [ -n "\$SSTATE_DIR" ]; then
385 BB_ENV_EXTRAWHITE="\$BB_ENV_EXTRAWHITE SSTATE_DIR"
387 export BB_ENV_EXTRAWHITE
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
400 info "------------ $SCRIPT: Done"