-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild-sysroot.sh
executable file
·206 lines (183 loc) · 6.27 KB
/
build-sysroot.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
#!/bin/bash
set -e
SRC_ROOT=$(pwd)
DISTRIBUTION_NAME=$1
DISTRIUBTION_VERSION=$2
SYSROOT=$3
if [ -z $SYSROOT ]; then
SYSROOT=sysroot-$DISTRIBUTION_NAME-$DISTRIUBTION_VERSION
fi
SYSROOT=$(pwd)/$SYSROOT
DISTRIBUTION="$DISTRIBUTION_NAME:$DISTRIUBTION_VERSION"
case $DISTRIUBTION_VERSION in
"focal")
INSTALL_DEPS_CMD=" \
apt-get update && \
apt-get install -y \
libc6-dev \
libgcc-9-dev \
libicu-dev \
libstdc++-9-dev \
libstdc++6 \
linux-libc-dev \
zlib1g-dev \
libcurl4-openssl-dev \
libxml2-dev \
libsystemd-dev \
"
;;
"bullseye")
RASPIOS_VERSION="2024-10-22"
RASPIOS_URL=https://downloads.raspberrypi.com/raspios_oldstable_lite_armhf/images/raspios_oldstable_lite_armhf-2024-10-28
INSTALL_DEPS_CMD=" \
apt-get update && \
apt-get install -y \
libc6-dev \
libgcc-10-dev \
libicu-dev \
libstdc++-10-dev \
libstdc++6 \
linux-libc-dev \
zlib1g-dev \
libcurl4-openssl-dev \
libxml2-dev \
libsystemd-dev \
"
;;
"jammy" | "bookworm")
RASPIOS_VERSION="2024-11-19"
RASPIOS_URL=https://downloads.raspberrypi.com/raspios_lite_armhf/images/raspios_lite_armhf-$RASPIOS_VERSION
INSTALL_DEPS_CMD=" \
apt-get update && \
apt-get install -y \
libc6-dev \
libgcc-12-dev \
libicu-dev \
libstdc++-12-dev \
libstdc++6 \
linux-libc-dev \
zlib1g-dev \
libcurl4-openssl-dev \
libxml2-dev \
libsystemd-dev \
"
;;
"mantic" | "noble")
INSTALL_DEPS_CMD=" \
apt-get update && \
apt-get install -y \
libc6-dev \
libgcc-13-dev \
libicu-dev \
libstdc++-13-dev \
libstdc++6 \
linux-libc-dev \
zlib1g-dev \
libcurl4-openssl-dev \
libxml2-dev \
libsystemd-dev \
"
;;
*)
echo "Unsupported distribution $DISTRIBUTION!"
echo "If you'd like to support it, update this script to add the apt package list for it."
exit
;;
esac
if [[ $DISTRIBUTION_NAME = "raspios" ]]; then
INSTALL_DEPS_CMD="$INSTALL_DEPS_CMD symlinks"
fi
if [ ! -z $EXTRA_PACKAGES ]; then
echo "Including extra packages: $EXTRA_PACKAGES"
INSTALL_DEPS_CMD="$INSTALL_DEPS_CMD && apt-get install -y $EXTRA_PACKAGES"
fi
# This is for supporting armv6
if [[ $DISTRIBUTION_NAME = "raspios" ]]; then
echo "Installing host dependencies..."
sudo apt update && sudo apt install qemu-user-static p7zip xz-utils
mkdir artifacts && true
cd artifacts
echo "Downloading raspios $RASPIOS_VERSION for $DISTRIUBTION_VERSION..."
IMAGE_FILE=$RASPIOS_VERSION-raspios-$DISTRIUBTION_VERSION-armhf-lite.img
DOWNLOAD_URL=$RASPIOS_URL/$IMAGE_FILE.xz
wget -q -N $DOWNLOAD_URL
if [ ! -f $IMAGE_FILE ]; then
echo "Uncompressing $IMAGE_FILE.gz and extracting contents..."
xz -dk $IMAGE_FILE.xz && true
fi
7z e -y $IMAGE_FILE
echo "Mounting 1.img and needed passthroughs..."
sudo umount -R sysroot && true
rm -rf sysroot && mkdir sysroot
sudo mount -o loop 1.img sysroot
sudo mount --bind /dev sysroot/dev
sudo mount --bind /dev/pts sysroot/dev/pts
sudo mount --bind /proc sysroot/proc
sudo mount --bind /sys sysroot/sys
echo "Starting chroot to update dependencies & fix symlinks..."
REMOVE_DEPS_CMD="apt-get remove -y --purge \
apparmor \
bluez \
network-manager \
linux-image* \
*firmware* \
openssh* \
p7zip* \
perl \
perl-modules* \
raspi* \
rpi* \
libqt5core5a \
"
sudo cp /usr/bin/qemu-arm-static sysroot/usr/bin
sudo chroot sysroot qemu-arm-static /bin/bash -c "$REMOVE_DEPS_CMD && $INSTALL_DEPS_CMD && apt-get autoremove -y"
sudo chroot sysroot qemu-arm-static /bin/bash -c "apt list --installed && symlinks -cr /usr/include && symlinks -cr /usr/lib"
echo "Copying files from sysroot to $SYSROOT..."
rm -rf $SYSROOT
mkdir -p $SYSROOT/usr/lib
cp -r sysroot/lib $SYSROOT/lib
cp -r sysroot/usr/include $SYSROOT/usr/include
cp -r sysroot/usr/lib/ld-linux-armhf.so.3 $SYSROOT/usr/lib/
cp -r sysroot/usr/lib/os-release $SYSROOT/usr/lib/
cp -r sysroot/usr/lib/arm-linux-gnueabihf $SYSROOT/usr/lib/
cp -r sysroot/usr/lib/linux $SYSROOT/usr/lib/ && true
cp -r sysroot/usr/lib/gcc $SYSROOT/usr/lib/
# Cleanup
rm -rf $SYSROOT/usr/include/aarch64-linux-gnu
rm -rf $SYSROOT/usr/lib/gcc/arm-linux-gnueabihf/7
rm -rf $SYSROOT/usr/lib/gcc/arm-linux-gnueabihf/7.5.0
rm -rf $SYSROOT/usr/lib/gcc/arm-linux-gnueabihf/8
echo "Umounting and cleaning up..."
sudo umount -R sysroot
rm -f *.fat
rm -f *.img
else
echo "Starting up qemu emulation"
docker run --privileged --rm tonistiigi/binfmt --install all
CONTAINER_NAME=swift-armhf-sysroot
echo "Building $DISTRIBUTION distribution for sysroot"
docker rm --force $CONTAINER_NAME
docker run \
--platform linux/armhf \
--name $CONTAINER_NAME \
$DISTRIBUTION \
/bin/bash -c "$INSTALL_DEPS_CMD"
echo "Extracting sysroot folders to $SYSROOT"
rm -rf $SYSROOT
mkdir -p $SYSROOT/usr
docker cp $CONTAINER_NAME:/lib $SYSROOT/lib
docker cp $CONTAINER_NAME:/usr/include $SYSROOT/usr/include
docker cp $CONTAINER_NAME:/usr/lib $SYSROOT/usr/lib
# Find broken links, re-copy
cd $SYSROOT
BROKEN_LINKS=$(find . -xtype l)
while IFS= read -r link; do
# Ignore empty links
if [ -z "${link}" ]; then continue; fi
echo "Replacing broken symlink: $link"
link=$(echo $link | sed '0,/./ s/.//')
docker cp -L $CONTAINER_NAME:$link $(dirname .$link)
done <<< "$BROKEN_LINKS"
echo "Cleaning up"
docker rm $CONTAINER_NAME
fi