GNU Radio 3.9 OOT Module Porting Guide: Difference between revisions
Line 19: | Line 19: | ||
(NOT MERGED - future changes) | (NOT MERGED - future changes) | ||
=== OOT Workflow === | As of the GNU Radio 3.9 release, python bindings are handled using pybind11, which is inherently different than they were in previous releases | ||
=== Dependencies === | |||
* pybind11 > 2.4.3 [https://pybind11.readthedocs.io/ https://pybind11.readthedocs.io/] | |||
* pygccxml [https://pygccxml.readthedocs.io/en/develop/install.html https://pygccxml.readthedocs.io/en/develop/install.html] | |||
** This is an optional dependency and basic functionality for OOT generation can be performed without pygccxml | |||
** It is required for automatically generating bindings for most of the GR source tree | |||
=== Components === | |||
Python bindings are contained in the <code>python/.../bindings</code> directory | |||
<source lang="sh">./python | |||
└── module_name | |||
├── bindings | |||
│ ├── blockname1_python.cc | |||
│ ├── blockname2_python.cc | |||
│ ├── CMakeLists.txt | |||
│ ├── docstrings | |||
│ │ ├── blockname1_pydoc_template.h | |||
│ │ ├── blockname1_pydoc_template.h | |||
</source> | |||
The bindings for each block exist in blockname_python.cc under the <code>python/bindings</code> directory. Additionally, a template header file for each block that is used as a placeholder for the scraped docstrings lives in the <code>docstrings/</code> dir | |||
==== blockname_python.cc ==== | |||
===== Comment Block ===== | |||
Each block binding file contains an automatically generated and maintained comment block that informs CMake when the bindings are out of sync with the header file they refer to, and what to do about it | |||
<source lang="cpp">/***********************************************************************************/ | |||
/* This file is automatically generated using bindtool and can be manually edited */ | |||
/* The following lines can be configured to regenerate this file during cmake */ | |||
/* If manual edits are made, the following tags should be modified accordingly. */ | |||
/* BINDTOOL_GEN_AUTOMATIC(0) */ | |||
/* BINDTOOL_USE_PYGCCXML(0) */ | |||
/* BINDTOOL_HEADER_FILE(basic_block.h) */ | |||
/* BINDTOOL_HEADER_FILE_HASH(549c06530e2afdf6f2c989017cb5f36e) */ | |||
/***********************************************************************************/ | |||
</source> | |||
<code>BINDTOOL_GEN_AUTOMATIC</code>: Many times for complex in-tree blocks, the automated tools are not entirely sufficient to generate all of the bindings in an automated fashion. In this case, the flag should be set to 0, and the bindings need to be updated manually. If the flag is set to 1, CMake will override the binding file ''in the source tree'' when it detects out of sync bindings. This should only be done in simple cases. | |||
<code>BINDTOOL_USE_PYGCCXML</code>: Currently there are limitations on the amount of code generation that can be accomplished without the <code>pygccxml</code> dependency. If a block needs pygccxml for the bindings to be properly generated automatically, this should be set to <code>1</code> | |||
<code>BINDTOOL_HEADER_FILE</code>: The header file that bindings are based on, filename only | |||
<code>BINDTOOL_HEADER_FILE_HASH</code>: The MD5 hash of the header file that the bindings | |||
=== Workflow === | |||
==== Out-of-Tree modules ==== | |||
The steps for creating an out of tree module with pybind11 bindings are as follows: | |||
# Use <code>gr_modtool</code> to create an out of tree module and add blocks | |||
<source lang="sh">gr_modtool newmod foo | |||
gr_modtool add bar | |||
</source> | |||
<ol start="2"> | |||
<li>Update the parameters or functions in the public include file and rebind with <code>gr_modtool bind bar</code></li></ol> | |||
'''NOTE''': without pygccxml, only the make function is currently accounted for, similar to <code>gr_modtool makeyaml</code> | |||
If the public API changes, just call <code>gr_modtool bind [blockname]</code> to regenerate the bindings | |||
When the public header file for a block is changed, CMake will fail as it checks the hash of the header file compared to the hash stored in the bindings file until the bindings are updated | |||
<ol start="3"> | |||
<li>Build and install</li></ol> | |||
=== Docstrings === | |||
If Doxygen is enabled in GNU Radio and/or the OOT, Docstrings are scraped from the header files, and placed in auto-generated <code>[blockname]_pydoc.h</code> files in the build directory on compile. Generated templates (via the binding steps described above) are placed in the <code>python/bindings/docstrings</code> directory and are used as placeholders for the scraped strings | |||
Upon compilation, docstrings are scraped from the module and stored in a dictionary (using <code>update_pydoc.py scrape</code>) and then the values are substituted in the template file (using <code>update_pydoc.py sub</code>) | |||
=== OOT Migration === | === OOT Migration === | ||
TBD | |||
=== Caveats === | === Caveats === |
Revision as of 11:40, 12 June 2020
The major changes in the (in-progress) GNU Radio 3.9 release that will impact OOTs are:
- C++ modernization (C++11/14?)
- Replacement of SWIG with Pybind11
C++ Modernization
(Currently merged onto master branch)
The most obvious change that will impact OOTs is that Boost shared pointers have been replaced with std:: shared pointers and memory management. At the top level of each block, the instantiation will need to change, e.g.
In include/blockname_xx.h:
typedef std::shared_ptr<blockname_xx> sptr;
Pybind11 Python Bindings
(NOT MERGED - future changes)
As of the GNU Radio 3.9 release, python bindings are handled using pybind11, which is inherently different than they were in previous releases
Dependencies
- pybind11 > 2.4.3 https://pybind11.readthedocs.io/
- pygccxml https://pygccxml.readthedocs.io/en/develop/install.html
- This is an optional dependency and basic functionality for OOT generation can be performed without pygccxml
- It is required for automatically generating bindings for most of the GR source tree
Components
Python bindings are contained in the python/.../bindings
directory
./python
└── module_name
├── bindings
│ ├── blockname1_python.cc
│ ├── blockname2_python.cc
│ ├── CMakeLists.txt
│ ├── docstrings
│ │ ├── blockname1_pydoc_template.h
│ │ ├── blockname1_pydoc_template.h
The bindings for each block exist in blockname_python.cc under the python/bindings
directory. Additionally, a template header file for each block that is used as a placeholder for the scraped docstrings lives in the docstrings/
dir
blockname_python.cc
Comment Block
Each block binding file contains an automatically generated and maintained comment block that informs CMake when the bindings are out of sync with the header file they refer to, and what to do about it
/***********************************************************************************/
/* This file is automatically generated using bindtool and can be manually edited */
/* The following lines can be configured to regenerate this file during cmake */
/* If manual edits are made, the following tags should be modified accordingly. */
/* BINDTOOL_GEN_AUTOMATIC(0) */
/* BINDTOOL_USE_PYGCCXML(0) */
/* BINDTOOL_HEADER_FILE(basic_block.h) */
/* BINDTOOL_HEADER_FILE_HASH(549c06530e2afdf6f2c989017cb5f36e) */
/***********************************************************************************/
BINDTOOL_GEN_AUTOMATIC
: Many times for complex in-tree blocks, the automated tools are not entirely sufficient to generate all of the bindings in an automated fashion. In this case, the flag should be set to 0, and the bindings need to be updated manually. If the flag is set to 1, CMake will override the binding file in the source tree when it detects out of sync bindings. This should only be done in simple cases.
BINDTOOL_USE_PYGCCXML
: Currently there are limitations on the amount of code generation that can be accomplished without the pygccxml
dependency. If a block needs pygccxml for the bindings to be properly generated automatically, this should be set to 1
BINDTOOL_HEADER_FILE
: The header file that bindings are based on, filename only
BINDTOOL_HEADER_FILE_HASH
: The MD5 hash of the header file that the bindings
Workflow
Out-of-Tree modules
The steps for creating an out of tree module with pybind11 bindings are as follows:
- Use
gr_modtool
to create an out of tree module and add blocks
gr_modtool newmod foo
gr_modtool add bar
- Update the parameters or functions in the public include file and rebind with
gr_modtool bind bar
NOTE: without pygccxml, only the make function is currently accounted for, similar to gr_modtool makeyaml
If the public API changes, just call gr_modtool bind [blockname]
to regenerate the bindings
When the public header file for a block is changed, CMake will fail as it checks the hash of the header file compared to the hash stored in the bindings file until the bindings are updated
- Build and install
Docstrings
If Doxygen is enabled in GNU Radio and/or the OOT, Docstrings are scraped from the header files, and placed in auto-generated [blockname]_pydoc.h
files in the build directory on compile. Generated templates (via the binding steps described above) are placed in the python/bindings/docstrings
directory and are used as placeholders for the scraped strings
Upon compilation, docstrings are scraped from the module and stored in a dictionary (using update_pydoc.py scrape
) and then the values are substituted in the template file (using update_pydoc.py sub
)
OOT Migration
TBD
Caveats
Pybind11 bound methods do not implicitly convert int to enum, so blocks that take enum as input, must have either "raw" or "enum" in the grc yml definition of the block. "Raw" will allow the value to be changed by another variable in the flowgraph.
Block inheritance must be specified completely in the python bindings in order to use the inherited methods. For instance, if a block inherits from sync_block, both block and basic_block must be included in the inheritance specification of the class:
py::class_<atsc_interleaver, gr::sync_block, gr::block, gr::basic_block, std::shared_ptr<atsc_interleaver>>( m, "atsc_interleaver", D(atsc_interleaver))