Building MP as a static library

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
nelfata
Posts: 74
Joined: Wed Apr 30, 2014 10:50 pm

Building MP as a static library

Post by nelfata » Sun Jul 13, 2014 6:25 am

Did someone build MP as a static library, to be linked with an application?
If so please provide Makefile (hopefully some slight changes). Thanks.

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

Re: Building MP as a static library

Post by stijn » Mon Jul 14, 2014 8:29 am

Compiling a static library is just a matter of invoking ar. This seems to work, here's the diff:

Code: Select all

diff --git a/py/mkenv.mk b/py/mkenv.mk
index 4bc71ed..24b56e7 100644
--- a/py/mkenv.mk
+++ b/py/mkenv.mk
@@ -45,6 +45,7 @@ SED = sed
 PYTHON = python
 
 AS = $(CROSS_COMPILE)as
+AR = $(CROSS_COMPILE)ar
 CC = $(CROSS_COMPILE)gcc
 LD = $(CROSS_COMPILE)ld
 OBJCOPY = $(CROSS_COMPILE)objcopy
diff --git a/py/mkrules.mk b/py/mkrules.mk
index 6127ece..fc30cd9 100644
--- a/py/mkrules.mk
+++ b/py/mkrules.mk
@@ -79,10 +79,16 @@ ifndef DEBUG
 endif
        $(Q)$(SIZE) $(PROG)
 
-clean: clean-prog
+staticlib: $(OBJ)
+       $(ECHO) "LIB $(PROG)"
+       $(Q)$(AR) rcs $(PROG).a $(OBJ)
+
+clean: clean-prog clean-staticlib
 clean-prog:
        $(RM) -f $(PROG)
        $(RM) -f $(PROG).map
+clean-staticlib:
+       $(RM) -f $(PROG).a
 
 .PHONY: clean-prog
 endif
diff --git a/unix/main.c b/unix/main.c
index d0222de..59a6573 100644
--- a/unix/main.c
+++ b/unix/main.c
@@ -264,7 +264,7 @@ void pre_process_options(int argc, char **argv) {
 #define PATHLIST_SEP_CHAR ':'
 #endif
 
-int main(int argc, char **argv) {
+int mpmain(int argc, char **argv) {
     mp_stack_set_limit(32768);
 
     pre_process_options(argc, argv);
This produces microptython.a lib after invoking `make staticlib`. Note I renamed main() to mpmain().
Then creat a new source file and build an executable using this static lib:

Code: Select all

int main(int argc, char** argv) {
    return mpmain(argc, argv);
}
gcc altmain.c micropython.a -lreadline -ldl -lffi -lm

nelfata
Posts: 74
Joined: Wed Apr 30, 2014 10:50 pm

Re: Building MP as a static library

Post by nelfata » Tue Jul 15, 2014 4:06 pm

This is great. Thank you.

nelfata
Posts: 74
Joined: Wed Apr 30, 2014 10:50 pm

Re: Building MP as a static library

Post by nelfata » Sat Feb 14, 2015 4:23 am

Hi again,
in order to use the MPY as a library, I would like to know how to use the stack and heap and what to set them to.
Taking some example from the minimal port there is a heap variable being set and passed to gc_init().
With stmhal mp_stack_set_limit() is used to set the stack limit.
I intend to use the MPY as a library within an RTOS task.

Please provide any recommendations on setting the memory stack/heap and what would be a reasonable size.

Thanks.

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

Re: Building MP as a static library

Post by stijn » Sat Feb 14, 2015 2:56 pm

The reasonable size really depends on your platform (available memory, max stack size of threads if any) and application (i.e. whether you need a lot of memory available or not) so it's hard to tell.

If you compile with MICROPY_ENABLE_GC you have to set both heap size and stack limit. I'd set it to something reasonable, then run your application and use

Code: Select all

import micropython
micropython.mem_info()
to show what is actually being used. Then tune the numbers according to that.

nelfata
Posts: 74
Joined: Wed Apr 30, 2014 10:50 pm

Re: Building MP as a static library

Post by nelfata » Sat Feb 14, 2015 4:42 pm

Ok will give it a try.
Any minimums to set initially?

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Post by Damien » Sat Feb 14, 2015 9:28 pm

Stack around 4k bytes and heap of 32k will get you started.

nelfata
Posts: 74
Joined: Wed Apr 30, 2014 10:50 pm

Re: Building MP as a static library

Post by nelfata » Tue Feb 17, 2015 4:51 am

If not too much trouble to ask for Makefile help.
I tried to build the library in bare-arm (make staticlib) with no luck, under unix it builds.
Any info on how to get the bare-arm to build as a library (based on the .mk modifications suggested earlier).

Thanks.

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

Re: Building MP as a static library

Post by stijn » Tue Feb 17, 2015 8:23 am

I don't have any target atm I can test bare-arm on, but maybe you can already post the error message(s) you get so at least we know more about what is going on?

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Post by Damien » Tue Feb 17, 2015 9:23 am

Note that the new cc3200 port uses FreeRTOS and embeds uPy within it running as a task. This might be a better place to start from.

Post Reply