All of lore.kernel.org
 help / color / mirror / Atom feed
* [Buildroot] [PATCH v2 1/1] package/micropython: add support for manifest.py in the configuration
@ 2024-02-12  2:12 Abilio Marques
  2026-11-21 22:38 ` [Buildroot] [PATCH v3 1/2] " Angelo Compagnucci
  0 siblings, 1 reply; 3+ messages in thread
From: Abilio Marques @ 2024-02-12  2:12 UTC (permalink / raw)
  To: buildroot; +Cc: Chris Packham, Yann E . MORIN, Abilio Marques

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.

Signed-off-by: Abilio Marques <abiliojr@gmail.com>
---
 package/micropython/Config.in      | 17 +++++++++++++++++
 package/micropython/micropython.mk |  5 +++++
 2 files changed, 22 insertions(+)

diff --git a/package/micropython/Config.in b/package/micropython/Config.in
index 26a00baab0..6b99cfd2ff 100644
--- a/package/micropython/Config.in
+++ b/package/micropython/Config.in
@@ -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"
diff --git a/package/micropython/micropython.mk b/package/micropython/micropython.mk
index 125a0edcfb..5a2c136547 100644
--- a/package/micropython/micropython.mk
+++ b/package/micropython/micropython.mk
@@ -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 \
-- 
2.43.0

_______________________________________________
buildroot mailing list
buildroot@buildroot.org
https://lists.buildroot.org/mailman/listinfo/buildroot

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [Buildroot] [PATCH v3 1/2] package/micropython: add support for manifest.py in the configuration
  2024-02-12  2:12 [Buildroot] [PATCH v2 1/1] package/micropython: add support for manifest.py in the configuration Abilio Marques
@ 2026-11-21 22:38 ` Angelo Compagnucci
  2026-11-21 22:38   ` [Buildroot] [PATCH v3 2/2] support/testing: add micropython manifest test Angelo Compagnucci
  0 siblings, 1 reply; 3+ messages in thread
From: Angelo Compagnucci @ 2026-11-21 22:38 UTC (permalink / raw)
  To: buildroot; +Cc: Chris Packham, Julien Olivain, Abilio Marques, Yann E . MORIN

From: Abilio Marques <abiliojr@gmail.com>

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.

Signed-off-by: Abilio Marques <abiliojr@gmail.com>
Co-authored-by: Angelo Compagnucci <angelo.compagnucci@gmail.com>
Tested-by: Angelo Compagnucci <angelo.compagnucci@gmail.com>
Signed-off-by: Angelo Compagnucci <angelo.compagnucci@gmail.com>
---
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/20240212021255.246556-1-abiliojr@gmail.com/

 package/micropython/Config.in      | 13 +++++++++++++
 package/micropython/micropython.mk |  6 ++++++
 2 files changed, 19 insertions(+)

diff --git a/package/micropython/Config.in b/package/micropython/Config.in
index 6109e95109..290802b307 100644
--- a/package/micropython/Config.in
+++ b/package/micropython/Config.in
@@ -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"
diff --git a/package/micropython/micropython.mk b/package/micropython/micropython.mk
index e260b9fde8..553765165a 100644
--- a/package/micropython/micropython.mk
+++ b/package/micropython/micropython.mk
@@ -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 \
-- 
2.53.0

_______________________________________________
buildroot mailing list
buildroot@buildroot.org
https://lists.buildroot.org/mailman/listinfo/buildroot

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [Buildroot] [PATCH v3 2/2] support/testing: add micropython manifest test
  2026-11-21 22:38 ` [Buildroot] [PATCH v3 1/2] " Angelo Compagnucci
@ 2026-11-21 22:38   ` Angelo Compagnucci
  0 siblings, 0 replies; 3+ messages in thread
From: Angelo Compagnucci @ 2026-11-21 22:38 UTC (permalink / raw)
  To: buildroot; +Cc: Chris Packham, Julien Olivain, Abilio Marques, Yann E . MORIN

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.

Signed-off-by: Angelo Compagnucci <angelo.compagnucci@gmail.com>
---
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

diff --git a/support/testing/tests/package/test_micropython.py b/support/testing/tests/package/test_micropython.py
index cc0e5854c8..fc061345f3 100644
--- a/support/testing/tests/package/test_micropython.py
+++ b/support/testing/tests/package/test_micropython.py
@@ -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"')
diff --git a/support/testing/tests/package/test_micropython/manifest/frozen_hello.py b/support/testing/tests/package/test_micropython/manifest/frozen_hello.py
new file mode 100644
index 0000000000..3f6516857a
--- /dev/null
+++ b/support/testing/tests/package/test_micropython/manifest/frozen_hello.py
@@ -0,0 +1 @@
+MSG = "Hello from a frozen module"
diff --git a/support/testing/tests/package/test_micropython/manifest/manifest.py b/support/testing/tests/package/test_micropython/manifest/manifest.py
new file mode 100644
index 0000000000..0bd5911fe5
--- /dev/null
+++ b/support/testing/tests/package/test_micropython/manifest/manifest.py
@@ -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")
-- 
2.53.0

_______________________________________________
buildroot mailing list
buildroot@buildroot.org
https://lists.buildroot.org/mailman/listinfo/buildroot

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-21 22:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-12  2:12 [Buildroot] [PATCH v2 1/1] package/micropython: add support for manifest.py in the configuration Abilio Marques
2026-11-21 22:38 ` [Buildroot] [PATCH v3 1/2] " Angelo Compagnucci
2026-11-21 22:38   ` [Buildroot] [PATCH v3 2/2] support/testing: add micropython manifest test Angelo Compagnucci

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.