8000 py/py.mk: Make user-C-module handling self-contained in py.mk. · micropython/micropython@5c6feb6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5c6feb6

Browse files
jimmoletgo0007
authored andcommitted
py/py.mk: Make user-C-module handling self-contained in py.mk.
Removes the need for the port to add anything to OBJS or SRC_QSTR. Also makes it possible for user-C-modules to differentiate between code that should be processed for QSTR vs other files (e.g. helpers and libraries). Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
1 parent 91e0c94 commit 5c6feb6

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

docs/develop/cmodules.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,17 @@ A MicroPython user C module is a directory with the following files:
4949
in your ``micropython.mk`` to a local make variable,
5050
eg ``EXAMPLE_MOD_DIR := $(USERMOD_DIR)``
5151

52-
Your ``micropython.mk`` must add your modules source files relative to your
53-
expanded copy of ``$(USERMOD_DIR)`` to ``SRC_USERMOD``, eg
54-
``SRC_USERMOD += $(EXAMPLE_MOD_DIR)/example.c``
52+
Your ``micropython.mk`` must add your modules source files to the
53+
``SRC_USERMOD_C`` or ``SRC_USERMOD_LIB_C`` variables. The former will be
54+
processed for ``MP_QSTR_`` and ``MP_REGISTER_MODULE`` definitions, the latter
55+
will not (e.g. helpers and library code that isn't MicroPython-specific).
56+
These paths should include your expaned copy of ``$(USERMOD_DIR)``, e.g.::
57+
58+
SRC_USERMOD_C += $(EXAMPLE_MOD_DIR)/modexample.c
59+
SRC_USERMOD_LIB_C += $(EXAMPLE_MOD_DIR)/utils/algorithm.c
60+
61+
Similarly, use ``SRC_USERMOD_CXX`` and ``SRC_USERMOD_LIB_CXX`` for C++
62+
source files.
5563

5664
If you have custom compiler options (like ``-I`` to add directories to search
5765
for header files), these should be added to ``CFLAGS_USERMOD`` for C code

ports/samd/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ endif
8181
# Flags for optional C++ source code
8282
CXXFLAGS += $(filter-out -std=c99,$(CFLAGS))
8383
CXXFLAGS += $(CXXFLAGS_MOD)
84-
ifneq ($(SRC_CXX)$(SRC_MOD_CXX),)
84+
# TODO make this common -- shouldn't be using these "private" vars from py.mk
85+
ifneq ($(SRC_CXX)$(SRC_USERMOD_CXX)$(SRC_USERMOD_LIB_CXX),)
8586
LIBSTDCPP_FILE_NAME = "$(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a)"
8687
LDFLAGS += -L"$(shell dirname $(LIBSTDCPP_FILE_NAME))"
8788
endif

ports/stm32/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ endif
157157
# Flags for optional C++ source code
158158
CXXFLAGS += $(filter-out -Wmissing-prototypes -Wold-style-definition -std=gnu99,$(CFLAGS))
159159
CXXFLAGS += $(CXXFLAGS_MOD)
160-
ifneq ($(SRC_CXX)$(SRC_MOD_CXX),)
160+
# TODO make this common -- shouldn't be using these "private" vars from py.mk
161+
ifneq ($(SRC_CXX)$(SRC_USERMOD_CXX)$(SRC_USERMOD_LIB_CXX),)
161162
LIBSTDCPP_FILE_NAME = "$(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a)"
162163
LDFLAGS += -L"$(shell dirname $(LIBSTDCPP_FILE_NAME))"
163164
endif

py/py.mk

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,44 @@ endif
3232
ifneq ($(USER_C_MODULES),)
3333
# pre-define USERMOD variables as expanded so that variables are immediate
3434
# expanded as they're added to them
35-
SRC_USERMOD :=
35+
36+
# C/C++ files that are included in the QSTR/module build
37+
SRC_USERMOD_C :=
3638
SRC_USERMOD_CXX :=
39+
# Other C/C++ files (e.g. libraries or helpers)
40+
SRC_USERMOD_LIB_C :=
41+
SRC_USERMOD_LIB_CXX :=
42+
# Optionally set flags
3743
CFLAGS_USERMOD :=
3844
CXXFLAGS_USERMOD :=
3945
LDFLAGS_USERMOD :=
46+
47+
# Backwards compatibility with older user c modules that set SRC_USERMOD
48+
# added to SRC_USERMOD_C below
49+
SRC_USERMOD :=
50+
4051
$(foreach module, $(wildcard $(USER_C_MODULES)/*/micropython.mk), \
4152
$(eval USERMOD_DIR = $(patsubst %/,%,$(dir $(module))))\
4253
$(info Including User C Module from $(USERMOD_DIR))\
4354
$(eval include $(module))\
4455
)
4556

46-
SRC_MOD += $(patsubst $(USER_C_MODULES)/%.c,%.c,$(SRC_USERMOD))
47-
SRC_MOD_CXX += $(patsubst $(USER_C_MODULES)/%.cpp,%.cpp,$(SRC_USERMOD_CXX))
48-
CFLAGS_MOD += $(CFLAGS_USERMOD)
49-
CXXFLAGS_MOD += $(CXXFLAGS_USERMOD)
50-
LDFLAGS_MOD += $(LDFLAGS_USERMOD)
57+
SRC_USERMOD_C += $(SRC_USERMOD)
58+
59+
SRC_USERMOD_PATHFIX_C += $(patsubst $(USER_C_MODULES)/%.c,%.c,$(SRC_USERMOD_C))
60+
SRC_USERMOD_PATHFIX_CXX += $(patsubst $(USER_C_MODULES)/%.cpp,%.cpp,$(SRC_USERMOD_CXX))
61+
SRC_USERMOD_PATHFIX_LIB_C += $(patsubst $(USER_C_MODULES)/%.c,%.c,$(SRC_USERMOD_LIB_C))
62+
SRC_USERMOD_PATHFIX_LIB_CXX += $(patsubst $(USER_C_MODULES)/%.cpp,%.cpp,$(SRC_USERMOD_LIB_CXX))
63+
64+
CFLAGS += $(CFLAGS_USERMOD)
65+
CXXFLAGS += $(CXXFLAGS_USERMOD)
66+
LDFLAGS += $(LDFLAGS_USERMOD)
67+
68+
SRC_QSTR += $(SRC_USERMOD_PATHFIX_C) $(SRC_USERMOD_PATHFIX_CXX)
69+
PY_O += $(addprefix $(BUILD)/, $(SRC_USERMOD_PATHFIX_C:.c=.o))
70+
PY_O += $(addprefix $(BUILD)/, $(SRC_USERMOD_PATHFIX_CXX:.cpp=.o))
71+
PY_O += $(addprefix $(BUILD)/, $(SRC_USERMOD_PATHFIX_LIB_C:.c=.o))
72+
PY_O += $(addprefix $(BUILD)/, $(SRC_USERMOD_PATHFIX_LIB_CXX:.cpp=.o))
5173
endif
5274

5375
# py object files

0 commit comments

Comments
 (0)
0