Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interpreters/python: create Python's config files dynamically and other tweaks #2982

Merged

Conversation

tmedicci
Copy link
Contributor

Summary

  • interpreters/python: add wrapper to initialize Python

    • This wrapper application checks if the Python's modules are already mounted (and mounts them, if not), sets the necessary environment variables and, then, runs the Python interpreter.
  • interpreters/python: create Python's config files dynamically

    • The Setup.local and the config.site files are used by Python's build system to, respectively, enable or disable Python's modules and set/unset available functions in the target system. These files are now set according to NuttX's configs, enabling or disabling Python's features according to the configs set on NuttX.
  • interpreters/python: set ROMFS-generated data to const char

    • This allows the data to be placed in the .rodata section, which can be allocated in the flash or other read-only storage, freeing the
      internal memory.
  • interpreters/python: add patch to set _PyRuntime section

    • By setting a specific region for the _PyRuntime structure, it is possible to move it to the external memory, for instance, freeing the internal memory (this structure occupies around 140KiB).

Impact

This PR provides a set of improvements that allow running Python on NuttX easily: it provides a wrapper that enables running the Python interpreter directly with python and enables it to be built by architectures other than the RISC-V QEMU. Documentation will be updated on NuttX's OS repository as soon as we merge this PR.

Testing

Internal CI testing + rv-virt:python:

Building

$ make -j distclean && ./tools/configure.sh rv-virt:python && make -j$(nproc)

Running

$ qemu-system-riscv32 -semihosting -M virt,aclint=on -cpu rv32 -smp 8 \ 
   -global virtio-mmio.force-legacy=false \
   -device virtio-serial-device,bus=virtio-mmio-bus.0 \
   -chardev socket,telnet=on,host=127.0.0.1,port=3450,server=on,wait=off,id=foo \
   -device virtconsole,chardev=foo \
   -device virtio-rng-device,bus=virtio-mmio-bus.1 \
   -netdev user,id=u1,hostfwd=tcp:127.0.0.1:10023-10.0.2.15:23,hostfwd=tcp:127.0.0.1:15001-10.0.2.15:5001 \
   -device virtio-net-device,netdev=u1,bus=virtio-mmio-bus.2 \
   -drive file=./mydisk-1gb.img,if=none,format=raw,id=hd \
   -device virtio-blk-device,bus=virtio-mmio-bus.3,drive=hd \
   -bios none -kernel ./nuttx -nographic
ABC[    0.041394] board_userled: LED 1 set to 0
[    0.042074] board_userled: LED 2 set to 0
[    0.042178] board_userled: LED 3 set to 0
telnetd [4:100]

NuttShell (NSH) NuttX-10.4.0
nsh> python
[    2.321624] check_and_mount_romfs: Mounting ROMFS filesystem at target=/usr/local/lib with source=/dev/ram1
Python 3.13.0 (main, Jan 31 2025, 12:41:21) [GCC 13.2.0] on nuttx
Type "help", "copyright", "credits" or "license" for more information.
>>>

By setting a specific region for the `_PyRuntime` structure, it is
possible to move it to the external memory, for instance, freeing
the internal memory (this structure occupies around 140KiB).
The `Setup.local` and the `config.site` files are used by Python's
build system to, respectively, enable or disable Python's modules
and set/unset available functions in the target system. These files
are now set according to NuttX's configs, enabling or disabling
Python's features according to the configs set on NuttX.
@nuttxpr
Copy link

nuttxpr commented Jan 31, 2025

[Experimental Bot, please feedback here]

Yes, this PR appears to meet the NuttX requirements. It provides a clear summary of the changes, their purpose, and how they work. The impact section is well-detailed, covering various aspects including user experience, build process, hardware, documentation, security, and compatibility. The testing section demonstrates verification on a relevant target (rv-virt:python) and provides build and run instructions along with example output, confirming the functionality.

This wrapper application checks if the Python's modules are already
mounted (and mounts them, if not), sets the necessary environment
variables and, then, runs the Python interpreter.
This allows the data to be placed in the .rodata section, which can
be allocated in the flash or other read-only storage, freeing the
internal memory.
@tmedicci
Copy link
Contributor Author

@lupyuen , rv-virt: python is being tested on the NuttX build farm (accessible through nuttx-dashboard.org). Is it possible to trigger a test with a specific apps version (to test this PR, for instance)?

Copy link
Contributor

@cederom cederom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @tmedicci amazing work!! :-)

@lupyuen
Copy link
Member

lupyuen commented Feb 1, 2025

rv-virt: python is being tested on the NuttX build farm (accessible through nuttx-dashboard.org). Is it possible to trigger a test with a specific apps version (to test this PR, for instance)?

@tmedicci Sorry NuttX Build Farm doesn't support on-demand builds because:
(1) Build Farm is designed to plough through all builds as quickly as possible (to catch build problems earlier)
(2) So supporting on-demand builds might slow down the other builds
(3) Also: Build Farm runs on Home PCs. We have to think about Security when allowing on-demand builds on unmerged code

If NuttX has extra funding, we could probably:
(1) Set up (and maintain) a proper Build Farm with Security-Hardened Servers in a proper Data Centre
(2) Then we can allow on-demand build jobs for unmerged code
(3) Security Setup will probably look like this

@cederom cederom linked an issue Feb 1, 2025 that may be closed by this pull request
@cederom cederom merged commit 43439a6 into apache:master Feb 1, 2025
37 checks passed
tmedicci added a commit to tmedicci/nuttx that referenced this pull request Feb 20, 2025
After apache/nuttx-apps#2982, there is no
need to mount the modules and manually set the required environment
variables: they are all set by the new Python wrapper application.

Signed-off-by: Tiago Medicci Serrano <[email protected]>
tmedicci added a commit to tmedicci/nuttx that referenced this pull request Feb 20, 2025
After apache/nuttx-apps#2982, there is no
need to mount the modules and manually set the required environment
variables: they are all set by the new Python wrapper application.

Signed-off-by: Tiago Medicci Serrano <[email protected]>
tmedicci added a commit to tmedicci/nuttx that referenced this pull request Feb 20, 2025
After apache/nuttx-apps#2982, there is no
need to mount the modules and manually set the required environment
variables: they are all set by the new Python wrapper application.

Signed-off-by: Tiago Medicci Serrano <[email protected]>
jerpelea pushed a commit to apache/nuttx that referenced this pull request Feb 21, 2025
After apache/nuttx-apps#2982, there is no
need to mount the modules and manually set the required environment
variables: they are all set by the new Python wrapper application.

Signed-off-by: Tiago Medicci Serrano <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[PYTHON] NuttX RTOS with Python and MicroPython
5 participants