-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.sh
executable file
·495 lines (419 loc) · 12.9 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/bin/bash
#
# Yocto build system initiator.
#
# Copyright (C) 2019-2023 Savoir-faire Linux, Inc.
# This program is distributed under the Apache 2 license.
# Name: print_usage
# Brief: Print script usage
print_usage()
{
cat <<EOF
This script builds a Yocto distribution
./$MYNAME [OPTIONS]
Build configuration options:
-d, --dl-dir DIR use DIR as downloads cache directory
-i, --image IMGNAME build a specific image
(default: $default_image)
--distro DISTRO build a specific distribution
(default: $default_distro)
-m, --machine MACHINE build a specific target machine
(default: $default_machine)
-s, --sstate-dir DIR use DIR as sstate-cache directory
(default: ($(basename $BUILDDIR)/sstate-cache/))
-k, --sdk compile SDK (bitbake -c populate_sdk)
Running custom build commands:
-- CUSTOM_COMMAND run custom command instead of default:
bitbake $default_image
Performance options:
-t, --tasks NUM run NUM parallel Bitbake tasks
(default: number of cores=$(nproc))
-j, --jobs NUM parallel jobs/threads per task
(default: number of cores=$(nproc))
Misc. options:
-r, --remove-build-dir remove build directory ($(basename $BUILDDIR)/)
--no-layers-update don't auto-update bblayers.conf
-v, --verbose ignored
-q, --quiet quiet mode (hide build output)
--debug print all executed commands
-h, --help display this help message
EOF
}
# Name: apply_patch
# Brief: Test and apply a patch file if needed
# Param[in]: Patch file
apply_patch()
{
local patch_file="$1"
local done_flag="$patch_file".done
if [ -f "$done_flag" ]; then
log info "Patch $patch_file already applied -- skipping"
return 0
fi
log info "Checking $patch_file..."
patch --dry-run --silent --strip=1 --force --input "$patch_file"
assert "$?" "Could not apply $patch_file"
# At this point we know the patch is valid
patch --strip=1 --force --input "$patch_file"
assert "$?" "Error while applying $patch_file"
log info "Successfully applied $patch_file"
touch "$done_flag"
}
# Name: parse_options
# Brief: Parse options from command line
# Param[in]: Command line parameters
parse_options()
{
ARGS=$(getopt -o "d:i:j:khm:rs:t:v" -l "distro:,dl-dir:,help,image:,jobs:,machine:,no-layers-update,debug,remove-build-dir,sdk,sstate-dir:,tasks:,verbose" -n "build.sh" -- "$@")
# Bad arguments
if [ $? -ne 0 ]; then
exit 1
fi
eval set -- "$ARGS"
ncpus=$(nproc)
while true; do
case "$1" in
--distro)
export DISTRO="$2"
shift 2
;;
-d|--dl-dir)
if [ ! -d "$2" ]; then
log error "Fatal: specified dl-dir does not exist"
exit 1
fi
export DL_DIR=$(readlink -f "$2")
export BB_GENERATE_MIRROR_TARBALLS="1"
shift 2
;;
--debug)
set -x
shift
;;
-i|--image)
export IMAGE="$2"
shift 2
;;
--no-layers-update)
NO_LAYERS_UPDATE=yes
shift
;;
-m|--machine)
export MACHINE="$2"
shift 2
;;
-r|--remove-build-dir)
REMOVE_BUILDDIR=1
shift
;;
-k|--sdk)
COMPILE_SDK=1
shift
;;
-s|--sstate-dir)
if [ ! -d "$2" ]; then
log error "Fatal: specified state-dir does not exist"
exit 1
fi
export SSTATE_DIR=$(readlink -f "$2")
shift 2
;;
-j|--jobs)
if [ "$2" -le 0 ] || [ "$2" -gt "$ncpus" ]; then
log error "Fatal: specified jobs=$2 is invalid, valid range is [1-$ncpus]"
exit 1
fi
export PARALLEL_MAKE="-j $2"
shift 2
;;
-t|--tasks)
if [ "$2" -le 0 ] || [ "$2" -gt "$ncpus" ]; then
log error "Fatal: specified tasks=$2 is invalid, valid range is [1-$ncpus]"
exit 1
fi
export BB_NUMBER_THREADS=$2
shift 2
;;
--meta-list-file)
export META_LIST_FILE=$(readlink -f "$2")
shift 2
;;
-v|--verbose)
# ignored, for backwards compatibility purposes
shift
;;
-q|--quiet)
VERBOSE=0
shift
;;
-h|--help)
print_usage
exit 1
shift
break
;;
-|--)
shift
CMD="$*"
break
;;
*)
print_usage
exit 1
shift
break
;;
esac
done
}
# Name: run_cmd
# Brief: Run given command with enhanced display and return code checked
# Param[in]: Command description to print
# Param[in]: The command itself
run_cmd()
{
# Description to display
description=$1
log info "$description"
# Remove description from parameters
shift
# Launch command
eval $*
# Check command result, exit on error
assert $?
}
# Name filter_layers_blocklist
# Brief Filter out layers listed in layers.blocklist from stdin
filter_layers_blocklist()
{
local filter
local layer
local sep
# Build $filter, a |-separated list of layers to exclude
if [ -s "$TOPDIR/layers.blocklist" ]; then
while read -r layer; do
[ -z "$layer" ] && continue
if [ -d "$SOURCESDIR/$layer" ]; then
filter+="${sep}${SOURCESDIR}/$layer"
sep="|" # separator for 2nd and next items
else
log error "layers.blocklist entry not found: $layer"
fi
done < "$TOPDIR/layers.blocklist"
filter="($filter)"
fi
if [ "$filter" ]; then
# eg. "^(meta-foobar|meta-foobiz)$"
grep -Ev "^$filter$"
else
cat
fi
}
# Name update_layers
# Brief Add layers in bblayers.conf using bitbake-layers add-layer,
# unless NO_LAYERS_UPDATE is set.
update_layers()
{
local layers_to_add
[ -n "$NO_LAYERS_UPDATE" ] && return 0
layers_to_add=$(find "$SOURCESDIR"/meta-* \
"$SOURCESDIR"/poky/meta-* \
-type f \
-path '*/conf/layer.conf' \
-print0 |
xargs -0 -n1 dirname |
xargs -n1 dirname |
filter_layers_blocklist)
if [ -n "$layers_to_add" ]; then
run_cmd "update layers" "bitbake-layers add-layer $layers_to_add"
fi
}
# Name C
# Brief Print colorized string
# arg1 color name (see below)
# arg2 string
C()
{
local color="$1"; shift
local text="$*"
local nc='\033[0m'
local c
# Only colorize a few terminal types
case "$TERM" in
linux*|xterm*|screen|vt102) ;;
*)
echo "$@"
return
;;
esac
case "$color" in
gray) c='\033[1;30m' ;;
red) c='\033[1;31m' ;;
green) c='\033[1;32m' ;;
yellow) c='\033[1;33m' ;;
blue) c='\033[1;34m' ;;
purple) c='\033[1;35m' ;;
cyan) c='\033[1;36m' ;;
orange_bg) c='\033[48;2;255;165;0m'
esac
printf "${c}${text}${nc}"
}
# Name log
# Brief Provide message logging to the terminal
# arg1 Log level (debug, info, warn, error - default : info)
# arg2.. Message to print
log() {
local level="$1"; shift
local color="cyan"
local uplevel
local upname
# Sanitize log level
case "$level" in
debug|info|warn|error) ;;
*) level="info" ;;
esac
# Apply formatting
case "$level" in
debug) color="purple" ;;
info) color="cyan" ;;
warn) color="orange_bg" ;;
error) color="red" ;;
esac
# level -> LEVEL
uplevel=$(echo $level | tr '[:lower:]' '[:upper:]')
upname=$(echo $MYNAME | tr '[:lower:]' '[:upper:]')
echo $(C $color "[$upname $uplevel] $*")
}
# Name assert
# Brief Check $arg1 as return code, exit program if nonzero
# arg1 Return code to check
# arg2.. (optional) Message to print in case of error
assert() {
local retcode="$1"; shift
local message="$*"
case "$retcode" in
0) ;;
*)
[ "$message" ] && log error "Fatal: $message"
exit $retcode
;;
esac
}
##########################
########## MAIN ##########
##########################
#### Local vars ####
# Be verbose by default
export VERBOSE=1
# Keep directory to retrieve tools
MYNAME="$(basename $0)"
TOPDIR=$(dirname $(readlink -f "$0"))
BUILDDIR=$TOPDIR/build
SOURCESDIR=$TOPDIR/sources
POKYDIR=$(dirname $(find "$SOURCESDIR" -name "oe-init-build-env" -print -quit))
# Change to top directory
cd "$TOPDIR"
# Check for Poky directory
if [ -z "$POKYDIR" ]; then
log error "poky directory cannot be found"
exit 1
fi
# build.conf contains defaults for image, machine and distro
if [ -f "$TOPDIR"/build.conf ]; then
. "$TOPDIR"/build.conf
else
log info "No build.conf file found - Using defaults for image/machine/distro"
default_image=seapath-host-efi-image
default_machine="seapath-hypervisor"
default_distro="seapath-host"
fi
# Parse options
parse_options "$@"
# Display VARIABLES
log debug "CMD = '$CMD'"
log debug "DL_DIR = '$DL_DIR'"
log debug "BB_GENERATE_MIRROR_TARBALLS" = "$BB_GENERATE_MIRROR_TARBALLS"
log debug "SSTATE_DIR = '$SSTATE_DIR'"
log debug "BB_NUMBER_THREADS = '$BB_NUMBER_THREADS'"
log debug "PARALLEL_MAKE = '$PARALLEL_MAKE'"
# Apply patches
if [ -d patches ] && ls patches/*.patch 1>/dev/null 2>/dev/null ; then
log info "Applying patches..."
for patch in patches/*.patch; do
apply_patch "$patch" || exit 1
done
fi
# Set image to build, default to core-image-minimal
export IMAGE=${IMAGE:-"seapath-host-efi-image"}
export MACHINE=${MACHINE:-"seapath-hypervisor"}
export DISTRO=${DISTRO:-"seapath-host"}
export ACCEPT_FSL_EULA="1"
export LSB_WARN='0'
export SKIP_META_SECURITY_SANITY_CHECK="1"
export SKIP_META_VIRT_SANITY_CHECK="1"
export CVE_CHECK_SHOW_WARNINGS="0"
if [ -f seapath.conf ] ; then
for seapath_env in $(bash -c \
'( source seapath.conf ; set -o posix ; set \
| grep -e "^SEAPATH" )') ; do
seapath_env_key=$(echo ${seapath_env} | cut -d '=' -f 1)
seapath_env_value=$(echo ${seapath_env} | cut -d '=' -f 2-)
if [ -z $(printenv "${seapath_env_key}") ] ; then
export "${seapath_env_key}"="${seapath_env_value}"
fi
BB_ENV_PASSTHROUGH_ADDITIONS="$BB_ENV_PASSTHROUGH_ADDITIONS ${seapath_env_key}"
done
else
log error "Fatal: seapath.conf does not exists."
log error "Look at the README to create a seapath.conf file"
exit 1
fi
for seapath_env in $(printenv | grep -e "^SEAPATH") ; do
log debug "$(echo $seapath_env | cut -d '=' -f 1) =
$(echo $seapath_env | cut -d '=' -f 2-)"
done
# Set variable readable from command line
export BB_ENV_PASSTHROUGH_ADDITIONS="$BB_ENV_PASSTHROUGH_ADDITIONS \
ACCEPT_FSL_EULA \
BB_GENERATE_MIRROR_TARBALLS \
BB_NUMBER_THREADS \
DISTRO \
DL_DIR \
LSB_WARN \
MACHINE \
PARALLEL_MAKE \
SKIP_META_SECURITY_SANITY_CHECK \
SKIP_META_VIRT_SANITY_CHECK \
SSTATE_DIR \
CVE_CHECK_SHOW_WARNINGS \
"
# Set image to build, default to core-image-minimal
export IMAGE=${IMAGE:-"$default_image"}
export MACHINE=${MACHINE:-"$default_machine"}
export DISTRO=${DISTRO:-"$default_distro"}
# For NXP BSPs, auto-accept their EULA
export ACCEPT_FSL_EULA="1"
if [ -n "$REMOVE_BUILDDIR" ]; then
# Clean directory
run_cmd "Removing build directory" rm -Rf "$BUILDDIR"
elif [ -z "$NO_LAYERS_UPDATE" ]; then
# Clean layers
run_cmd "Removing bblayers.conf" rm -f "$BUILDDIR"/conf/bblayers.conf
fi
# Init poky build
set "$BUILDDIR"
. "$POKYDIR"/oe-init-build-env 1>/dev/null
# Add layers
update_layers
# Build Yocto
if [ -n "$CMD" ]; then
run_cmd "Launching command..." "$CMD"
elif [ -z "$COMPILE_SDK" ]; then
run_cmd "Building image..." bitbake "$IMAGE"
else
run_cmd "Building sdk..." bitbake "$IMAGE" -c populate_sdk
fi
# Generate the documentation
if [ "$(type -p asciidoctor-pdf)" ]; then
asciidoctor-pdf -o tmp/deploy/images/README.pdf ../README.adoc
fi