|
| 1 | +import sys |
| 2 | +import subprocess |
| 3 | +import importlib |
| 4 | +import itertools |
| 5 | + |
| 6 | +from semver.version import Version |
| 7 | + |
| 8 | +from . import _mp_version_to_mpy_abi_version |
| 9 | + |
| 10 | + |
| 11 | +def _run(): |
| 12 | + """ |
| 13 | + Run mpy-cross directly with extra arg for getting the right version. |
| 14 | + """ |
| 15 | + |
| 16 | + # remove the first argument (the script name) |
| 17 | + # if there is --micropython=X.Y, split it into two arguments |
| 18 | + args = list( |
| 19 | + itertools.chain( |
| 20 | + *( |
| 21 | + arg.split("=") if arg.startswith("--micropython") else (arg,) |
| 22 | + for arg in sys.argv[1:] |
| 23 | + ) |
| 24 | + ) |
| 25 | + ) |
| 26 | + |
| 27 | + try: |
| 28 | + idx = args.index("--micropython") |
| 29 | + except ValueError: |
| 30 | + # default if argument is not given |
| 31 | + mp_ver = "1.22" |
| 32 | + else: |
| 33 | + # argument is given |
| 34 | + mp_ver = args.pop(idx + 1) |
| 35 | + # also remove the --micropython flag |
| 36 | + del args[idx] |
| 37 | + |
| 38 | + # validate the version argument |
| 39 | + try: |
| 40 | + mp_semver = Version.parse(mp_ver, optional_minor_and_patch=True) |
| 41 | + except ValueError: |
| 42 | + print( |
| 43 | + f"Error: invalid version format for --micropython: '{mp_ver}'", |
| 44 | + file=sys.stderr, |
| 45 | + ) |
| 46 | + sys.exit(1) |
| 47 | + |
| 48 | + try: |
| 49 | + abi = _mp_version_to_mpy_abi_version(mp_semver) |
| 50 | + except NotImplementedError: |
| 51 | + print( |
| 52 | + f"Error: targeting MicroPython v{mp_semver} is not supported", |
| 53 | + file=sys.stderr, |
| 54 | + ) |
| 55 | + sys.exit(1) |
| 56 | + |
| 57 | + # get the right mpy-cross version for the target ABI |
| 58 | + mpy_cross = importlib.import_module(f"mpy_cross_v{abi.replace('.', '_')}") |
| 59 | + |
| 60 | + # run mpy-cross with the remaining arguments |
| 61 | + proc = subprocess.run([mpy_cross.MPY_CROSS_PATH] + args) |
| 62 | + sys.exit(proc.returncode) |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + _run() |
0 commit comments