Skip to content

Commit ce8ef66

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.
1 parent 55e1c80 commit ce8ef66

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

micropython/mip/mip/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@
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', 'x64',
31+
'armv6', 'armv6m', 'armv7m', 'armv7em', 'armv7emsp', 'armv7emdp',
32+
'xtensa', 'xtensawin', 'rv32imc', 'rv64imc'
33+
]
34+
35+
def _get_mpy_arch_version():
36+
37+
sys_mpy = sys.implementation._mpy
38+
sys_arch = (sys_mpy >> 10) & 0x0F
39+
40+
mpy_major = sys_mpy & 0xff
41+
mpy_minor = sys_mpy >> 8 & 3
42+
mpy_arch = MPY_ARCHITECTURES[sys_arch]
43+
44+
return mpy_arch, mpy_major, mpy_minor
45+
2746

2847
# This implements os.makedirs(os.dirname(path))
2948
def _ensure_path_exists(path):
@@ -74,6 +93,12 @@ def _check_exists(path, short_hash):
7493

7594

7695
def _rewrite_url(url, branch=None):
96+
# substitute markers for native modules (if present)
97+
mpy_arch, mpy_major, mpy_minor = _get_mpy_arch_version()
98+
mpy_version = f'{mpy_major}.{mpy_minor}'
99+
url = url.replace('{MPY_VERSION}', mpy_version)
100+
url = url.replace('{MPY_ARCH}', mpy_arch)
101+
77102
for provider, url_format in _HOSTS.items():
78103
if not url.startswith(provider):
79104
continue

0 commit comments

Comments
 (0)