Skip to content

Commit 713a38d

Browse files
authored
Merge pull request adafruit#1754 from dmazzella/dmazzella-patch-1
add support for USER_C_MODULES
2 parents b5e283a + 8428fa0 commit 713a38d

File tree

6 files changed

+151
-4
lines changed

6 files changed

+151
-4
lines changed

ports/nrf/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ ifeq ($(INTERNAL_LIBM),1)
209209
OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))
210210
endif
211211
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
212+
OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o))
212213

213214
$(BUILD)/$(FATFS_DIR)/ff.o: COPT += -Os
214215
$(filter $(PY_BUILD)/../extmod/vfs_fat_%.o, $(PY_O)): COPT += -Os

py/makemoduledefs.py

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env python
2+
3+
# This pre-processor parses provided objects' c files for
4+
# MP_REGISTER_MODULE(module_name, obj_module, enabled_define)
5+
# These are used to generate a header with the required entries for
6+
# "mp_rom_map_elem_t mp_builtin_module_table[]" in py/objmodule.c
7+
8+
from __future__ import print_function
9+
10+
import re
11+
import os
12+
import argparse
13+
14+
15+
pattern = re.compile(
16+
r"[\n;]\s*MP_REGISTER_MODULE\((.*?),\s*(.*?),\s*(.*?)\);",
17+
flags=re.DOTALL
18+
)
19+
20+
21+
def find_c_file(obj_file, vpath):
22+
""" Search vpaths for the c file that matches the provided object_file.
23+
24+
:param str obj_file: object file to find the matching c file for
25+
:param List[str] vpath: List of base paths, similar to gcc vpath
26+
:return: str path to c file or None
27+
"""
28+
c_file = None
29+
relative_c_file = os.path.splitext(obj_file)[0] + ".c"
30+
relative_c_file = relative_c_file.lstrip('/\\')
31+
for p in vpath:
32+
possible_c_file = os.path.join(p, relative_c_file)
33+
if os.path.exists(possible_c_file):
34+
c_file = possible_c_file
35+
break
36+
37+
return c_file
38+
39+
40+
def find_module_registrations(c_file):
41+
""" Find any MP_REGISTER_MODULE definitions in the provided c file.
42+
43+
:param str c_file: path to c file to check
44+
:return: List[(module_name, obj_module, enabled_define)]
45+
"""
46+
global pattern
47+
48+
if c_file is None:
49+
# No c file to match the object file, skip
50+
return set()
51+
52+
with open(c_file) as c_file_obj:
53+
return set(re.findall(pattern, c_file_obj.read()))
54+
55+
56+
def generate_module_table_header(modules):
57+
""" Generate header with module table entries for builtin modules.
58+
59+
:param List[(module_name, obj_module, enabled_define)] modules: module defs
60+
:return: None
61+
"""
62+
63+
# Print header file for all external modules.
64+
mod_defs = []
65+
print("// Automatically generated by makemoduledefs.py.\n")
66+
for module_name, obj_module, enabled_define in modules:
67+
mod_def = "MODULE_DEF_{}".format(module_name.upper())
68+
mod_defs.append(mod_def)
69+
print((
70+
"#if ({enabled_define})\n"
71+
" extern const struct _mp_obj_module_t {obj_module};\n"
72+
" #define {mod_def} {{ MP_ROM_QSTR({module_name}), MP_ROM_PTR(&{obj_module}) }},\n"
73+
"#else\n"
74+
" #define {mod_def}\n"
75+
"#endif\n"
76+
).format(module_name=module_name, obj_module=obj_module,
77+
enabled_define=enabled_define, mod_def=mod_def)
78+
)
79+
80+
print("\n#define MICROPY_REGISTERED_MODULES \\")
81+
82+
for mod_def in mod_defs:
83+
print(" {mod_def} \\".format(mod_def=mod_def))
84+
85+
print("// MICROPY_REGISTERED_MODULES")
86+
87+
88+
def main():
89+
parser = argparse.ArgumentParser()
90+
parser.add_argument("--vpath", default=".",
91+
help="comma separated list of folders to search for c files in")
92+
parser.add_argument("files", nargs="*",
93+
help="list of c files to search")
94+
args = parser.parse_args()
95+
96+
vpath = [p.strip() for p in args.vpath.split(',')]
97+
98+
modules = set()
99+
for obj_file in args.files:
100+
c_file = find_c_file(obj_file, vpath)
101+
modules |= find_module_registrations(c_file)
102+
103+
generate_module_table_header(sorted(modules))
104+
105+
106+
if __name__ == '__main__':
107+
main()

py/mkrules.mk

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ endif
2020
# can be located. By following this scheme, it allows a single build rule
2121
# to be used to compile all .c files.
2222

23-
vpath %.S . $(TOP)
23+
vpath %.S . $(TOP) $(USER_C_MODULES)
2424
$(BUILD)/%.o: %.S
2525
$(STEPECHO) "CC $<"
2626
$(Q)$(CC) $(CFLAGS) -c -o $@ $<
2727

28-
vpath %.s . $(TOP)
28+
vpath %.s . $(TOP) $(USER_C_MODULES)
2929
$(BUILD)/%.o: %.s
3030
$(STEPECHO) "AS $<"
3131
$(Q)$(AS) -o $@ $<
@@ -42,7 +42,7 @@ $(Q)$(CC) $(CFLAGS) -c -MD -o $@ $<
4242
$(RM) -f $(@:.o=.d)
4343
endef
4444

