forked from ericpaulbishop/gargoyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·541 lines (434 loc) · 16.5 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
#!/bin/bash
set_constant_variables()
{
#working directories
scriptpath="$(readlink -f "$0")"
top_dir="${scriptpath%/${0##*/}}"
targets_dir="$top_dir/targets"
patches_dir="$top_dir/patches-generic"
compress_js_dir="$top_dir/compressed_javascript"
#script for building netfilter patches
netfilter_patch_script="$top_dir/netfilter-match-modules/integrate_netfilter_modules.sh"
#cores / build threads
num_cores=$(grep -c "^processor" /proc/cpuinfo 2>/dev/null)
if [ -z "$num_cores" ] ; then num_cores=1 ; fi
num_build_threads=$(($num_cores + 2)) # more threads than cores, since each thread will sometimes block for i/o
}
set_version_variables()
{
#openwrt branch
branch_name="Attitude Adjustment"
branch_id="attitude_adjustment"
branch_is_trunk="0"
branch_packages_path="branches/packages_12.09"
# set svn revision number to use
# you can set this to an alternate revision
# or empty to checkout latest
rnum=35786
#set date here, so it's guaranteed the same for all images
#even though build can take several hours
build_date=$(date +"%B %d, %Y")
gargoyle_git_revision=$(git log -1 --pretty=format:%h)
# Full display version in gargoyle web interface
if [ -z "$full_gargoyle_version" ] ; then
full_gargoyle_version="$1"
fi
if [ -z "$full_gargoyle_version" ] ; then
full_gargoyle_version="Unknown"
fi
# Used in gargoyle banner
short_gargoyle_version=$(echo "$full_gargoyle_version" | awk '{ print $1 ; }' | sed 's/[^0-9^A-Z^a-z^\.^\-^_].*$//g' )
# Used for file naming
lower_short_gargoyle_version=$(echo "$short_gargoyle_version" | tr 'A-Z' 'a-z' )
# Used for package versioning/numbering, needs to be a numeric, eg 1.2.3
numeric_gargoyle_version=$(echo "$short_gargoyle_version" | sed 's/[Xx]/0/' )
not_numeric=$(echo "$numeric_gargoyle_version" | sed 's/[\.0123456789]//g')
if [ -n "$not_numeric" ] ; then
numeric_gargoyle_version="0.9.9"
fi
#echo "full = \"$full_gargoyle_version\""
#echo "short = \"$short_gargoyle_version\""
#echo "short lower = \"$lower_short_gargoyle_version\""
#echo "numeric = \"$numeric_gargoyle_version\""
}
get_target_from_config()
{
config_file_path="$1"
cat .config | grep "^CONFIG_TARGET_BOARD=" | sed 's/\"$//g' | sed 's/^.*\"//g'
}
create_gargoyle_banner()
{
echo "BUILDING BANNER"
local target="$1"
local profile="$2"
local date="$3"
local gargoyle_version="$4"
local gargoyle_commit="$5"
local openwrt_branch="$6"
local openwrt_revision="$7"
local banner_file_path="$8"
local revision_save_dir="$9"
local openwrt_branch_str="OpenWrt $openwrt_branch branch"
if [ "$openwrt_branch" = "trunk" ] ; then
openwrt_branch_str="OpenWrt trunk"
fi
local top_line=$(printf "| %-26s| %-35s|" "Gargoyle version $gargoyle_version" "$openwrt_branch_str")
local middle_line=$(printf "| %-26s| %-35s|" "Gargoyle revision $gargoyle_commit" "OpenWrt revision r$openwrt_revision")
local bottom_line=$(printf "| %-26s| %-35s|" "Built $date" "Target $target/$profile")
cat << 'EOF' >"$banner_file_path"
------------------------------------------------------------------
| _____ _ |
| | __ \ | | |
| | | \/ __ _ _ __ __ _ ___ _ _| | ___ |
| | | __ / _` | '__/ _` |/ _ \| | | | |/ _ \ |
| | |_\ \ (_| | | | (_| | (_) | |_| | | __/ |
| \____/\__,_|_| \__, |\___/ \__, |_|\___| |
| __/ | __/ | |
| |___/ |___/ |
| |
|----------------------------------------------------------------|
EOF
echo "$top_line" >> "$banner_file_path"
echo "$middle_line" >> "$banner_file_path"
echo "$bottom_line" >> "$banner_file_path"
echo '------------------------------------------------------------------' >> "$banner_file_path"
#save openwrt variables for rebuild
echo "$openwrt_revision" > "$revision_save_dir/OPENWRT_REVISION"
echo "$openwrt_branch" > "$revision_save_dir/OPENWRT_BRANCH"
}
######################################################################################################
## Begin Main Body of Build Script ##
######################################################################################################
if [ -z "${BASH_VERSION}" ] || [ "${BASH_VERSION:0:1}" -lt '4' ]; then
echo 'Build script was designed to work with bash in version 4 (at least). Exiting...'
exit 1
fi
#initialize constants
set_constant_variables
cd "$top_dir"
#parse parameters
targets="$1"
full_gargoyle_version="$2"
verbosity="$3"
custom_template="$4"
js_compress="$5"
specified_profile="$6"
if [ "$targets" = "ALL" ] || [ -z "$targets" ] ; then
targets=$(ls $targets_dir | sed 's/custom//g' 2>/dev/null)
fi
if [ -z "$js_compress" ] ; then
js_compress="true"
fi
set_version_variables "$full_gargoyle_version"
#compress javascript
if [ "$js_compress" = "true" ] || [ "$js_compress" = "TRUE" ] || [ "$js_compress" = "1" ] ; then
cd "$top_dir"
uglify_test=$( echo 'var abc = 1;' | uglifyjs 2>/dev/null )
if [ "$uglify_test" != 'var abc=1' ] && [ "$uglify_test" != 'var abc=1;' ] ; then
node_bin="$top_dir/node/node"
uglifyjs_bin="$top_dir/UglifyJS/bin/uglifyjs"
if [ ! -e "$node_bin" ] && [ ! -e "$uglifyjs_bin" ] ; then
echo ""
echo "**************************************************************************"
echo "** uglifyjs is not installed globally, attempting to build it **"
echo "**************************************************************************"
echo ""
#node
git clone git://github.com/joyent/node.git
cd node
git checkout v0.7.12
./configure
make
cd "$top_dir"
#uglifyjs
git clone git://github.com/mishoo/UglifyJS.git
cd UglifyJS/bin
git checkout v1.3.1
cd "$top_dir"
fi
uglify_test=$( echo 'var abc = 1;' | "$node_bin" "$uglifyjs_bin" 2>/dev/null )
if [ "$uglify_test" = 'var abc=1' ] || [ "$uglify_test" = 'var abc=1;' ] ; then
js_compress="true"
rm -rf "$compress_js_dir"
cp -r "package/gargoyle/files/www/js" "$compress_js_dir"
cd "$compress_js_dir"
jsfiles=*.js
for jsf in $jsfiles ; do
"$node_bin" "$uglifyjs_bin" "$jsf" > "$jsf.cmp"
mv "$jsf.cmp" "$jsf"
done
else
js_compress="false"
echo ""
echo "**************************************************************************"
echo "** WARNING: Cannot compress javascript -- uglifyjs could not be built **"
echo "**************************************************************************"
echo ""
fi
else
js_compress="true"
rm -rf "$compress_js_dir"
cp -r "package/gargoyle/files/www/js" "$compress_js_dir"
cd "$compress_js_dir"
jsfiles=*.js
for jsf in $jsfiles ; do
uglifyjs "$jsf" > "$jsf.cmp"
mv "$jsf.cmp" "$jsf"
done
fi
cd "$top_dir"
fi
#create common download directory if it doesn't exist
if [ ! -d "$top_dir/downloaded" ] ; then
mkdir "$top_dir/downloaded"
fi
openwrt_src_dir="$top_dir/downloaded/$branch_id"
openwrt_package_dir="$top_dir/downloaded/$branch_id-packages"
if [ -n "$rnum" ] ; then
openwrt_src_dir="$top_dir/downloaded/$branch_id-$rnum"
openwrt_package_dir="$top_dir/downloaded/$branch_id-packages-$rnum"
else
rm -rf "$openwrt_src_dir"
rm -rf "$openwrt_package_dir"
fi
#download openwrt source if we haven't already
if [ ! -d "$openwrt_src_dir" ] ; then
revision=""
if [ -n "$rnum" ] ; then
revision=" -r $rnum "
fi
echo "fetching openwrt source"
rm -rf "$branch_name" "$branch_id"
if [ "$branch_is_trunk" = "1" ] ; then
svn checkout $revision svn://svn.openwrt.org/openwrt/trunk "$branch_id"
else
svn checkout $revision svn://svn.openwrt.org/openwrt/branches/$branch_id/
fi
if [ ! -d "$branch_id" ] ; then
echo "ERROR: could not download source, exiting"
exit
fi
cd "$branch_id"
find . -name ".svn" | xargs -r rm -rf
cd "$top_dir"
mv "$branch_id" "$openwrt_src_dir"
fi
rm -rf "$openwrt_src_dir/dl"
ln -s "$top_dir/downloaded" "$openwrt_src_dir/dl"
for target in $targets ; do
#remove old build files
rm -rf "$target-src"
if [ -z "$specified_profile" ] ; then
rm -rf "$top_dir/built/$target"
rm -rf "$top_dir/images/$target"
else
profile_images=$(cat "$targets_dir/$target/profiles/$specified_profile/profile_images" 2>/dev/null)
mkdir -p "$top_dir/images/$target/"
for pi in $profile_images ; do
rm -rf "$top_dir/images/$target/"*"$pi"*
done
fi
#copy source to new, target build directory
cp -r "$openwrt_src_dir" "$target-src"
#copy gargoyle-specific packages to build directory
package_dir="package"
gargoyle_packages=$(ls "$package_dir" )
for gp in $gargoyle_packages ; do
if [ -d "$target-src/package/$gp" ] ; then
rm -rf "$target-src/package/$gp"
fi
cp -r "$package_dir/$gp" "$target-src/package"
done
#copy compressed javascript to build directory
if [ "$js_compress" = "true" ] ; then
rm -rf "$target-src/package/gargoyle/files/www/js"
cp -r "$compress_js_dir" "$target-src/package/gargoyle/files/www/js"
fi
# specify default build profile
default_profile="default"
if [ -n "$specified_profile" ] ; then
default_profile="$specified_profile"
fi
profile_target_dir="$target"
if [ "$target" = "custom" ] && [ -n "$custom_template" ] ; then
profile_target_dir="$custom_template"
fi
if [ ! -e "$targets_dir/$profile_target_dir/profiles/$default_profile/config" ] ; then
profile_dir=""
profile_dirs="$targets_dir/$profile_target_dir/profiles"/*
for pd in $profile_dirs ; do
if [ -z "$profile_dir" ] && [ -e "$pd/config" ] ; then
profile_dir="$pd"
default_profile=$(echo "$profile_dir" | sed 's/^.*\///g' | sed 's/^.*\\//g')
fi
done
fi
profile_name="$default_profile"
if [ "$target" = "custom" ] ; then
profile_name="custom"
fi
echo ""
echo ""
echo "**************************************************************************"
echo " Gargoyle is now building target: $target / $profile_name"
echo "**************************************************************************"
echo ""
echo ""
#copy this target configuration to build directory
cp "$targets_dir/$target/profiles/$default_profile/config" "$top_dir/${target}-src/.config"
#if target is custom, checkout optional packages and copy all that don't
#share names with gargoyle-specific packages to build directory
if [ "$target" = "custom" ] ; then
if [ ! -d "$openwrt_package_dir" ] ; then
if [ "$branch_is_trunk" = "1" ] ; then
svn checkout $revision svn://svn.openwrt.org/openwrt/packages "$openwrt_package_dir"
else
svn checkout $revision "svn://svn.openwrt.org/openwrt/$branch_packages_path" "$openwrt_package_dir"
fi
cd "$openwrt_package_dir"
find . -name ".svn" | xargs rm -rf
for gp in $gargoyle_packages ; do
find . -name "$gp" | xargs rm -rf
done
cd "$top_dir"
fi
other_packages=$(ls "$openwrt_package_dir" )
for other in $other_packages ; do
if [ ! -d "$target-src/package/$other" ] ; then
cp -r "$openwrt_package_dir/$other" $target-src/package
fi
done
fi
#enter build directory and make sure we get rid of all those pesky .svn files,
#and any crap left over from editing
cd "$top_dir/$target-src"
find . -name ".svn" | xargs rm -rf
find . -name "*~" | xargs rm -rf
find . -name ".*sw*" | xargs rm -rf
#Set gargoyle official version parameter in gargoyle package
echo "OFFICIAL_VERSION:=$full_gargoyle_version" > .ver
cat .ver "$package_dir/gargoyle/Makefile" >.vermake
rm .ver
mv .vermake "$package_dir/gargoyle/Makefile"
#build, if verbosity is 0 dump most output to /dev/null, otherwise dump everything
if [ "$verbosity" = "0" ] ; then
scripts/patch-kernel.sh . "$patches_dir/" >/dev/null 2>&1
scripts/patch-kernel.sh . "$targets_dir/$target/patches/" >/dev/null 2>&1
if [ "$target" = "custom" ] ; then
sh $netfilter_patch_script . "$top_dir/netfilter-match-modules" 1 0 >/dev/null 2>&1
make menuconfig
sh $netfilter_patch_script . "$top_dir/netfilter-match-modules" 0 1 >/dev/null 2>&1
else
sh $netfilter_patch_script . "$top_dir/netfilter-match-modules" 1 1 >/dev/null 2>&1
fi
openwrt_target=$(get_target_from_config "./.config")
create_gargoyle_banner "$openwrt_target" "$profile_name" "$build_date" "$short_gargoyle_version" "$gargoyle_git_revision" "$branch_name" "$rnum" "package/base-files/files/etc/banner" "."
make -j $num_build_threads GARGOYLE_VERSION="$numeric_gargoyle_version"
else
scripts/patch-kernel.sh . "$patches_dir/"
scripts/patch-kernel.sh . "$targets_dir/$target/patches/"
if [ "$target" = "custom" ] ; then
sh $netfilter_patch_script . "$top_dir/netfilter-match-modules" 1 0
make menuconfig
sh $netfilter_patch_script . "$top_dir/netfilter-match-modules" 0 1
else
sh $netfilter_patch_script . "$top_dir/netfilter-match-modules" 1 1
fi
openwrt_target=$(get_target_from_config "./.config")
create_gargoyle_banner "$openwrt_target" "$profile_name" "$build_date" "$short_gargoyle_version" "$gargoyle_git_revision" "$branch_name" "$rnum" "package/base-files/files/etc/banner" "."
make -j $num_build_threads V=99 GARGOYLE_VERSION="$numeric_gargoyle_version"
fi
#copy packages to built/target directory
mkdir -p "$top_dir/built/$target"
package_files=$(find bin -name "*.ipk")
index_files=$(find bin -name "Packa*")
if [ -n "$package_files" ] && [ -n "$index_files" ] ; then
for p in $package_files ; do
cp "$p" "$top_dir/built/$target"
done
for i in $index_files ; do
cp "$i" "$top_dir/built/$target"
done
fi
#copy images to images/target directory
mkdir -p "$top_dir/images/$target"
arch=$(ls bin)
image_files=$(ls bin/$arch/ 2>/dev/null)
if [ ! -e "$targets_dir/$target/profiles/$default_profile/profile_images" ] ; then
for i in $image_files ; do
if [ ! -d "bin/$arch/$i" ] ; then
newname=$(echo "$i" | sed "s/openwrt/gargoyle_$lower_short_gargoyle_version/g")
cp "bin/$arch/$i" "$top_dir/images/$target/$newname"
fi
done
else
profile_images=$(cat "$targets_dir/$target/profiles/$default_profile/profile_images" 2>/dev/null)
for pi in $profile_images ; do
candidates=$(ls bin/$arch/*$pi* 2>/dev/null | sed 's/^.*\///g')
for c in $candidates ; do
if [ ! -d "bin/$arch/$c" ] ; then
newname=$(echo "$c" | sed "s/openwrt/gargoyle_$lower_short_gargoyle_version/g")
cp "bin/$arch/$c" "$top_dir/images/$target/$newname"
fi
done
done
fi
#if we didn't build anything, die horribly
if [ -z "$image_files" ] ; then
exit
fi
other_profiles=""
if [ "$target" != "custom" ] && [ -z "$specified_profile" ] ; then
other_profiles=$(ls "$targets_dir/$target/profiles" | grep -v "^$default_profile$" )
fi
for p in $other_profiles ; do
profile_name="$p"
#copy profile config and rebuild
cp "$targets_dir/$target/profiles/$p/config" .config
openwrt_target=$(get_target_from_config "./.config")
create_gargoyle_banner "$openwrt_target" "$profile_name" "$build_date" "$short_gargoyle_version" "$gargoyle_git_revision" "$branch_name" "$rnum" "package/base-files/files/etc/banner" "."
echo ""
echo ""
echo "**************************************************************************"
echo " Gargoyle is now building target: $target / $p"
echo "**************************************************************************"
echo ""
echo ""
if [ "$verbosity" = "0" ] ; then
make -j $num_build_threads GARGOYLE_VERSION="$numeric_gargoyle_version"
else
make -j $num_build_threads V=99 GARGOYLE_VERSION="$numeric_gargoyle_version"
fi
#if we didn't build anything, die horribly
image_files=$(ls "bin/$arch/" 2>/dev/null)
if [ -z "$image_files" ] ; then
exit
fi
#copy packages to build/target directory
mkdir -p "$top_dir/built/$target"
arch=$(ls bin)
package_files=$(find bin -name "*.ipk")
index_files=$(find bin -name "Packa*")
if [ -n "$package_files" ] && [ -n "$index_files" ] ; then
for p in $package_files ; do
cp "$p" "$top_dir/built/$target"
done
for i in $index_files ; do
cp "$i" "$top_dir/built/$target"
done
fi
#copy relevant images for which this profile applies
profile_images=$(cat "$targets_dir/$target/profiles/$p/profile_images" 2>/dev/null)
for pi in $profile_images ; do
candidates=$(ls "bin/$arch/"*"$pi"* 2>/dev/null | sed 's/^.*\///g')
for c in $candidates ; do
if [ ! -d "bin/$arch/$c" ] ; then
newname=$(echo "$c" | sed "s/openwrt/gargoyle_$lower_short_gargoyle_version/g")
cp "bin/$arch/$c" "$top_dir/images/$target/$newname"
fi
done
done
done
#cd back to parent directory for next target (if there is one)
cd "$top_dir"
done