Skip to content

Commit d87f42b

Browse files
committed
examples/usercmodules: Simplify user C module enabling.
It's a bit of a pitfall with user C modules that including them in the build does not automatically enable them. This commit changes the docs and examples for user C modules to encourage writers of user C modules to enable them unconditionally. This makes things simpler and covers most use cases. See discussion in issue adafruit#6960, and also adafruit#7086. Signed-off-by: Damien George <damien@micropython.org>
1 parent ec79e44 commit d87f42b

File tree

7 files changed

+22
-25
lines changed

7 files changed

+22
-25
lines changed

docs/develop/cmodules.rst

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,23 @@ applying 2 modifications:
149149
150150
- all modules found in this directory (or added via ``include`` in the top-level
151151
``micropython.cmake`` when using CMake) will be compiled, but only those which are
152-
explicitly enabled will be available for importing. Enabling a module is done
153-
by setting the preprocessor define from its module registration to 1.
152+
enabled will be available for importing. If a module is to always be enabled,
153+
which is usually the case for custom modules and custom builds, then it is
154+
enough to supply "1" as the third parameter to the registration macro, like:
154155

155-
For example if the source code defines the module with
156+
.. code-block:: c
157+
158+
MP_REGISTER_MODULE(MP_QSTR_cexample, example_user_cmodule, 1);
159+
160+
Alternatively, to make the module disabled by default but selectable through
161+
a preprocessor configuration option, use:
156162

157163
.. code-block:: c
158164
159165
MP_REGISTER_MODULE(MP_QSTR_cexample, example_user_cmodule, MODULE_CEXAMPLE_ENABLED);
160166
161167
162-
then ``MODULE_CEXAMPLE_ENABLED`` has to be set to 1 to make the module available.
168+
Then ``MODULE_CEXAMPLE_ENABLED`` has to be set to 1 to make the module available.
163169
This can be done by adding ``CFLAGS_EXTRA=-DMODULE_CEXAMPLE_ENABLED=1`` to
164170
the ``make`` command, or editing ``mpconfigport.h`` or ``mpconfigboard.h``
165171
to add
@@ -179,7 +185,7 @@ directory can be built for the unix port:
179185
.. code-block:: bash
180186
181187
cd micropython/ports/unix
182-
make USER_C_MODULES=../../examples/usercmodule CFLAGS_EXTRA=-DMODULE_CEXAMPLE_ENABLED=1 all
188+
make USER_C_MODULES=../../examples/usercmodule all
183189
184190
The build output will show the modules found::
185191

@@ -205,7 +211,6 @@ The CMake build output lists the modules by name::
205211
...
206212

207213

208-
Note that the ``micropython.cmake`` files define ``DMODULE_<name>_ENABLED=1`` automatically.
209214
The top-level ``micropython.cmake`` can be used to control which modules are enabled.
210215

211216

@@ -215,8 +220,7 @@ including both modules and building the stm32 port for example:
215220
.. code-block:: bash
216221
217222
cd my_project/micropython/ports/stm32
218-
make USER_C_MODULES=../../../modules \
219-
CFLAGS_EXTRA="-DMODULE_EXAMPLE1_ENABLED=1 -DMODULE_EXAMPLE2_ENABLED=1" all
223+
make USER_C_MODULES=../../../modules all
220224
221225
222226
Module usage in MicroPython

examples/usercmodule/cexample/examplemodule.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@ const mp_obj_module_t example_user_cmodule = {
3131
};
3232

3333
// Register the module to make it available in Python.
34-
MP_REGISTER_MODULE(MP_QSTR_cexample, example_user_cmodule, MODULE_CEXAMPLE_ENABLED);
34+
// Note: the "1" in the third argument means this module is always enabled.
35+
// This "1" can be optionally replaced with a macro like MODULE_CEXAMPLE_ENABLED
36+
// which can then be used to conditionally enable this module.
37+
MP_REGISTER_MODULE(MP_QSTR_cexample, example_user_cmodule, 1);

