From 4e0ade7c499329ef0e65a28e86644d5ad64c5df2 Mon Sep 17 00:00:00 2001 From: hqzing Date: Thu, 19 Nov 2026 20:55:55 +0800 Subject: [PATCH 1/2] gh-162319: Add OpenHarmony platform identifiers --- Doc/library/sys.rst | 1 + Lib/sysconfig/__init__.py | 2 ++ Lib/test/test_sysconfig.py | 19 +++++++++++++++++++ ...-11-20-17-34-37.gh-issue-162319.OHOS01.rst | 1 + Misc/platform_triplet.c | 11 +++++++++++ config.guess | 2 ++ config.sub | 4 ++-- configure | 13 ++++++++++++- configure.ac | 13 ++++++++++++- 9 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Build/2026-11-20-17-34-37.gh-issue-162319.OHOS01.rst diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst index c3485204fb8b4b9..af7f3639051ce0c 100644 --- a/Doc/library/sys.rst +++ b/Doc/library/sys.rst @@ -1703,6 +1703,7 @@ always available. Unless explicitly noted otherwise, all variables are read-only iOS ``'ios'`` Linux ``'linux'`` macOS ``'darwin'`` + OpenHarmony ``'ohos'`` Windows ``'win32'`` Windows/Cygwin ``'cygwin'`` WASI ``'wasi'`` diff --git a/Lib/sysconfig/__init__.py b/Lib/sysconfig/__init__.py index aeb0a904e9cdc84..d5c1d361224fe31 100644 --- a/Lib/sysconfig/__init__.py +++ b/Lib/sysconfig/__init__.py @@ -723,6 +723,8 @@ def get_platform(): "i686": "x86", "x86_64": "x86_64", }[machine] + elif sys.platform == "ohos": + return f"ohos-{machine}" elif osname == "linux": # At least on Linux/Intel, 'machine' is the processor -- # i386, etc. diff --git a/Lib/test/test_sysconfig.py b/Lib/test/test_sysconfig.py index af642e25ac8c3df..5567b2c97f45ac2 100644 --- a/Lib/test/test_sysconfig.py +++ b/Lib/test/test_sysconfig.py @@ -392,6 +392,14 @@ def test_get_platform(self): '#1 Tue Jan 9 20:35:43 UTC 2018', machine)) self.assertEqual(get_platform(), f'android-9-{abi}') + # OpenHarmony + os.name = 'posix' + sys.platform = 'ohos' + self._set_uname(('Linux', 'localhost', '5.10.43', '#1 SMP', 'aarch64')) + self.assertEqual(get_platform(), 'ohos-aarch64') + with patch.dict(os.environ, {'_PYTHON_HOST_PLATFORM': 'ohos-aarch64'}): + self.assertEqual(get_platform(), 'ohos-aarch64') + # XXX more platforms to tests here @unittest.skipIf(is_wasi, "Incompatible with WASI mapdir and OOT builds") @@ -614,6 +622,17 @@ def test_android_ext_suffix(self): }[machine] self.assertEndsWith(suffix, f"-{expected_triplet}.so") + @unittest.skipUnless(sys.platform == 'ohos', 'OpenHarmony-specific test') + def test_ohos_ext_suffix(self): + machine = platform.machine() + suffix = sysconfig.get_config_var('EXT_SUFFIX') + expected_triplet = { + 'aarch64': 'aarch64-linux-ohos', + 'arm': 'arm-linux-ohos', + 'x86_64': 'x86_64-linux-ohos', + }[machine] + self.assertEndsWith(suffix, f'-{expected_triplet}.so') + @unittest.skipUnless(sys.platform == 'darwin', 'OS X-specific test') def test_osx_ext_suffix(self): suffix = sysconfig.get_config_var('EXT_SUFFIX') diff --git a/Misc/NEWS.d/next/Build/2026-11-20-17-34-37.gh-issue-162319.OHOS01.rst b/Misc/NEWS.d/next/Build/2026-11-20-17-34-37.gh-issue-162319.OHOS01.rst new file mode 100644 index 000000000000000..23ca9eee28f6804 --- /dev/null +++ b/Misc/NEWS.d/next/Build/2026-11-20-17-34-37.gh-issue-162319.OHOS01.rst @@ -0,0 +1 @@ +Add OpenHarmony platform identifiers for the ``linux-ohos`` target triple. diff --git a/Misc/platform_triplet.c b/Misc/platform_triplet.c index 6c1863c943b3539..343fe8319191b75 100644 --- a/Misc/platform_triplet.c +++ b/Misc/platform_triplet.c @@ -26,6 +26,17 @@ PLATFORM_TRIPLET=arm-linux-androideabi # error unknown Android platform # endif +#elif defined(__OHOS__) +# if defined(__aarch64__) +PLATFORM_TRIPLET=aarch64-linux-ohos +# elif defined(__arm__) +PLATFORM_TRIPLET=arm-linux-ohos +# elif defined(__x86_64__) +PLATFORM_TRIPLET=x86_64-linux-ohos +# else +# error unknown OpenHarmony platform +# endif + #elif defined(__linux__) /* * BEGIN of Linux block diff --git a/config.guess b/config.guess index cdfc4392047ce38..4412302abe1bd11 100755 --- a/config.guess +++ b/config.guess @@ -157,6 +157,8 @@ Linux|GNU|GNU/*) cat <<-EOF > "$dummy.c" #if defined(__ANDROID__) LIBC=android + #elif defined(__OHOS__) + LIBC=ohos #else #include #if defined(__UCLIBC__) diff --git a/config.sub b/config.sub index 49febd56a37cc00..be22b630275d229 100755 --- a/config.sub +++ b/config.sub @@ -1769,7 +1769,7 @@ case $os in | onefs* | tirtos* | phoenix* | fuchsia* | redox* | bme* \ | midnightbsd* | amdhsa* | unleashed* | emscripten* | wasi* \ | nsk* | powerunix* | genode* | zvmoe* | qnx* | emx* | zephyr* \ - | fiwix* | mlibc* | cos* | mbr* | ironclad* ) + | fiwix* | mlibc* | cos* | mbr* | ironclad* | ohos* ) ;; # This one is extra strict with allowed versions sco3.2v2 | sco3.2v[4-9]* | sco5v6*) @@ -1830,7 +1830,7 @@ esac case $kernel-$os-$obj in linux-gnu*- | linux-android*- | linux-dietlibc*- | linux-llvm*- \ | linux-mlibc*- | linux-musl*- | linux-newlib*- \ - | linux-relibc*- | linux-uclibc*- ) + | linux-relibc*- | linux-uclibc*- | linux-ohos*- ) ;; uclinux-uclibc*- ) ;; diff --git a/configure b/configure index 0e93826d3494c6d..b5053fc2aa4b196 100755 --- a/configure +++ b/configure @@ -4191,6 +4191,9 @@ then *-*-linux-android*) ac_sys_system=Linux-android ;; + *-*-linux-ohos*) + ac_sys_system=Linux-ohos + ;; *-*-linux*) ac_sys_system=Linux ;; @@ -4231,7 +4234,14 @@ then esac ac_sys_release= else - ac_sys_system=`uname -s` + case "$host" in + *-*-linux-ohos*) + ac_sys_system=Linux-ohos + ;; + *) + ac_sys_system=`uname -s` + ;; + esac if test "$ac_sys_system" = "AIX"; then ac_sys_release=`uname -v` else @@ -4248,6 +4258,7 @@ then aix*) MACHDEP="aix";; freebsd*) MACHDEP="freebsd";; linux-android*) MACHDEP="android";; + linux-ohos*) MACHDEP="ohos";; linux*) MACHDEP="linux";; cygwin*) MACHDEP="cygwin";; darwin*) MACHDEP="darwin";; diff --git a/configure.ac b/configure.ac index 234e2fbc920822f..55d2332ebdc1557 100644 --- a/configure.ac +++ b/configure.ac @@ -336,6 +336,9 @@ then *-*-linux-android*) ac_sys_system=Linux-android ;; + *-*-linux-ohos*) + ac_sys_system=Linux-ohos + ;; *-*-linux*) ac_sys_system=Linux ;; @@ -376,7 +379,14 @@ then esac ac_sys_release= else - ac_sys_system=`uname -s` + case "$host" in + *-*-linux-ohos*) + ac_sys_system=Linux-ohos + ;; + *) + ac_sys_system=`uname -s` + ;; + esac if test "$ac_sys_system" = "AIX"; then ac_sys_release=`uname -v` else @@ -393,6 +403,7 @@ then aix*) MACHDEP="aix";; freebsd*) MACHDEP="freebsd";; linux-android*) MACHDEP="android";; + linux-ohos*) MACHDEP="ohos";; linux*) MACHDEP="linux";; cygwin*) MACHDEP="cygwin";; darwin*) MACHDEP="darwin";; From 7df32890266c4ebb967cf7629214d131b3ef439b Mon Sep 17 00:00:00 2001 From: hqzing Date: Sun, 22 Nov 2026 17:02:57 +0800 Subject: [PATCH 2/2] gh-162319: Add OpenHarmony API-aware platform tags and version query --- Doc/library/platform.rst | 18 +++++++++ Lib/platform.py | 29 ++++++++++++++ Lib/sysconfig/__init__.py | 5 ++- Lib/test/test_platform.py | 22 ++++++++++ Lib/test/test_sysconfig.py | 6 ++- ...-11-20-17-34-37.gh-issue-162319.OHOS01.rst | 2 +- Tools/build/check_capi_macros_ignored.txt | 1 + configure | 40 +++++++++++++++++++ configure.ac | 20 ++++++++++ pyconfig.h.in | 3 ++ 10 files changed, 141 insertions(+), 5 deletions(-) diff --git a/Doc/library/platform.rst b/Doc/library/platform.rst index f728a7b32e789f4..fcbf912e669bca0 100644 --- a/Doc/library/platform.rst +++ b/Doc/library/platform.rst @@ -348,6 +348,24 @@ Android platform .. versionadded:: 3.13 +OpenHarmony platform +-------------------- + +.. function:: ohos_ver(release="", api_level=0) + + Get OpenHarmony device information. Returns a :func:`~collections.namedtuple` + with the following attributes. Values which cannot be determined are set to + the defaults given as parameters. + + * ``release`` - OpenHarmony version, as a string + (e.g. ``"7.0.0.109"``). + + * ``api_level`` - OpenHarmony base API level of the running device, as an + integer (e.g. ``26``). To get the API level which Python was built against, + see ``sysconfig.get_config_var("OHOS_API_LEVEL")``. + + .. versionadded:: 3.16 + .. _platform-cli: Command-line usage diff --git a/Lib/platform.py b/Lib/platform.py index c87bf2fca37e031..d76f8300886983e 100644 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -583,6 +583,35 @@ def getprop(name, default): release, api_level, manufacturer, model, device, is_emulator) +OhosVer = collections.namedtuple("OhosVer", "release api_level") + +def ohos_ver(release="", api_level=0): + if sys.platform == "ohos": + try: + from ctypes import CDLL, c_char_p, c_int + from ctypes.util import wrap_dll_function + except ImportError: + pass + else: + deviceinfo = CDLL("libdeviceinfo_ndk.z.so") + + @wrap_dll_function(deviceinfo) + def OH_GetOSFullName() -> c_char_p: + pass + + @wrap_dll_function(deviceinfo) + def OH_GetSdkApiVersion() -> c_int: + pass + + os_full_name = OH_GetOSFullName() + if os_full_name is not None: + release = os_full_name.decode("UTF-8", "backslashreplace") + release = release.removeprefix("OpenHarmony-") + api_level = OH_GetSdkApiVersion() + + return OhosVer(release, api_level) + + ### System name aliasing def system_alias(system, release, version): diff --git a/Lib/sysconfig/__init__.py b/Lib/sysconfig/__init__.py index d5c1d361224fe31..8d582fa095a786b 100644 --- a/Lib/sysconfig/__init__.py +++ b/Lib/sysconfig/__init__.py @@ -723,8 +723,9 @@ def get_platform(): "i686": "x86", "x86_64": "x86_64", }[machine] - elif sys.platform == "ohos": - return f"ohos-{machine}" + elif osname == "ohos" or sys.platform == "ohos": + osname = "ohos" + release = get_config_var("OHOS_API_LEVEL") elif osname == "linux": # At least on Linux/Intel, 'machine' is the processor -- # i386, etc. diff --git a/Lib/test/test_platform.py b/Lib/test/test_platform.py index cf3ca9021eea3fb..2bf74bd0e507a4c 100644 --- a/Lib/test/test_platform.py +++ b/Lib/test/test_platform.py @@ -679,6 +679,28 @@ def test_android_ver(self): self.assertEqual(res.device, "delta") self.assertEqual(res.is_emulator, True) + def test_ohos_ver(self): + res = platform.ohos_ver() + self.assertIsInstance(res, tuple) + self.assertEqual(res, (res.release, res.api_level)) + + if sys.platform == "ohos": + self.assertIsInstance(res.release, str) + self.assertNotEqual(res.release, "") + self.assertFalse(res.release.startswith("OpenHarmony-")) + self.assertIsInstance(res.api_level, int) + self.assertGreater(res.api_level, 0) + + # When not running on OHOS, it should return the default values. + else: + self.assertEqual(res.release, "") + self.assertEqual(res.api_level, 0) + + # Default values may also be overridden using parameters. + res = platform.ohos_ver("7.0.0.109", 26) + self.assertEqual(res.release, "7.0.0.109") + self.assertEqual(res.api_level, 26) + @support.cpython_only def test__comparable_version(self): from platform import _comparable_version as V diff --git a/Lib/test/test_sysconfig.py b/Lib/test/test_sysconfig.py index 5567b2c97f45ac2..dcc8a2974049252 100644 --- a/Lib/test/test_sysconfig.py +++ b/Lib/test/test_sysconfig.py @@ -395,10 +395,12 @@ def test_get_platform(self): # OpenHarmony os.name = 'posix' sys.platform = 'ohos' + get_config_vars()['OHOS_API_LEVEL'] = 26 self._set_uname(('Linux', 'localhost', '5.10.43', '#1 SMP', 'aarch64')) - self.assertEqual(get_platform(), 'ohos-aarch64') + self.assertEqual(get_platform(), 'ohos-26-aarch64') + sys.platform = 'linux' with patch.dict(os.environ, {'_PYTHON_HOST_PLATFORM': 'ohos-aarch64'}): - self.assertEqual(get_platform(), 'ohos-aarch64') + self.assertEqual(get_platform(), 'ohos-26-aarch64') # XXX more platforms to tests here diff --git a/Misc/NEWS.d/next/Build/2026-11-20-17-34-37.gh-issue-162319.OHOS01.rst b/Misc/NEWS.d/next/Build/2026-11-20-17-34-37.gh-issue-162319.OHOS01.rst index 23ca9eee28f6804..080f43467d70d04 100644 --- a/Misc/NEWS.d/next/Build/2026-11-20-17-34-37.gh-issue-162319.OHOS01.rst +++ b/Misc/NEWS.d/next/Build/2026-11-20-17-34-37.gh-issue-162319.OHOS01.rst @@ -1 +1 @@ -Add OpenHarmony platform identifiers for the ``linux-ohos`` target triple. +Add OpenHarmony platform identifiers for the ``linux-ohos`` target triple, including API-aware platform tags and :func:`platform.ohos_ver`. diff --git a/Tools/build/check_capi_macros_ignored.txt b/Tools/build/check_capi_macros_ignored.txt index 154f1dbfca039c9..5663f98b2b15c2b 100644 --- a/Tools/build/check_capi_macros_ignored.txt +++ b/Tools/build/check_capi_macros_ignored.txt @@ -644,6 +644,7 @@ NCURSES_OPAQUE NOWAIT_LOCK NTDDI_VERSION NT_THREADS +OHOS_API_LEVEL PACKAGE_BUGREPORT PACKAGE_NAME PACKAGE_STRING diff --git a/configure b/configure index b5053fc2aa4b196..4e6d3804b7cc9a4 100755 --- a/configure +++ b/configure @@ -7793,6 +7793,46 @@ printf "%s\n" "not Android" >&6; } fi rm -f conftest.c conftest.out +case $ac_sys_system in #( + Linux-ohos) : + + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for the OpenHarmony API level" >&5 +printf %s "checking for the OpenHarmony API level... " >&6; } + OHOS_LIBC=`$CC -print-file-name=libc.so` + if test ! -f "$OHOS_LIBC" +then : + + as_fn_error $? "could not locate the OHOS libc through the compiler" "$LINENO" 5 + +fi + OHOS_SDK_NATIVE=`cd "$(dirname "$OHOS_LIBC")/../../../.." && pwd` + OH_UNI_PACKAGE_JSON="$OHOS_SDK_NATIVE/oh-uni-package.json" + if test ! -f "$OH_UNI_PACKAGE_JSON" +then : + + as_fn_error $? "OpenHarmony SDK metadata file oh-uni-package.json was not found" "$LINENO" 5 + +fi + OHOS_API_LEVEL=`awk -F '"' '$0 ~ /apiVersion/ { print $4; exit }' "$OH_UNI_PACKAGE_JSON"` + if test "$OHOS_API_LEVEL" -gt 0 2>/dev/null +then : + +else case e in #( + e) + as_fn_error $? "apiVersion in oh-uni-package.json must be a positive integer" "$LINENO" 5 + ;; +esac +fi + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $OHOS_API_LEVEL" >&5 +printf "%s\n" "$OHOS_API_LEVEL" >&6; } + +printf "%s\n" "#define OHOS_API_LEVEL $OHOS_API_LEVEL" >>confdefs.h + + ;; #( + *) : + ;; +esac + # Check for unsupported systems case $ac_sys_system/$ac_sys_release in #( atheos*|Linux*/1*) : diff --git a/configure.ac b/configure.ac index 55d2332ebdc1557..a75cdffe434a37f 100644 --- a/configure.ac +++ b/configure.ac @@ -1511,6 +1511,26 @@ else fi rm -f conftest.c conftest.out +AS_CASE([$ac_sys_system], [Linux-ohos], [ + AC_MSG_CHECKING([for the OpenHarmony API level]) + OHOS_LIBC=`$CC -print-file-name=libc.so` + AS_IF([test ! -f "$OHOS_LIBC"], [ + AC_MSG_ERROR([could not locate the OHOS libc through the compiler]) + ]) + OHOS_SDK_NATIVE=`cd "$(dirname "$OHOS_LIBC")/../../../.." && pwd` + OH_UNI_PACKAGE_JSON="$OHOS_SDK_NATIVE/oh-uni-package.json" + AS_IF([test ! -f "$OH_UNI_PACKAGE_JSON"], [ + AC_MSG_ERROR([OpenHarmony SDK metadata file oh-uni-package.json was not found]) + ]) + OHOS_API_LEVEL=`awk -F '"' '$0 ~ /apiVersion/ { print $4; exit }' "$OH_UNI_PACKAGE_JSON"` + AS_IF([test "$OHOS_API_LEVEL" -gt 0 2>/dev/null], [], [ + AC_MSG_ERROR([apiVersion in oh-uni-package.json must be a positive integer]) + ]) + AC_MSG_RESULT([$OHOS_API_LEVEL]) + AC_DEFINE_UNQUOTED([OHOS_API_LEVEL], [$OHOS_API_LEVEL], + [The OpenHarmony API level.]) +]) + # Check for unsupported systems AS_CASE([$ac_sys_system/$ac_sys_release], [atheos*|Linux*/1*], [ diff --git a/pyconfig.h.in b/pyconfig.h.in index 1d4f445a8b1fdaa..9a43de1ecb1866b 100644 --- a/pyconfig.h.in +++ b/pyconfig.h.in @@ -1677,6 +1677,9 @@ /* Define if mvwdelch in curses.h is an expression. */ #undef MVWDELCH_IS_EXPRESSION +/* The OpenHarmony API level. */ +#undef OHOS_API_LEVEL + /* Define to the address where bug reports for this package should be sent. */ #undef PACKAGE_BUGREPORT