Skip to main content

fprime-cpp-codegen

A Python package for generating C++ code for F Prime.

It builds C++ documents — one .hpp plus one or more .cpp files — through a Builder-pattern API, where nesting in the generated C++ follows nesting in the Python. It generates general-purpose C++: classes, structs, templates, namespaces, enums, free functions, statements. Nothing in it knows about the FPP model, and it has no runtime dependencies.

Installation

pip install git+https://github.com/fprime-community/fprime-cpp-codegen.git

Requires Python 3.10 or newer.

Quick start

from fprime_cpp_codegen import CppDocBuilder, Output

doc = CppDocBuilder("Ring", description="a fixed-capacity ring buffer",
                    namespaces=["Demo"], tool_name="my-generator")
doc.include("Fw/FPrimeBasicTypes.hpp")
doc.include("Ring.hpp", output=Output.CPP)

with doc.namespace("Demo") as ns:
    with ns.class_("Ring", comment="A ring buffer of fixed capacity") as cls:
        with cls.public("Constructors and destructors"):
            ctor = cls.constructor(explicit=True, comment="Construct an empty ring")
            ctor.param("U32", "capacity", comment="The capacity")
            ctor.init("m_head(0)", "m_size(0)", "m_capacity(capacity)")

        with cls.public("Public member functions"):
            push = cls.function("push", ret="bool", comment="Append one item")
            push.param("U32", "item", comment="The item to append")
            with push.body as b:
                with b.if_("m_size == m_capacity"):
                    b.line("return false;")
                b.line("m_data[(m_head + m_size) % CAPACITY] = item;")
                b.line("m_size++;")
                b.line("return true;")

            cls.function("size", ret="U32", const=True, inline=True,
                         body="return m_size;", comment="How many items are stored")

        with cls.private("Member variables"):
            cls.var("U32", "CAPACITY", init="64", static=True, constexpr=True)
            cls.var("U32", "m_data", array="CAPACITY")
            cls.var("U32", "m_head")
            cls.var("U32", "m_size")
            cls.var("U32", "m_capacity", const=True)

doc.write("build-artifacts")

write() puts Ring.hpp and Ring.cpp in build-artifacts/, declarations in the one and definitions in the other. render_hpp() / render_cpp() return the text instead, and files() returns every file as a name-to-text mapping.

Generated files can optionally be passed through clang-format, which needs no compilation database or include paths — it is purely lexical, so standalone text is fine. Note that it discards the F Prime autocoder's own layout.

from fprime_cpp_codegen import ClangFormat

doc.write("build-artifacts", formatter=ClangFormat())   # or style="LLVM"

Any Callable[[str, str], str] taking (text, file_name) works as a formatter.

with is optional throughout — a builder attaches to its parent as soon as you create it, so you can keep filling it in afterwards. It is worth using on access sections and preprocessor guards, which delete themselves when nothing lands inside them.

Examples

Run any of these to print the C++ they generate, or pass a directory to write it:

python examples/ring_buffer.py           # print both files
python examples/ring_buffer.py build-dir # write them out
Script What it covers
ring_buffer.py The class above, in full. Start here.
fpp_constants.py A document with no class in it: constants at namespace scope, split across the two files by extern.
fpp_enum.py A port of FPP's enum autocoder — the biggest one, and the closest to a real generator.

The two fpp_* scripts are ports of fpp-to-cpp's own autocoders, driven by a small Python data class in place of the FPP model. Both are checked against unmodified reference output from fpp's test suite, kept in tests/goldens/fpp/, and reproduce it byte for byte — bar two lines in the enum header where upstream's own indentation is inconsistent.

Development

python -m venv .venv
.venv/bin/pip install -e '.[dev]'
.venv/bin/python -m pytest
.venv/bin/python -m mypy    # strict
.venv/bin/python -m black src tests examples

Formatting is black with its default settings, as F Prime uses.

The compile-check tests need a C++ compiler on PATH (g++, clang++ or c++) and skip themselves if there is none.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fprime_cpp_codegen-0.1.0.tar.gz (86.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fprime_cpp_codegen-0.1.0-py3-none-any.whl (54.4 kB view details)

Uploaded Python 3

File details

Details for the file fprime_cpp_codegen-0.1.0.tar.gz.

File metadata

  • Download URL: fprime_cpp_codegen-0.1.0.tar.gz
  • Upload date:
  • Size: 86.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for fprime_cpp_codegen-0.1.0.tar.gz
Algorithm Hash digest
SHA256 86938a8a640dbfcb90415f0f79d7511111ec8616d5a1b9268ce8f877c2b91ddf
MD5 a04a0e0daf2ba42613eff4d390deebbf
BLAKE2b-256 6ce9b51cd05c26887e390b2182b2a57e31e3ba48eb3b5e02b0315624dc700581

See more details on using hashes here.

Provenance

The following attestation bundles were made for fprime_cpp_codegen-0.1.0.tar.gz:

Publisher: publish.yml on fprime-community/fprime-cpp-codegen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fprime_cpp_codegen-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fprime_cpp_codegen-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8a096b9f045ee4234e0c75eb26b42384bfa95ea022ea4c0baaa4537ecf5b17db
MD5 a73f3967f9fd29e391a5366526cac246
BLAKE2b-256 82b6323be5ec9d7f3e50504a9e496666f98f7a44b6fad3997f140802988ad955

See more details on using hashes here.

Provenance

The following attestation bundles were made for fprime_cpp_codegen-0.1.0-py3-none-any.whl:

Publisher: publish.yml on fprime-community/fprime-cpp-codegen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

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