From 479a9031f5179a4e36595cdfae87ebdfbf3a903a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Mon, 1 Sep 2025 10:53:44 +0200 Subject: [PATCH] utils: codegen: gen-formats.py: Use jinja MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently the gen-formats.py script can only be used to generate C++ code because it hard-codes part of the template. Use jinja to fully remove any such dependency. Signed-off-by: Barnabás Pőcze Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- include/libcamera/formats.h.in | 4 +++- utils/codegen/gen-formats.py | 28 +++++++++++----------------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/include/libcamera/formats.h.in b/include/libcamera/formats.h.in index 6ae7634f..9bbf1912 100644 --- a/include/libcamera/formats.h.in +++ b/include/libcamera/formats.h.in @@ -35,7 +35,9 @@ constexpr uint64_t __mod(unsigned int vendor, unsigned int mod) } /* namespace */ -${formats} +{% for f in formats -%} +constexpr PixelFormat {{f.name}}(__fourcc({{f.fourcc}}), __mod({{f.mod}})); +{% endfor %} } /* namespace formats */ diff --git a/utils/codegen/gen-formats.py b/utils/codegen/gen-formats.py index 0c0932a5..1f9def58 100755 --- a/utils/codegen/gen-formats.py +++ b/utils/codegen/gen-formats.py @@ -7,6 +7,7 @@ # Generate formats definitions from YAML import argparse +import jinja2 import re import string import sys @@ -52,9 +53,7 @@ class DRMFourCC(object): return self.vendors[vendor], value -def generate_h(formats, drm_fourcc): - template = string.Template('constexpr PixelFormat ${name}{ __fourcc(${fourcc}), __mod(${mod}) };') - +def generate_formats(formats, drm_fourcc): fmts = [] for format in formats: @@ -73,17 +72,9 @@ def generate_h(formats, drm_fourcc): if mod: data['mod'] = '%u, %u' % drm_fourcc.mod(mod) - fmts.append(template.substitute(data)) + fmts.append(data) - return {'formats': '\n'.join(fmts)} - - -def fill_template(template, data): - - template = open(template, 'rb').read() - template = template.decode('utf-8') - template = string.Template(template) - return template.substitute(data) + return fmts def main(argv): @@ -104,15 +95,18 @@ def main(argv): formats = yaml.safe_load(data)['formats'] drm_fourcc = DRMFourCC(args.drm_fourcc) - data = generate_h(formats, drm_fourcc) - data = fill_template(args.template, data) + env = jinja2.Environment() + template = env.from_string(open(args.template, 'r', encoding='utf-8').read()) + string = template.render({ + 'formats': generate_formats(formats, drm_fourcc), + }) if args.output: output = open(args.output, 'wb') - output.write(data.encode('utf-8')) + output.write(string.encode('utf-8')) output.close() else: - sys.stdout.write(data) + sys.stdout.write(string) return 0