-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbuild.sh
executable file
·1391 lines (1148 loc) · 43.3 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
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Copyright (C) 2006 - present, Stephan Enderlein<stephan@freifunk-dresden.de
# GNU General Public License Version 3
#usage: see below
SCRIPT_VERSION="23"
# gitlab variables
# FF_REGISTERKEY_PREFIX
# FF_BUILD_TAG
# FF_MESH_KEY
# check terms
case "$TERM" in
xterm*) _TERM=1 ;;
screen) _TERM=1 ;;
vt1*) _TERM=1 ;;
*) _TERM=0 ;;
esac
#change to directory where build.sh is
cd $(dirname $0)
# target file
PLATFORMS_JSON="build.json"
USE_DOCKER=false
DOCKER_IMAGE="freifunkdresden/openwrt-docker-build"
DOCKER_FINAL_TGZ="docker-final-output.tgz"
DOCKER_CONTAINER_NAME="ffbuild"
DL_DIR=dl
WORK_DIR=workdir
FINAL_OUTPUT_DIR="final_output" # used by gen-upload.sh (docker)
LOCAL_OUTPUT_DIR="output" # location to copy images/packages to, after each targets
CONFIG_DIR=openwrt-configs
CONFIG_DEFAULT_FILE="default.config"
OPENWRT_PATCHES_DIR=openwrt-patches
DDMESH_STATUS_DIR=".ddmesh" # used to store build infos like openwrt_patches_target states
DDMESH_PATCH_STATUS_DIR="$DDMESH_STATUS_DIR/patches-applied"
COMPILE_STATUS_DIR=".compile-status"
COMPILE_STATUS_FILE_SUFFIX="compile-status.json"
# -------------------------------------------------------------------
#Black 0;30 Dark Gray 1;30
#Red 0;31 Light Red 1;31
#Green 0;32 Light Green 1;32
#Brown/Orange 0;33 Yellow 1;33
#Blue 0;34 Light Blue 1;34
#Purple 0;35 Light Purple 1;35
#Cyan 0;36 Light Cyan 1;36
#Light Gray 0;37 White 1;37
# 0 normal
# 1 highlight
# 2 darker
# 3 kursiv
# 4
# 5 blink
# 7 swap background/foreground
# 8 fg/bg same color
# 9 strike
C_NONE='\033[0m' # No Color
C_GREY='\033[1;30m'
C_RED='\033[0;31m'
C_LRED='\033[1;31m'
C_GREEN='\033[0;32m'
C_LGREEN='\033[1;32m'
C_ORANGE='\033[0;33m'
C_YELLOW='\033[1;33m'
C_BLUE='\033[0;34m'
C_LBLUE='\033[1;34m'
C_PURPLE='\033[0;35m'
C_LPURPLE='\033[1;35m'
C_CYAN='\033[0;36m'
C_LCYAN='\033[1;36m'
C_GREY='\033[0;37m'
C_LGREY='\033[1;37m'
C_BLINK='\033[5m'
BG_BLACK='\033[40m'
BG_RED='\033[41m'
BG_GREEN='\033[42m'
BG_YELLOW='\033[43m'
BG_BLUE='\033[44m'
BG_PURBLE='\033[45m'
BG_CYAN='\033[46m'
BG_WHITE='\033[47m'
BG_GRAY='\033[48m'
if true; then
PBC_RUNNING="${C_YELLOW}${C_BLINK}*${C_NONE}"
PBC_ERROR="${C_LRED}E${C_NONE}"
PBC_IGNORE="${C_RED}i${C_NONE}"
PBC_SUCCESS="${C_GREEN}+${C_NONE}"
PBC_SKIP="${C_GREEN}-${C_NONE}"
else
PBC_RUNNING="*"
PBC_ERROR="E"
PBC_IGNORE="i"
PBC_SUCCESS="+"
PBC_SKIP="-"
fi
# define color lookup table used when displaying targets via "list"
declare -A list_color
list_color['18.06']="${BG_CYAN}"
list_color['21.02']="${BG_PURBLE}"
list_color['22.03']="${BG_BLUE}"
#printf "${list_color['18.06']}aaa ${list_color['21.02']}%s ${C_NONE}bbb\n" "value"
#printf "${list_color[$v]}aaa ${list_color['21.02']}%s ${C_NONE}bbb\n" "value"
#save current directory when copying config file
RUN_DIR=$(pwd)
global_error=0
############# progress bar ##########################
progressbar()
{
_value=$1
shift
_maxValue=$1
shift
_marker=$1
shift
_char_array=("$@") # get all other strings and create array again
if [ "$_TERM" = "1" -a -n "$_value" -a -n "$_maxValue" ]; then
# get current number of terminal colums
cols=$(tput cols)
title="Progress: "
title_len=${#title} # title length
let _progress=(${_value}*100/${_maxValue})
progress_string="$(printf ' %3u%% (%u/%u)' $_progress $_value $_maxValue )"
progress_strlen=${#progress_string}
# calulate length of bar
len=$(( cols - title_len - 2 - progress_strlen))
charsPerValue=$(( len / _maxValue))
# make len multiple of charsPerValue
len=$(( charsPerValue * _maxValue))
charSteps=$((len / charsPerValue))
# reduce len by number of _maxValue, because
# marker are inserted into _bar which makes it
# longer again.
# consider length marker string
if [ -n "${_marker}" ]; then
len=$((len - (charSteps*${#_marker})))
charsPerValue=$((charsPerValue-${#_marker}))
fi
absCharPos=$((charsPerValue * _value))
_bar=""
pos=0
nextMarkerPos=$charsPerValue
barCharIdx=0
while [ $pos -lt $len ]
do
pos=$((pos + 1))
if [ $pos -le $absCharPos ]; then
# _bar="${_bar}#"
_bar="${_bar}${_char_array[$barCharIdx]}"
else
_bar="${_bar} " # use space character for empty progress
fi
[ $pos -ge $len ] && break;
if [ -n "${_marker}" -a $pos -eq $nextMarkerPos ]; then
_bar="${_bar}${_marker}"
nextMarkerPos=$(( nextMarkerPos + charsPerValue))
barCharIdx=$((barCharIdx +1 ))
fi
done
# construct complete bar
#printf "%s[%s]%s" "${title}" "${_bar}" "${progress_string}"
echo -e -n "${title}[${_bar}]${progress_string}"
# clear until end of line
tput el
fi
}
# clean up screen and
clean_up_exit()
{
EXIT="$1"
if [ "$_TERM" = "1" ]; then
# reset region
if [ -n "$row" ]; then
printf "\\033[r\n"
tput cup $row 0
printf "\n"
fi
fi
exit ${EXIT:=0}
}
show_progress()
{
if [ "$_TERM" = "1" ]; then
# dont overwrite last value, when no parameter was given (window resize signal)
[ -n "$1" ] && _count=$1
shift
[ -n "$1" ] && _max=$1
shift
[ -n "$1" ] && _bar_char_array=("$@") # get all other strings and create array again
[ -z "$_count" ] && return
[ -z "$_max" -o "$_max" -eq 0 ] && return
row=$(tput lines)
# empty second line
tput cup 1 0
tput el
# print progress bar at bottom
tput cup $(( $row - 1)) 0
progressbar $_count $_max "|" "${_bar_char_array[@]}" # pass last array as separate parameters
# but as one argument with "" to allow
# special characters like *
# print empty line above
tput cup $(( $row - 2)) 0
tput el
# define scroll region before setting cursor. else it would overwrite progress bar
# leave out second row parameter to use max
printf "\\033[0;%dr" $(( $row - 2 ))
# set cursor into last line of region
tput cup $(( $row - 3)) 0
fi
}
############# build.sh functions ####################
getTargetsJson()
{
cat $RUN_DIR/$PLATFORMS_JSON | sed -n "
#delete comments
s/#.*//
# delete empty lines
# delete leading and tailing spaces
s/^[ ]*//
s/[ ]*$//
/^$/d
p
" | jq "[ .targets[] ] | sort_by(.name) "
}
listTargets()
{
OPT="--raw-output" # do not excape values
cleanJson=$(getTargetsJson)
# ARG_regexTarget='ip'
# echo "$cleanJson" | jq --raw-output ".[] | select( .name | test (\"${ARG_regexTarget}\") ) | .name"
# first read default
entry=$(echo "$cleanJson" | jq '.[] | select(.name == "default")')
if [ -n "$entry" ]; then
_def_name=$(echo $entry | jq $OPT '.name')
_def_selector_config=$(echo $entry | jq $OPT '.["selector-config"]')
_def_selector_files=$(echo $entry | jq $OPT '.["selector-files"]')
_def_selector_feeds=$(echo $entry | jq $OPT '.["selector-feeds"]')
_def_selector_patches=$(echo $entry | jq $OPT '.["selector-patches"]')
_def_target_patches=$(echo $entry | jq $OPT '.["target-patches"]')
_def_openwrt_rev=$(echo $entry | jq $OPT '.openwrt_rev')
_def_openwrt_variant=$(echo $entry | jq $OPT '.openwrt_variant')
_def_feeds=$(echo $entry | jq $OPT '.feeds')
_def_packages=$(echo $entry | jq $OPT '.packages')
#echo name:$_def_name
#echo orev:$_def_openwrt_rev
#echo ovariant:$_def_openwrt_variant
#echo selconf:$_def_selector_config
#echo selfeeds:$_def_selector_feeds
#echo selfile:$_def_selector_files
#echo selpatch:$_def_selector_patches
#echo feeds:$_def_feeds
#echo packages:$_def_packages
fi
printf -- '----------------------------------+------------+---------+----------+---------+---------+---------+------------------------------\n'
printf " %-31s | %-10.10s | %-7.7s | %-8.8s | %-7.7s | %-7.7s | %-7.7s | Build date\n" Name Openwrt Openwrt Openwrt Feeds Files Patches
printf " %-31s | %-10.10s | %-7.7s | %-8.8s | %-7.7s | %-7.7s | %-7.7s |\n" "" Revision Variant Selector "" "" ""
printf -- '----------------------------------+------------+---------+----------+---------+---------+---------+------------------------------\n'
# run through all of json
targetIdx=0
while true
do
entry=$(echo "$cleanJson" | jq ".[$targetIdx]")
if [ "$entry" = "null" ]; then
break; # last entry
fi
_config_name=$(echo $entry | jq $OPT '.name')
# ignore default entry
if [ "${_config_name}" = "default" ]; then
targetIdx=$(( targetIdx + 1 ))
continue
fi
# create env variables and parse with one call to jq (it is faster than repeatly call it)
x='"_config_name=\(.name); _openwrt_rev=\(.openwrt_rev); _openwrt_variant=\(.openwrt_variant); _selector_config=\(.["selector-config"]); _selector_feeds=\(.["selector-feeds"]); _selector_files=\(.["selector-files"]); _selector_patches=\(.["selector-patches"]) "'
eval $(echo $entry | jq $OPT "$x")
test -z "${_config_name}" && echo "error: configuration has no name" && break
test "$_openwrt_rev" = "null" && _openwrt_rev="$_def_openwrt_rev"
test "$_openwrt_variant" = "null" && _openwrt_variant="$_def_openwrt_variant"
test "$_selector_config" = "null" && _selector_config="$_def_selector_config"
test "$_selector_files" = "null" && _selector_files="$_def_selector_files"
test "$_selector_feeds" = "null" && _selector_feeds="$_def_selector_feeds"
test "$_selector_patches" = "null" && _selector_patches="$_def_selector_patches"
# get status
buildroot="$WORK_DIR/${_openwrt_rev:0:9}"
test -n "$_openwrt_variant" && buildroot="$buildroot.$_openwrt_variant"
_compile_status_dir="$RUN_DIR/$buildroot/${LOCAL_OUTPUT_DIR}/${COMPILE_STATUS_DIR}"
compile_status_file="${_compile_status_dir}/${_config_name}-${COMPILE_STATUS_FILE_SUFFIX}"
compile_status=""
compile_data=""
if [ -f "${compile_status_file}" ]; then
eval $(cat "${compile_status_file}" | jq $OPT '"compile_data=\"\(.date)\";compile_status=\(.status)"')
fi
case "${compile_status}" in
"0") cstatus="${C_NONE}${BG_GREEN}+${C_NONE}" ;;
"1") cstatus="${C_NONE}${BG_RED}E${C_NONE}" ;;
"2") cstatus="${C_NONE}${C_YELLOW}${C_BLINK}*${C_NONE}" ;;
*)
if [ -n "${compile_status}" ]; then
cstatus="${C_LBLUE}${BG_WHITE}${compile_status}${C_NONE}"
else
cstatus="${C_NONE}${BG_RED}-${C_NONE}"
fi
;;
esac
printf $cstatus" %-31s | %-10.10s | %-7.7s | ${list_color[$_selector_config]}%-8.8s${C_NONE} | ${list_color[$_selector_files]}%-7.7s${C_NONE} | ${list_color[$_selector_feeds]}%-7.7s${C_NONE} | ${list_color[$_selector_patches]}%-7.7s${C_NONE} | %s\n" "${_config_name}" "${_openwrt_rev:0:9}" "$_openwrt_variant" "$_selector_config" "$_selector_feeds" "$_selector_files" "$_selector_patches" "$compile_data"
targetIdx=$(( targetIdx + 1 ))
done
printf -- '----------------------------------+------------+---------+----------+---------+---------+---------+------------------------------\n'
}
listTargetsNames()
{
cleanJson=$(getTargetsJson)
echo "$cleanJson" | jq --raw-output '.[] | select(.name != "default") | .name'
}
# returns number of targets in build.json
numberOfTargets()
{
ARG_regexTarget=$1
[ -z "$ARG_regexTarget" ] && ARG_regexTarget='.*'
cleanJson=$(getTargetsJson)
echo "$cleanJson" | jq --raw-output "[ .[] | select(.name != \"default\")
| select( .name | test (\"${ARG_regexTarget}\") ) ] | length"
}
search_target()
{
target=$1
awk 'BEGIN {IGNORECASE=1;} /^CONFIG_TARGET_.*'$target'/{line=gsub(/^CONFIG_TARGET_.*DEVICE_/,"");print FILENAME"\t\t,"$0}' openwrt-configs/*/*
}
print_devices_for_target()
{
target=$1
cleanJson=$(getTargetsJson)
def_selector=$(echo "$cleanJson" | jq --raw-output '.[] | select(.name == "default") | . "selector-config"')
# get all targets
if [ -n "$target" ]; then
targets=$target
else
targets=$(listTargetsNames)
fi
for _target in ${targets}
do
selector=$(echo "$cleanJson" | jq --raw-output ".[] | select(.name == \"$_target\") | . \"selector-config\"")
test "${selector}" = "null" && selector=${def_selector}
config_filename=$(echo "$cleanJson" | jq --raw-output ".[] | select(.name == \"$_target\") | .config")
echo "target: [$_target] (${selector})"
grep "^CONFIG_TARGET_DEVICE.*=y" "openwrt-configs/${selector}/${config_filename}"
echo ""
done
}
# this function checks if all firmware files are generated for selected devices for a specific target
verify_firmware_present()
{
target=$1
firmware_path=$2
cleanJson=$(getTargetsJson)
# get selector
def_selector=$(echo "$cleanJson" | jq --raw-output '.[] | select(.name == "default") | . "selector-config"')
selector=$(echo "$cleanJson" | jq --raw-output ".[] | select(.name == \"$target\") | . \"selector-config\"")
test "${selector}" = "null" && selector=${def_selector}
# get config filename
config=$(echo "$cleanJson" | jq --raw-output ".[] | select(.name == \"$target\") | .config")
# extract selected devices from openwrt config file and check
# if sysupgrade file is present
status=0
for dev in $(grep "^CONFIG_TARGET_DEVICE.*=y" "${RUN_DIR}/openwrt-configs/${selector}/${config}")
do
# extract firmware device name part
dev="${dev/*_DEVICE_/}"
dev_str="${dev/=*/}"
printf "verify firmware for: ${dev_str}: "
p1="$(ls -1 ${firmware_path}/*${dev_str}*sysupgrade.bin 2>/dev/null)"
p2="$(ls -1 ${firmware_path}/*${dev_str}*.img.gz 2>/dev/null)"
if [ -n "$p1" -o -n "$p2" ]; then
printf -- "${C_GREEN}ok${C_NONE}\n"
else
printf -- "${C_RED}failed${C_NONE}\n"
status=1
fi
done
return ${status};
}
setup_dynamic_firmware_config()
{
FILES="$1"
# modify registration credentials from envronment variable passed in by gitlabs
# FF_REGISTERKEY_PREFIX is set in gitlab UI of freifunk-dresden-firmware: settings->CI/CD->Environment
sed -i "/register_service_url/s#registerkey='#registerkey=${FF_REGISTERKEY_PREFIX//_/:}'#" $FILES/etc/config/credentials
# modify key
[ -z "${FF_MESH_KEY}" ] && FF_MESH_KEY='custom-firmware-key'
sed -i "/wifi_mesh_key/s#ffkey-placeholder#${FF_MESH_KEY}#" $FILES/etc/config/credentials
}
#----------------- process argument ----------------------------------
usage_short()
{
echo "Version: $SCRIPT_VERSION"
echo "usage: $(basename $0) [options] <command | target-pattern > [clean | menuconfig | rerun] [-- < make params ... > ]"
}
usage()
{
# create a simple menu
usage_short
cat <<EOM
options:
-h docker host, if not specified environment variable 'FF_DOCKER_HOST' or 'DOCKER_HOST' is used.
FF_DOCKER_HOST is used in favour to DOCKER_HOST. -h still has highest preference
e.g: tcp://192.168.123.123
-d use docker for compiling (keep workdir)
-D use docker for compiling (clear workdir)
-s opens a shell to running docker
commands:
list - lists all available targets
lt | list-targets - lists only target names for usage in IDE
watch - same as 'list' but updates display
devices [target] - displays all selected routers for a target
search <string> - search specific router (target)
feed-revisions - displays the openwrt default feed revisions.
The revisions then could be set in build.json
target-pattern - target to build
that are defined by build.json. use 'list' for supported targets.
'all' - builds all targets
'failed' - builds only previously failed or not built targets
'ramips* ' - builds all ramips targets only
'ramips.rt305x.generic' - builds exact this target
'*generic' - builds all that end with generic
flags
clean - cleans buildroot/bin and buildroot/build_dir (keeps toolchains)
menuconfig - displays configuration menu
rerun - enables a second compilation with make option 'V=s'
If first make failes a second make is tried with this option
-- make params - all paramerters that follows are passed to make command
Devel-Notes:
To compile a specific feed change to workdir and call something like:
make package/feeds/ddmesh/fastd/compile -j1 V=s
EOM
}
# overwrite DOCKER_HOST variable if FF_DOCKER_HOST is present. this value can still be overwritten by -h option
test -n "${FF_DOCKER_HOST}" && export DOCKER_HOST="${FF_DOCKER_HOST}"
# check if there are options
while getopts "sDdh:" arg
do
case "$arg" in
h)
test -z "${OPTARG}" && echo "docker host"
export DOCKER_HOST="${OPTARG}" # export it here, to not overwrite external possible
;; # variable
d)
USE_DOCKER=true
DOCKER_RM_WORKDIR=false
if [ -z "$(which docker)" ]; then
echo "Error: no docker installed"
exit 1
fi
;;
D)
USE_DOCKER=true
DOCKER_RM_WORKDIR=true
if [ -z "$(which docker)" ]; then
echo "Error: no docker installed"
exit 1
fi
;;
s)
USE_DOCKER=true
RUN_DOCKER_SHELL="1"
if [ -z "$(which docker)" ]; then
echo "Error: no docker installed"
exit 1
fi
;;
\?) exit 1 ;;
esac
done
shift $(( OPTIND - 1 ))
#for a; do echo "arg [$a]"; done
#echo $@
if [ -z "$1" -a "$RUN_DOCKER_SHELL" != "1" ]; then
usage
exit 1
fi
# if docker is used, this script should be called from docker container
if $USE_DOCKER; then
echo "Using Docker at ${DOCKER_HOST:=localhost}"
docker_tar="$(mktemp -u).tgz"
docker_tar=$(basename ${docker_tar}) # remove path
# check connection
docker info 2>/dev/null >/dev/null || {
echo "Error: docker host not reachable"
exit 1
}
# create container if it does not exisits and upload current directory
docker inspect ${DOCKER_CONTAINER_NAME} >/dev/null 2>/dev/null || {
echo -e "${C_CYAN}create container${C_NONE}"
docker create -it --name ${DOCKER_CONTAINER_NAME} --user $(id -u) ${DOCKER_IMAGE}
}
echo -e "${C_CYAN}start container${C_NONE}"
docker start ${DOCKER_CONTAINER_NAME}
if [ "$RUN_DOCKER_SHELL" = "1" ]; then
docker exec -it ${DOCKER_CONTAINER_NAME} bash
else
# create file to upload
echo -e "${C_CYAN}create project archive${C_NONE}"
tar -cz --exclude ${WORK_DIR} --exclude ${DL_DIR} --exclude ${FINAL_OUTPUT_DIR} \
--exclude-backups --exclude-vcs --exclude-vcs-ignores \
-f "/tmp/${docker_tar}" ./
# upload and extract (workdir is not included)
echo -e "${C_CYAN}copy project to container${C_NONE}"
docker cp "/tmp/${docker_tar}" ${DOCKER_CONTAINER_NAME}:/builds/
docker exec -it ${DOCKER_CONTAINER_NAME} sh -c "rm -rf files feeds openwrt-configs; tar -xzf ${docker_tar} && rm ${docker_tar}"
rm /tmp/${docker_tar}
# remove workdir from previous usage of this container (when still available)
${DOCKER_RM_WORKDIR} && {
echo -e "$${C_LCYAN}remove workdir${C_NONE}"
docker exec -it ${DOCKER_CONTAINER_NAME} rm -rf ${WORK_DIR}
}
docker exec -it ${DOCKER_CONTAINER_NAME} git config --global http.sslverify false
echo -e "${C_CYAN}run build${C_NONE}"
# need to pass target as one parameter. $@ does separate target list ("ar71xx.tiny.lowmem ath79.generic lantiq.xrx200")
target="$1"
shift
docker exec -it -e FF_REGISTERKEY_PREFIX=$FF_REGISTERKEY_PREFIX ${DOCKER_CONTAINER_NAME} ./build.sh "$target" $@
# ignore some operations for some arguments. it makes no sence to start those short commands. can be done locally
case "$1" in
list) ;;
lt | list-targets) ;;
search) ;;
clean) ;;
feed-revisions) ;;
*)
if [ "$target" = "all" ]; then
echo -e "${C_CYAN}generate upload${C_NONE}"
docker exec -it ${DOCKER_CONTAINER_NAME} ./gen-upload.sh all
fi
# create tar and copy results back
echo -e"${C_CYAN}copy out results to [${C_YELLOW}${DOCKER_FINAL_TGZ}]${C_NONE}"
docker exec -it ${DOCKER_CONTAINER_NAME} tar czf ${DOCKER_FINAL_TGZ} final_output
docker cp ${DOCKER_CONTAINER_NAME}:/builds/${DOCKER_FINAL_TGZ} "${DOCKER_FINAL_TGZ}"
# extract it in local folder
tar xzf "${DOCKER_FINAL_TGZ}"
rm "${DOCKER_FINAL_TGZ}"
;;
esac
# stop (keep container)
echo -e "${C_CYAN}stop container${C_NONE}"
docker stop -t0 ${DOCKER_CONTAINER_NAME}
fi
exit 0
else
echo -e "${C_CYAN}build locally.${C_NONE}"
fi
#---- process parameters -----------
targetRegex=""
case "$1" in
list)
listTargets;
exit 0
;;
watch)
while sleep 1
do
view=$(listTargets)
clear
echo -e "$view"
date
done
exit 0
;;
lt|list-targets)
listTargetsNames
exit 0
;;
search)
if [ -z "$2" ]; then
echo "Error: missing parameter"
exit 1
fi
search_target $2
exit 0
;;
devices)
# if [ -z "$2" ]; then
# echo "Error: missing target"
# exit 1
# fi
print_devices_for_target $2
#verify_firmware_present $2 $3 && echo ok || echo fehler
exit 0
;;
feed-revisions)
echo "!!! go to openwrt build directory and look at 'feeds.conf.default'"
exit 0
;;
failed)
ARG_CompiledFailedOnly=1
targetRegex=".*"
;;
*)
# targets, menuconfig, rerun and make parameters are passed as argument with its parameters
# check for "menuconfig"
# check for "rerun"
# check for make parameters "--"
while [ -n "$1" ]
do
endLoop=0;
arg="$1"
shift # prepare for next loop
case "${arg}" in
clean)
MAKE_CLEAN=1
endLoop=1
;;
menuconfig)
MENUCONFIG=1
endLoop=1
;;
rerun)
REBUILD_ON_FAILURE=1
endLoop=1
;;
--)
BUILD_PARAMS=$*
endLoop=1
;;
all)
ARG_TARET_ALL=1
targetRegex=".*"
;;
*)
# process and append target "${arg}" to targetRegex
echo "target:${arg}"
# replace wildcard with regex wildcard
targetRegex="${targetRegex} ${arg//\*/.*}"
;;
esac
[ $endLoop = 1 ] && break
done
;;
esac
# check of we have a targetRegex
if [ -z "$targetRegex" ]; then
echo "Error: no target specified"
usage_short
exit 1
fi
#--- process targets ---
# remove invalid characters: '/','$'
chars='[/$]'
targetRegex=${targetRegex//$chars/}
# remove leading and trailing spaces
targetRegex=$(echo "${targetRegex}" | sed 's#[ ]\+$##;s#^[ ]\+##')
# replace any "space" with '$|^'. space can be used as separator lile '|'
# This ensures that full target names are only considered instead of processing
# targets that just start with the given name
# If wildecards are needed, user has to add them
targetRegex=$(echo "${targetRegex}" | sed 's#[ ]\+#$|^#g')
# append '$' to targetRegex, to ensure that 'ar71xx.generic.xyz' is not built
# when 'ar71xx.generic' was specified. Use 'ar71xx.generic.*' if both
# targets should be created
targetRegex="^$targetRegex\$"
echo "### target-regex:[$targetRegex] MENUCONFIG=${MENUCONFIG} CLEAN=${MAKE_CLEAN} REBUILD_ON_FAILURE=${REBUILD_ON_FAILURE} BUILD_PARAMS=[${BUILD_PARAMS}]"
cleanJson=$(getTargetsJson)
# prinzip:
# 1. separate each object from array and pip it to 'select'
# 2. 'select' only let pass objects when true
# 3. .name is piped to both 'test' functions at same time.
# 4. 'not' function seams to have highere priority than 'and'. brackets are not needed, but I
# have added those for more clarifications
# The second 'test' outcome (true or false) are piped to 'not' and negates the output of the
# second 'test'
# 5. first 'test' second 'test' are logical evaluated with 'and', and determines the input of
# 'select' function
echo "$cleanJson" | jq --raw-output ".[]
| select( .name |
( test (\"${targetRegex}\") and ( test(\"^default\") | not ) )
) | .name"
# same as first command; but creates an array from what 'select' filters, which in turn is
# then counted by function 'length'
echo "$cleanJson" | jq --raw-output "[ .[]
| select( .name |
( test (\"${targetRegex}\") and ( test(\"^default\") | not ) )
) ] | length"
# emtpy line needed, else tput stuff clears some. So this emtpy line is cleared
echo " "
if [ "$_TERM" = "1" ]; then
trap clean_up_exit SIGINT SIGTERM
trap show_progress WINCH
fi
setup_buildroot ()
{
buildroot=$1
openwrt_rev=$2
openwrt_dl_dir=$3
openwrt_patches_dir=$4
firmware_files=$5
openwrt_dl_tgz="$openwrt_dl_dir/openwrt-$openwrt_rev.tgz"
git_url="https://git.openwrt.org/openwrt/openwrt.git"
# check if directory exists. I'm not just checking
# the build root itself, because gitlab left a working directory
# only with freifunk files, but without all other openwrt files
if [ ! -d "$buildroot/toolchain" ]
then
echo "directory [$buildroot/toolchain] not present -> re-create '$buildroot'"
# ensure we have a clean workdir, after gitlab runner had removed
# openwrt.org files (e.g. toolchain)
rm -rf "$buildroot"
# re-create buildroot
mkdir -p "$buildroot"
if [ ! -d "$buildroot" ]; then
echo "Error: '$buildroot' could not be created. exit."
clean_up_exit 1
fi
#check if we have already downloaded the openwrt revision
if [ -f $openwrt_dl_tgz ]
then
#extract into buildroot dir
echo "using already downloaded $openwrt_dl_tgz"
cd $buildroot
tar xzf $RUN_DIR/$openwrt_dl_tgz
else
#clone from openwrt
echo "cloning openwrt"
git clone $git_url $buildroot
echo "switch to specific revision"
cd $buildroot
git checkout $openwrt_rev >/dev/null
echo "create openwrt tgz"
tar czf $RUN_DIR/$openwrt_dl_tgz .
fi
cd $RUN_DIR
#apply openwrt patches
if [ -d $openwrt_patches_dir ]; then
for i in $openwrt_patches_dir/*.patch
do
echo -e "${C_CYAN}apply openwrt patch:${C_NONE} $i to buildroot:$buildroot"
# --no-backup-if-mismatch avoids creating backup files for files
# with different names or if not exist (new files)
patch --no-backup-if-mismatch --directory=$buildroot -p1 < $i
done
fi
else
echo -e "${C_CYAN}Buildroot [$buildroot]${C_NONE} already present"
fi
echo -n -e $C_CYAN"create dl directory/links"$C_NONE": "
rm -f $buildroot/dl
ln -s ../../$openwrt_dl_dir $buildroot/dl
echo "done."
# -------- common files -----------
# copy common files first
echo -n -e "${C_CYAN}copy rootfs (common)${C_NONE}: "
rm -rf $buildroot/files
mkdir -p $buildroot/files
# --remove-destination forces copy (first remove (e.g. symlinks))
cp -a --remove-destination $RUN_DIR/files/common/* $buildroot/files/
echo " done."
# -------- specific files -----------
# copy specific files over (may overwrite common)
echo -n -e "${C_CYAN}copy specific files ${C_NONE} [${C_GREEN}${firmware_files}${C_NONE}]: "
if [ -n "${firmware_files}" -a -d "$RUN_DIR/files/${firmware_files}" ]; then
# --remove-destination forces copy (first remove (e.g. symlinks))
cp -a --remove-destination $RUN_DIR/files/${firmware_files}/* $buildroot/files/
echo "done."
else
echo "no specific files."
fi
echo -n -e $C_CYAN"create rootfs/etc/built_info file: "$C_NONE
mkdir -p $buildroot/files/etc
> $buildroot/files/etc/built_info
echo "done."
# more dynamic changes
echo -n -e $C_CYAN"setup dynamic firmware config: "$C_NONE
setup_dynamic_firmware_config "$buildroot/files"
echo "done."
echo "----- generate built_info ----"
git_openwrt_rev=$(cd $buildroot && git log -1 --format=%H)
git_openwrt_branch=$(cd $buildroot && git name-rev --name-only $git_openwrt_rev | sed 's#.*/##')
echo "git_openwrt_rev:$git_openwrt_rev" >> $buildroot/files/etc/built_info
echo "git_openwrt_branch:$git_openwrt_branch" >> $buildroot/files/etc/built_info
# when running from gitlab only specific revision is cloned. there are no branch infos.
# So I check if FF_BUILD_TAG is set and then use this. If not defined I use
# the one I can determine.
git_ddmesh_rev="$(git log -1 --format=%H)"
if [ -n "$FF_BUILD_TAG" ]; then
git_ddmesh_branch="$FF_BUILD_TAG"
else
git_ddmesh_branch="$(git name-rev --tags --name-only $git_ddmesh_rev | sed 's#.*/##')"
if [ "$git_ddmesh_branch" = "undefined" ]; then
git_ddmesh_branch="$(git branch | sed -n 's#^\* \(.*\)#\1#p')"
echo ""
echo -e $C_RED"WARNING: building from UN-TAGGED (git) sources"$C_NONE
echo ""
sleep 5
fi
fi
echo "git_ddmesh_rev:$git_ddmesh_rev" >> $buildroot/files/etc/built_info
echo "git_ddmesh_branch:$git_ddmesh_branch" >> $buildroot/files/etc/built_info
echo "builtdate:$(date)" >> $buildroot/files/etc/built_info
echo "git_openwrt_rev: $git_openwrt_rev"
echo "git_openwrt_branch: $git_openwrt_branch"
echo "git_ddmesh_rev: $git_ddmesh_rev"
echo "git_ddmesh_branch: $git_ddmesh_branch"
echo ""
} # setup_buildroot
# status:
# 0 - success
# 1 - error
# 2 - compiling
write_compile_status()
{
local config="$1"
local status="$2"
local file="$3"
mkdir -p $(dirname $file)
test "$status" = "0" && d="$(date)" | d=""
echo "{\"config\":\"${config}\", \"date\":\"$d\", \"status\":\"${status}\"}" > "${file}"
}
# ---------- create directories: dl, workdir -----------
# only create top-level directories if thoses do not not
# exisist. I do not simply call 'mkdir -p'. This is because
# gitlab runner may store the content somewhere else to
# and creates a symlink to this location.
# This avoids using caching and artifacts copying between
# jobs and stages. cache and workdir are only stored on
# server where the runner is running.
# If owner of the runner doesn't setup a symlink then
# all files are stored within current directory (where build.sh
# is located)
test -L $DL_DIR || mkdir -p $DL_DIR
test -L $WORK_DIR || mkdir -p $WORK_DIR
# ---------- process all targets ------------
# first read default values
OPT="--raw-output" # do not excape values
entry=$(getTargetsJson | jq '.[] | select(.name == "default")')
if [ -n "$entry" ]; then
_def_name=$(echo $entry | jq $OPT '.name')
_def_config=$(echo $entry | jq $OPT '.config')
_def_selector_config=$(echo $entry | jq $OPT '.["selector-config"]')
_def_selector_files=$(echo $entry | jq $OPT '.["selector-files"]')
_def_selector_feeds=$(echo $entry | jq $OPT '.["selector-feeds"]')
_def_selector_patches=$(echo $entry | jq $OPT '.["selector-patches"]')
_def_target_patches=$(echo $entry | jq $OPT '.["target-patches"]')
_def_openwrt_rev=$(echo $entry | jq $OPT '.openwrt_rev')
_def_openwrt_variant=$(echo $entry | jq $OPT '.openwrt_variant')