Skip to content

Commit 342800b

Browse files
committed
Add support for OpenHarmony platform
1 parent 6798d1d commit 342800b

5 files changed

Lines changed: 151 additions & 0 deletions

File tree

nvm.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,6 +2253,18 @@ nvm_get_os() {
22532253
AIX\ *) NVM_OS=aix ;;
22542254
CYGWIN* | MSYS* | MINGW*) NVM_OS=win ;;
22552255
esac
2256+
2257+
# OpenHarmony can't be identified from `uname` alone: a device may run either
2258+
# a Linux kernel or a HarmonyOS kernel. Ask the userland, but only when
2259+
# `uname` has not already identified some other OS.
2260+
case "${NVM_OS-}" in
2261+
'' | linux)
2262+
if [ -x /system/bin/param ] && /system/bin/param get const.ohos.fullname </dev/null 2>/dev/null | nvm_grep -q '^OpenHarmony'; then
2263+
NVM_OS=openharmony
2264+
fi
2265+
;;
2266+
esac
2267+
22562268
nvm_echo "${NVM_OS-}"
22572269
}
22582270

test/fast/Unit tests/nvm_get_os

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#!/bin/sh
2+
3+
# Save the PATH as it was when the test started to restore it when it finishes
4+
ORIG_PATH="${PATH}"
5+
6+
cleanup() {
7+
# Restore the PATH as it was when the test started
8+
export PATH="${ORIG_PATH}"
9+
rm -rf "${TMP_DIR}"
10+
}
11+
12+
die() {
13+
cleanup
14+
echo "$@"
15+
exit 1
16+
}
17+
18+
: nvm.sh
19+
\. ../../../nvm.sh
20+
21+
# Directory where mocked binaries used by nvm_get_os for each OS/arch are located
22+
MOCKS_DIR="$(pwd)/../../mocks"
23+
24+
# Sets the PATH for these tests to include the symlinks to the mocked binaries
25+
export PATH=".:${PATH}"
26+
27+
TMP_DIR=$(mktemp -d)
28+
29+
# Setups mock binaries for a given OS and arch that mimic the output of
30+
# the real binaries used by nvm_get_os to guess the OS of a given system.
31+
setup_mock_os() {
32+
local OS
33+
OS=$1
34+
local ARCH
35+
ARCH=$2
36+
ln -sf "${MOCKS_DIR}/uname_${OS}_${ARCH}" ./uname
37+
}
38+
39+
# Cleans up the setup done by setup_mock_os.
40+
cleanup_mock_os() {
41+
rm -f ./uname
42+
}
43+
44+
# Runs nvm_get_os for OS $OS and arch $ARCH, and compares the expected
45+
# output $EXPECTED_OUTPUT with the actual output. Does nothing and exits
46+
# cleanly if they match, dies otherwise.
47+
run_test() {
48+
local EXPECTED
49+
EXPECTED=$1
50+
local OUTPUT
51+
OUTPUT="$(nvm_get_os)"
52+
[ "_${OUTPUT}" = "_${EXPECTED}" ] ||
53+
die "nvm_get_os produced \"${OUTPUT}\", expected \"${EXPECTED}\""
54+
}
55+
56+
# Pre-existing arms
57+
58+
setup_mock_os linux x86_64
59+
run_test "linux"
60+
cleanup_mock_os
61+
62+
setup_mock_os osx amd64
63+
run_test "darwin"
64+
cleanup_mock_os
65+
66+
setup_mock_os smartos amd64
67+
run_test "sunos"
68+
cleanup_mock_os
69+
70+
# HarmonyOS kernel without OpenHarmony userland
71+
72+
setup_mock_os harmonyos aarch64
73+
run_test ""
74+
cleanup_mock_os
75+
76+
# OpenHarmony detection via chroot
77+
78+
setup_chroot() {
79+
chroot_dir=$1
80+
param_mock=$2
81+
82+
# Directories
83+
mkdir -p "${chroot_dir}/etc"
84+
mkdir -p "${chroot_dir}/bin"
85+
mkdir -p "${chroot_dir}/usr/bin"
86+
mkdir -p "${chroot_dir}/lib64"
87+
mkdir -p "${chroot_dir}/dev"
88+
mkdir -p "${chroot_dir}/system/bin"
89+
90+
# Files and binaries
91+
cp ../../../nvm.sh "${chroot_dir}/"
92+
cp /bin/sh /usr/bin/dirname /usr/bin/uname /usr/bin/grep "${chroot_dir}/usr/bin/"
93+
ln -sf ../usr/bin/sh "${chroot_dir}/bin/sh"
94+
95+
# Fake OpenHarmony param binary
96+
cp "${param_mock}" "${chroot_dir}/system/bin/param"
97+
chmod +x "${chroot_dir}/system/bin/param"
98+
99+
# Libraries
100+
for binary in /bin/sh /usr/bin/dirname /usr/bin/uname /usr/bin/grep; do
101+
for lib in $(ldd $binary | awk '{print $3}' | grep "^/"); do
102+
dir=$(dirname "${lib}")
103+
mkdir -p "${chroot_dir}${dir}"
104+
cp "${lib}" "${chroot_dir}${dir}/"
105+
done
106+
done
107+
108+
# Dynamic linker
109+
cp /lib64/ld-linux-x86-64.so.2 "${chroot_dir}/lib64/"
110+
111+
# /dev/null
112+
sudo mknod "${chroot_dir}/dev/null" c 1 3
113+
}
114+
115+
CHROOT_WITH_PARAM="${TMP_DIR}/with_param"
116+
CHROOT_WITH_OTHER="${TMP_DIR}/with_other_param"
117+
118+
setup_chroot "${CHROOT_WITH_PARAM}" "${MOCKS_DIR}/param_openharmony"
119+
setup_chroot "${CHROOT_WITH_OTHER}" "${MOCKS_DIR}/param_other"
120+
121+
# Run tests in chroot environments
122+
OS_WITH_PARAM=$(sudo chroot "${CHROOT_WITH_PARAM}" /bin/sh -c ". ./nvm.sh && nvm_get_os")
123+
[ "${OS_WITH_PARAM}" = "openharmony" ] || die "Expected openharmony in chroot with OpenHarmony param but got ${OS_WITH_PARAM}"
124+
125+
OS_WITH_OTHER=$(sudo chroot "${CHROOT_WITH_OTHER}" /bin/sh -c ". ./nvm.sh && nvm_get_os")
126+
[ "${OS_WITH_OTHER}" != "openharmony" ] || die "Did not expect openharmony in chroot with non-OpenHarmony param but got ${OS_WITH_OTHER}"
127+
128+
cleanup

test/mocks/param_openharmony

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if [ "$#" -eq 2 ] && [ "$1" = "get" ] && [ "$2" = "const.ohos.fullname" ]; then
2+
echo "OpenHarmony-6.1.0.31"
3+
fi

test/mocks/param_other

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if [ "$#" -eq 2 ] && [ "$1" = "get" ] && [ "$2" = "const.ohos.fullname" ]; then
2+
echo "SomeOtherOS-1.2.3"
3+
fi

test/mocks/uname_harmonyos_aarch64

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if [ "_$1" = "_-m" ]; then
2+
echo "aarch64"
3+
else
4+
echo "HarmonyOS localhost HongMeng Kernel 1.12.0 #1 SMP Tue Jul 14 09:40:51 UTC 2026 aarch64 Toybox"
5+
fi

0 commit comments

Comments
 (0)