-
Notifications
You must be signed in to change notification settings - Fork 5
/
aosc-sunxi-builder.sh
290 lines (281 loc) · 9.88 KB
/
aosc-sunxi-builder.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
#!/bin/bash
function print_tick() {
echo -e "\e[92m✔\e[0m"
}
function print_cross() {
echo -e "\e[91m✘\e[0m"
}
function adie() {
echo -e "[!] \e[91mError(s) occurred! See logs for more information.\e[0m"
grep -n -r "Error" "${LOGDIR}"
exit 1
}
function print_progress() {
# 1. build stage 2. build pace 3. sub items
if [[ ! "${EXDESC}" ]]; then
LAST_PRGS="${1} ${2} ${3}"
fi
printf "\r%s %s" "${LAST_PRGS}" "${EXDESC}"
unset EXDESC
}
function fetch_kernel_version() {
TMPFILE="$(mktemp --suffix=".py")"
cat << EOF > "${TMPFILE}"
import json, sys
dist = json.load(sys.stdin)
for i in dist['releases']:
if i['moniker'] == "${BUILD_MONIKER}":
print(i['source'] or i['gitweb'])
EOF
TBLURL=$(bash -c "curl --silent 'https://www.kernel.org/releases.json' | python \"${TMPFILE}\"")
if [[ $? -eq 0 ]]; then
print_tick
rm "${TMPFILE}"
return 0
else
print_cross
rm "${TMPFILE}"
return 1
fi
}
function fetch_uboot_version() {
TMPFILE="$(mktemp --suffix=".py")"
cat << EOF > "${TMPFILE}"
import json, sys
dist = json.load(sys.stdin)
print(dist[0]['name'], dist[0]['tarball_url'], sep='\n')
EOF
RETDATA=$(bash -c "curl -H 'Accept: application/vnd.github.v3+json' --silent 'https://api.github.com/repos/u-boot/u-boot/tags' | python \"${TMPFILE}\"")
if [[ $? -eq 0 ]]; then
print_tick
rm "${TMPFILE}"
return 0
else
print_cross
rm "${TMPFILE}"
return 1
fi
}
function cp_dtb() {
printf "[*] Copying final Device Tree Blobs... "
for i in $DTB_TARGETS
do
DTB_CNAME="$(echo "$i" | cut -d = -f 1)"
DTB_AOSCNAME="$(echo "$i" | cut -d = -f 2)"
mkdir -p "$OUTDIR"/dtb-"$DTB_AOSCNAME"
cp "$LINUX_DIR"/arch/arm/boot/dts/"$DTB_CNAME".dtb "$OUTDIR"/dtb-"$DTB_AOSCNAME"/dtb.dtb > /dev/null 2>&1 || { print_cross; adie; }
done
print_tick
}
function uboot_binman_hack() {
cp lib/libfdt/pylibfdt/libfdt.i tools/
LDFLAGS="" CFLAGS= VERSION="u-boot-2017.07-rc2" CPPFLAGS="-Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -include ./include/libfdt_env.h -idirafterinclude -idirafter./arch/arm/include -I./lib/libfdt -I./tools -DUSE_HOSTCC -D__KERNEL_STRICT_NAMES -D_GNU_SOURCE " OBJDIR=tools SOURCES="lib/libfdt/fdt.c lib/libfdt/fdt_ro.c lib/libfdt/fdt_wip.c lib/libfdt/fdt_sw.c lib/libfdt/fdt_rw.c lib/libfdt/fdt_strerror.c lib/libfdt/fdt_empty_tree.c lib/libfdt/fdt_addresses.c lib/libfdt/fdt_overlay.c lib/libfdt/fdt_region.c tools/libfdt.i" SWIG_OPTS="-I./lib/libfdt -I./lib" python2 lib/libfdt/pylibfdt/setup.py --quiet build_ext --build-lib tools > /dev/null 2>&1
LDFLAGS="" CFLAGS= VERSION="u-boot-2017.07-rc2" CPPFLAGS="-Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -include ./include/libfdt_env.h -idirafterinclude -idirafter./arch/arm/include -I./lib/libfdt -I./tools -DUSE_HOSTCC -D__KERNEL_STRICT_NAMES -D_GNU_SOURCE " OBJDIR=tools SOURCES="lib/libfdt/fdt.c lib/libfdt/fdt_ro.c lib/libfdt/fdt_wip.c lib/libfdt/fdt_sw.c lib/libfdt/fdt_rw.c lib/libfdt/fdt_strerror.c lib/libfdt/fdt_empty_tree.c lib/libfdt/fdt_addresses.c lib/libfdt/fdt_overlay.c lib/libfdt/fdt_region.c tools/libfdt.i" SWIG_OPTS="-I./lib/libfdt -I./lib" python3 lib/libfdt/pylibfdt/setup.py --quiet build_ext --build-lib tools > /dev/null 2>&1
}
function build_uboot() {
STAGE="[*] Building U-Boot... "
TGT_COUNT=$(echo "${UBOOT_TARGETS}" | wc -w)
PRGS=0
if [[ $(python -c "import sys; print(sys.version_info[0])") == "3" ]]; then
NEED_HACK=1
fi
for i in $UBOOT_TARGETS; do
((PRGS+=1))
UBOOT_CNAME="$(echo "$i" | cut -d = -f 1)"
UBOOT_AOSCNAME="$(echo "$i" | cut -d = -f 2)"
print_progress "${STAGE}" "[${PRGS}/${TGT_COUNT}]" "${UBOOT_AOSCNAME}"
EXDESC="> Decompressing " print_progress
tar xf "${UBOOTTBLNAME}"
UBOOT_DIR="$(echo u-boot-*/)"
pushd "$UBOOT_DIR" > /dev/null
EXDESC="> Patching " print_progress
for j in ../patches/u-boot/*
do
patch -Np1 -s -i "${j}" >> "${LOGDIR}/patch.log"
done
if [[ $NEED_HACK ]]; then uboot_binman_hack; fi
EXDESC="> Building " print_progress
{ echo "${UBOOT_CNAME}"; make "${UBOOT_CNAME}"_defconfig; } >> "${LOGDIR}/u-boot.log" 2>&1
make CROSS_COMPILE="${CROSS_COMPILE}" -j"$(nproc)" >> "${LOGDIR}/u-boot.log" 2>&1
if [[ $? -ne 0 ]]; then
print_cross
adie
fi
EXDESC="> Completed " print_progress
mkdir -p "$OUTDIR"/u-boot-"$UBOOT_AOSCNAME"/
cp u-boot-sunxi-with-spl.bin "$OUTDIR"/u-boot-"$UBOOT_AOSCNAME"/
popd > /dev/null
rm -r "$UBOOT_DIR"
done;
print_tick
}
function build_linux() {
LINUX_SRC="$(echo linux-*.tar*)"
LINUX_DIR="${TBLNAME//\.tar*/}"
if [[ "${BUILD_CHOICE}" != "1" ]]; then
git reset --hard
if ! git checkout -f staging; then
echo "[!] Failed to switch branch!"
adie
fi
fi
STAGE="[*] Building Linux kernel... "
TGT_COUNT=2
PRGS=0
for tgt in 'sunxi-kvm-config' 'sunxi-nokvm-config';
do
((PRGS+=1))
print_progress "${STAGE}" "[$PRGS/$TGT_COUNT]" "$tgt"
if [[ ! -d "${LINUX_DIR}" ]]; then
EXDESC="> Decompressing " print_progress
tar xf "${LINUX_SRC}"
LINUX_DIR="${LINUX_VER}"
pushd "${LINUX_DIR}" > /dev/null
EXDESC="> Patching " print_progress
for i in ../patches/linux/*
do
patch -Np1 -s -i "$i" >> "${LOGDIR}/patch.log" 2>&1
done
else
pushd "$LINUX_DIR" > /dev/null
fi
EXDESC="> Building kernel " print_progress
cp "../${tgt}" .config
make ARCH=arm CROSS_COMPILE="${CROSS_COMPILE}" -j"$(nproc)" >> "${LOGDIR}/linux-${tgt}.log" 2>&1
TMPDIR=$(mktemp -d)
EXDESC="> Installing modules " print_progress
make ARCH=arm CROSS_COMPILE="${CROSS_COMPILE}" INSTALL_MOD_PATH="$TMPDIR" modules_install >> "${LOGDIR}/linux-${tgt}.log" 2>&1
mkdir -p "$OUTDIR/linux-$tgt"
cp -- arch/arm/boot/zImage "$OUTDIR/linux-$tgt/zImage"
EXDESC="> Building extra modules " print_progress
EXTRA_KMOD_DIR="$(echo "$TMPDIR"/lib/modules/*)/kernel/extra"
mkdir -p "$EXTRA_KMOD_DIR"
for i in ../extra-kmod/*
do
EXDESC="> Building extra modules > $(basename "${i}") " print_progress
export KDIR=$PWD ARCH=arm CROSS_COMPILE="${CROSS_COMPILE}"
pushd "$i" > /dev/null
sh build.sh >> "${LOGDIR}/extramods.log" 2>&1
cp -- *.ko "$EXTRA_KMOD_DIR/"
popd > /dev/null
unset KDIR
done
EXDESC="> Installing extra modules " print_progress
depmod -b "$TMPDIR" "$(basename "$(readlink -f $EXTRA_KMOD_DIR/../..)")"
cp -r -- "$TMPDIR"/lib/modules/ "$OUTDIR/linux-$tgt/"
rm -r -- "$TMPDIR"
popd > /dev/null
done
print_progress "${STAGE}" && print_tick
}
if [[ ! "${CROSS_COMPILE}" ]]; then
echo "Please set \`CROSS_COMPILE' environment variable."
exit 1
fi
printf "Please select a channel:\n\t\t[1] stable\n\t\t[2] RC\n\t\t[3] next\n"
read -n 1 -r -p "[*] Your choice? [Default: 1] " BUILD_CHOICE
echo
case "${BUILD_CHOICE}" in
2 )
echo "[*] Will build RC version of kernel image. (Potentially unstable)"
BUILD_MONIKER="mainline";;
3 )
echo "[*] Will build linux-next version of kernel image. (Warriors ONLY!)"
BUILD_MONIKER="linux-next";;
* )
echo "[*] Will build stable version of kernel image."
BUILD_CHOICE=1 && BUILD_MONIKER="stable";;
esac
printf "[*] Fetching Linux kernel releases information... "
if ! fetch_kernel_version; then
exit 1
fi
printf "[*] Fetching Das U-Boot releases information... "
if ! fetch_uboot_version; then
exit 1
fi
for i in $RETDATA;
do if [[ ${i:0:1} == 'v' ]]; then
UBOOT_VERSION="${i}"
else
UBOOT_URL="${i}"
fi;
done
TBLNAME=$(basename "${TBLURL}")
UBOOTTBLNAME="u-boot-${UBOOT_VERSION}.tar.gz"
echo "[*] Downloading U-Boot: ${UBOOT_VERSION}..."
if ! test -f "${UBOOTTBLNAME}"; then
if ! (wget -c "${UBOOT_URL}" -O "${UBOOTTBLNAME}.dl" && mv "${UBOOTTBLNAME}.dl" "${UBOOTTBLNAME}"); then
exit 127
fi
fi
LINUX_VER=${TBLNAME//\.tar*/}
echo "[*] Downloading Linux kernel: ${LINUX_VER}..."
if [[ ${BUILD_CHOICE} -eq 3 ]]; then
TBLURL="https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/snapshot/linux-next-${TBLNAME//\.tar*/}.tar.gz"
TBLNAME="linux-${TBLNAME}.tar.gz"
fi
if ! test -f "${TBLNAME}"; then
if ! (wget -c "${TBLURL}" -O "${TBLNAME}.dl" && mv "${TBLNAME}.dl" "${TBLNAME}"); then
exit 127
fi
fi
if [[ ! "${OUTDIR}" ]]; then
mkdir out || rm -r out/*
OUTDIR="$(readlink -f out)"
fi
echo "[*] Fetching AOSC SUNXI building receipt..."
if test -d "aosc-os-armel-sunxi-boot/.git"; then
pushd aosc-os-armel-sunxi-boot
if ! ( git gc && git fetch --all && git pull -f ); then
echo "[!] Git repository messed up!!! Please fix it manually or remove it!"
exit 127
fi
popd
else
if ! git clone "https://github.com/AOSC-Dev/aosc-os-armel-sunxi-boot"; then
print_cross
exit 127
fi
fi
echo "[*] Output directory: ${OUTDIR}"
pushd "aosc-os-armel-sunxi-boot"
rm -rf logs || true
mkdir logs
LOGDIR="$(readlink -f ./logs)"
echo "[*] Log directory: ${LOGDIR}"
unlink linux-*.tar*
unlink u-boot-*.tar*
ln -s ../"${TBLNAME}" .
ln -s ../"${UBOOTTBLNAME}" .
chmod a+x ./list.sh
. ./list.sh
if [[ "${BUILD_UBOOT}" == "0" ]]; then
echo "[*] Not building U-Boot as required."
else
if ! build_uboot; then adie; fi
fi
if [[ "${BUILD_LINUX}" == "0" ]]; then
echo "[*] Not building Linux kernel as required."
else
if ! build_linux; then adie; fi
fi
cp_dtb
GIT_REV=$(git rev-parse --short HEAD)
popd
FILE_COUNT=$(find "${OUTDIR}" | wc -l)
if [[ $((FILE_COUNT)) -lt 76 ]]; then
echo "[!] No enough files collected, suspecting a build failure!"
adie
fi
echo "[*] Tarring final tarball..."
TARBALL_NAME="aosc-os-armel-sunxi-boot-$(date +%Y%m%d)-g${GIT_REV}-$(basename "${LINUX_DIR}")-$(basename ${UBOOT_VERSION})"
tar cJf "$(pwd)/${TARBALL_NAME}.tar.xz" -C "${OUTDIR}" .
FILE_SIZE=$(stat -c "%s" "$(pwd)/${TARBALL_NAME}.tar.xz")
if [[ $((FILE_SIZE)) -lt 20000000 ]]; then
echo "[!] Resulting file too small (only ${FILE_SIZE} bytes), suspecting a build failure!"
rm -- "${TARBALL_NAME}.tar.xz"
adie
fi
echo "[+] Product: ${TARBALL_NAME}.tar.xz"
echo "[+] Size: ${FILE_SIZE} bytes"