I would like to know how to build micropython as a lib

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
cduran
Posts: 80
Joined: Thu Mar 17, 2016 4:52 pm

I would like to know how to build micropython as a lib

Post by cduran » Thu Feb 27, 2020 7:26 pm

I'm trying to build mp as a stand alone library. I'm trying to make sense of the embedded example in the examples folder. I tried to build it but I'm getting the following make error:

make[1]: *** No rule to make target 'ports/unix/main.c', needed by 'build/genhdr/qstr.i.last'.

Do I absolutely need a main to build the lib? Why the unix port, I'm more likely to use the minimal port since my application will be cross platform with no file system?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: I would like to know how to build micropython as a lib

Post by jimmo » Mon Mar 02, 2020 1:28 am

cduran wrote:
Thu Feb 27, 2020 7:26 pm
Do I absolutely need a main to build the lib? Why the unix port, I'm more likely to use the minimal port since my application will be cross platform with no file system?
Hi, sorry for the delay replying.

You're absolutely right -- you do not need the unix port's main.c (in fact there's a duplicate definition of "int main()" here).

This example is extremely confusing as it's showing three things:
- How to provide the basic set of support machinery needed to embed micropython (i.e. fundamental things like gccollect.c)
- Extra useful things like input/output (unix_mphal.c)
- How to provide more features (built-in modules, filesystem, etc).
...but for convenience, it just reaches in and grabs these bits out of the unix port, but that really isn't very illustrative or expository.

So yes, you can pretty much remove a fairly large chunk of Makefile.upylib to build just the library, and instead put all the required bits in your host, rather than in lmicropython.

(If you get this working it would be great to send a PR adding a clearer minimal example).

cduran
Posts: 80
Joined: Thu Mar 17, 2016 4:52 pm

Re: I would like to know how to build micropython as a lib

Post by cduran » Mon Mar 02, 2020 4:45 pm

jimmo wrote:
Mon Mar 02, 2020 1:28 am
cduran wrote:
Thu Feb 27, 2020 7:26 pm
Do I absolutely need a main to build the lib? Why the unix port, I'm more likely to use the minimal port since my application will be cross platform with no file system?
Hi, sorry for the delay replying.

You're absolutely right -- you do not need the unix port's main.c (in fact there's a duplicate definition of "int main()" here).

This example is extremely confusing as it's showing three things:
- How to provide the basic set of support machinery needed to embed micropython (i.e. fundamental things like gccollect.c)
- Extra useful things like input/output (unix_mphal.c)
- How to provide more features (built-in modules, filesystem, etc).
...but for convenience, it just reaches in and grabs these bits out of the unix port, but that really isn't very illustrative or expository.

So yes, you can pretty much remove a fairly large chunk of Makefile.upylib to build just the library, and instead put all the required bits in your host, rather than in lmicropython.

(If you get this working it would be great to send a PR adding a clearer minimal example).
I removed the references to files from the unix port

Code: Select all

# source files
#SRC_C = $(addprefix ports/minimal/,\
#	$(MAIN_C) \
#	gccollect.c \
#	unix_mphal.c \
#	input.c \
#	file.c \
#	modmachine.c \
#	modos.c \
#	moduselect.c \
#	alloc.c \
#	coverage.c \
#	fatfs_port.c \
#	$(SRC_MOD) \
#	)
And using the mpconfigport.h from the minimal port. Is there anything else I need to do to the Makefile.upylib? Now I'm getting build errors saying it can't find mphal.c

My build output:

In file included from ../../py/mpprint.c:33:0:
../../py/mphal.h:34:23: fatal error: mphalport.h: No such file or directory
compilation terminated.
In file included from ../../py/modmicropython.c:33:0:
../../py/mphal.h:34:23: fatal error: mphalport.h: No such file or directory
compilation terminated.
In file included from ../../extmod/machine_pulse.h:30:0,
from ../../extmod/machine_pulse.c:29:
../../py/mphal.h:34:23: fatal error: mphalport.h: No such file or directory
compilation terminated.
In file included from ../../extmod/machine_i2c.c:32:0:
../../py/mphal.h:34:23: fatal error: mphalport.h: No such file or directory
compilation terminated.
In file included from ../../extmod/machine_spi.h:30:0,
from ../../extmod/machine_spi.c:31:
../../py/mphal.h:34:23: fatal error: mphalport.h: No such file or directory
compilation terminated.
In file included from ../../extmod/modbluetooth.c:36:0:
../../py/mphal.h:34:23: fatal error: mphalport.h: No such file or directory
compilation terminated.
In file included from ../../lib/utils/printf.c:34:0:
../../py/mphal.h:34:23: fatal error: mphalport.h: No such file or directory
compilation terminated.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: I would like to know how to build micropython as a lib

Post by jimmo » Mon Mar 02, 2020 10:14 pm

cduran wrote:
Mon Mar 02, 2020 4:45 pm
And using the mpconfigport.h from the minimal port. Is there anything else I need to do to the Makefile.upylib? Now I'm getting build errors saying it can't find mphal.c
All "ports" are required to provide some basic support stuff (this is the first bullet point above). mpconfigport/mphalport two mechanisms for this. (Embedded MicroPython works just like a port).

So you're expected to provide these two files at a minimum.

Probably the best thing I can suggest is to take a look at ports/minimal and see how it works -- it provides the absolute bare minimum (and compiles for both linux and arm).

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: I would like to know how to build micropython as a lib

Post by stijn » Tue Mar 03, 2020 8:41 am

I removed the references to files from the unix port
Removing all of those is likely going to cause linking errors at one point since some of those files are required (I think, didn't check in detail). You can also try it the other way around: take a port to start from, rename main() to something else (or delete it, but I prefer to keep it in the first place to test if embedding works) and build a static library. Remove things when needed. This target worked for me in the past btw:

Code: Select all

AR = $(CROSS_COMPILE)ar

staticlib: $(OBJ)
       $(Q)$(AR) rcs $(PROG).a $(OBJ)

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: I would like to know how to build micropython as a lib

Post by jimmo » Fri Apr 24, 2020 6:59 am

cduran wrote:
Thu Feb 27, 2020 7:26 pm
I'm trying to build mp as a stand alone library.
Somewhat inspired by this thread, I put together https://github.com/micropython/micropython/pull/5964 to overhaul the embedding example and hopefully make it a bit simpler to work with.

Post Reply