diff --git a/Doc/library/platform.rst b/Doc/library/platform.rst index f728a7b32e789f..fcbf912e669bca 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/Doc/library/sys.rst b/Doc/library/sys.rst index c3485204fb8b4b..af7f3639051ce0 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/platform.py b/Lib/platform.py index c87bf2fca37e03..d76f8300886983 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 aeb0a904e9cdc8..8d582fa095a786 100644 --- a/Lib/sysconfig/__init__.py +++ b/Lib/sysconfig/__init__.py @@ -723,6 +723,9 @@ def get_platform(): "i686": "x86", "x86_64": "x86_64", }[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 cf3ca9021eea3f..2bf74bd0e507a4 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 af642e25ac8c3d..dcc8a297404925 100644 --- a/Lib/test/test_sysconfig.py +++ b/Lib/test/test_sysconfig.py @@ -392,6 +392,16 @@ 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' + get_config_vars()['OHOS_API_LEVEL'] = 26 + self._set_uname(('Linux', 'localhost', '5.10.43', '#1 SMP', '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-26-aarch64') + # XXX more platforms to tests here @unittest.skipIf(is_wasi, "Incompatible with WASI mapdir and OOT builds") @@ -614,6 +624,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 00000000000000..080f43467d70d0 --- /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, including API-aware platform tags and :func:`platform.ohos_ver`. diff --git a/Misc/platform_triplet.c b/Misc/platform_triplet.c index 6c1863c943b353..343fe8319191b7 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/Tools/build/check_capi_macros_ignored.txt b/Tools/build/check_capi_macros_ignored.txt index 154f1dbfca039c..5663f98b2b15c2 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/config.guess b/config.guess index cdfc4392047ce3..4412302abe1bd1 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 49febd56a37cc0..be22b630275d22 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 b33ea2fb0c9bce..b6de0a6d5902e8 100755 --- a/configure +++ b/configure @@ -4196,6 +4196,9 @@ then *-*-linux-android*) ac_sys_system=Linux-android ;; + *-*-linux-ohos*) + ac_sys_system=Linux-ohos + ;; *-*-linux*) ac_sys_system=Linux ;; @@ -4236,7 +4239,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 @@ -4253,6 +4263,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";; @@ -7787,6 +7798,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 b0368ae4b94e95..893acf67d49b21 100644 --- a/configure.ac +++ b/configure.ac @@ -340,6 +340,9 @@ then *-*-linux-android*) ac_sys_system=Linux-android ;; + *-*-linux-ohos*) + ac_sys_system=Linux-ohos + ;; *-*-linux*) ac_sys_system=Linux ;; @@ -380,7 +383,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 @@ -397,6 +407,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";; @@ -1504,6 +1515,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 1d4f445a8b1fda..9a43de1ecb1866 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