How to add a C module to minimal port?

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
yang8621
Posts: 4
Joined: Mon Jul 20, 2020 8:45 am

How to add a C module to minimal port?

Post by yang8621 » Mon Sep 07, 2020 7:26 am

take ulab (https://github.com/v923z/micropython-ulab/) as an example,

to build ulab module by "make USER_C_MODULES=../../../ulab all"

It can be built with unix port:
Including User C Module from ../../../ulab/code
.......
CC ../../../ulab/code/ndarray.c
CC ../../../ulab/code/ulab_create.c
CC ../../../ulab/code/linalg/linalg.c
CC ../../../ulab/code/vector/vectorise.c
CC ../../../ulab/code/poly/poly.c
CC ../../../ulab/code/fft/fft.c
CC ../../../ulab/code/numerical/numerical.c
CC ../../../ulab/code/filter/filter.c
CC ../../../ulab/code/compare/compare.c
CC ../../../ulab/code/approx/approx.c
CC ../../../ulab/code/user/user.c
CC ../../../ulab/code/ulab.c
... ...


But if use minimal port, it's not built actually:
Including User C Module from ../../../ulab/code
... ...


Is it possible to build with minimal port?

Thanks.

yang8621
Posts: 4
Joined: Mon Jul 20, 2020 8:45 am

Re: How to add a C module to minimal port?

Post by yang8621 » Mon Sep 07, 2020 8:52 am

manually append SRC_MOD to SRC_C to build the C module.
But there are so many compiling errors.
Seems there are some dependencies to enable C module with minimal port. any flag to enable?

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

Re: How to add a C module to minimal port?

Post by jimmo » Tue Sep 08, 2020 1:14 am

yang8621 wrote:
Mon Sep 07, 2020 8:52 am
manually append SRC_MOD to SRC_C to build the C module.
In order to build the user module, it needs SRC_MOD, CFLAGS_MOD, and CFLAGS_EXTRA

Here's the diff I applied to minimal/Makefile to make it work with a simple demo module:

Code: Select all

diff --git a/ports/minimal/Makefile b/ports/minimal/Makefile
index bce544ec1..d13be7ba0 100644
--- a/ports/minimal/Makefile
+++ b/ports/minimal/Makefile
@@ -41,6 +41,8 @@ CFLAGS += -Os -DNDEBUG
 CFLAGS += -fdata-sections -ffunction-sections
 endif
 
+CFLAGS += $(CFLAGS_MOD) $(CFLAGS_EXTRA)
+
 LIBS =
 
 SRC_C = \
@@ -51,6 +53,7 @@ SRC_C = \
        lib/utils/pyexec.c \
        lib/mp-readline/readline.c \
        $(BUILD)/_frozen_mpy.c \
+       $(SRC_MOD) \
 
 ifeq ($(CROSS), 1)
 SRC_C += lib/libc/string0.c
yang8621 wrote:
Mon Sep 07, 2020 8:52 am
But there are so many compiling errors.
All of the compile errors are due to the fact that the minimal port doesn't support floating point (which is important for ulab!).

So you need to add to minimal/mpconfigport.h

#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)

Then there will be linker errors for the math functions, so add -lm to the LDFLAGS.

Then it will be missing builtin slice and array support, so add

#define MICROPY_PY_ARRAY (1)
#define MICROPY_PY_BUILTINS_SLICE (1)
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
#define MICROPY_PY_BUILTINS_SLICE_ATTRS (1)
#define MICROPY_PY_BUILTINS_SLICE_INDICES (1)

to minimal/mpconfigport.h (removing the existing lines that set these to zero)


That will all work if you're using the native build of the minimal port. But if you're using the cross compiler, you might need additional work instead of -lm to add the libm functions (look at the stm32 port for example).

yang8621
Posts: 4
Joined: Mon Jul 20, 2020 8:45 am

Re: How to add a C module to minimal port?

Post by yang8621 » Tue Sep 08, 2020 5:34 am

Thanks. It's very helpful.

Post Reply