Skip to main content

header2whatever (h2w)

Generate arbitrary files from C/C++ header files using CppHeaderParser to read the input files and Jinja2 templates to generate the outputs.

This grew out of a desire to generate pybind11 wrapping code from C++ header files. pybind11gen was created, but then I wanted to generate more things…

There are still rough edges, and the documentation is mostly nonexistent, but pull requests with fixes/improvements are very welcome!

As of 0.3.0, h2w requires Python 3.3+

Install

pip install header2whatever

Usage

First, you need to create a jinja2 template that represents whatever you want to generate from the header file. For example, maybe you want to describe the functions in yaml:

---
{% for header in headers %}
{% for fn in header.functions %}
{{ fn.name }}:
  returns: {{ fn.returns }}
  params:
  {% for param in fn.parameters %}
  - { name: {{ param.name }}, type: "{{ param.type }}" }
  {% endfor %}

{% endfor %}
{% endfor %}

And let’s say you have the following header file foo.h:

void some_fn(int i);
int returns_int(int p1, char* p2);

You can execute the following:

h2w foo.h -o foo.yml

And you’ll get the following output:

---
returns_int:
  returns: int
  params:
  - { name: p1, type: "int" }
  - { name: p2, type: "char *" }

some_fn:
  returns: void
  params:
  - { name: i, type: "int" }

As you can see, while this is a silly example, this approach is very flexible and fairly powerful.

Currently, the data structure passed to the template isn’t documented – but it’s a filtered version of whatever CppHeaderParser outputs when it parses a header.

See the examples folder for more examples.

Batch mode

If you need to process multiple files, or just want to record the parameters for autogenerating a file without writing a shell script, batch mode is useful. You pass two parameters: a yaml file with the configuration, and an output directory to write the files to.

Using data from external sources

Sometimes you want to mix in data that CppHeaderParser can’t give you. If you pass the --yaml option, it will load the yaml into a dictionary and make it available to the template as the ‘data’ variable.

You can also pass key=value parameters via the --param option, and the specified keys will be available to the template.

Custom processing

When you need to do more complex logic that a jinja2 template just isn’t appropriate for, you can specify a python file to load custom hooks from.

See [the default hooks](header2whatever/default_hooks.py) for documentation.

License

Apache 2

Author

Dustin Spicuzza (dustin@virtualroadside.com)

Metadata

Release files for header2whatever 0.4.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for header2whatever 0.4.8
File Size Uploaded Provenance
header2whatever-0.4.8.tar.gz 78.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for header2whatever 0.4.8
File Interpreter ABI Platform
header2whatever-0.4.8-py3-none-any.whl Python 3 none any Details

Total release size: 159.5 kB

Release files / header2whatever-0.4.8.tar.gz

Download URL header2whatever-0.4.8.tar.gz
Size 78.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7ecdbcc3eb79a546cf89505749d106615b6897014f0d602d59aced2ce16dcbc6
BLAKE2b-256 checksum
How to use checksums
e4912f3982a76ece3b14543612408929da953cc60c5b91d365f0b08b1f630bc0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.15

Release files / header2whatever-0.4.8-py3-none-any.whl

Download URL header2whatever-0.4.8-py3-none-any.whl
Size 81.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ff3ccdc3159027f4b15e271977946adaf60d939ea0950b0245b7d725a5741402
BLAKE2b-256 checksum
How to use checksums
033f6e342843b42162ff82c8412f17686cdc90e0d39322731c53316f1ed7f017
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.15

Security for header2whatever 0.4.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page