Skip to content

Commit d860fcb

Browse files
committed
implement support for building the image on BSD
Some BSD-based operating systems support Linux Docker images by implementing a `docker` command that has a backend powered by virtualization and an internal virtualized Linux kernel. The result is that all code running inside the container is only exposed to a Linux kernel and is unaware of BSD, but all code running outside the container is directly exposed to the BSD-based environment BSD-based operating systems used to develop this: - MacOS - FreeBSD - OpenBSD It's often hard to find where exactly the `gtar`, `gar`, and `bash`-version-4+ commands are on BSD-based operating systems, so if you see errors related to those, try to find and install the dependencies for your OS and then add them to `$PATH`.
1 parent 0428681 commit d860fcb

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

generate.sh

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ else
1616
SUDO=""
1717
fi
1818

19+
case "$(uname -s)" in
20+
Linux*) BUILD_MACHINE=Linux;;
21+
*BSD|Darwin*) BUILD_MACHINE=BSD;;
22+
*) BUILD_MACHINE="UNKNOWN:$(uname -s)"
23+
esac
24+
echo "Operating System used to build termux-docker image: ${BUILD_MACHINE}"
25+
1926
# This determines the architecture of the image being built,
2027
# but should also be an architecture that is compatible with the computer
2128
# running this script, so that the RUN step in the Dockerfile
@@ -25,9 +32,9 @@ if [ -z "${TERMUX_ARCH-}" ]; then
2532
fi
2633

2734
case "$TERMUX_ARCH" in
28-
aarch64) TERMUX_ARCH="aarch64" PLATFORM_TAG="linux/arm64" ;;
29-
armv7l|armv8l|arm) TERMUX_ARCH="arm" PLATFORM_TAG="linux/arm/v7" ;;
30-
x86_64) TERMUX_ARCH="x86_64" PLATFORM_TAG="linux/amd64" ;;
35+
aarch64|arm64) TERMUX_ARCH="aarch64" PLATFORM_TAG="linux/arm64" ;;
36+
arm*) TERMUX_ARCH="arm" PLATFORM_TAG="linux/arm/v7" ;;
37+
x86_64|amd64) TERMUX_ARCH="x86_64" PLATFORM_TAG="linux/amd64" ;;
3138
i686) TERMUX_ARCH="i686" PLATFORM_TAG="linux/386" ;;
3239
*)
3340
echo "error: ${TERMUX_ARCH} is not a valid architecture!"
@@ -77,6 +84,7 @@ esac
7784
# but aosp-utils, aosp-libs and libandroid-stub will get automatically updated
7885
# by user-invoked instances of 'pkg upgrade' since they are in the main repository.
7986
TERMUX_DOCKER__DEPENDS="aosp-utils, libandroid-stub, dnsmasq"
87+
TERMUX_DOCKER__BUILD_DEPENDS="ar, awk, curl, docker, grep, gzip, find, sed, tar, xargs, xz, unzip, jq"
8088
TERMUX_APP__PACKAGE_NAME="com.termux"
8189
TERMUX_APP__DATA_DIR="/data/data/$TERMUX_APP__PACKAGE_NAME"
8290
TERMUX__PREFIX_SUBDIR="usr"
@@ -90,15 +98,27 @@ TERMUX_DOCKER__ROOTFS="$(pwd)/termux-docker-rootfs"
9098
TERMUX_DOCKER__TMPDIR="$(mktemp -d "/tmp/termux-docker-tmp.XXXXXXXX")"
9199
TERMUX_DOCKER__PKGDIR="${TERMUX_DOCKER__TMPDIR}/packages-${TERMUX_ARCH}"
92100
unset TERMUX_DOCKER__DEPENDS_ARRAY
101+
unset TERMUX_DOCKER__BUILD_DEPENDS_ARRAY
93102
IFS=, read -a TERMUX_DOCKER__DEPENDS_ARRAY <<< "${TERMUX_DOCKER__DEPENDS// /}"
103+
IFS=, read -a TERMUX_DOCKER__BUILD_DEPENDS_ARRAY <<< "${TERMUX_DOCKER__BUILD_DEPENDS// /}"
94104
unset PACKAGE_METADATA
95105
unset PACKAGE_URLS
96106
declare -A PACKAGE_METADATA
97107
declare -A PACKAGE_URLS
108+
AR=ar
109+
TAR=tar
98110

