Skip to content

Commit 16d7cd0

Browse files
authored
Merge pull request #37 from emlearn/user-module-build
Extmod: Merge .py module code for trees, setup Unix build and tests in CI
2 parents cc273c6 + 539a10e commit 16d7cd0

File tree

11 files changed

+48
-11
lines changed

11 files changed

+48
-11
lines changed

.github/workflows/build.yaml

+7-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
runs-on: ubuntu-24.04
1111
env:
1212
MPY_DIR: ./micropython
13-
MICROPYTHON_BIN: ./micropython/ports/unix/build-standard/micropython
13+
MICROPYTHON_BIN: ./micropython/ports/unix/build-nomodules/micropython
1414
steps:
1515
- uses: actions/checkout@v4
1616
with:
@@ -29,8 +29,12 @@ jobs:
2929
run: pip install -r requirements.txt
3030
- name: Setup MicroPython X86
3131
working-directory: micropython
32-
run: source tools/ci.sh && ci_unix_32bit_setup && ci_unix_standard_build
33-
- name: Run test and build module x64
32+
run: |
33+
source tools/ci.sh && ci_unix_32bit_setup && ci_unix_standard_build
34+
mv ./ports/unix/build-standard/ ./ports/unix/build-nomodules/
35+
- name: Build custom firmware with user modules, and tests. Unix/x64
36+
run: make check_unix V=1
37+
- name: Build .mpy modules and run tests Unix/x64
3438
run: make check ARCH=x64 V=1
3539
- name: Setup MicroPython ARM
3640
working-directory: micropython

Makefile

+20-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ VERSION := $(shell git describe --tags --always)
88

99
MPY_DIR_ABS = $(abspath $(MPY_DIR))
1010

11+
C_MODULES_SRC_PATH = $(abspath ./src)
12+
MANIFEST_PATH = $(abspath ./src/manifest.py)
13+
14+
PORT=unix
1115
MODULES_PATH = ./dist/$(ARCH)_$(MPY_ABI_VERSION)
16+
PORT_DIR = ./dist/ports/$(PORT)
17+
UNIX_MICROPYTHON = ./dist/ports/unix/micropython
1218

1319
$(MODULES_PATH)/emlearn_trees.mpy:
1420
make -C src/emlearn_trees/ ARCH=$(ARCH) MPY_DIR=$(MPY_DIR_ABS) V=1 clean dist
@@ -61,7 +67,20 @@ emlearn_iir_q15.results: $(MODULES_PATH)/emlearn_iir_q15.mpy
6167
emlearn_arrayutils.results: $(MODULES_PATH)/emlearn_arrayutils.mpy
6268
MICROPYPATH=$(MODULES_PATH) $(MICROPYTHON_BIN) tests/test_arrayutils.py
6369

64-
.PHONY: clean
70+
$(PORT_DIR):
71+
mkdir -p $@
72+
73+
$(UNIX_MICROPYTHON): $(PORT_DIR)
74+
make -C $(MPY_DIR)/ports/unix V=1 USER_C_MODULES=$(C_MODULES_SRC_PATH) FROZEN_MANIFEST=$(MANIFEST_PATH) CFLAGS_EXTRA='-Wno-unused-function -Wno-unused-function' -j4
75+
cp $(MPY_DIR)/ports/unix/build-standard/micropython $@
76+
77+
unix: $(UNIX_MICROPYTHON)
78+
79+
check_unix: $(UNIX_MICROPYTHON)
80+
$(UNIX_MICROPYTHON) tests/test_trees.py
81+
$(UNIX_MICROPYTHON) tests/test_iir.py
82+
83+
.PHONY: clean unix
6584

6685
clean:
6786
make -C src/emlearn_trees/ ARCH=$(ARCH) MPY_DIR=$(MPY_DIR_ABS) V=1 clean

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
emlearn>=0.21.1
1+
emlearn>=0.21.2
22
scikit-learn>=1.0.0
33
ar>=1.0.0
44
pyelftools>=0.31

src/emlearn_fft/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ $(DIST_FILE): $(MOD).mpy $(DIST_DIR)
3333
include $(MPY_DIR)/py/dynruntime.mk
3434

3535

36-
CFLAGS += -I$(EMLEARN_DIR)
36+
CFLAGS += -I$(EMLEARN_DIR) -Wno-unused-function
3737

3838
dist: $(DIST_FILE)

src/emlearn_iir/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ $(DIST_FILE): $(MOD).mpy $(DIST_DIR)
3232
# Include to get the rules for compiling and linking the module
3333
include $(MPY_DIR)/py/dynruntime.mk
3434

35-
CFLAGS += -I$(EMLEARN_DIR)
35+
CFLAGS += -I$(EMLEARN_DIR) -Wno-unused-function
3636

3737
dist: $(DIST_FILE)

src/emlearn_iir_q15/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ LIBGCC_FILENAME = $(shell $(CROSS)gcc $(CFLAGS) -print-libgcc-file-name)
4141
$(info $(LIBGCC_FILENAME))
4242

4343
CFLAGS += -I$(CMSIS_DSP_DIR)/Include
44+
CFLAGS += -Wno-unused-function
4445

4546
$(CMSIS_DSP_DIR)/iir_q15.patched:
4647
cd $(CMSIS_DSP_DIR) && git apply -v ../df1_q15_disable_dsp.patch

src/emlearn_neighbors/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ $(DIST_DIR):
3333
$(DIST_FILE): $(MOD).mpy $(DIST_DIR)
3434
cp $< $@
3535

36-
CFLAGS += -I$(EMLEARN_DIR)
36+
CFLAGS += -I$(EMLEARN_DIR) -Wno-unused-function
3737

3838
dist: $(DIST_FILE)

src/emlearn_trees/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ DIST_DIR := ../../dist/$(ARCH)_$(MPY_ABI_VERSION)
1919
MOD = emlearn_trees
2020

2121
# Source files (.c or .py)
22-
SRC = trees.c trees.py
22+
SRC = trees.c emlearn_trees.py
2323

2424
# Releases
2525
DIST_FILE = $(DIST_DIR)/$(MOD).mpy
@@ -32,6 +32,6 @@ $(DIST_FILE): $(MOD).mpy $(DIST_DIR)
3232
# Include to get the rules for compiling and linking the module
3333
include $(MPY_DIR)/py/dynruntime.mk
3434

35-
CFLAGS += -I$(EMLEARN_DIR) -DDYNAMIC_RUNTIME=1
35+
CFLAGS += -I$(EMLEARN_DIR) -Wno-unused-function
3636

3737
dist: $(DIST_FILE)

src/emlearn_trees/trees.py renamed to src/emlearn_trees/emlearn_trees.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11

2+
# When used as external C module, the .py is the top-level import,
3+
# and we need to merge the native module symbols at import time
4+
# When used as dynamic native modules (.mpy), .py and native code is merged at build time
5+
try:
6+
from emlearn_trees_c import *
7+
except ImportError as e:
8+
pass
29

310
def load_model(builder, f):
411

src/emlearn_trees/trees.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ const mp_obj_module_t emlearn_trees_cmodule = {
332332
.globals = (mp_obj_dict_t *)&emlearn_trees_globals,
333333
};
334334

335-
MP_REGISTER_MODULE(MP_QSTR_emlearn_trees, emlearn_trees_cmodule);
335+
// External module name is XXX_c to allow .py file to be the entrypoint
336+
MP_REGISTER_MODULE(MP_QSTR_emlearn_trees_c, emlearn_trees_cmodule);
336337

337338
#endif
338339

src/manifest.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
# Manifest is used to include .py files for external C module build
3+
# NOTE: this is a different mechanism than
4+
# Ref https://docs.micropython.org/en/latest/reference/manifest.html
5+
module("emlearn_trees.py", base_path='./emlearn_trees')

0 commit comments

Comments
 (0)