Skip to contentSkip to conversation

package/micropython: add support for manifest.py in the configuration

PATCH New Abilio Marques 2 participants 3 messages Feb 12, 2024 – Nov 21

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 package/micropython: add support for manifest.py in the configuration

Micropython can embed packages an modules as frozen bytecode. What code gets built this way can be defined by means of a "manifest.py" file.

This commit exposes the variable FROZEN_MANIFEST to Buildroot users through a new variable called BR2_PACKAGE_MICROPYTHON_MANIFEST.

Please check Micropython's documentation for more information:
https://docs.micropython.org/en/latest/reference/manifest.html

We only support freezing of modules from micropython-lib. Freezing arbitrary modules would require some handling of absoulte paths, and a guarantee that the files be available before micropython is built, which is considered a bit too complex; an interested party can provide such support in the future.

---
 package/micropython/Config.in      | 17 +++++++++++++++++
 package/micropython/micropython.mk |  5 +++++
 2 files changed, 22 insertions(+)
package/micropython/Config.in +17 -0
index 26a00baab0..6b99cfd2ff 100644
@@ -17,6 +17,23 @@ config BR2_PACKAGE_MICROPYTHON_LIB
 	help
 	  Core Python libraries ported to MicroPython.
 
+config BR2_PACKAGE_MICROPYTHON_MANIFEST
+	string "Path to a manifest.py file"
+	help
+	  MicroPython allows Python code to be “frozen” as bytecode
+	  into its binary, as an alternative to loading code from
+	  the filesystem. 
+
+	  A list of "frozen" modules can be provided using a "manifest.py"
+	  file. You can set the path to such file using this variable.
+
+	  See MicroPython's documentation for more information on how to
+	  use the file.
+
+	  Note: in Buildroot, only modules provided with micropython-lib
+      can be frozen with a manifest; freezing arbitrary files is
+	  currently not supported.
+
 endif # BR2_PACKAGE_MICROPYTHON
 
 comment "micropython needs a toolchain w/ threads, dynamic library"
package/micropython/micropython.mk +5 -0
index 125a0edcfb..5a2c136547 100644
@@ -43,6 +43,11 @@ else
 MICROPYTHON_MAKE_OPTS += MICROPY_PY_FFI=0
 endif
 
+ifneq ($(BR2_PACKAGE_MICROPYTHON_MANIFEST),"")
+MICROPYTHON_MAKE_OPTS += \
+	FROZEN_MANIFEST=$(BR2_PACKAGE_MICROPYTHON_MANIFEST)
+endif
+
 define MICROPYTHON_BUILD_CMDS
 	$(TARGET_MAKE_ENV) $(MAKE) -C $(@D)/mpy-cross
 	$(TARGET_MAKE_ENV) $(MAKE) -C $(@D)/ports/unix \
Signature · 6 lines
2.43.0

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#1 lore raw

PATCH v3 1/2 package/micropython: add support for manifest.py in the configuration

From: Abilio Marques <[email protected]>

Micropython can embed packages an modules as frozen bytecode. What code gets built this way can be defined by means of a "manifest.py" file.

This commit exposes the variable FROZEN_MANIFEST to Buildroot users through a new variable called BR2_PACKAGE_MICROPYTHON_MANIFEST.

Please check Micropython's documentation for more information:
https://docs.micropython.org/en/latest/reference/manifest.html

The build runs in ports/unix, so the path is made absolute. Paths in the manifest are relative to its own directory: modules next to it can be frozen too, not only the micropython-lib ones.

---
Changes v2 -> v3:
- rebase on master
- make the manifest path absolute, as the build runs in ports/unix:
a path relative to the Buildroot directory didn't work
- drop the note that only micropython-lib modules can be frozen:
with an absolute path, modules next to the manifest work too
- say in the help text that the manifest replaces the default one, and how to keep the default frozen modules
- use qstrip to test the option, and fix the help text indent, trailing whitespace and quotes

Supersedes v2: https://patchwork.buildroot.org/project/buildroot/patch/[email protected]/

 package/micropython/Config.in      | 13 +++++++++++++
 package/micropython/micropython.mk |  6 ++++++
 2 files changed, 19 insertions(+)
package/micropython/Config.in +13 -0
index 6109e95109..290802b307 100644
@@ -36,6 +36,19 @@ config BR2_PACKAGE_MICROPYTHON_LIB_UNIXFFI
 
 endif # BR2_PACKAGE_MICROPYTHON_LIB
 