45-
vpath %.c . $(TOP)
45+
vpath %.c . $(TOP) $(USER_C_MODULES)
4646
$(BUILD)/%.o: %.c
4747
$(call compile_c)
4848

@@ -56,7 +56,7 @@ $(BUILD)/%.o: %.c
5656

5757
QSTR_GEN_EXTRA_CFLAGS += -I$(BUILD)/tmp
5858

59-
vpath %.c . $(TOP)
59+
vpath %.c . $(TOP) $(USER_C_MODULES)
6060

6161
$(BUILD)/%.pp: %.c
6262
$(STEPECHO) "PreProcess $<"

py/obj.h

+7
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,13 @@ typedef struct _mp_rom_obj_t { mp_const_obj_t o; } mp_rom_obj_t;
338338
#define MP_DEFINE_CONST_STATICMETHOD_OBJ(obj_name, fun_name) const mp_rom_obj_static_class_method_t obj_name = {{&mp_type_staticmethod}, fun_name}
339339
#define MP_DEFINE_CONST_CLASSMETHOD_OBJ(obj_name, fun_name) const mp_rom_obj_static_class_method_t obj_name = {{&mp_type_classmethod}, fun_name}
340340

341+
// Declare a module as a builtin, processed by makemoduledefs.py
342+
// param module_name: MP_QSTR_<module name>
343+
// param obj_module: mp_obj_module_t instance
344+
// prarm enabled_define: used as `#if (enabled_define) around entry`
345+
346+
#define MP_REGISTER_MODULE(module_name, obj_module, enabled_define)
347+
341348
// Underlying map/hash table implementation (not dict object or map function)
342349

343350
typedef struct _mp_map_elem_t {

py/objmodule.c

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include "py/runtime.h"
3333
#include "py/builtin.h"
3434

35+
#include "genhdr/moduledefs.h"
36+
3537
STATIC void module_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
3638
(void)kind;
3739
mp_obj_module_t *self = MP_OBJ_TO_PTR(self_in);
@@ -252,6 +254,11 @@ STATIC const mp_rom_map_elem_t mp_builtin_module_table[] = {
252254
// extra builtin modules as defined by a port
253255
MICROPY_PORT_BUILTIN_MODULES
254256

257+
#ifdef MICROPY_REGISTERED_MODULES
258+
// builtin modules declared with MP_REGISTER_MODULE()
259+
MICROPY_REGISTERED_MODULES
260+
#endif
261+
255262
#if defined(MICROPY_DEBUG_MODULES) && defined(MICROPY_PORT_BUILTIN_DEBUG_MODULES)
256263
, MICROPY_PORT_BUILTIN_DEBUG_MODULES
257264
#endif

py/py.mk

+25
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,24 @@ $(BUILD)/$(BTREE_DIR)/%.o: CFLAGS += -Wno-old-style-definition -Wno-sign-compare
105105
$(BUILD)/extmod/modbtree.o: CFLAGS += $(BTREE_DEFS)
106106
endif
107107

108+
# External modules written in C.
109+
ifneq ($(USER_C_MODULES),)
110+
# pre-define USERMOD variables as expanded so that variables are immediate
111+
# expanded as they're added to them
112+
SRC_USERMOD :=
113+
CFLAGS_USERMOD :=
114+
LDFLAGS_USERMOD :=
115+
$(foreach module, $(wildcard $(USER_C_MODULES)/*/micropython.mk), \
116+
$(eval USERMOD_DIR = $(patsubst %/,%,$(dir $(module))))\
117+
$(info Including User C Module from $(USERMOD_DIR))\
118+
$(eval include $(module))\
119+
)
120+
121+
SRC_MOD += $(patsubst $(USER_C_MODULES)/%.c,%.c,$(SRC_USERMOD))
122+
CFLAGS_MOD += $(CFLAGS_USERMOD)
123+
LDFLAGS_MOD += $(LDFLAGS_USERMOD)
124+
endif
125+
108126
# py object files
109127
PY_CORE_O_BASENAME = $(addprefix py/,\
110128
mpstate.o \
@@ -287,6 +305,13 @@ $(HEADER_BUILD)/mpversion.h: FORCE | $(HEADER_BUILD)
287305
$(STEPECHO) "GEN $@"
288306
$(Q)$(PYTHON) $(PY_SRC)/makeversionhdr.py $@
289307

308+
# build a list of registered modules for py/objmodule.c.
309+
$(HEADER_BUILD)/moduledefs.h: $(SRC_QSTR) $(QSTR_GLOBAL_DEPENDENCIES) | $(HEADER_BUILD)/mpversion.h
310+
@$(ECHO) "GEN $@"
311+
$(Q)$(PYTHON) $(PY_SRC)/makemoduledefs.py --vpath="., $(TOP), $(USER_C_MODULES)" $(SRC_QSTR) > $@
312+
313+
SRC_QSTR += $(HEADER_BUILD)/moduledefs.h
314+
290315
# mpconfigport.mk is optional, but changes to it may drastically change
291316
# overall config, so they need to be caught
292317
MPCONFIGPORT_MK = $(wildcard mpconfigport.mk)

0 commit comments

Comments
 (0)