Skip to content

Commit 1f06e36

Browse files
committed
SWPROT-8953: build: Improve detection/setup of foreign arch like armhf
It was tested on github, the alternative would not to support arm images provided by github. For the record, native build on arm64 takes up to 5h on github. Relate-to: https://medium.com/swlh/using-github-actions-to-build-arm-based-docker-images-413a8d498ee Origin: #14 Signed-off-by: Philippe Coval <philippe.coval@silabs.com>
1 parent c248cb5 commit 1f06e36

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

scripts/build-rootfs.sh

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ EOF
1616
project="unifysdk"
1717
debian_suite="bookworm"
1818

19+
debian_arch=$(dpkg --print-architecture)
20+
1921
# Can be overloaded from env eg: ARCH=arm64"
20-
target_debian_arch=${ARCH:="$(dpkg --print-architecture)"}
22+
target_debian_arch=${ARCH:="${debian_arch}"}
2123

2224
debian_mirror_url="http://deb.debian.org/debian"
2325
sudo="sudo"
@@ -27,45 +29,76 @@ MAKE="/usr/bin/make"
2729
CURDIR="$PWD"
2830
chroot="systemd-nspawn"
2931
packages="debootstrap \
30-
binfmt-support \
3132
debian-archive-keyring \
32-
qemu-user-static \
3333
systemd-container \
3434
time \
3535
"
3636
case $target_debian_arch in
3737
amd64)
38+
qemu_system="qemu-system-${CMAKE_SYSTEM_PROCESSOR}"
3839
export CMAKE_SYSTEM_PROCESSOR="x86_64"
3940
export CARGO_TARGET_TRIPLE="${CMAKE_SYSTEM_PROCESSOR}-unknown-linux-gnu"
40-
packages="${packages} qemu-system-x86"
41-
qemu_system="qemu-system-${CMAKE_SYSTEM_PROCESSOR}"
41+
qemu_package="qemu-system-x86"
4242
;;
4343

4444
arm64)
4545
qemu_arch="aarch64"
4646
qemu_system="qemu-system-${qemu_arch}"
4747
export CMAKE_SYSTEM_PROCESSOR="${qemu_arch}"
4848
export CARGO_TARGET_TRIPLE="${CMAKE_SYSTEM_PROCESSOR}-unknown-linux-gnu"
49-
packages="${packages} qemu-system-arm"
49+
qemu_package="qemu-system-arm"
5050
;;
5151

52+
armhf)
53+
debian_arch="armhf"
54+
qemu_arch="arm"
55+
qemu_system="qemu-system-${qemu_arch}"
56+
export CMAKE_SYSTEM_PROCESSOR="armv7l"
57+
export CARGO_TARGET_TRIPLE="armv7-unknown-linux-gnueabihf"
58+
qemu_package="qemu-system-arm"
59+
60+
# Workaround: https://github.com/armbian/build/issues/5330
61+
binfmt_file="/var/lib/binfmts/qemu-${qemu_arch}"
62+
[ -e "$binfmt_file" ] || cat<<EOF \
63+
| { sudo mkdir -p /var/lib/binfmts && sudo tee "$binfmt_file" ; }
64+
package qemu-user-static
65+
interpreter /usr/libexec/qemu-binfmt/arm-binfmt-P
66+
magic \x7f\x45\x4c\x46\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00
67+
offset 0
68+
mask \xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff
69+
credentials yes
70+
fix_binary no
71+
preserve yes
72+
EOF
73+
;;
5274
*)
5375
CMAKE_SYSTEM_PROCESSOR="$ARCH"
5476
CARGO_TARGET_TRIPLE="${CMAKE_SYSTEM_PROCESSOR}-unknown-linux-gnu"
5577
qemu_arch="${ARCH}"
5678
qemu_system="qemu-system-${qemu_arch}"
79+
qemu_package="${qemu_system}"
5780
echo "error: Not supported yet"
5881

5982
exit 1
6083
;;
6184
esac
85+
qemu_file="/usr/bin/${qemu_system}"
86+
binfmt_file="/var/lib/binfmts/qemu-${qemu_arch}"
6287

6388
${sudo} apt-get update
6489
${sudo} apt install -y ${packages}
65-
if [ -e "/var/lib/binfmts/qemu-${qemu_arch}" ]; then
66-
${sudo} update-binfmts --enable "qemu-${qemu_arch}"
90+
91+
if [ "${debian_target_arch}" != "${debian_arch}" ] ; then
92+
echo "log: ${ARCH}: Support foreign arch: ${qemu_arch}"
93+
${sudo} apt-get update
94+
packages="binfmt-support qemu-user-static ${qemu_package}"
95+
${sudo} apt install -y ${packages}
96+
97+
if [ -e "${qemu_file}" ]; then
98+
${sudo} update-binfmts --enable "qemu-${qemu_arch}" \
99+
|| find /usr/libexec/qemu-binfmt/
100+
fi
67101
fi
68-
qemu_file="/usr/bin/${qemu_system}"
69102

70103
if [ ! -d "${rootfs_dir}" ] ; then
71104
${sudo} mkdir -pv "${rootfs_dir}"

0 commit comments

Comments
 (0)