99111
# Check for some important utilities that may not be available for
100112
# some reason.
101-
for cmd in ar awk curl grep gzip find sed tar xargs xz zip jq; do
113+
for cmd in "${TERMUX_DOCKER__BUILD_DEPENDS_ARRAY[@]}"; do
114+
if [ "$cmd" = "ar" ] && [ -n "$(command -v gar)" ]; then
115+
AR=gar
116+
continue
117+
fi
118+
if [ "$cmd" = "tar" ] && [ -n "$(command -v gtar)" ]; then
119+
TAR=gtar
120+
continue
121+
fi
102122
if [ -z "$(command -v $cmd)" ]; then
103123
echo "[!] Utility '$cmd' is not available in PATH."
104124
exit 1
@@ -191,7 +211,7 @@ pull_package_apt() {
191211

192212
echo "[*] Extracting '$package_name'..."
193213
(cd "$package_tmpdir"
194-
ar x package.deb
214+
$AR x package.deb
195215

196216
# data.tar may have extension different from .xz
197217
if [ -f "./data.tar.xz" ]; then
@@ -214,17 +234,17 @@ pull_package_apt() {
214234
fi
215235

216236
# Extract files.
217-
tar xf "$data_archive" -C "$TERMUX_DOCKER__ROOTFS"
237+
$TAR xf "$data_archive" -C "$TERMUX_DOCKER__ROOTFS"
218238

219239
# Register extracted files.
220-
tar tf "$data_archive" | sed -E -e 's@^\./@/@' -e 's@^/$@/.@' -e 's@^([^./])@/\1@' > "${TERMUX_DOCKER__ROOTFS}${TERMUX__PREFIX}/var/lib/dpkg/info/${package_name}.list"
240+
$TAR tf "$data_archive" | sed -E -e 's@^\./@/@' -e 's@^/$@/.@' -e 's@^([^./])@/\1@' > "${TERMUX_DOCKER__ROOTFS}${TERMUX__PREFIX}/var/lib/dpkg/info/${package_name}.list"
221241

222242
# Generate checksums (md5).
223-
tar xf "$data_archive"
243+
$TAR xf "$data_archive"
224244
find data -type f -print0 | xargs -0 -r md5sum | sed 's@^\.$@@g' > "${TERMUX_DOCKER__ROOTFS}${TERMUX__PREFIX}/var/lib/dpkg/info/${package_name}.md5sums"
225245

226246
# Extract metadata.
227-
tar xf "$control_archive"
247+
$TAR xf "$control_archive"
228248
{
229249
cat control
230250
echo "Status: install ok installed"
@@ -300,7 +320,7 @@ pull_package_pacman() {
300320
mkdir -p "${TERMUX_DOCKER__ROOTFS}${TERMUX__PREFIX}/var/lib/pacman/local/${package_desc}"
301321
{
302322
echo "%FILES%"
303-
tar xvf package.pkg.tar.xz -C "$TERMUX_DOCKER__ROOTFS" .INSTALL .MTREE data 2> /dev/null | grep '^data/' || true
323+
$TAR xvf package.pkg.tar.xz -C "$TERMUX_DOCKER__ROOTFS" .INSTALL .MTREE data 2> /dev/null | grep '^data/' || true
304324
} >> "${TERMUX_DOCKER__ROOTFS}${TERMUX__PREFIX}/var/lib/pacman/local/${package_desc}/files"
305325
mv "${TERMUX_DOCKER__ROOTFS}/.MTREE" "${TERMUX_DOCKER__ROOTFS}${TERMUX__PREFIX}/var/lib/pacman/local/${package_desc}/mtree"
306326
if [ -f "${TERMUX_DOCKER__ROOTFS}/.INSTALL" ]; then
@@ -402,14 +422,22 @@ if [ "${TERMUX_PACKAGE_MANAGER}" = "apt" ]; then
402422
-type f -exec \
403423
chmod 700 "{}" \;
404424
fi
425+
case "${BUILD_MACHINE}" in
426+
BSD)
427+
find_executable_arg="-perm +111"
428+
;;
429+
*)
430+
find_executable_arg="-executable"
431+
;;
432+
esac
405433
find -L "${TERMUX_DOCKER__ROOTFS}/system" \
406-
-type f -executable -exec \
434+
-type f $find_executable_arg -exec \
407435
chmod 755 "{}" \;
408436
find -L "${TERMUX_DOCKER__ROOTFS}/system" \
409-
-type f ! -executable -exec \
437+
-type f ! $find_executable_arg -exec \
410438
chmod 644 "{}" \;
411439

412-
echo "[*] Rootfs generation complete. Building Docker image..."
440+
echo "[*] Rootfs generation complete. Building termux-docker image..."
413441
$SUDO $OCI ${OCI_ARG} \
414442
--no-cache \
415443
-t "${TERMUX_DOCKER__IMAGE_NAME}:${TERMUX_ARCH}" \

0 commit comments

Comments
 (0)