[New] Add support for OpenHarmony platform

also: `nvm_get_os`: set `NVM_DIR` in the OpenHarmony chroots, and skip them without a glibc x86-64 layout

`sudo` resets the environment, and `nvm.sh` returns early when it cannot detect `NVM_DIR`,
so in the chroots `nvm_get_os` was undefined and every check got an empty result.
The chroot setup needs `ldd` and `/lib64/ld-linux-x86-64.so.2`, which macOS does not have.

Co-authored-by: hqzing <hqzing@outlook.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
This commit is contained in:
hqzingandJordan Harband authored and Jordan Harband committed 2026-08-18 17:23:11 +08:00
1 parent 0054ed6022
commit e8144d4194
5 files changed
+125

No files matched your search

+11
View File
@@ -3656,6 +3656,17 @@ nvm_get_os() {
esac
fi
# OpenHarmony can't be identified from `uname` alone: a device may run either
# a Linux kernel or a HarmonyOS kernel. Ask the userland, but only when neither
# `/etc/os-release` nor `uname` has identified some other OS.
case "${NVM_OS-}" in
'' | linux)
if [ -x /system/bin/param ] && /system/bin/param get const.ohos.fullname </dev/null 2>/dev/null | nvm_grep -q '^OpenHarmony'; then
NVM_OS=openharmony
fi
;;
esac
nvm_echo "${NVM_OS-}"
}
+103
View File
@@ -146,4 +146,107 @@ assert_uname_os 'FreeBSD host 14.0-STABLE' "${OS_RELEASE}" 'freebsd'
printf 'NAME=Helios\nID=helios\n' > "${OS_RELEASE}"
assert_uname_os 'SunOS host 5.11' "${OS_RELEASE}" 'sunos'
# a HarmonyOS kernel without the OpenHarmony userland is not an OS `nvm` knows
assert_uname_os 'HarmonyOS localhost HongMeng Kernel 1.12.0 #1 SMP Tue Jul 14 09:40:51 UTC 2026 aarch64 Toybox' "${MISSING_OS_RELEASE}" ''
# openharmony on aarch64 composes the slug a mirror has to serve; this runs on every host,
# and pins that `nvm_get_arch` gives `arm64`, not Alpine's `arm64-musl`
NVM_MOCK_UNAME=aarch64
export NVM_MOCK_UNAME
SLUG="$(nvm_get_os() { nvm_echo openharmony; }; PATH="${MOCK_BIN}:${PATH}" nvm_get_download_slug node binary v22.22.0)"
unset NVM_MOCK_UNAME
[ "${SLUG}" = 'node-v22.22.0-openharmony-arm64' ] || die "nvm_get_download_slug produced \"${SLUG}\", expected \"node-v22.22.0-openharmony-arm64\""
# OpenHarmony detection via chroot
MOCKS_DIR="$(pwd)/../../mocks"
setup_chroot() {
chroot_dir=$1
param_mock=$2
uname_mock=$3
# Directories
mkdir -p "${chroot_dir}/etc"
mkdir -p "${chroot_dir}/bin"
mkdir -p "${chroot_dir}/usr/bin"
mkdir -p "${chroot_dir}/lib64"
mkdir -p "${chroot_dir}/dev"
mkdir -p "${chroot_dir}/system/bin"
# Files and binaries
cp ../../../nvm.sh "${chroot_dir}/"
cp /bin/sh /usr/bin/dirname /usr/bin/grep "${chroot_dir}/usr/bin/"
cp "${uname_mock}" "${chroot_dir}/usr/bin/uname"
chmod +x "${chroot_dir}/usr/bin/uname"
ln -sf ../usr/bin/sh "${chroot_dir}/bin/sh"
# Fake OpenHarmony param binary
cp "${param_mock}" "${chroot_dir}/system/bin/param"
chmod +x "${chroot_dir}/system/bin/param"
# Libraries
for binary in /bin/sh /usr/bin/dirname /usr/bin/uname /usr/bin/grep; do
for lib in $(ldd $binary | awk '{print $3}' | grep "^/"); do
dir=$(dirname "${lib}")
mkdir -p "${chroot_dir}${dir}"
cp "${lib}" "${chroot_dir}${dir}/"
done
done
# Dynamic linker
cp /lib64/ld-linux-x86-64.so.2 "${chroot_dir}/lib64/"
# /dev/null
sudo mknod "${chroot_dir}/dev/null" c 1 3
}
# The chroot fixtures assume a glibc x86-64 layout (`ldd`, a fixed dynamic-linker path
# under /lib64), which macOS and musl Alpine do not have; skip the chroot checks there.
if [ -f "/etc/alpine-release" ] || ! nvm_has ldd || [ ! -e /lib64/ld-linux-x86-64.so.2 ]; then
echo "no glibc x86-64 layout; skipping chroot OpenHarmony checks"
else
CHROOT_UNAME_LINUX_PARAM_OPENHARMONY="${TEST_DIR}/uname_linux_param_openharmony"
CHROOT_UNAME_LINUX_PARAM_OTHER="${TEST_DIR}/uname_linux_param_other"
CHROOT_UNAME_HARMONYOS_PARAM_OPENHARMONY="${TEST_DIR}/uname_harmonyos_param_openharmony"
CHROOT_UNAME_HARMONYOS_PARAM_OTHER="${TEST_DIR}/uname_harmonyos_param_other"
CHROOT_UNAME_OSX_PARAM_OPENHARMONY="${TEST_DIR}/uname_osx_param_openharmony"
setup_chroot "${CHROOT_UNAME_LINUX_PARAM_OPENHARMONY}" "${MOCKS_DIR}/param_openharmony" "${MOCKS_DIR}/uname_linux_aarch64"
setup_chroot "${CHROOT_UNAME_LINUX_PARAM_OTHER}" "${MOCKS_DIR}/param_other" "${MOCKS_DIR}/uname_linux_aarch64"
setup_chroot "${CHROOT_UNAME_HARMONYOS_PARAM_OPENHARMONY}" "${MOCKS_DIR}/param_openharmony" "${MOCKS_DIR}/uname_harmonyos_aarch64"
setup_chroot "${CHROOT_UNAME_HARMONYOS_PARAM_OTHER}" "${MOCKS_DIR}/param_other" "${MOCKS_DIR}/uname_harmonyos_aarch64"
setup_chroot "${CHROOT_UNAME_OSX_PARAM_OPENHARMONY}" "${MOCKS_DIR}/param_openharmony" "${MOCKS_DIR}/uname_osx_amd64"
# Linux kernel with an OpenHarmony param -> openharmony
OS_UNAME_LINUX_PARAM_OPENHARMONY=$(sudo chroot "${CHROOT_UNAME_LINUX_PARAM_OPENHARMONY}" /bin/sh -c "NVM_DIR=/; export NVM_DIR; . ./nvm.sh && nvm_get_os")
if [ "${OS_UNAME_LINUX_PARAM_OPENHARMONY}" != "openharmony" ]; then
die "nvm_get_os produced \"${OS_UNAME_LINUX_PARAM_OPENHARMONY}\", expected \"openharmony\""
fi
# Linux kernel with an unrelated param stays linux
OS_UNAME_LINUX_PARAM_OTHER=$(sudo chroot "${CHROOT_UNAME_LINUX_PARAM_OTHER}" /bin/sh -c "NVM_DIR=/; export NVM_DIR; . ./nvm.sh && nvm_get_os")
if [ "${OS_UNAME_LINUX_PARAM_OTHER}" != "linux" ]; then
die "nvm_get_os produced \"${OS_UNAME_LINUX_PARAM_OTHER}\", expected \"linux\""
fi
# HarmonyOS kernel with an OpenHarmony param -> openharmony
OS_UNAME_HARMONYOS_PARAM_OPENHARMONY=$(sudo chroot "${CHROOT_UNAME_HARMONYOS_PARAM_OPENHARMONY}" /bin/sh -c "NVM_DIR=/; export NVM_DIR; . ./nvm.sh && nvm_get_os")
if [ "${OS_UNAME_HARMONYOS_PARAM_OPENHARMONY}" != "openharmony" ]; then
die "nvm_get_os produced \"${OS_UNAME_HARMONYOS_PARAM_OPENHARMONY}\", expected \"openharmony\""
fi
# HarmonyOS kernel with an unrelated param stays empty
OS_UNAME_HARMONYOS_PARAM_OTHER=$(sudo chroot "${CHROOT_UNAME_HARMONYOS_PARAM_OTHER}" /bin/sh -c "NVM_DIR=/; export NVM_DIR; . ./nvm.sh && nvm_get_os")
if [ "_${OS_UNAME_HARMONYOS_PARAM_OTHER}" != "_" ]; then
die "nvm_get_os produced \"${OS_UNAME_HARMONYOS_PARAM_OTHER}\", expected \"\""
fi
# an OS that `uname` already identified is never re-probed
OS_UNAME_OSX_PARAM_OPENHARMONY=$(sudo chroot "${CHROOT_UNAME_OSX_PARAM_OPENHARMONY}" /bin/sh -c "NVM_DIR=/; export NVM_DIR; . ./nvm.sh && nvm_get_os")
if [ "${OS_UNAME_OSX_PARAM_OPENHARMONY}" != "darwin" ]; then
die "nvm_get_os produced \"${OS_UNAME_OSX_PARAM_OPENHARMONY}\", expected \"darwin\""
fi
fi
cleanup
+3
View File
@@ -0,0 +1,3 @@
if [ "$#" -eq 2 ] && [ "$1" = "get" ] && [ "$2" = "const.ohos.fullname" ]; then
echo "OpenHarmony-6.1.0.31"
fi
+3
View File
@@ -0,0 +1,3 @@
if [ "$#" -eq 2 ] && [ "$1" = "get" ] && [ "$2" = "const.ohos.fullname" ]; then
echo "NotOpenHarmony-1.2.3"
fi
+5
View File
@@ -0,0 +1,5 @@
if [ "_$1" = "_-m" ]; then
echo "aarch64"
else
echo "HarmonyOS localhost HongMeng Kernel 1.12.0 #1 SMP Tue Jul 14 09:40:51 UTC 2026 aarch64 Toybox"
fi