-
Notifications
You must be signed in to change notification settings - Fork 1
/
onion_buildenv
executable file
·222 lines (167 loc) · 3.63 KB
/
onion_buildenv
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
#!/bin/bash
cd $(dirname $0)
. ./profile
[ -z "$OPENWRT_VERSION" ] && exit 1
[ -z "$TARGET" ] && exit 1
[ -z "$SUBTARGET" ] && exit 1
BUILD_DIR="$PWD"
TARGET_SDK_DIR="$BUILD_DIR/openwrt-sdk"
SDK_ZIP="$BUILD_DIR/${SDK_URL##*/}"
SDK_FILE="${SDK_ZIP//.tar.xz}"
download_file() {
local url=$1
local output=$2
[ -e $output ] && return 0
if ! curl "$url" -o "$output"; then
echo "ERROR downloading from $url"
return 1
fi
return 0
}
verify_archive() {
local file=$1
if ! tar tJf "$file" > /dev/null; then
echo "ERROR: invalid archive $file"
return 1
fi
return 0
}
extract_archive() {
local dir=$1
local file=$2
if ! tar -C "$dir" -xJf "$file"; then
echo "ERROR: decompression failed for $file"
return 1
fi
return 0
}
sdk_exist() {
[ -d "$TARGET_SDK_DIR" ] && return 0
echo "SDK not found. To prepare SDK run"
echo "$0 setup_sdk"
return 1
}
clean_sdk() {
[ -d "$TARGET_SDK_DIR" ] && rm -rf "$TARGET_SDK_DIR"
[ -f "$SDK_ZIP" ] && rm -f "$SDK_ZIP"
}
verify_sdk() {
echo "> Verifying download"
verify_archive "$SDK_ZIP"
}
extract_sdk() {
echo "> Extracting compressed SDK"
extract_archive "$BUILD_DIR" "$SDK_ZIP" || return 1
clean_sdk
mv "$SDK_FILE" "$TARGET_SDK_DIR" || return 1
cp $TARGET_SDK_DIR/feeds.conf.default $TARGET_SDK_DIR/feeds.conf.default.org
return 0
}
download_sdk() {
echo "> Downloading compressed SDK"
download_file "$SDK_URL" "$SDK_ZIP"
}
prepare_sdk() {
export LC_ALL=C
## copy over the repo keys
if [ -n "$KEYS_DIR" ] && [ -d "$KEYS_DIR" ]; then
cp $KEYS_DIR/key-build* $TARGET_SDK_DIR
fi
$TARGET_SDK_DIR/scripts/feeds update -a
$TARGET_SDK_DIR/scripts/feeds install -a
## refresh the package config (selects all packages for compilation)
make -C "$TARGET_SDK_DIR" defconfig
}
update_package_feeds() {
## add packages feeds
echo "> Updating SDK Package Feeds and preparing SDK"
[ -z "$PACKAGE_FEEDS" ] && return 0
echo -e "${PACKAGE_FEEDS}" > $TARGET_SDK_DIR/feeds.conf.default
cat $TARGET_SDK_DIR/feeds.conf.default.org >> $TARGET_SDK_DIR/feeds.conf.default
prepare_sdk || return 1
return 0
}
update_sdk() {
sdk_exist || return 1
update_package_feeds || return 1
}
setup_sdk() {
download_sdk || return 1
verify_sdk || return 1
extract_sdk || return 1
update_package_feeds || return 1
}
compile_package() {
local pkg="$1"
make -C "$TARGET_SDK_DIR" package/$pkg/compile
retval=$?
if [ $retval -ne 0 ]; then
make -C "$TARGET_SDK_DIR" package/$pkg/compile V=99
COMPILATION_ERROR=1
echo "\n>> ERROR: Compile of package ${pkg} returned code $retval"
else
COMPILATION_ERROR=0
fi
}
list_packages() {
sdk_exist || return 1
$TARGET_SDK_DIR/scripts/feeds list $@
}
build_packages() {
local packages="$@"
local pkg
sdk_exist || return 1
packages="${packages:-${SDK_PACKAGES}}"
for pkg in $packages; do
compile_package "$pkg"
done
if [ $COMPILATION_ERROR -ne 0 ]; then
echo "\n>> PACKAGE COMPILATION ERROR!\n"
return 1
else
echo "> Compile success"
fi
}
index_packages() {
echo "> Creating Package Index"
sdk_exist || return 1
make -C "$TARGET_SDK_DIR" package/index
}
build_all_packages() {
echo "> Building all packages"
sdk_exist || return 1
build_packages || return 1
index_packages || return 1
}
if [ -z "$TARGET_SDK_DIR" ]; then
echo "TARGET_SDK_DIR can not be empty string"
exit 1
fi
commands="
setup_sdk
update_sdk
clean_sdk
list_packages
build_packages
build_all_packages
index_packages
"
usage_help() {
local cmd
echo "$0: "
for cmd in $commands; do
echo -e "\t$cmd"
done
}
if [ $# -lt 1 ]; then
usage_help
exit 1
fi
if [ "$(type -t $1)" != "function" ]; then
echo "$1: function not found"
usage_help
exit 1
fi
cmd=$1
shift
$cmd $@