docs/develop/cmodules.rst: Document C-modules and micropython.cmake.

Documents the micropython.cmake file required to make user C modules
compatible with the CMake build system.

Signed-off-by: Phil Howard <phil@pimoroni.com>
This commit is contained in:
Phil Howard 2021-02-24 12:57:53 +00:00 committed by Damien George
parent cc497d4c6a
commit 8e5756e2b6

View File

@ -53,6 +53,30 @@ A MicroPython user C module is a directory with the following files:
for header files), these should be added to ``CFLAGS_USERMOD`` for C code for header files), these should be added to ``CFLAGS_USERMOD`` for C code
and to ``CXXFLAGS_USERMOD`` for C++ code. and to ``CXXFLAGS_USERMOD`` for C++ code.
* ``micropython.cmake`` contains the CMake configuration for this module.
In ``micropython.cmake``, you may use ``${CMAKE_CURRENT_LIST_DIR}`` as the path to
the current module.
Your ``micropython.cmake`` should define an ``INTERFACE`` library and associate
your source files, compile definitions and include directories with it.
The library should then be linked to the ``usermod`` target.
.. code-block:: cmake
add_library(usermod_cexample INTERFACE)
target_sources(usermod_cexample INTERFACE
${CMAKE_CURRENT_LIST_DIR}/examplemodule.c
)
target_include_directories(usermod_cexample INTERFACE
${CMAKE_CURRENT_LIST_DIR}
)
target_link_libraries(usermod INTERFACE usermod_cexample)
See below for full usage example. See below for full usage example.
@ -70,9 +94,11 @@ and has a source file and a Makefile fragment with content as descibed above::
└──usercmodule/ └──usercmodule/
└──cexample/ └──cexample/
├── examplemodule.c ├── examplemodule.c
└── micropython.mk ├── micropython.mk
└── micropython.cmake
Refer to the comments in these 2 files for additional explanation.
Refer to the comments in these files for additional explanation.
Next to the ``cexample`` module there's also ``cppexample`` which Next to the ``cexample`` module there's also ``cppexample`` which
works in the same way but shows one way of mixing C and C++ code works in the same way but shows one way of mixing C and C++ code
in MicroPython. in MicroPython.
@ -97,10 +123,13 @@ applying 2 modifications:
├── modules/ ├── modules/
│ └──example1/ │ └──example1/
│ ├──example1.c │ ├──example1.c
│ └──micropython.mk │ ├──micropython.mk
│ └──micropython.cmake
│ └──example2/ │ └──example2/
│ ├──example2.c │ ├──example2.c
│ └──micropython.mk │ ├──micropython.mk
│ └──micropython.cmake
│ └──micropython.cmake
└── micropython/ └── micropython/
├──ports/ ├──ports/
... ├──stm32/ ... ├──stm32/
@ -109,10 +138,21 @@ applying 2 modifications:
with ``USER_C_MODULES`` set to the ``my_project/modules`` directory. with ``USER_C_MODULES`` set to the ``my_project/modules`` directory.
- all modules found in this directory will be compiled, but only those A top level ``micropython.cmake`` - found directly in the ``my_project/modules``
which are explicitly enabled will be availabe for importing. Enabling a directory - should ``include`` all of your modules.
module is done by setting the preprocessor define from its module
registration to 1. For example if the source code defines the module with .. code-block:: cmake
include(${CMAKE_CURRENT_LIST_DIR}/example1/micropython.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/example2/micropython.cmake)
- all modules found in this directory (or added via ``include`` in the top-level
``micropython.cmake`` when using CMake) will be compiled, but only those which are
explicitly enabled will be available for importing. Enabling a module is done
by setting the preprocessor define from its module registration to 1.
For example if the source code defines the module with
.. code-block:: c .. code-block:: c
@ -149,6 +189,26 @@ The build output will show the modules found::
... ...
For a CMake-based port such as rp2, this will look a little different:
.. code-block:: bash
cd micropython/ports/rp2
make USER_C_MODULES=../../examples/usercmodule all
The CMake build output lists the modules by name::
...
Including User C Module(s) from ../../examples/usercmodule/micropython.cmake
Found User C Module(s): usermod_cexample, usermod_cppexample
...
Note that the ``micropython.cmake`` files define ``DMODULE_<name>_ENABLED=1`` automatically.
The top-level ``micropython.cmake`` can be used to control which modules are enabled.
Or for your own project with a directory structure as shown above, Or for your own project with a directory structure as shown above,
including both modules and building the stm32 port for example: including both modules and building the stm32 port for example: