forked from FreifunkFranken/firmware
-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildscript
executable file
·610 lines (515 loc) · 15.4 KB
/
buildscript
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
#!/bin/bash
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
builddir=./build
# OpenWrt: openwrt-19.07.4+ from 2020-10-16
OPENWRTREV="7dd822983b8728772e433c7718739b741c3d6de0"
PACKAGEREV="0f7f9c96a86f0600226a2118859fa1ae8741c0a3"
ROUTINGREV="b77498bd56d5e45ab4577a1f4ad6ffc55b4a86b7"
# Gluon packages: master from 2020-02-04
GLUONREV="12e41d0ff07ec54bbd67a31ab50d12ca04f2238c"
OPENWRT_PKGS="gpioctl-sysfs libugpio fastd haserl micrond mtr bmon libpam libgnutls liblzma libnetsnmp libopenldap libidn2 libssh2 jansson libcap"
ROUTING_PKGS="kmod-batman-adv batctl alfred babeld"
GLUON_PKGS="simple-tc uradvd"
OPENWRTURL="https://git.openwrt.org/openwrt/openwrt.git"
## Feed definition [0]: name aka directory, [1]: url, [2]: revision, [3..n]: patches
#official openwrt packages
OPENWRT=(openwrt
https://git.openwrt.org/feed/packages.git
$PACKAGEREV)
## Be careful: FFF uses COMPAT_VERSION 15 as default at the moment.
## See http://www.open-mesh.org/projects/batman-adv/wiki/Compatversion
GLUON=(gluon
https://github.com/freifunk-gluon/packages.git
$GLUONREV)
#official openwrt routing packages
ROUTING=(routing
https://git.openwrt.org/feed/routing.git
$ROUTINGREV)
FFF=(fff)
FFF_PKGS="-a"
FEEDS=(OPENWRT ROUTING GLUON FFF)
# Disable dnsmasq and other useless stuff
export DEVICE_TYPE=
checkout_git(){
local DIRECTORY=$1
local REPO_URL=$2
local COMMITID=$3
local MYGIT="git -C $DIRECTORY"
echo "checking out $REPO_URL to $DIRECTORY in version $COMMITID"
if [ -d "$DIRECTORY" ]; then
if $MYGIT remote -v | grep -q "$REPO_URL" ; then
echo "Right remote detected"
if ! $MYGIT checkout "$COMMITID" ; then
echo "commitid not found trying to fetch new commits"
$MYGIT pull && $MYGIT checkout "$COMMITID"
fi
else
echo "wrong remote or not an git repo at all -> deleting whole directory"
/bin/rm -rf "$DIRECTORY"
#needs to be without -C!!!
git clone "$REPO_URL" "$DIRECTORY"
$MYGIT checkout "$COMMITID"
fi
else
echo "We need to do a fresh checkout"
#needs to be without -C!!!
git clone "$REPO_URL" "$DIRECTORY"
$MYGIT checkout "$COMMITID"
fi
}
get_source() {
test -d src || mkdir src
cd src
#Get the OpenWrt Core Source for Firmware
checkout_git openwrt $OPENWRTURL $OPENWRTREV
test -d packages || mkdir packages
cd packages
#checkout needed package feeds
for FEEDVAR in "${FEEDS[@]}" ; do
FEED="${FEEDVAR}[@]"
FEED=("${!FEED}")
local NAME=${FEED[0]}
local URL=${FEED[1]}
local REV=${FEED[2]}
if [ -n "$URL" ] && [ -n "$REV" ]; then
checkout_git "$NAME" "$URL" "$REV"
# Patches for feeds are stored in known directories like feed_patches/$NAME/
for PATCH in $(ls ../../feed_patches/${NAME}/*.patch 2>/dev/null); do
if [ -s "$PATCH" ] ; then
apply_feedpatch "$NAME" "../$PATCH"
else
echo "Empty patch $PATCH ignored."
fi
done
fi
done
cd .. # packages
cd .. # src
}
patch_target() {
for patch in $(ls "$PWD"/build_patches/openwrt/*.patch 2>/dev/null); do
apply_buildpatch "$builddir" "$patch"
done
}
apply_buildpatch() {
local target="$1"
local patch="$2"
echo "Applying $patch:"
patch --no-backup-if-mismatch -p1 -d "$target" -i "$patch"
}
undo_buildpatch() {
local target="$1"
local patch="$2"
echo "Applying $patch:"
patch --no-backup-if-mismatch -R -p1 -d "$target" -i "$patch"
}
apply_feedpatch() {
local target="$1"
local patch="$2"
echo "Applying $patch"
git -C "$target" am --whitespace=nowarn "$patch"
}
get_make_variant() {
local variant
if [ -n "$1" ]; then
variant=$1
else
variant=$(cat selected_variant)
fi
echo "$variant" | sed -e 's/[0-9]*$//' -e 's/-[a-z0-9]*$//'
}
get_file_variant() {
echo "src/packages/fff/fff/variant-$(get_make_variant $1).mk"
}
set_variant() {
# set the variant for this build
cp "./$(get_file_variant)" "$builddir"/variant.mk
# force the reevaluation of this Makefile to make note of the new variant
touch ./src/packages/fff/fff/Makefile
}
prepare() {
get_source
test -d $builddir || mkdir $builddir
/bin/rm -rf "$builddir"
cp -a src/openwrt "$builddir"
patch_target
# set the variant for this build
set_variant
#saves ~200MB for each build
test -d ./src/dl || mkdir ./src/dl
ln -s ../src/dl "$builddir"/dl
update_feeds
}
update_feeds() {
## generate own feeds.conf
#this local variable should be globally configure variable used in get_source and here
local PACKAGEBASE=${PWD}/src/packages
rm -f "$builddir"/feeds.conf
for FEEDVAR in "${FEEDS[@]}" ; do
FEED="${FEEDVAR}[@]"
FEED=("${!FEED}")
local NAME=${FEED[0]}
echo "adding $NAME to package feeds"
echo src-link "$NAME" "$PACKAGEBASE"/"$NAME" >> "$builddir"/feeds.conf
done
echo "cleaning feeds"
"$builddir"/scripts/feeds clean
"$builddir"/scripts/feeds update
for FEEDVAR in "${FEEDS[@]}" ; do
FEED="${FEEDVAR}[@]"
FEED=("${!FEED}")
local NAME=${FEED[0]}
local PACKAGESVAR="${FEEDVAR}_PKGS"
PACKAGESVAR="${PACKAGESVAR}[@]"
PACKAGESVAR=(${!PACKAGESVAR})
if [[ -n "${PACKAGESVAR[@]}" ]] ; then
echo "adding ${PACKAGESVAR[*]} from feed $NAME to available packages"
"$builddir"/scripts/feeds install -p "$NAME" "${PACKAGESVAR[@]}"
fi
done
}
prebuild() {
#create filesdir for our config
/bin/rm -rf "$builddir"/files
mkdir "$builddir"/files
cp -r ./bsp/default/root_file_system/* "$builddir"/files/
cp "$configfile" "$builddir"/.config
while IFS= read -r -d '' template
do
echo "Translating $template .."
$tpl_translate "$template" > "$(dirname "$template")"/"$(basename "$template" .tpl)"
/bin/rm "$template"
done < <(find "${builddir}/files" -name '*.tpl' -print0)
#insert actual firware version informations into release file
variant=$(cat selected_variant)
version=$(git describe --tags --dirty)
if [ 0 -ne $? ]; then
version=$(git log -1 --pretty=format:%h)
fi
version="${variant}_${version#*_}"
echo "Building version: $version"
{
echo "FIRMWARE_VERSION=\"$version\""
echo "VARIANT=\"$variant\""
echo "BUILD_DATE=\"build date: $(date)\""
echo "OPENWRT_CORE_REVISION=\"${OPENWRTREV}\""
echo "OPENWRT_FEEDS_PACKAGES_REVISION=\"${PACKAGEREV}\""
} > "$builddir"/files/etc/firmware_release
opath=$(pwd)
cd "$builddir"
make defconfig
cd "$opath"
}
build() {
set_variant
prebuild
rm_firmware
opath=$(pwd)
cd "$builddir"
cpus=$(grep -c processor /proc/cpuinfo)
case "$1" in
"debug")
make V=99
;;
"fast")
ionice -c 2 -- nice -n 1 -- make -j $((cpus*2))
;;
*)
ionice -c 3 -- nice -n 10 -- make -j $((cpus+1))
;;
esac
cd "$opath"
cp_firmware
}
config() {
prebuild
opath=$(pwd)
cd "$builddir"
case "$1" in
"openwrt")
make menuconfig
;;
esac
save=""
until [ "$save" = "y" -o "$save" = "n" ]; do
echo "Do you want to save the generated config? (y/n)"
read save
done
if [ "$save" = "y" ]; then
case "$1" in
"openwrt")
echo "# Generated using \"./buildscript config openwrt\"." > "$opath"/bsp/config/"$machine".openwrt
echo "# Do no edit manually" >> "$opath"/bsp/config/"$machine".openwrt
echo "#" >> "$opath"/bsp/config/"$machine".openwrt
./scripts/diffconfig.sh >> "$opath"/bsp/config/"$machine".openwrt
;;
esac
fi
cd "$opath"
}
rm_firmware() {
[ -n "$subtarget" ] || subtarget="generic"
rm -rf "$builddir/bin/targets/${chipset}/${subtarget}"
}
cp_firmware() {
[ -n "$subtarget" ] || subtarget="generic"
mkdir -p "bin/$variant"
imagesrcpath="$builddir/bin/targets/${chipset}/${subtarget}"
imagedestpath="./bin/$variant"
for dev in $devices; do
fullsrcpattern="$imagesrcpath/openwrt-${imagetarget/_/-}-${dev}-squashfs-*"
if ! ls $fullsrcpattern >/dev/null 2>/dev/null; then
echo "Warning: Image for $dev not found."
continue
fi
for f in $fullsrcpattern; do
filename_build=${f##*/}
filename_build=${filename_build//openwrt/fff-${version}}
filename_build=${filename_build//${chipset}-${subtarget}-/${chipset}-}
filename_build=${filename_build//squashfs-/}
cp "$f" "$imagedestpath/$filename_build" && \
echo "Success: $filename_build"
done
done
}
buildrelease() {
if [ "$1" = "all" ];then
all=true
elif [ "$1" = "fast" ];then
fast=fast
fi
if [ "$2" = "all" ];then
all=true
elif [ "$2" = "fast" ];then
fast=fast
fi
if [ $all ]; then
buildall $fast
else
build $fast
fi
cd bin/$variant
for binary in *.bin; do
md5sum "$binary" > ./"$binary".md5
sha256sum "$binary" > ./"$binary".sha256
done
echo -e "VERSION:$version" > release.nfo
cd ../..
}
clean() {
/bin/rm -rf bin $builddir src/openwrt
# remove downloaded package feeds
for FEEDVAR in "${FEEDS[@]}" ; do
FEED="${FEEDVAR}[@]"
FEED=("${!FEED}")
local NAME=${FEED[0]}
local URL=${FEED[1]}
local REV=${FEED[2]}
if [ -n "$URL" ] && [ -n "$REV" ]; then
/bin/rm -rf src/packages/"$NAME"
fi
done
}
loadBSP()
{
localbsppath=$(/bin/ls -l selected_bsp | awk '{ print $11 }')
if ! [ -s selected_bsp ]; then
echo "Error: $localbsppath does not exist. Aborting ..."
exit 1
fi
echo "Working with $localbsppath"
machine=$(basename $(realpath selected_bsp) .bsp)
if [ -z "$machine" ]; then
echo "Error: BSP file naming scheme incorrect!"
exit 1
fi
configfile="./bsp/config/${machine}.config"
[ ! -d ./bsp/config ] && mkdir ./bsp/config
> "$configfile"
. ./bsp/buildlib
. selected_bsp
}
loadVariant()
{
echo "Working with build variant \"$(cat selected_variant)\""
}
setVariant()
{
echo "$1" > selected_variant
}
setBSP()
{
/bin/rm -rf selected_bsp
/bin/ln -s "$1" selected_bsp
loadBSP
}
buildall() {
for bsp in $(/bin/ls bsp/*.bsp); do
setBSP "$bsp"
build "$1"
done
}
if [ "$(/usr/bin/id -u)" -eq 0 ]; then
echo "don't run buildscript as root"
exit 1
fi
if [ "$1" != "selectbsp" -a "$1" != "selectvariant" ]; then
if [ ! -h selected_bsp ]; then
echo "Please select a Board-Support-Package using:"
echo "$0 selectbsp"
exit
fi
if [ ! -f selected_variant ]; then
echo "Please select a build variant using:"
echo "$0 selectvariant"
exit
fi
loadBSP
loadVariant
echo
fi
case "$1" in
"selectbsp")
if [ "$2" = "help" ] || [ "$2" = "" ]; then
echo "Select a Board-Support-Package:"
echo
echo "Usage: $0 $1 <bsp-file>"
echo "available packages:"
/bin/ls bsp/*.bsp
echo
else
if [ ! -f "$2" ]; then
echo "Could not find $2"
else
setBSP "$2"
fi
fi
;;
"selectvariant")
if [ "$2" = "help" ] || [ "$2" = "" ]; then
echo "Select a build varaint:"
echo
echo "Usage: $0 $1 <name of variant>"
echo "available variants: "
/bin/ls src/packages/fff/fff/variant-*.mk | sed 's#.*/variant-\(.*\)\.mk#\1*#g'
echo
else
if [ ! -f "$(get_file_variant $2)" ]; then
echo "Could not find variant $2"
else
setVariant $2
fi
fi
;;
"prepare")
if [ "$2" = "help" ] || [ "$2" = "x" ]; then
echo "This option fetches the sources for the images and configurates the build so that it can be compiled"
echo
echo "Usage: $0 $1"
echo
else
prepare
fi
;;
"updatefeeds")
update_feeds
;;
"patchbuild")
apply_buildpatch "$2" "$3"
;;
"unpatchbuild")
undo_buildpatch "$2" "$3"
;;
"patchfeed")
apply_feedpatch "$2" "$3"
;;
"prebuild")
set_variant
prebuild
;;
"build")
if [ "$2" = "help" ] || [ "$2" = "x" ]; then
echo "This option compiles the firmware"
echo "Normaly the build uses lower IO and System priorities, "
echo "you can append \"fast\" option, to use normal user priorities"
echo
echo "Usage: $0 $1 [fast|debug]"
echo
else
build "$2"
fi
;;
"config")
case "$2" in
openwrt)
config openwrt
;;
*)
echo "This open the OpenWrt menuconfig dialog"
echo
echo "Usage: $0 $1 openwrt"
echo
;;
esac
;;
"clean")
if [ "$2" = "help" ] || [ "$2" = "x" ]; then
echo "This option cleans all build files."
echo
echo "Usage: $0 $1"
echo
else
clean
fi
;;
"buildall")
if [ "$2" = "help" ]; then
echo "This option builds the firmware for all routers."
echo
echo "Usage: $0 $1 [fast]"
echo
else
buildall "$2"
fi
;;
"release")
if [ "$2" = help ]; then
echo "This option builds the firmware for a given board. It also creates hash sums and a the file release.nfo"
echo
echo "Usage: $0 $1 [all] [fast]"
echo
else
buildrelease "$2" "$3"
fi
;;
*)
echo "This is the Build Environment Script of the Freifunk Community Franken."
echo
echo "Usage: $0 command"
echo "command:"
echo " selectbsp <bsp-file>"
echo " selectvariant <name of variant>"
echo " patchbuild <target> <patch>"
echo " unpatchbuild <target> <patch>"
echo " patchfeed <target> <patch>"
echo " prepare"
echo " updatefeeds"
echo " config openwrt"
echo " prebuild"
echo " build [fast|debug]"
echo " buildall [fast]"
echo " release [all] [fast]"
echo " clean"
echo ""
echo "If you need help to one of these options just type: $0 <command> help"
echo
;;
esac