Skip to content

Compile .py to .mpy with mpy-cross on upload #4

Description

@bdbarnett

Summary

Add optional host-side compilation of .py files to .mpy via mpy-cross when transferring files to a MicroPython board (CLI put/cp and File Transfer UI).

Motivation

Uploading bytecode instead of source:

  • Uses less flash
  • Speeds up imports (no parse/compile on device)
  • Matches how mip often installs packages (.mpy by default)

mpftp already builds host mpy-cross as part of firmware builds (firmware_engine.py), but file transfer always sends .py as-is. Exposing compile-on-upload would close that gap for app/lib deployment workflows.

Proposed behavior

  1. Opt-in setting / CLI flag, e.g. mpftp.compileOnUpload and --mpy / --compile on put / cp.
  2. When enabled and the destination is MicroPython:
    • For each uploaded .py, run mpy-cross on the host
    • Upload the resulting .mpy (same basename, .mpy extension)
    • Skip files that should remain source (boot.py, main.py, or a configurable allowlist/denylist)
  3. Compatibility: pass the correct -march / mpy version for the connected firmware (probe board or reuse firmware-workspace mpy-cross built for that tree). Mismatch should fail clearly rather than silently upload incompatible bytecode.
  4. Tooling discovery (in order): firmware-workspace mpy-cross binary → PATH → error with install/build hint.
  5. CircuitPython: out of scope or explicitly unsupported (different bytecode / tooling), unless a clear CP path exists later.

Acceptance criteria

  • CLI can upload a .py as .mpy via mpy-cross with an explicit flag
  • Setting enables the same for File Transfer uploads
  • Incompatible mpy-cross / board combo produces a clear error
  • boot.py / main.py (and documented exceptions) are not rewritten to .mpy by default
  • Docs: user guide + CLI help covering flags, discovery, and compatibility

Notes

Related existing pieces:

  • Firmware build already invokes make -C mpy-cross
  • mip has --mpy / source install choices; this request is about user file transfer, not package install

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions