Skip to content

Commit 8677961

Browse files
committed
natmod: Allow linking static libraries.
Signed-off-by: Volodymyr Shymanskyy <vshymanskyi@gmail.com>
1 parent 495ce91 commit 8677961

File tree

8 files changed

+354
-23
lines changed

8 files changed

+354
-23
lines changed

.github/workflows/ports_esp32.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ jobs:
3434
run: source tools/ci.sh && echo "IDF_VER=$IDF_VER" | tee "$GITHUB_OUTPUT"
3535

3636
- name: Cached ESP-IDF install
37+
if: 0 # Temporary: skip cache for PR verification
3738
id: cache_esp_idf
3839
uses: actions/cache@v4
3940
with:

docs/develop/natmod.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ The known limitations are:
7070
So, if your C code has writable data, make sure the data is defined globally,
7171
without an initialiser, and only written to within functions.
7272

73+
The native module is not automatically linked against the standard static libraries
74+
like ``libm.a`` and ``libgcc.a``, which can lead to ``undefined symbol`` errors.
75+
You can link the runtime libraries by setting ``MICROPY_LINK_RUNTIME = 1``
76+
in your Makefile. Custom static libraries can also be linked by adding
77+
``MPY_LD_FLAGS += -l path/to/library.a``. Note that these are linked into
78+
the native module and will not be shared with other modules or the system.
79+
7380
Linker limitation: the native module is not linked against the symbol table of the
7481
full MicroPython firmware. Rather, it is linked against an explicit table of exported
7582
symbols found in ``mp_fun_table`` (in ``py/nativeglue.h``), that is fixed at firmware

examples/natmod/features2/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ SRC = main.c prod.c test.py
1010
# Architecture to build for (x86, x64, armv7m, xtensa, xtensawin)
1111
ARCH = x64
1212

13+
# Link with libm.a and libgcc.a from the toolchain
14+
MICROPY_LINK_RUNTIME = 1
15+
1316
# Include to get the rules for compiling and linking the module
1417
include $(MPY_DIR)/py/dynruntime.mk

examples/natmod/features2/main.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* This example demonstrates the following features in a native module:
22
- using floats
3+
- calling math functions from libm.a
34
- defining additional code in Python (see test.py)
45
- have extra C code in a separate file (see prod.c)
56
*/
@@ -10,6 +11,9 @@
1011
// Include the header for auxiliary C code for this module
1112
#include "prod.h"
1213

14+
// Include standard library header
15+
#include <math.h>
16+
1317
// Automatically detect if this module should include double-precision code.
1418
// If double precision is supported by the target architecture then it can
1519
// be used in native module regardless of what float setting the target
@@ -41,6 +45,12 @@ static mp_obj_t add_d(mp_obj_t x, mp_obj_t y) {
4145
static MP_DEFINE_CONST_FUN_OBJ_2(add_d_obj, add_d);
4246
#endif
4347

48+
// A function that uses libm
49+
static mp_obj_t call_round(mp_obj_t x) {
50+
return mp_obj_new_float_from_f(round(mp_obj_get_float_to_f(x)));
51+
}
52+
static MP_DEFINE_CONST_FUN_OBJ_1(round_obj, call_round);
53+
4454
// A function that computes the product of floats in an array.
4555
// This function uses the most general C argument interface, which is more difficult
4656
// to use but has access to the globals dict of the module via self->globals.
@@ -74,6 +84,7 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a
7484
#if USE_DOUBLE
7585
mp_store_global(MP_QSTR_add_d, MP_OBJ_FROM_PTR(&add_d_obj));
7686
#endif
87+
mp_store_global(MP_QSTR_round, MP_OBJ_FROM_PTR(&round_obj));
7788

7889
// The productf function uses the most general C argument interface
7990
mp_store_global(MP_QSTR_productf, MP_DYNRUNTIME_MAKE_FUNCTION(productf));

py/dynruntime.mk

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ CFLAGS += -Wall -Werror -DNDEBUG
2929
CFLAGS += -DNO_QSTR
3030
CFLAGS += -DMICROPY_ENABLE_DYNRUNTIME
3131
CFLAGS += -DMP_CONFIGFILE='<$(CONFIG_H)>'
32-
CFLAGS += -fpic -fno-common
33-
CFLAGS += -U _FORTIFY_SOURCE # prevent use of __*_chk libc functions
34-
#CFLAGS += -fdata-sections -ffunction-sections
32+
33+
MICROPY_ARCH_CFLAGS += -fpic -fno-common
34+
MICROPY_ARCH_CFLAGS += -U_FORTIFY_SOURCE # prevent use of __*_chk libc functions
35+
#MICROPY_ARCH_CFLAGS += -fdata-sections -ffunction-sections
3536

3637
MPY_CROSS_FLAGS += -march=$(ARCH)
3738

@@ -47,58 +48,57 @@ ifeq ($(ARCH),x86)
4748

4849
# x86
4950
CROSS =
50-
CFLAGS += -m32 -fno-stack-protector
51+
MICROPY_ARCH_CFLAGS += -m32 -fno-stack-protector
5152
MICROPY_FLOAT_IMPL ?= double
5253

5354
else ifeq ($(ARCH),x64)
5455

5556
# x64
5657
CROSS =
57-
CFLAGS += -fno-stack-protector
58+
MICROPY_ARCH_CFLAGS += -fno-stack-protector
5859
MICROPY_FLOAT_IMPL ?= double
5960

6061
else ifeq ($(ARCH),armv6m)
6162

6263
# thumb
6364
CROSS = arm-none-eabi-
64-
CFLAGS += -mthumb -mcpu=cortex-m0
65+
MICROPY_ARCH_CFLAGS += -mthumb -mcpu=cortex-m0
6566
MICROPY_FLOAT_IMPL ?= none
6667

6768
else ifeq ($(ARCH),armv7m)
6869

6970
# thumb
7071
CROSS = arm-none-eabi-
71-
CFLAGS += -mthumb -mcpu=cortex-m3
72+
MICROPY_ARCH_CFLAGS += -mthumb -mcpu=cortex-m3
7273
MICROPY_FLOAT_IMPL ?= none
7374

7475
else ifeq ($(ARCH),armv7emsp)
7576

7677
# thumb
7778
CROSS = arm-none-eabi-
78-
CFLAGS += -mthumb -mcpu=cortex-m4
79-
CFLAGS += -mfpu=fpv4-sp-d16 -mfloat-abi=hard
79+
MICROPY_ARCH_CFLAGS += -mthumb -mcpu=cortex-m4
80+
MICROPY_ARCH_CFLAGS += -mfpu=fpv4-sp-d16 -mfloat-abi=hard
8081
MICROPY_FLOAT_IMPL ?= float
8182

8283
else ifeq ($(ARCH),armv7emdp)
8384

8485
# thumb
8586
CROSS = arm-none-eabi-
86-
CFLAGS += -mthumb -mcpu=cortex-m7
87-
CFLAGS += -mfpu=fpv5-d16 -mfloat-abi=hard
87+
MICROPY_ARCH_CFLAGS += -mthumb -mcpu=cortex-m7
88+
MICROPY_ARCH_CFLAGS += -mfpu=fpv5-d16 -mfloat-abi=hard
8889
MICROPY_FLOAT_IMPL ?= double
8990

9091
else ifeq ($(ARCH),xtensa)
9192

9293
# xtensa
9394
CROSS = xtensa-lx106-elf-
94-
CFLAGS += -mforce-l32
95+
MICROPY_ARCH_CFLAGS += -mforce-l32
9596
MICROPY_FLOAT_IMPL ?= none
9697

9798
else ifeq ($(ARCH),xtensawin)
9899

99100
# xtensawin
100101
CROSS = xtensa-esp32-elf-
101-
CFLAGS +=
102102
MICROPY_FLOAT_IMPL ?= float
103103

104104
else ifeq ($(ARCH),rv32imc)
@@ -122,7 +122,13 @@ $(error architecture '$(ARCH)' not supported)
122122
endif
123123

124124
MICROPY_FLOAT_IMPL_UPPER = $(shell echo $(MICROPY_FLOAT_IMPL) | tr '[:lower:]' '[:upper:]')
125-
CFLAGS += -DMICROPY_FLOAT_IMPL=MICROPY_FLOAT_IMPL_$(MICROPY_FLOAT_IMPL_UPPER)
125+
CFLAGS += $(MICROPY_ARCH_CFLAGS) -DMICROPY_FLOAT_IMPL=MICROPY_FLOAT_IMPL_$(MICROPY_FLOAT_IMPL_UPPER)
126+
127+
ifeq ($(MICROPY_LINK_RUNTIME),1)
128+
LIBGCC_PATH := $(realpath $(shell $(CROSS)gcc $(CFLAGS) --print-libgcc-file-name))
129+
LIBM_PATH := $(realpath $(shell $(CROSS)gcc $(CFLAGS) --print-file-name=libm.a))
130+
MPY_LD_FLAGS += $(addprefix -l, $(LIBGCC_PATH) $(LIBM_PATH))
131+
endif
126132

127133
CFLAGS += $(CFLAGS_EXTRA)
128134

@@ -165,7 +171,7 @@ $(BUILD)/%.mpy: %.py
165171
# Build native .mpy from object files
166172
$(BUILD)/$(MOD).native.mpy: $(SRC_O)
167173
$(ECHO) "LINK $<"
168-
$(Q)$(MPY_LD) --arch $(ARCH) --qstrs $(CONFIG_H) -o $@ $^
174+
$(Q)$(MPY_LD) --arch $(ARCH) --qstrs $(CONFIG_H) $(MPY_LD_FLAGS) -o $@ $^
169175

170176
# Build final .mpy from all intermediate .mpy files
171177
$(MOD).mpy: $(BUILD)/$(MOD).native.mpy $(SRC_MPY)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy