Skip to content

Commit 347e9ef

Browse files
committed
feat(build): Add build-rootfs.sh to build for foreign arch
This script can be used to build and run package without CI Output will be: ./build/*-*${arch}.deb It has been tested on debian-12. This change has been adapted from version I made in other projects (see related link). Also, I have adjusted the licence to comply current OSS policy, as it is not embedded code, it can be relaxed from MSLA to ZLIB. Origin: #11 Relate-to: SiliconLabs/zipgateway#4 Relate-to: SiliconLabsSoftware/z-wave-protocol-controller#5 Signed-off-by: Philippe Coval <philippe.coval@silabs.com>
1 parent 031b248 commit 347e9ef

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

extra/build/build-rootfs.sh

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#! /usr/bin/env bash
2+
# -*- mode: Bash; tab-width: 2; indent-tabs-mode: nil; coding: utf-8 -*-
3+
# vim:shiftwidth=4:softtabstop=4:tabstop=4:
4+
# SPDX-License-Identifier: Zlib
5+
# SPDX-License-URL: https://spdx.org/licenses/Zlib
6+
# SPDX-FileCopyrightText: Silicon Laboratories Inc. https://www.silabs.com
7+
8+
set -e
9+
set -x
10+
11+
cat<<EOF
12+
Usage:
13+
14+
ARCH=arm64 ./scripts/build-rootfs.sh
15+
EOF
16+
17+
project="z-wave-nvm-migration-tool"
18+
debian_suite="${debian_suite:=bookworm}"
19+
debian_arch=$(dpkg --print-architecture)
20+
21+
# Can be overloaded from env eg: ARCH=arm64"
22+
target_debian_arch=${ARCH:="${debian_arch}"}
23+
24+
debian_mirror_url="http://deb.debian.org/debian"
25+
sudo="sudo"
26+
machine="${project}-${debian_suite}-${target_debian_arch}-rootfs"
27+
rootfs_dir="/var/tmp/var/lib/machines/${machine}"
28+
MAKE="/usr/bin/make"
29+
CURDIR="$PWD"
30+
chroot="systemd-nspawn"
31+
packages="debootstrap \
32+
debian-archive-keyring \
33+
systemd-container \
34+
time \
35+
"
36+
37+
# Guess values to be adjusted
38+
CMAKE_SYSTEM_PROCESSOR="$ARCH"
39+
qemu_arch="${ARCH}"
40+
qemu_system="qemu-system-${qemu_arch}"
41+
qemu_package="${qemu_system}"
42+
43+
case $target_debian_arch in
44+
amd64)
45+
qemu_system="qemu-system-${CMAKE_SYSTEM_PROCESSOR}"
46+
export CMAKE_SYSTEM_PROCESSOR="x86_64"
47+
qemu_package="qemu-system-x86"
48+
;;
49+
50+
arm64)
51+
qemu_arch="aarch64"
52+
qemu_system="qemu-system-${qemu_arch}"
53+
export CMAKE_SYSTEM_PROCESSOR="${qemu_arch}"
54+
qemu_package="qemu-system-arm"
55+
;;
56+
57+
armhf)
58+
debian_arch="armhf"
59+
qemu_arch="arm"
60+
qemu_system="qemu-system-${qemu_arch}"
61+
export CMAKE_SYSTEM_PROCESSOR="armv7l"
62+
qemu_package="qemu-system-arm"
63+
64+
# Workaround: https://github.com/armbian/build/issues/5330
65+
binfmt_file="/var/lib/binfmts/qemu-${qemu_arch}"
66+
[ -e "$binfmt_file" ] || cat<<EOF \
67+
| { sudo mkdir -p /var/lib/binfmts && sudo tee "$binfmt_file" ; }
68+
package qemu-user-static
69+
interpreter /usr/libexec/qemu-binfmt/arm-binfmt-P
70+
magic \x7f\x45\x4c\x46\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00
71+
offset 0
72+
mask \xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff
73+
credentials yes
74+
fix_binary no
75+
preserve yes
76+
EOF
77+
;;
78+
i386)
79+
MAKE="linux32 ${MAKE}"
80+
;;
81+
*)
82+
echo "warning: Not supported yet"
83+
;;
84+
esac
85+
qemu_file="/usr/bin/${qemu_system}"
86+
binfmt_file="/var/lib/binfmts/qemu-${qemu_arch}"
87+
88+
${sudo} apt-get update
89+
${sudo} apt install -y ${packages}
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
101+
fi
102+
103+
if [ ! -d "${rootfs_dir}" ] ; then
104+
${sudo} mkdir -pv "${rootfs_dir}"
105+
time ${sudo} debootstrap \
106+
--arch="${target_debian_arch}" \
107+
"${debian_suite}" "${rootfs_dir}" "${debian_mirror_url}"
108+
${sudo} chmod -v u+rX "${rootfs_dir}"
109+
fi
110+
111+
### Environement to pass
112+
113+
env_vars_options=""
114+
for i in $env_vars; do
115+
env_vars_options="$env_vars_options --setenv=$i"
116+
done
117+
118+
### Workarounds/Optimizations
119+
120+
case ${chroot} in
121+
"systemd-nspawn")
122+
rootfs_shell="${sudo} systemd-nspawn \
123+
--directory="${rootfs_dir}" \
124+
--machine="${machine}" \
125+
--bind="${CURDIR}:${CURDIR}" \
126+
$env_vars_options
127+
"
128+
if [ -e "${qemu_file}" ] ; then
129+
rootfs_shell="$rootfs_shell --bind ${qemu_file}"
130+
fi
131+
;;
132+
*)
133+
rootfs_shell="${sudo} chroot ${rootfs_dir}"
134+
l="dev dev/pts sys proc"
135+
for t in $l ; do
136+
$sudo mkdir -p "${rootfs_dir}/$t"
137+
$sudo mount --bind "/$t" "${rootfs_dir}/$t"
138+
done
139+
;;
140+
esac
141+
142+
${rootfs_shell} \
143+
apt-get install -y -- make sudo util-linux git
144+
145+
# echo "log: To prevent 'fatal: detected dubious ownership in repository'"
146+
${rootfs_shell} \
147+
git config --global --add safe.directory "${CURDIR}"
148+
149+
args="help setup default"
150+
[ "$1" = "" ] || args="$@"
151+
152+
${rootfs_shell} \
153+
${MAKE} \
154+
--directory="${CURDIR}" \
155+
--file="${CURDIR}/helper.mk" \
156+
USER="${USER}" \
157+
${env_vars} \
158+
-- \
159+
${args} \
160+
target_debian_arch="${target_debian_arch}" \
161+
CMAKE_SYSTEM_PROCESSOR="${CMAKE_SYSTEM_PROCESSOR}" \
162+
# EoL
163+
164+
echo "sudo du -hs -- '/var/tmp/var/lib/machines/${machine}' # can be removed now"

0 commit comments

Comments
 (0)