Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 20 additions & 1 deletion pybricksdev/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -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)


Expand Down