-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathbuild_lib
executable file
·356 lines (289 loc) · 9.37 KB
/
build_lib
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
#!/bin/bash
# Library of functions to help build sudowrt firmware.
# Used by build_pre and build scripts.
#
set -e
set -x
BUILD_DIR=built_firmware
workdir=$(pwd)
OPENWRT_CHECKOUT_DIR="${workdir}/${BUILD_DIR}/openwrt"
params="$@"
SYSTEM_CORES=$(cat /proc/cpuinfo | grep bogomips | wc -l)
validate_architecture() {
arch_requested=$1
# Check if architecture is valid
all_architectures=$(cat openwrt_config/architectures)
for arch in $all_architectures; do
if [[ "$arch_requested" == "$arch" ]]; then
found=1
break
fi
if [ ! -d "${workdir}/${BUILD_DIR}/openwrt/target/linux/${arch}" ]; then
echo "ERROR: OpenWRT itself does not support the architecture '${arch}'"
exit 1
fi
if [ ! -f "${workdir}/openwrt_config/arch_configs/${arch}" ]; then
echo "ERROR: '${arch}' missing from openwrt_config/arch_configs/"
exit 1
fi
done
if [[ $found == 0 ]]; then
echo "ERROR: Invalid architecture '${arch_requested}' specified."
echo " Architecture must be in openwrt_config/architectures"
exit 1
fi
}
validate() {
architecture=$1
target=$2
if [ -z "${architecture}" ]; then
echo "Usage: ${0} <architecture>"
echo " "
echo " "
echo "Example:"
echo " "
echo " ${0} ar71xx"
echo "or"
echo " ${0} atheros"
echo " "
exit 1
fi
echo "Checking working directory"
if [[ ! -d openwrt_config ]]; then
echo "Invalid working directory!"
exit 1
fi
}
validate $params
# Create list of packages to build
all_packages="$(cat openwrt_config/packages)"
openwrt_feeds_configure() {
# Inject feeds and configurations
echo "Configuring feeds..."
feedlines=()
feed_file="${workdir}/openwrt_config/feeds"
while read -r line
do
feedlines=("${feedlines[@]}" "${line}")
done < "$feed_file"
feed_titles=()
feed_sources=()
for feedline in "${feedlines[@]}"; do
IFS=: read -a ARRAY <<< "${feedline}"
feed_title=${ARRAY[*]:0:1}
feed_titles=("${feed_titles[@]}" "${feed_title}")
SAVE_IFS=$IFS
IFS=":"
feed_source="${ARRAY[*]:1}"
IFS=$SAVE_IFS
feed_sources=("${feed_sources[@]}" "${feed_source}")
done
cat /dev/null > "${OPENWRT_CHECKOUT_DIR}/feeds.conf"
for feed_source in "${feed_sources[@]}"; do
echo "${feed_source}" >> "${OPENWRT_CHECKOUT_DIR}/feeds.conf"
done
echo "Configuring feeds done."
}
openwrt_feeds_import() {
cd "${OPENWRT_CHECKOUT_DIR}"
# Importing feeds
echo "Importing feeds"
./scripts/feeds update -a > /dev/null
for pkg in ${all_packages}; do
# Check if package name begins with a minus
if [[ ${pkg:0:1} != "-" ]]; then
./scripts/feeds install "${pkg}"
fi
done
}
openwrt_clone_and_patch() {
# Add the git commit hash and build date to the firmware
build_number="$(git log --pretty=format:'%H' -n 1)"
mkdir -p files/opt/sudowrt
echo "$build_number | built on $(date)" > files/opt/sudowrt/build_version
mkdir -p "${workdir}/${BUILD_DIR}"
openwrt_version=$(cat openwrt_config/version)
openwrt_path=$(echo "${openwrt_version}" | cut -d ':' -f 1)
openwrt_rev=$(echo "${openwrt_version}" | cut -d ':' -f 2)
echo "Removing and re-cloning openwrt build root";
echo "OpenWRT path: ${openwrt_path}"
echo "OpenWRT revision: ${openwrt_rev}"
echo "Checking out OpenWRT into ${BUILD_DIR}, this could take some time"
if [ -d "$OPENWRT_CHECKOUT_DIR" ]; then
cd "${OPENWRT_CHECKOUT_DIR}"
git clean -f
else
git clone "git://git.archive.openwrt.org/${openwrt_path}/openwrt.git" "${OPENWRT_CHECKOUT_DIR}"
fi
cd "${OPENWRT_CHECKOUT_DIR}"
git checkout "${openwrt_rev}"
cd "${workdir}"
if [ $? -ne 0 ]; then
echo "Error during svn checkout of OpenWRT"
exit $?
fi
if [ -d "openwrt_addons" ] ; then
echo "Adding extra files."
cp -r openwrt_addons/* "${OPENWRT_CHECKOUT_DIR}"
fi
# Remove configuration file already present in the generic build dir
rm -f "${workdir}/${BUILD_DIR}/openwrt/.config"
openwrt_feeds_configure
echo "Applying patches for OpenWRT base tree..."
cd "${OPENWRT_CHECKOUT_DIR}"
if [ ! -h "patches" ] ; then
ln -s "${workdir}/openwrt_patches" patches
fi
if [ ! -f "patches/series" ] ; then
quilt import patches/*
fi
quilt push -f -a -q || [ $? -eq 2 ] #quilt returns a 2 if there is nothing more to do
openwrt_feeds_import
# change back to initial directory
cd "${workdir}"
}
openwrt_build_configure()
{
local arch=$1
local kconfig=${workdir}/${BUILD_DIR}/openwrt/scripts/kconfig.pl
local config_dir=${workdir}/openwrt_config/arch_configs
local config=${workdir}/${BUILD_DIR}/openwrt/config.${arch}
$kconfig 'm+' "${config_dir}/generic" "${config_dir}/${arch}" > "${config}"
# Include all common packages
for pkg in ${all_packages}; do
# Check if package name begins with a minus
if [[ ${pkg:0:1} == "-" ]]; then
pkg=${pkg:1:${#pkg}-1} # strip first character
echo "CONFIG_PACKAGE_${pkg}=n" >> "${config}"
else
echo "CONFIG_PACKAGE_${pkg}=y" >> "${config}"
fi
done
}
openwrt_buildprep()
{
local arch=$1
local build_dir=${workdir}/${BUILD_DIR}/builder.${arch}
if [ ! -d "$build_dir" ]; then
# Make hardlinks to avoid space duplication but to still isolate builds
cp -lr "${workdir}/${BUILD_DIR}/openwrt" "${build_dir}"
fi
rsync --delete -a files "${build_dir}/"
local toolchain_dir_bin
for file in ${build_dir}/staging_dir/*; do
if [[ "${file}" == *"toolchain"* ]]; then
toolchain_dir_bin="${file}/bin"
fi
done
local staging_dir_bin=${build_dir}/staging_dir/host/bin
if grep -q "^export PATH=" ~/.bashrc
then
local path_line
path_line=$(grep "^export PATH=" ~/.bashrc)
path=${path_line#'export PATH='\'''}
if [[ "${path}" != *"$staging_dir_bin"* ]]; then
path=${staging_dir_bin}":"$path
fi
if [[ "${path}" != *"$toolchain_dir_bin"* ]]; then
path=${toolchain_dir_bin}":"$path
fi
sed -i "/export PATH=/c\export PATH='$PATH:${path}" ~/.bashrc
# code if found
else
echo "export PATH='$PATH:${staging_dir_bin}:${toolchain_dir_bin}'" >> ~/.bashrc
fi
# Prepare configuration file and build
cd "${build_dir}"
cp "config.${arch}" .config
make defconfig V=s &> "${build_dir}/build.echo"
if [ "$?" != "0" ]; then
echo "Default configuration preparation for '${arch}' has failed. See build.echo for details!"
exit 1
fi
# download dependencies (tools, toolchain, packages, feeds)
make download
# find all dependencies in package directory and force download them, related to issue #116
#for d in $(find package/ -mindepth 1 -maxdepth 2 -type d); do
# if [ -e $d/Makefile ]; then
# make $d/download V=s
# else
# echo "not a package"
# fi
#done
}
openwrt_package_builder()
{
# rarely used script salvaged from build_package
echo "Compiling package ${1}"
make "package/${1}/compile"
make "package/${1}/install"
make package/index
}
# adapted from https://bugzilla.yoctoproject.org/show_bug.cgi?id=7338#c7
# also see https://github.com/sudomesh/sudowrt-firmware/issues/113
clean_confdir3_leftover() {
# When running in a Docker container, getcwd-path-max.m4 leaves behind
# a deeply-nested structure of confdir3/ directories that can't be deleted using rm -fr.
# See https://github.com/docker/docker/issues/13451
echo "Removing [$1]..."
mv $1/confdir3/confdir3/ $1/fred || true
rm -fr $1/fred/ $1/confdir3/
echo "Removing [$1] done."
}
openwrt_builder()
{
# According to https://github.com/jkilpatr
# in https://github.com/sudomesh/sudowrt-firmware/pull/115#discussion_r152340787 :
# in my experience highly parallel make runs get stuck on disk IO and you get better cpu saturation with a multiplier.
BUILD_CORES=$(($SYSTEM_CORES * 2))
if [ -v $SUDOWRT_DEBUG ]; then
BUILD_CORES=1
fi
local arch=$1
local build_dir=${workdir}/${BUILD_DIR}/builder.${arch}
mkdir -p ${build_dir}
echo "Building [${arch}] in dir: ${build_dir}..."
make -j$BUILD_CORES -C "${build_dir}" 2>&1 | tee -a "${build_dir}/build.log"
if [ "$?" != "0" ]; then
echo "Building [${arch}] failed. See ${build_dir}/build.log for details!"
exit 1
fi
clean_confdir3_leftover "${build_dir}/build_dir/host/findutils-4.4.2"
echo "Building [${arch}] done."
}
remove_images()
{
local image_dir=$1
if [ -e ${image_dir} ]; then
rm -r ${image_dir}
else
echo "Warning: ${image_dir} not found, maybe this is an initial build"
fi
}
openwrt_rebuilder()
{
BUILD_CORES=$(($SYSTEM_CORES * 2))
if [ -v $SUDOWRT_DEBUG ]; then
BUILD_CORES=1
fi
local arch=$1
local target=$2
# Rebuild any changes made to openwrt_configs
local build_dir=${workdir}/${BUILD_DIR}/builder.${arch}
local kconfig=${build_dir}/scripts/kconfig.pl
local config_dir=${workdir}/openwrt_config/arch_configs
local config=${build_dir}/config.${arch}
$kconfig 'm+' "${config_dir}/generic" "${config_dir}/${arch}" > "${config}"
cp ${config} ${build_dir}/.config
make defconfig -C ${build_dir}
echo "Deleting old binaries in dir: ${build_dir}/bin/${arch}..."
remove_images "${build_dir}/bin/${arch}"
echo "Rebuilding [${arch}] in dir: ${build_dir}..."
make -j$BUILD_CORES -C "${build_dir}" 2>&1 | tee -a "${build_dir}/build.log"
if [ "$?" != "0" ]; then
echo "Rebuilding [${arch}] failed. See ${build_dir}/build.log for details!"
exit 1
fi
clean_confdir3_leftover "${build_dir}/build_dir/host/findutils-4.4.2"
echo "Rebuilding [${arch}] done."
}