Skip to content

Commit

Permalink
py/py.mk: Support C++ code for user C modules.
Browse files Browse the repository at this point in the history
Support C++ code in .cpp files by providing CXX counterparts of the
_USERMOD_ flags we have for C already.  This merely enables the Makefile of
user C modules to use variables specific to C++ compilation, it is still up
to each port's main Makefile to also include these in the build.
  • Loading branch information
stinos authored and dpgeorge committed Oct 29, 2020
1 parent 78c8b55 commit 0153148
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 5 additions & 4 deletions docs/develop/cmodules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ limitations with the Python environment, often due to an inability to access
certain hardware resources or Python speed limitations.

If your limitations can't be resolved with suggestions in :ref:`speed_python`,
writing some or all of your module in C is a viable option.
writing some or all of your module in C (and/or C++ if implemented for your port)
is a viable option.

If your module is designed to access or work with commonly available
hardware or libraries please consider implementing it inside the MicroPython
Expand All @@ -29,7 +30,7 @@ Structure of an external C module

A MicroPython user C module is a directory with the following files:

* ``*.c`` and/or ``*.h`` source code files for your module.
* ``*.c`` / ``*.cpp`` / ``*.h`` source code files for your module.

These will typically include the low level functionality being implemented and
the MicroPython binding functions to expose the functions and module(s).
Expand All @@ -44,12 +45,12 @@ 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 C files relative to your
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``

If you have custom ``CFLAGS`` settings or include folders to define, these
should be added to ``CFLAGS_USERMOD``.
should be added to ``CFLAGS_USERMOD``, or ``CXXFLAGS_USERMOD``.

See below for full usage example.

Expand Down
4 changes: 4 additions & 0 deletions py/py.mk
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ ifneq ($(USER_C_MODULES),)
# pre-define USERMOD variables as expanded so that variables are immediate
# expanded as they're added to them
SRC_USERMOD :=
SRC_USERMOD_CXX :=
CFLAGS_USERMOD :=
CXXFLAGS_USERMOD :=
LDFLAGS_USERMOD :=
$(foreach module, $(wildcard $(USER_C_MODULES)/*/micropython.mk), \
$(eval USERMOD_DIR = $(patsubst %/,%,$(dir $(module))))\
Expand All @@ -42,7 +44,9 @@ $(foreach module, $(wildcard $(USER_C_MODULES)/*/micropython.mk), \
)

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)
endif

Expand Down

0 comments on commit 0153148

Please sign in to comment.