Skip to content
Open
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
18 changes: 18 additions & 0 deletions Doc/library/platform.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Doc/library/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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'``
Expand Down
29 changes: 29 additions & 0 deletions Lib/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions Lib/sysconfig/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions Lib/test/test_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add OpenHarmony platform identifiers for the ``linux-ohos`` target triple, including API-aware platform tags and :func:`platform.ohos_ver`.
11 changes: 11 additions & 0 deletions Misc/platform_triplet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Tools/build/check_capi_macros_ignored.txt
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ NCURSES_OPAQUE
NOWAIT_LOCK
NTDDI_VERSION
NT_THREADS
OHOS_API_LEVEL
PACKAGE_BUGREPORT
PACKAGE_NAME
PACKAGE_STRING
Expand Down
2 changes: 2 additions & 0 deletions config.guess
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ Linux|GNU|GNU/*)
cat <<-EOF > "$dummy.c"
#if defined(__ANDROID__)
LIBC=android
#elif defined(__OHOS__)
LIBC=ohos
#else
#include <features.h>
#if defined(__UCLIBC__)
Expand Down
4 changes: 2 additions & 2 deletions config.sub
Original file line number Diff line number Diff line change
Expand Up @@ -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*)
Expand Down Expand Up @@ -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*- )
;;
Expand Down
53 changes: 52 additions & 1 deletion configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 32 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ then
*-*-linux-android*)
ac_sys_system=Linux-android
;;
*-*-linux-ohos*)
ac_sys_system=Linux-ohos
;;
*-*-linux*)
ac_sys_system=Linux
;;
Expand Down Expand Up @@ -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
Expand All @@ -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";;
Expand Down Expand Up @@ -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*], [
Expand Down
3 changes: 3 additions & 0 deletions pyconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading