Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Icon*
/build/
/dist/
*.spec
**/build/

# Sublime Text
*.sublime-workspace
Expand Down
76 changes: 53 additions & 23 deletions bakelite/generator/cli.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,88 @@
# pylint: disable=redefined-builtin
"""Command-line interface for bakelite code generation."""

import sys
from pathlib import Path

import click

from bakelite.generator import cpptiny, parse, python
from bakelite.generator import cpptiny, ctiny, parse, python


@click.group()
def cli() -> None:
pass
"""Bakelite protocol code generator."""


@cli.command()
@click.option("--language", "-l", required=True)
@click.option("--input", "-i", required=True)
@click.option("--output", "-o", required=True)
def gen(language: str, input: str, output: str) -> None:
with open(input, "r", encoding="utf-8") as f:
@click.option("--language", "-l", required=True, help="Target language (python, cpptiny, ctiny)")
@click.option("--input", "-i", "input_file", required=True, help="Input protocol file")
@click.option("--output", "-o", "output_file", required=True, help="Output file")
@click.option(
"--runtime-import",
"runtime_import",
is_flag=False,
flag_value="bakelite.proto",
default=None,
help="Import path for runtime. No value=bakelite.proto, omit=runtime",
)
@click.option(
"--unpacked",
is_flag=True,
default=False,
help="Generate aligned structs with memmove shuffle (for Cortex-M0, RISC-V, ESP32, PIC32)",
)
def gen(
language: str, input_file: str, output_file: str, runtime_import: str | None, unpacked: bool
) -> None:
"""Generate protocol code from a definition file."""
with open(input_file, encoding="utf-8") as f:
proto = f.read()

proto_def = parse(proto)

if language == "python":
generated_file = python.render(*proto_def)
# Default to "bakelite_runtime" (relative import) if not specified
import_path = runtime_import if runtime_import is not None else "bakelite_runtime"
generated_file = python.render(*proto_def, runtime_import=import_path)
elif language == "cpptiny":
generated_file = cpptiny.render(*proto_def)
generated_file = cpptiny.render(*proto_def, unpacked=unpacked)
elif language == "ctiny":
generated_file = ctiny.render(*proto_def, unpacked=unpacked)
else:
print(f"Unknown language: {language}")
sys.exit(1)

with open(output, "w", encoding="utf-8") as f:
with open(output_file, "w", encoding="utf-8") as f:
f.write(generated_file)


@cli.command()
@click.option("--language", "-l", required=True)
@click.option("--output", "-o", required=True)
def runtime(language: str, output: str) -> None:
runtime_func = None

@click.option("--language", "-l", required=True, help="Target language (python, cpptiny, ctiny)")
@click.option("--output", "-o", "output_path", default=".", help="Output directory")
@click.option("--name", default="bakelite_runtime", help="Runtime folder name (python only)")
def runtime(language: str, output_path: str, name: str) -> None:
"""Generate runtime support code."""
if language == "cpptiny":
runtime_func = cpptiny.runtime
generated_file = cpptiny.runtime()
with open(output_path, "w", encoding="utf-8") as f:
f.write(generated_file)
elif language == "ctiny":
generated_file = ctiny.runtime()
with open(output_path, "w", encoding="utf-8") as f:
f.write(generated_file)
elif language == "python":
runtime_dir = Path(output_path) / name
runtime_dir.mkdir(parents=True, exist_ok=True)
for filename, content in python.runtime().items():
(runtime_dir / filename).write_text(content)
print(f"Generated Python runtime in {runtime_dir}")
else:
print(f"Unkown language: {language}")
print(f"Unknown language: {language}")
sys.exit(1)

generated_file = runtime_func()

with open(output, "w", encoding="utf-8") as f:
f.write(generated_file)


def main() -> None:
"""Main entry point."""
cli()


Expand Down
25 changes: 20 additions & 5 deletions bakelite/generator/cpptiny.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,19 @@ def render(
structs: list[ProtoStruct],
proto: Protocol | None,
comments: list[str],
*,
unpacked: bool = False,
) -> str:
"""Render a protocol definition to C++ source code."""
"""Render a protocol definition to C++ source code.

Args:
enums: Enum definitions from the protocol
structs: Struct definitions from the protocol
proto: Protocol definition (framing, CRC, message IDs)
comments: Top-level comments from the protocol file
unpacked: If True, generate aligned structs with memmove shuffle.
If False (default), generate packed structs for zero-copy.
"""
enums_types = {enum.name: enum for enum in enums}
structs_types = {struct.name: struct for struct in structs}

Expand Down Expand Up @@ -189,17 +200,21 @@ def _read_type(member: ProtoStructMember) -> str:
read_type=_read_type,
framer=framer,
message_ids=message_ids,
unpacked=unpacked,
)


def runtime() -> str:
"""Generate the C++ runtime support code."""
runtimes_dir = os.path.join(os.path.dirname(__file__), "runtimes")

def include(filename: str) -> str:
with open(
os.path.join(os.path.dirname(__file__), "runtimes", "cpptiny", filename),
encoding="utf-8",
) as f:
# Support both cpptiny/ and common/ subdirectories
if filename.startswith("common/"):
filepath = os.path.join(runtimes_dir, filename)
else:
filepath = os.path.join(runtimes_dir, "cpptiny", filename)
with open(filepath, encoding="utf-8") as f:
return f.read()

runtime_template = env.get_template("cpptiny-bakelite.h.j2")
Expand Down
Loading
Loading