diff --git a/CHANGELOG.md b/CHANGELOG.md index 9761f51..591a978 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +- Add support for including precompiled .mpy libraries ## [1.0.0-alpha.36] - 2023-02-18 diff --git a/pybricksdev/compile.py b/pybricksdev/compile.py index 674cd44..3ea0ccf 100644 --- a/pybricksdev/compile.py +++ b/pybricksdev/compile.py @@ -113,7 +113,8 @@ async def compile_multi_file(path: str, abi: int): """ # compile files using Python to find imports contained within the same directory as path - finder = ModuleFinder([os.path.dirname(path)]) + search_path = [os.path.dirname(path)] + finder = ModuleFinder(search_path) finder.run_script(path) # we expect missing modules, namely builtin MicroPython packages like pybricks.* @@ -123,12 +124,30 @@ async def compile_multi_file(path: str, abi: int): parts: List[bytes] = [] for name, module in finder.modules.items(): + if not module.__file__: + continue mpy = await compile_file(module.__file__, abi) parts.append(len(mpy).to_bytes(4, "little")) parts.append(name.encode() + b"\x00") parts.append(mpy) + # look for .mpy modules + for name in finder.any_missing(): + for spath in search_path: + try: + with open(os.path.join(spath, f"{name}.mpy"), "rb") as f: + mpy = f.read() + if mpy[1] != abi: + raise ValueError( + f"{name}.mpy has abi version {mpy[1]} while {abi} is required" + ) + parts.append(len(mpy).to_bytes(4, "little")) + parts.append(name.encode() + b"\x00") + parts.append(mpy) + except OSError: + continue + return b"".join(parts)