8000 [discussion] Sharing custom C modules is hard · Issue #4001 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

[discussion] Sharing custom C modules is hard #4001

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

Closed
sdfgeoff opened this issue Aug 1, 2018 · 8 comments
Closed

[discussion] Sharing custom C modules is hard #4001

sdfgeoff opened this issue Aug 1, 2018 · 8 comments

Comments

@sdfgeoff
Copy link
sdfgeoff commented Aug 1, 2018

Currently, when writing a custom C module, you have to make edits to the Makefile and mpconfigport.h.

This means that, that to distribute a custom C module, you can only do so within the context of a fork of the micropython repository. If I find one person who's written a micropython module for vectormath, and another person who's done a particular communication protocol, then in order to use both of these modules, I have to have a fork of the micropython repository and somehow merge in the code from both of these. There's no easy way to share custom C modules libraries.

I think this contributes hugely to why there are very few custom C modules publicly available for micropython - it's simply too hard to distribute them in a meaningful manner.

Ideally there would be some way of loading compiled libraries (similar to how ctypes allows importing .so files). This is probably impossible in a useful way, but it would be ideal ... allowing drag-drop loading of C modules.
A more realistic way would be to allow some way for code in subdirectories of the ports folder to be able to add their files to the Makefile and mpconfigport.h file at compile time.

I'm willing to invest some time into solving this problem, but was wondering if there are other solutions I missed, or if someone has a better idea about how to solve it.

@aykevl
Copy link
Contributor
aykevl commented Aug 1, 2018

There are two (mostly orthogonal) proposals to fix this:

  1. Dynamic native modules v2 #1627, which is a system for dynamically loadable modules (as an extension to .mpy files). The discussion got stuck at what to do with the ESP ports (using the Xtensa architecture) that don't support position-independent code. I also did some work on loading .so files in Native modules in ELF format #3311 but abandoned it in favor of the aforementioned branch by Damien (which is more lightweight).
  2. Implement a module system for external C modules. #3871 that I wrote myself, which statically compiles modules in the final binary. See the description for details.
    This one is more difficult to use than the first one (requiring a recompilation of MicroPython). It is also more powerful as it can call any internal MicroPython function and it is lighter on resources as no dynamic loading (or even a filesystem) is needed for it to work.

Note that both solve roughly the same problem, they do it in very different ways and are targeted at slightly different audiences / types of modules, so I think it's valuable to have both.

@sdfgeoff
Copy link
Author
sdfgeoff commented Aug 1, 2018

Hmm, your solution (#3871) looks like it would fit what I was expecting. Pity it hasn't been merged yet. I'll keep my eyes on that PR....

@adritium
Copy link
adritium commented Aug 1, 2018

A more realistic way would be to allow some way for code in subdirectories of the ports folder to be able to add their files to the Makefile and mpconfigport.h file at compile time.

That's what CMake does (and other systems): generate Makefile based on presence of .cmake config file in a directory containing one or more source files.

@stinos
Copy link
Contributor
stinos commented Aug 2, 2018

Ideally there would be some way of loading compiled libraries

stinos@e858818 is similar to the CPython way of loading dynamic libraries, so you give someone a Makefile and a bunch of C (or C++) code which creates a .so which can then be imported as a module. Only tested on unix and windows, not sure how it compares to the other solutions.

@andrewleech
Copy link
Contributor

While I am a big fan of the idea of dynamic C modules, they will always have a ram overhead in the same way as loading an mpy file from filesystem vs frozen bytecode.

Anything that's a file on the filesystem needs to be first read into ram before it can be run.
Frozen bytecode / compiled in c modules can be run directly from flash, so don't need extra ram.

For some applications, dynamic modules will be great, making it simple to add functionality without needing to recompile and flash micropython. For others, the lower overhead of compiled in functions will work better. I definitely support having both options available!

@adritium
Copy link

Anything that's a file on the filesystem needs to be first read into ram before it can be run.

Not true. With a few changes, mp_raw_code_load could be made to execute the mp_raw_code_t without importing to RAM.

@andrewleech
Copy link
Contributor

Hmm.. I'd say this would require a filesystem that guarantees that a file is in one contiguous (aligned?) block to be able to execute directly from it, if that's the case then sure, it should work.

That's not always the case though, files are often split over multiple blocks / sectors in a filesystem. tbh I'm not sure about the FatFS implementation currently used, though I'm pretty certain littlefs does not work this way if we do switch to that.

@dpgeorge
Copy link
Member

Support for custom user C modules was added in 2e51607. Also, MicroPython now supports dynamically loadable native modules.

tannewt pushed a commit to tannewt/circuitpython that referenced this issue Jan 21, 2021
MagTag: Add pin A1 as alias for AD1; A1 is preferred
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants
0