examples/usercmodule/cexample/micropython.cmake

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,5 @@ target_include_directories(usermod_cexample INTERFACE
1111
${CMAKE_CURRENT_LIST_DIR}
1212
)
1313

14-
# Enable the module automatically by adding the relevant compile definitions.
15-
target_compile_definitions(usermod_cexample INTERFACE
16-
MODULE_CEXAMPLE_ENABLED=1
17-
)
18-
1914
# Link our INTERFACE library to the usermod target.
2015
target_link_libraries(usermod INTERFACE usermod_cexample)
21-

examples/usercmodule/cppexample/examplemodule.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ const mp_obj_module_t cppexample_user_cmodule = {
2222
};
2323

2424
// Register the module to make it available in Python.
25-
MP_REGISTER_MODULE(MP_QSTR_cppexample, cppexample_user_cmodule, MODULE_CPPEXAMPLE_ENABLED);
25+
// Note: the "1" in the third argument means this module is always enabled.
26+
// This "1" can be optionally replaced with a macro like MODULE_CPPEXAMPLE_ENABLED
27+
// which can then be used to conditionally enable this module.
28+
MP_REGISTER_MODULE(MP_QSTR_cppexample, cppexample_user_cmodule, 1);

examples/usercmodule/cppexample/micropython.cmake

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,5 @@ target_include_directories(usermod_cppexample INTERFACE
1212
${CMAKE_CURRENT_LIST_DIR}
1313
)
1414

15-
# Enable the module automatically by adding the relevant compile definitions.
16-
target_compile_definitions(usermod_cppexample INTERFACE
17-
MODULE_CPPEXAMPLE_ENABLED=1
18-
)
19-
2015
# Link our INTERFACE library to the usermod target.
2116
target_link_libraries(usermod INTERFACE usermod_cppexample)
22-

ports/unix/variants/coverage/mpconfigvariant.mk

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ CFLAGS += \
77
-fprofile-arcs -ftest-coverage \
88
-Wformat -Wmissing-declarations -Wmissing-prototypes \
99
-Wold-style-definition -Wpointer-arith -Wshadow -Wuninitialized -Wunused-parameter \
10-
-DMICROPY_UNIX_COVERAGE \
11-
-DMODULE_CEXAMPLE_ENABLED=1 -DMODULE_CPPEXAMPLE_ENABLED=1
10+
-DMICROPY_UNIX_COVERAGE
1211

1312
LDFLAGS += -fprofile-arcs -ftest-coverage
1413

tools/ci.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ function ci_stm32_pyb_build {
211211
make ${MAKEOPTS} -C mpy-cross
212212
make ${MAKEOPTS} -C ports/stm32 submodules
213213
git submodule update --init lib/btstack
214-
make ${MAKEOPTS} -C ports/stm32 BOARD=PYBV11 MICROPY_PY_WIZNET5K=5200 MICROPY_PY_CC3K=1 USER_C_MODULES=../../examples/usercmodule CFLAGS_EXTRA="-DMODULE_CEXAMPLE_ENABLED=1 -DMODULE_CPPEXAMPLE_ENABLED=1"
214+
make ${MAKEOPTS} -C ports/stm32 BOARD=PYBV11 MICROPY_PY_WIZNET5K=5200 MICROPY_PY_CC3K=1 USER_C_MODULES=../../examples/usercmodule
215215
make ${MAKEOPTS} -C ports/stm32 BOARD=PYBD_SF2
216216
make ${MAKEOPTS} -C ports/stm32 BOARD=PYBD_SF6 NANBOX=1 MICROPY_BLUETOOTH_NIMBLE=0 MICROPY_BLUETOOTH_BTSTACK=1
217217
make ${MAKEOPTS} -C ports/stm32/mboot BOARD=PYBV10 CFLAGS_EXTRA='-DMBOOT_FSLOAD=1 -DMBOOT_VFS_LFS2=1'

0 commit comments

Comments
 (0)