summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2022-10-08 23:28:24 +1100
committerDamien George <damien@micropython.org>2022-10-11 23:23:18 +1100
commit3cc6decfc4d6b459014940ae01f90d4f1fae0da6 (patch)
tree8d43d29c91b2de7da2c6db42b179d708b8715b86
parent67d05ed02b26b6ba2dcc638f2deed937a08417d7 (diff)
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>
-rw-r--r--docs/develop/cmodules.rst14
-rw-r--r--ports/samd/Makefile3
-rw-r--r--ports/stm32/Makefile3
-rw-r--r--py/py.mk34
4 files changed, 43 insertions, 11 deletions
diff --git a/docs/develop/cmodules.rst b/docs/develop/cmodules.rst
index 143057f7f..1b3ba04da 100644
--- a/docs/develop/cmodules.rst
+++ b/docs/develop/cmodules.rst
@@ -49,9 +49,17 @@ A MicroPython user C module is a directory with the following files:
in your ``micropython.mk`` to a local make variable,
eg ``EXAMPLE_MOD_DIR := $(USERMOD_DIR)``
- Your ``micropython.mk`` must add your modules source files relative to your
- expanded copy of ``$(USERMOD_DIR)`` to ``SRC_USERMOD``, eg
- ``SRC_USERMOD += $(EXAMPLE_MOD_DIR)/example.c``
+ Your ``micropython.mk`` must add your modules source files to the
+ ``SRC_USERMOD_C`` or ``SRC_USERMOD_LIB_C`` variables. The former will be
+ processed for ``MP_QSTR_`` and ``MP_REGISTER_MODULE`` definitions, the latter
+ will not (e.g. helpers and library code that isn't MicroPython-specific).
+ These paths should include your expaned copy of ``$(USERMOD_DIR)``, e.g.::
+
+ SRC_USERMOD_C += $(EXAMPLE_MOD_DIR)/modexample.c
+ SRC_USERMOD_LIB_C += $(EXAMPLE_MOD_DIR)/utils/algorithm.c
+
+ Similarly, use ``SRC_USERMOD_CXX`` and ``SRC_USERMOD_LIB_CXX`` for C++
+ source files.
If you have custom compiler options (like ``-I`` to add directories to search
for header files), these should be added to ``CFLAGS_USERMOD`` for C code
diff --git a/ports/samd/Makefile b/ports/samd/Makefile
index 735d25bf2..9386a8dac 100644
--- a/ports/samd/Makefile
+++ b/ports/samd/Makefile
@@ -81,7 +81,8 @@ endif
# Flags for optional C++ source code
CXXFLAGS += $(filter-out -std=c99,$(CFLAGS))
CXXFLAGS += $(CXXFLAGS_MOD)
-ifneq ($(SRC_CXX)$(SRC_MOD_CXX),)
+# TODO make this common -- shouldn't be using these "private" vars from py.mk
+ifneq ($(SRC_CXX)$(SRC_USERMOD_CXX)$(SRC_USERMOD_LIB_CXX),)
LIBSTDCPP_FILE_NAME = "$(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a)"
LDFLAGS += -L"$(shell dirname $(LIBSTDCPP_FILE_NAME))"
endif
diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile
index 403b68b24..4e594d19f 100644
--- a/ports/stm32/Makefile
+++ b/ports/stm32/Makefile
@@ -157,7 +157,8 @@ endif
# Flags for optional C++ source code
CXXFLAGS += $(filter-out -Wmissing-prototypes -Wold-style-definition -std=gnu99,$(CFLAGS))
CXXFLAGS += $(CXXFLAGS_MOD)
-ifneq ($(SRC_CXX)$(SRC_MOD_CXX),)
+# TODO make this common -- shouldn't be using these "private" vars from py.mk
+ifneq ($(SRC_CXX)$(SRC_USERMOD_CXX)$(SRC_USERMOD_LIB_CXX),)
LIBSTDCPP_FILE_NAME = "$(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a)"
LDFLAGS += -L"$(shell dirname $(LIBSTDCPP_FILE_NAME))"
endif
diff --git a/py/py.mk b/py/py.mk
index 8aac460b4..ec69ca42d 100644
--- a/py/py.mk
+++ b/py/py.mk
@@ -32,22 +32,44 @@ endif
ifneq ($(USER_C_MODULES),)
# pre-define USERMOD variables as expanded so that variables are immediate
# expanded as they're added to them
-SRC_USERMOD :=
+
+# C/C++ files that are included in the QSTR/module build
+SRC_USERMOD_C :=
SRC_USERMOD_CXX :=
+# Other C/C++ files (e.g. libraries or helpers)
+SRC_USERMOD_LIB_C :=
+SRC_USERMOD_LIB_CXX :=
+# Optionally set flags
CFLAGS_USERMOD :=
CXXFLAGS_USERMOD :=
LDFLAGS_USERMOD :=
+
+# Backwards compatibility with older user c modules that set SRC_USERMOD
+# added to SRC_USERMOD_C below
+SRC_USERMOD :=
+
$(foreach module, $(wildcard $(USER_C_MODULES)/*/micropython.mk), \
$(eval USERMOD_DIR = $(patsubst %/,%,$(dir $(module))))\
$(info Including User C Module from $(USERMOD_DIR))\
$(eval include $(module))\
)
-SRC_MOD += $(patsubst $(USER_C_MODULES)/%.c,%.c,$(SRC_USERMOD))
-SRC_MOD_CXX += $(patsubst $(USER_C_MODULES)/%.cpp,%.cpp,$(SRC_USERMOD_CXX))
-CFLAGS_MOD += $(CFLAGS_USERMOD)
-CXXFLAGS_MOD += $(CXXFLAGS_USERMOD)
-LDFLAGS_MOD += $(LDFLAGS_USERMOD)
+SRC_USERMOD_C += $(SRC_USERMOD)
+
+SRC_USERMOD_PATHFIX_C += $(patsubst $(USER_C_MODULES)/%.c,%.c,$(SRC_USERMOD_C))
+SRC_USERMOD_PATHFIX_CXX += $(patsubst $(USER_C_MODULES)/%.cpp,%.cpp,$(SRC_USERMOD_CXX))
+SRC_USERMOD_PATHFIX_LIB_C += $(patsubst $(USER_C_MODULES)/%.c,%.c,$(SRC_USERMOD_LIB_C))
+SRC_USERMOD_PATHFIX_LIB_CXX += $(patsubst $(USER_C_MODULES)/%.cpp,%.cpp,$(SRC_USERMOD_LIB_CXX))
+
+CFLAGS += $(CFLAGS_USERMOD)
+CXXFLAGS += $(CXXFLAGS_USERMOD)
+LDFLAGS += $(LDFLAGS_USERMOD)
+
+SRC_QSTR += $(SRC_USERMOD_PATHFIX_C) $(SRC_USERMOD_PATHFIX_CXX)
+PY_O += $(addprefix $(BUILD)/, $(SRC_USERMOD_PATHFIX_C:.c=.o))
+PY_O += $(addprefix $(BUILD)/, $(SRC_USERMOD_PATHFIX_CXX:.cpp=.o))
+PY_O += $(addprefix $(BUILD)/, $(SRC_USERMOD_PATHFIX_LIB_C:.c=.o))
+PY_O += $(addprefix $(BUILD)/, $(SRC_USERMOD_PATHFIX_LIB_CXX:.cpp=.o))
endif
# py object files