Page 1 of 2

Building MP as a static library

Posted: Sun Jul 13, 2014 6:25 am
by nelfata
Did someone build MP as a static library, to be linked with an application?
If so please provide Makefile (hopefully some slight changes). Thanks.

Re: Building MP as a static library

Posted: Mon Jul 14, 2014 8:29 am
by stijn
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

Re: Building MP as a static library

Posted: Tue Jul 15, 2014 4:06 pm
by nelfata
This is great. Thank you.

Re: Building MP as a static library

Posted: Sat Feb 14, 2015 4:23 am
by nelfata
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.

Re: Building MP as a static library

Posted: Sat Feb 14, 2015 2:56 pm
by stijn
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.

Re: Building MP as a static library

Posted: Sat Feb 14, 2015 4:42 pm
by nelfata
Ok will give it a try.
Any minimums to set initially?

Posted: Sat Feb 14, 2015 9:28 pm
by Damien
Stack around 4k bytes and heap of 32k will get you started.

Re: Building MP as a static library

Posted: Tue Feb 17, 2015 4:51 am
by nelfata
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.

Re: Building MP as a static library

Posted: Tue Feb 17, 2015 8:23 am
by stijn
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?

Posted: Tue Feb 17, 2015 9:23 am
by Damien
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.