-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.sh
executable file
·402 lines (279 loc) · 9.8 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
#!/bin/bash
# Copyright (c) 2018 R. Diez - Licensed under the GNU AGPLv3
set -o errexit
set -o nounset
set -o pipefail
abort ()
{
echo >&2 && echo "Error in script \"$0\": $*" >&2
exit 1
}
replace_string_in_files ()
{
local DIR="$1"
local STRING_TO_REPLACE="$2"
local REPLACEMENT_STRING="$3"
find "$DIR" -type f -print0 | xargs -0 sed -i "s;$STRING_TO_REPLACE;$REPLACEMENT_STRING;g"
}
read_uptime_as_integer ()
{
local PROC_UPTIME_CONTENTS
PROC_UPTIME_CONTENTS="$(</proc/uptime)"
local PROC_UPTIME_COMPONENTS
IFS=$' \t' read -r -a PROC_UPTIME_COMPONENTS <<< "$PROC_UPTIME_CONTENTS"
local UPTIME_AS_FLOATING_POINT=${PROC_UPTIME_COMPONENTS[0]}
# The /proc/uptime format is not exactly documented, so I am not sure whether
# there will always be a decimal part. Therefore, capture the integer part
# of a value like "123" or "123.45".
# I hope /proc/uptime never yields a value like ".12" or "12.", because
# the following code does not cope with those.
local REGEXP="^([0-9]+)(\\.[0-9]+)?\$"
if ! [[ $UPTIME_AS_FLOATING_POINT =~ $REGEXP ]]; then
abort "Error parsing this uptime value: $UPTIME_AS_FLOATING_POINT"
fi
UPTIME=${BASH_REMATCH[1]}
}
get_human_friendly_elapsed_time ()
{
local -i SECONDS="$1"
if (( SECONDS <= 59 )); then
ELAPSED_TIME_STR="$SECONDS seconds"
return
fi
local -i V="$SECONDS"
ELAPSED_TIME_STR="$(( V % 60 )) seconds"
V="$(( V / 60 ))"
ELAPSED_TIME_STR="$(( V % 60 )) minutes, $ELAPSED_TIME_STR"
V="$(( V / 60 ))"
if (( V > 0 )); then
ELAPSED_TIME_STR="$V hours, $ELAPSED_TIME_STR"
fi
printf -v ELAPSED_TIME_STR "%s (%'d seconds)" "$ELAPSED_TIME_STR" "$SECONDS"
}
get_site_log_filename ()
{
local TEMPLATE_NAME="$1"
local SITE_CODE="$2"
LOG_FILENAME="$SANDBOX_DIR/assembled/$TEMPLATE_NAME/$SITE_CODE/build.log"
}
SBRANCH="$(date +%Y%m%d%H%M)"
# ffdus-hack: firmware-id ohne minute, dafür mit branch-namen
if [ "$(head -1 $1|cut -d" " -f3)" == "dus" ] ; then
SBRANCH="$(date +%Y%m%d%H)-$(head -1 $1|cut -c1-3)"
fi
generate_site_config ()
{
local RELBRANCH="$1"
local TEMPLATE_NAME="$2"
local SITE_CODE="$3"
echo "Generating site $SITE_CODE..."
local DIR="assembled/$TEMPLATE_NAME/$SITE_CODE"
mkdir -p "assembled/$TEMPLATE_NAME"
cp -r "templates/$TEMPLATE_NAME" "$DIR"
replace_string_in_files "$DIR" SITECODE "$SITE_CODE"
replace_string_in_files "$DIR" RELBRANCH "$RELBRANCH"
replace_string_in_files "$DIR" SBRANCH "$SBRANCH"
# Create the log file, or truncate it if it already exists.
get_site_log_filename "$TEMPLATE_NAME" "$SITE_CODE"
echo -n "" >"$LOG_FILENAME"
}
generate_all_site_configs ()
{
echo "Generating sites for sbranch $SBRANCH ..."
rm -rf assembled
local -i index
for (( index=0; index < ${#ALL_SITE_RELBRANCHES[@]}; index += 1 )); do
generate_site_config "${ALL_SITE_RELBRANCHES[$index]}" \
"${ALL_SITE_TEMPLATE_NAMES[$index]}" \
"${ALL_SITE_CODES[$index]}"
done
echo "Finished generating sites."
}
append_quoted_arg ()
{
local APPEND_TO_VAR_NAME="$1"
local APPEND_ARG_NAME="$2"
local APPEND_PATH="$3"
printf -v "$APPEND_TO_VAR_NAME" "%s $APPEND_ARG_NAME=%q" "${!APPEND_TO_VAR_NAME}" "$APPEND_PATH"
}
build_images_for_site ()
{
local RELBRANCH="$1"
local TEMPLATE_NAME="$2"
local SITE_CODE="$3"
local -i target_index
local TARGET
local ARGS=""
append_quoted_arg ARGS GLUON_SITEDIR "$SANDBOX_DIR/assembled/$TEMPLATE_NAME/$SITE_CODE"
append_quoted_arg ARGS GLUON_IMAGEDIR "$SANDBOX_DIR/images/$TEMPLATE_NAME/$SITE_CODE"
append_quoted_arg ARGS GLUON_MODULEDIR "$SANDBOX_DIR/modules"
append_quoted_arg ARGS GLUON_SITE_VERSION "201905"
# Setting GLUON_BRANCH enables the firmware autoupdater.
append_quoted_arg ARGS GLUON_BRANCH "$RELBRANCH"
local MAKE_CMD
local PREPARED_FILENAME="$SANDBOX_DIR/.prepared"
local PREPARED_CONTENTS
if [ -f "PREPARED_FILENAME" ]; then
PREPARED_CONTENTS=$(<"$PREPARED_FILENAME")
rm -- "$PREPARED_FILENAME"
else
PREPARED_CONTENTS=""
fi
if [[ "$PREPARED_CONTENTS" != "$TEMPLATE_NAME" ]]; then
# git reset --hard $2
echo "Gluon make update..."
printf -v MAKE_CMD "make update %s" "$ARGS"
echo "$MAKE_CMD"
eval "$MAKE_CMD"
for (( target_index=0; target_index < ${#TARGETS[@]}; target_index += 1 )); do
TARGET="${TARGETS[target_index]}"
if false; then
echo "Cleaning the firmware for site code: $SITE_CODE, target: $TARGET ..."
printf -v MAKE_CMD "make clean GLUON_TARGET=%q %s" "$TARGET" "$ARGS"
echo "$MAKE_CMD"
eval "$MAKE_CMD"
fi
done
# echo "Site prepare.sh ..."
# "$SANDBOX_DIR/assembled/$TEMPLATE_NAME/$SITE_CODE/prepare.sh"
fi
local MAKE_J_VAL
MAKE_J_VAL="$(( $(getconf _NPROCESSORS_ONLN) + 1 ))"
for (( target_index=0; target_index < ${#TARGETS[@]}; target_index += 1 )); do
TARGET="${TARGETS[target_index]}"
echo "Building the firmware for site code: $SITE_CODE, target: $TARGET ..."
printf -v MAKE_CMD "make GLUON_TARGET=%q" "$TARGET"
# For the Gluon build system, V=s means generate a full build log (show build commands, compiler warnings etc.).
MAKE_CMD+=" V=s"
# For the Gluon build system, BROKEN=1 means "use the experimental/unstable branch".
MAKE_CMD+=" BROKEN=1"
MAKE_CMD+=" $ARGS"
MAKE_CMD+=" -j $MAKE_J_VAL --output-sync=recurse"
echo "$MAKE_CMD"
eval "$MAKE_CMD"
done
echo "$TEMPLATE_NAME" > "$PREPARED_FILENAME"
echo "Making manifest..."
printf -v MAKE_CMD "make manifest %s" "$ARGS"
echo "$MAKE_CMD"
eval "$MAKE_CMD"
local SITE_IMAGE_DIR="$SANDBOX_DIR/images/$TEMPLATE_NAME/$SITE_CODE/site"
echo "Copying build result to \"$SITE_IMAGE_DIR\" ..."
# This directory may already exist from a previous run.
mkdir --parents -- "$SITE_IMAGE_DIR"
rsync --archive "$SANDBOX_DIR/assembled/$TEMPLATE_NAME/$SITE_CODE/" --exclude '*.old' --exclude '*.backup' --exclude '*~' --exclude '*.nonworking' "$SITE_IMAGE_DIR"
cp -- "$SANDBOX_DIR/build.sh" "$SITE_IMAGE_DIR/"
}
build_all_images ()
{
local -a TARGETS=("$@")
if (( ${#TARGETS[@]} == 0 )); then
TARGETS+=( ar71xx-generic )
TARGETS+=( ar71xx-tiny )
TARGETS+=( ar71xx-nand )
TARGETS+=( brcm2708-bcm2708 )
TARGETS+=( brcm2708-bcm2709 )
TARGETS+=( mpc85xx-generic )
TARGETS+=( ramips-mt7621 )
TARGETS+=( sunxi-cortexa7 )
TARGETS+=( x86-generic )
TARGETS+=( x86-geode )
TARGETS+=( x86-64 )
TARGETS+=( ipq40xx )
TARGETS+=( ramips-mt7620 )
TARGETS+=( ramips-mt76x8 )
#TARGETS+=( ramips-rt305x ) # excluded, bugs build fails
TARGETS+=( ar71xx-mikrotik )
TARGETS+=( brcm2708-bcm2710 )
TARGETS+=( ipq806x )
TARGETS+=( mvebu-cortexa9 )
fi
pushd "$GLUON_DIR" >/dev/null
echo "Git fetching..."
git fetch --all
local -i index
for (( index=0; index < ${#ALL_SITE_RELBRANCHES[@]}; index += 1 )); do
get_site_log_filename "${ALL_SITE_TEMPLATE_NAMES[$index]}" "${ALL_SITE_CODES[$index]}"
echo "Building the firmware for site code ${ALL_SITE_CODES[$index]} ..."
echo "The site build log file is: $LOG_FILENAME"
local UPTIME
read_uptime_as_integer
local SITE_UPTIME_BEGIN="$UPTIME"
{
build_images_for_site "${ALL_SITE_RELBRANCHES[$index]}" \
"${ALL_SITE_TEMPLATE_NAMES[$index]}" \
"${ALL_SITE_CODES[$index]}"
} 2>&1 | tee --append -- "$LOG_FILENAME"
# The whole build takes a long time. By recording the build time for each site,
# it is easier to measure any impact on the the build performance after
# making changes to the build system.
read_uptime_as_integer
local ELAPSED_TIME_STR
get_human_friendly_elapsed_time "$(( UPTIME - SITE_UPTIME_BEGIN ))"
echo "Finished building the firmware for site code ${ALL_SITE_CODES[$index]}. Elapsed time: $ELAPSED_TIME_STR."
# We could compress the log file here, but it is not worth it.
# It saves little space compared to the rest of the generated files,
# and it makes it more inconvenient to open the log file.
if false; then
gzip --best -- "$LOG_FILENAME"
fi
done
popd >/dev/null
# I do not think that we build any modules yet.
if [ -d "modules" ]; then
ARE_THERE_MODULES=true
else
ARE_THERE_MODULES=false
fi
mv "images" "images-$DATE_SUFFIX"
if $ARE_THERE_MODULES; then
mv "modules" "modules-$DATE_SUFFIX"
fi
echo "Finished building images:"
echo "- Images dir: images-$DATE_SUFFIX"
if $ARE_THERE_MODULES; then
echo "- Modules dir: modules-$DATE_SUFFIX"
fi
}
declare -a ALL_SITE_RELBRANCHES=()
declare -a ALL_SITE_GIT_BRANCHES=() # TODO: Not used at the moment.
declare -a ALL_SITE_TEMPLATE_NAMES=()
declare -a ALL_SITE_CODES=()
parse_sites_file ()
{
local FILENAME="$1"
local LINE
local COMPONENTS
while read -r LINE; do
# We could allow comments in the file. Here we would remove them.
if [ -z "$LINE" ]; then
continue
fi
IFS=$' \t' read -r -a COMPONENTS <<< "$LINE"
if (( ${#COMPONENTS[@]} != 4 )); then
abort "Syntax error parsing this line: $LINE"
fi
ALL_SITE_RELBRANCHES+=( "${COMPONENTS[0]}" )
ALL_SITE_GIT_BRANCHES+=( "${COMPONENTS[1]}" )
ALL_SITE_TEMPLATE_NAMES+=( "${COMPONENTS[2]}" )
ALL_SITE_CODES+=( "${COMPONENTS[3]}" )
done < "$FILENAME"
if (( ${#ALL_SITE_RELBRANCHES[@]} == 0 )); then
abort "Could not read any sites from the sites file."
fi
}
# ----------- Entry point -----------
if (( $# == 0 )); then
echo "Usage: build.sh <sites file> [target1] [target2] [...]"
exit 0
fi
if ! [ -f "$1" ]; then
abort "File \"$1\" does not exist."
fi
parse_sites_file "$1"
SANDBOX_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
generate_all_site_configs
GLUON_DIR="$SANDBOX_DIR/gluon"
DATE_SUFFIX="$(date +%s)"
shift
build_all_images "$@"