From patchwork Mon Sep 1 09:23:19 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 24275 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id E0A23BEFBE for ; Mon, 1 Sep 2025 09:23:26 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D636E6931D; Mon, 1 Sep 2025 11:23:25 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="KHi/yhpD"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C07EC613AC for ; Mon, 1 Sep 2025 11:23:23 +0200 (CEST) Received: from pb-laptop.local (185.221.143.232.nat.pool.zt.hu [185.221.143.232]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 75A9DEFE for ; Mon, 1 Sep 2025 11:22:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1756718536; bh=0rxqnRB5u+DCnovjRE0yZuRESlQ+IIZfqZyRqOKW8+8=; h=From:To:Subject:Date:From; b=KHi/yhpD387Y79QB+OfLEczHuJQTRedqoCwyN5WzwHhL0Blbi5to7WA2H2fb/cdeq 6Eg0DdRDex/y30Jg2wBzmjuIlK9va/xQa09xJnhwUiEEBgt8SLGn0y00OcEEAZmDq2 mln3Qo2ShW4wI67hwIMOGVaT1ihJrRdvteSOxGcc= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [PATCH v1 1/2] utils: codegen: gen-formats.py: Use jinja Date: Mon, 1 Sep 2025 11:23:19 +0200 Message-ID: <20250901092320.44615-1-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.51.0 MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" 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 | 34 ++++++++++++---------------------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/include/libcamera/formats.h.in b/include/libcamera/formats.h.in index 6ae7634fe..5ff9c3bf4 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 0c0932a5b..872f3fe34 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,24 +72,17 @@ def generate_h(formats, drm_fourcc): if mod: data['mod'] = '%u, %u' % drm_fourcc.mod(mod) - fmts.append(template.substitute(data)) - - return {'formats': '\n'.join(fmts)} - - -def fill_template(template, data): + fmts.append(data) - template = open(template, 'rb').read() - template = template.decode('utf-8') - template = string.Template(template) - return template.substitute(data) + return fmts def main(argv): # Parse command line arguments parser = argparse.ArgumentParser() - parser.add_argument('-o', dest='output', metavar='file', type=str, + parser.add_argument('-o', dest='output', metavar='file', + type=argparse.FileType('w', encoding='utf-8'), default=sys.stdout, help='Output file name. Defaults to standard output if not specified.') parser.add_argument('input', type=str, help='Input file name.') @@ -104,15 +96,13 @@ 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.close() - else: - sys.stdout.write(data) + args.output.write(string) return 0