+config BR2_PACKAGE_MICROPYTHON_MANIFEST
+	string "Path to a manifest.py file"
+	help
+	  Path to a manifest.py file, listing the Python modules to
+	  freeze as bytecode into the micropython binary. Modules next
+	  to the manifest and micropython-lib modules can be frozen.
+
+	  It replaces the default manifest of the unix port. To keep
+	  the default frozen modules, add this line to it:
+	  include("$(PORT_DIR)/variants/standard/manifest.py")
+
+	  https://docs.micropython.org/en/latest/reference/manifest.html
+
 endif # BR2_PACKAGE_MICROPYTHON
 
 comment "micropython needs a toolchain w/ threads, dynamic library"
package/micropython/micropython.mk +6 -0
index e260b9fde8..553765165a 100644
@@ -44,6 +44,12 @@ else
 MICROPYTHON_MAKE_OPTS += MICROPY_PY_FFI=0
 endif
 
+# The build runs in ports/unix: pass an absolute path to the manifest
+MICROPYTHON_MANIFEST = $(call qstrip,$(BR2_PACKAGE_MICROPYTHON_MANIFEST))
+ifneq ($(MICROPYTHON_MANIFEST),)
+MICROPYTHON_MAKE_OPTS += FROZEN_MANIFEST=$(abspath $(MICROPYTHON_MANIFEST))
+endif
+
 define MICROPYTHON_BUILD_CMDS
 	$(TARGET_MAKE_ENV) $(MAKE) -C $(@D)/mpy-cross
 	$(TARGET_MAKE_ENV) $(MAKE) -C $(@D)/ports/unix \
Signature · 6 lines
2.53.0

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#2 lore raw

PATCH v3 2/2 support/testing: add micropython manifest test

Test BR2_PACKAGE_MICROPYTHON_MANIFEST with a manifest that freezes:

- a module next to the manifest;
- a micropython-lib module (base64);
- the default modules of the unix port, kept with include().

micropython-lib is not installed in the image, so the modules can only be imported from the frozen bytecode.

---
Changes v2 -> v3:
- new patch

 .../testing/tests/package/test_micropython.py | 33 +++++++++++++++++++
 .../test_micropython/manifest/frozen_hello.py |  1 +
 .../test_micropython/manifest/manifest.py     |  6 ++++
 3 files changed, 40 insertions(+)
 create mode 100644 support/testing/tests/package/test_micropython/manifest/frozen_hello.py
 create mode 100644 support/testing/tests/package/test_micropython/manifest/manifest.py
support/testing/tests/package/test_micropython.py +33 -0
index cc0e5854c8..fc061345f3 100644
@@ -77,3 +77,36 @@ class TestMicroPython(infra.basetest.BRTest):
         out, ret = self.emulator.run(f"/root/zcat.py {gz_filename}")
         self.assertEqual(ret, 0)
         self.assertEqual(out[0], msg)
+
+
+class TestMicroPythonManifest(infra.basetest.BRTest):
+    # micropython-lib is not installed: the modules can only be frozen
+    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
+        f"""
+        BR2_PACKAGE_MICROPYTHON=y
+        BR2_PACKAGE_MICROPYTHON_MANIFEST="{infra.filepath("tests/package/test_micropython/manifest/manifest.py")}"
+        BR2_TARGET_ROOTFS_CPIO=y
+        # BR2_TARGET_ROOTFS_TAR is not set
+        """
+
+    def test_run(self):
+        cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
+        self.emulator.boot(arch="armv5",
+                           kernel="builtin",
+                           options=["-initrd", cpio_file])
+        self.emulator.login()
+
+        # A module next to the manifest
+        out, ret = self.emulator.run(
+            'micropython -c "import frozen_hello; print(frozen_hello.MSG)"')
+        self.assertEqual(ret, 0)
+        self.assertEqual(out[0], "Hello from a frozen module")
+
+        # A micropython-lib module
+        out, ret = self.emulator.run(
+            'micropython -c "import base64; print(base64.b64encode(b\'br\'))"')
+        self.assertEqual(ret, 0)
+        self.assertEqual(out[0], "b'YnI='")
+
+        # A module of the default manifest, kept by include()
+        self.assertRunOk('micropython -c "import asyncio"')
support/testing/tests/package/test_micropython/manifest/frozen_hello.py new file +1 -0
new file mode 100644
index 0000000000..3f6516857a
@@ -0,0 +1 @@
+MSG = "Hello from a frozen module"
support/testing/tests/package/test_micropython/manifest/manifest.py new file +6 -0
new file mode 100644
index 0000000000..0bd5911fe5
@@ -0,0 +1,6 @@
+# Keep the frozen modules of the default unix manifest
+include("$(PORT_DIR)/variants/standard/manifest.py")
+# A module next to this manifest
+module("frozen_hello.py")
+# A micropython-lib module
+require("base64")
Signature · 6 lines
2.53.0

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot