-
Notifications
You must be signed in to change notification settings - Fork 16
/
build_bela.sh
executable file
·170 lines (149 loc) · 4.04 KB
/
build_bela.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
#!/bin/bash -e
# this script downloads, builds and compiles an image (including kernel, bootloader and rootfs) for Bela
DEPENDENCIES="debootstrap qemu-arm-static autoreconf libtool arm-linux-gnueabihf-ranlib kpartx setuidgid wget bison flex pkg-config arm-linux-gnueabihf-gcc"
for a in $DEPENDENCIES; do
which $a > /dev/null ||\
{
echo "Dependency check failed: you should install \`$a' before continuing"
exit 1
}
done
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# Using full path as it is required by `make install` for xenomai-3
DIR=`pwd`/$(dirname "$0")
export DIR
targetdir=${DIR}/rootfs
targetdir_pre_chroot_backup=${DIR}/pre_chroot_backup
export targetdir
usage(){
echo "--no-downloads --no-kernel --fast-kernel --no-rootfs --cached-fs --do-not-cache-fs --no-bootloader --no-build-xenomai --emmc-flasher --clean"
}
clean_all()
{
rm -rf rootfs pre_chroot_backup downloads
git clean -fx
}
# parse commandline options
unset NO_DOWNLOADS
unset NO_KERNEL
unset FAST_KERNEL
unset NO_ROOTFS
unset NO_BOOTLOADER
unset CACHED_FS
unset NO_BUILD_XENOMAI
unset DO_NOT_CACHE_FS
unset EMMC_FLASHER
export CORES=$(getconf _NPROCESSORS_ONLN)
[ -f $DIR/config ] && . $DIR/config || {
echo Error: \`$DIR/config\` was not found. Use \`$DIR/config.template\` as a starting point. >&2
exit 1;
}
while [ ! -z "$1" ] ; do
case $1 in
-h|--help)
usage
exit
;;
--no-downloads)
NO_DOWNLOADS=true
;;
--no-kernel)
NO_KERNEL=true
;;
--fast-kernel)
FAST_KERNEL=true
;;
--no-rootfs)
NO_ROOTFS=true
;;
--no-bootloader)
NO_BOOTLOADER=true
;;
--no-build-xenomai)
NO_BUILD_XENOMAI=true
;;
--cached-fs)
CACHED_FS=true
;;
--do-not-cache-fs)
DO_NOT_CACHE_FS=true
;;
--clean)
clean_all
;;
--emmc-flasher)
EMMC_FLASHER=true
;;
*)
echo "Unknown option $1" >&2
usage
exit 1
esac
shift
done
export UNSU="setuidgid $SUDO_USER"
# download / clone latest versions of things we need
if [ -f ${NO_DOWNLOADS} ] ; then
$UNSU ${DIR}/scripts/downloads.sh
fi
# compile the kernel
if [ -f ${NO_KERNEL} ] ; then
export FAST_KERNEL
$UNSU ${DIR}/scripts/build_kernel.sh
fi
# this is normally from `config`
export CC
if [ -f ${NO_BUILD_XENOMAI} ] ; then
$UNSU ${DIR}/scripts/build_xenomai.sh
fi
# build the rootfs
if [ -f ${NO_ROOTFS} ] ; then
echo "~~~~ building debian stretch rootfs ~~~~"
rm -rf $targetdir
if [ -z "${CACHED_FS}" ] ; then
mkdir -p $targetdir
mkdir $targetdir/root
DEB_PACKAGES=`tr "\n" "," < ${DIR}/packages.txt | sed '$ s/.$//'`
sudo debootstrap --arch=armhf --foreign --components=main,free,non-free --include=${DEB_PACKAGES} stretch $targetdir
sudo cp /usr/bin/qemu-arm-static $targetdir/usr/bin/
sudo cp /etc/resolv.conf $targetdir/etc
sudo chroot $targetdir debootstrap/debootstrap --second-stage
if [ "${DO_NOT_CACHE_FS}" != "true" ] ; then
echo "Backing up the pre-chroot rootfs into $targetdir_pre_chroot_backup"
rm -rf $targetdir_pre_chroot_backup
mkdir -p $targetdir_pre_chroot_backup
sudo cp -ar $targetdir $targetdir_pre_chroot_backup
fi
else
echo "Using backup pre-chroot rootfs from $targetdir_pre_chroot_backup"
sudo cp -ar $targetdir_pre_chroot_backup/rootfs $targetdir
fi
${DIR}/scripts/pre-chroot.sh
sudo cp -v ${DIR}/scripts/chroot.sh $targetdir/
sudo CORES=$CORES chroot $targetdir/ /chroot.sh
sudo mkdir -p $targetdir/sys
sudo mkdir -p $targetdir/proc
sudo rm $targetdir/usr/bin/qemu-arm-static
sudo rm $targetdir/chroot.sh
fi
# compile and patch u-boot
if [ -f ${NO_BOOTLOADER} ] ; then
$UNSU ${DIR}/scripts/build_bootloader.sh
fi
if [ -n "${EMMC_FLASHER}" ] ; then
sudo cp /usr/bin/qemu-arm-static $targetdir/usr/bin/
sudo cp -v ${DIR}/scripts/emmc-flasher-chroot.sh $targetdir/
sudo chroot $targetdir/ /emmc-flasher-chroot.sh
sudo rm $targetdir/emmc-flasher-chroot.sh
sudo rm $targetdir/usr/bin/qemu-arm-static
fi
# something (at least `pip install` does it) may have found a way to create
# /home/$SUDO_USER . Let's undo that
sudo rm -rf $targetdir/home/$SUDO_USER
# create SD image
if [ -f ${NO_IMG} ] ; then
${DIR}/scripts/create_img.sh
fi