Skip to content

Commit 5d4e25d

Browse files
committed
mip: Support replacing MPY_VERSION and MPY_ARCH variables.
Allows to use a single URL to specify a native module package, and mip will then lookup the correct hardware architecture and MicroPython ABI version. Signed-off-by: Jon Nordby <jononor@gmail.com>
1 parent 55e1c80 commit 5d4e25d

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

micropython/mip/mip/__init__.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,35 @@
2424

2525
_ALLOWED_MIP_URL_PREFIXES = const(("http://", "https://", "codeberg:", "github:", "gitlab:"))
2626

27+
# NB: Must be in the correct order wrt sys.implementation._mpy
28+
MPY_ARCHITECTURES = [
29+
None,
30+
"x86",
31+
"x64",
32+
"armv6",
33+
"armv6m",
34+
"armv7m",
35+
"armv7em",
36+
"armv7emsp",
37+
"armv7emdp",
38+
"xtensa",
39+
"xtensawin",
40+
"rv32imc",
41+
"rv64imc",
42+
]
43+
44+
45+
def _get_mpy_arch_version():
46+
47+
sys_mpy = sys.implementation._mpy
48+
sys_arch = (sys_mpy >> 10) & 0x0F
49+
50+
mpy_major = sys_mpy & 0xFF
51+
mpy_minor = sys_mpy >> 8 & 3
52+
mpy_arch = MPY_ARCHITECTURES[sys_arch]
53+
54+
return mpy_arch, mpy_major, mpy_minor
55+
2756

2857
# This implements os.makedirs(os.dirname(path))
2958
def _ensure_path_exists(path):
@@ -74,6 +103,12 @@ def _check_exists(path, short_hash):
74103

75104

76105
def _rewrite_url(url, branch=None):
106+
# substitute markers for native modules (if present)
107+
mpy_arch, mpy_major, mpy_minor = _get_mpy_arch_version()
108+
mpy_version = f"{mpy_major}.{mpy_minor}"
109+
url = url.replace("{MPY_VERSION}", mpy_version)
110+
url = url.replace("{MPY_ARCH}", mpy_arch)
111+
77112
for provider, url_format in _HOSTS.items():
78113
if not url.startswith(provider):
79114
continue

0 commit comments

Comments
 (0)