这是indexloc提供的服务,不要输入任何密码
Skip to content

fix(main/zig): check the Kernel version and stop if unsupported at install #25126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/zig/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ TERMUX_PKG_LICENSE="MIT"
TERMUX_PKG_LICENSE_FILE="zig/LICENSE"
TERMUX_PKG_MAINTAINER="@termux"
TERMUX_PKG_VERSION="0.14.1"
TERMUX_PKG_REVISION=1
TERMUX_PKG_SRCURL=https://github.com/ziglang/zig/releases/download/${TERMUX_PKG_VERSION}/zig-bootstrap-${TERMUX_PKG_VERSION}.tar.xz
TERMUX_PKG_SHA256=89b2fce50bfbb1eee29c382193d22c6eb0c7da3a96b5ba6d05e0af2945b3ca3d
TERMUX_PKG_BUILD_IN_SRC=true
Expand Down Expand Up @@ -44,3 +45,17 @@ termux_step_post_massage() {
"$TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX_CLASSICAL/bin/zig" init
)
}

termux_step_create_debscripts() {
# Minimum supported kernel version for this version of Zig.
# Zig pegs this to the oldest supported Debian LTS build
# https://github.com/ziglang/zig/blob/0.14.1/lib/std/Target.zig#L454-L469
# https://github.com/ziglang/zig/commit/0d4d8dfc1583138a59147f1aab5eec803a23adb1
# https://github.com/ziglang/zig/issues/20423
local ZIG_MINIMUM="4.19.0"

sed -e "s|@ZIG_MINIMUM@|$ZIG_MINIMUM|g" \
-e "s|@TERMUX_PKG_VERSION@|$TERMUX_PKG_VERSION|g" \
-e "s|@TERMUX_PACKAGE_FORMAT@|$TERMUX_PACKAGE_FORMAT|g" \
"$TERMUX_PKG_BUILDER_DIR/preinst.sh.in" > ./preinst
}
44 changes: 44 additions & 0 deletions packages/zig/preinst.sh.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!@TERMUX_PREFIX@/bin/sh
# shellcheck shell=sh

echo "Checking that your device meets the"
echo "minimum kernel version for Zig @TERMUX_PKG_VERSION@"
echo

is_supported() {
_KERNEL_VERSION="$1"
_MINIMUM_VERSION="$2"
# shellcheck disable=SC2194 # We replace this in the build script
case "@TERMUX_PACKAGE_FORMAT@" in
apt)
if dpkg --compare-versions "$_KERNEL_VERSION" lt "$_MINIMUM_VERSION"; then
return 1
fi
;;
pacman)
if [ "$(vercmp "$_KERNEL_VERSION" "$_MINIMUM_VERSION")" != 1 ]; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will return 0 if kernel version is 3.16.0, should probably check if it is -1 instead

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you're right I flipped the condition 3.16.0 would return 0 which is fine.

return 1
fi
;;
esac
return 0
}

KERNEL_VERSION="$(uname -r)"
if ! is_supported "$KERNEL_VERSION" "@ZIG_MINIMUM@"; then
echo "Your device's kernel version is too old for Zig @TERMUX_PKG_VERSION@"
if is_supported "$KERNEL_VERSION" "3.16.0"; then
echo "Your device does however support Zig 0.13.0 or earlier."
# uncomment these if/when we move old Zig versions to the TUR.
# echo "You can install it from the TUR."
# echo ""
# echo "pkg up"
# echo "pkg i tur-repo"
# echo "pkg i zig-0.13"
else
echo "Your device does not support any version of Zig"
echo "There's nothing we can do to make it supported."
fi
exit 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failing in the preinst script is not a good idea, users then manually have to clean up by removing zig. It would be better to have the kernel version check in the actual zig binary.

Next best option would be to exit without an error here even if kernel version is not supported by zig, the error/warning is then printed and users might understand why the program does not work

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the package cannot function on the device it is being installed on, should that package not stop its own installation?

If stopping meant that it uninstalled itself again and cleaned everything up, then that would work. When preinst script fails it leaves apt unusable though until user manually fixes it, and I don't think that is a situation we should ever create on purpose

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep that's my mistake.
We can probably throw up a read -t 60 "press any key to continue" style warning.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nvm -t is a bash addition to read

fi
exit 0