From patchwork Fri Aug 9 00:59:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 20858 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 4FDEEBE173 for ; Fri, 9 Aug 2024 00:59:56 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D0F44633BF; Fri, 9 Aug 2024 02:59:55 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="Gmjkn5QP"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id ABE41633C0 for ; Fri, 9 Aug 2024 02:59:50 +0200 (CEST) Received: from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi [81.175.209.231]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B99C583F for ; Fri, 9 Aug 2024 02:58:56 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1723165137; bh=QLOZcqushEoVrUTD8zs45EtV+xtVBVXO3eX7CZghrcg=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Gmjkn5QPPvIQlOeIteGjdHUn9+bu8urAwd2P08pVDOrbnoGLyJ25culKECKmKGzWe 78iIRso9X6lOOXdTQ9JwhYuDmy6nS4SvwOnmwDS28+IghQOL5DqiRtp6ipha3Q1DcG J9Kb4Vm7cQ/UWPf6lSHmHbIJdjpKhnLON3iljjxc= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Subject: [PATCH 08/10] utils: codegen: gen-controls.py: Move helper classes to separate file Date: Fri, 9 Aug 2024 03:59:12 +0300 Message-ID: <20240809005914.20662-9-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.44.2 In-Reply-To: <20240809005914.20662-1-laurent.pinchart@ideasonboard.com> References: <20240809005914.20662-1-laurent.pinchart@ideasonboard.com> 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" The ControlEnum and Control helper classes defined in gen-controls.py are useful for other generator scripts. Move them to a separate file to make it possible to share them. Extend the Python build environment to add the path to the new Python module to PYTHONPATH, and use it when invoking the gen-controls.py script. Signed-off-by: Laurent Pinchart Reviewed-by: Daniel Scally --- include/libcamera/meson.build | 1 + src/libcamera/meson.build | 3 +- utils/codegen/controls.py | 112 ++++++++++++++++++++++++++++++++++ utils/codegen/gen-controls.py | 105 +------------------------------ 4 files changed, 116 insertions(+), 105 deletions(-) create mode 100644 utils/codegen/controls.py diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index d90a8615e52d..a969a95dbf7a 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -88,6 +88,7 @@ foreach mode, entry : controls_map command : [gen_controls, '-o', '@OUTPUT@', '--mode', mode, '-t', template_file, '-r', ranges_file, '@INPUT@'], + env : py_build_env, install : true, install_dir : libcamera_headers_install_dir) endforeach diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index 3fd3a87e9f95..aa9ab0291854 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -151,7 +151,8 @@ foreach mode, inout_files : controls_mode_files output : output_file, command : [gen_controls, '-o', '@OUTPUT@', '--mode', mode, '-t', template_file, - '-r', ranges_file, '@INPUT@']) + '-r', ranges_file, '@INPUT@'], + env : py_build_env) endforeach libcamera_public_sources += control_sources diff --git a/utils/codegen/controls.py b/utils/codegen/controls.py new file mode 100644 index 000000000000..7bafee599c80 --- /dev/null +++ b/utils/codegen/controls.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (C) 2019, Google Inc. +# +# Author: Laurent Pinchart +# +# Helper classes to handle source code generation for libcamera controls + + +class ControlEnum(object): + def __init__(self, data): + self.__data = data + + @property + def description(self): + """The enum description""" + return self.__data.get('description') + + @property + def name(self): + """The enum name""" + return self.__data.get('name') + + @property + def value(self): + """The enum value""" + return self.__data.get('value') + + +class Control(object): + def __init__(self, name, data, vendor): + self.__name = name + self.__data = data + self.__enum_values = None + self.__size = None + self.__vendor = vendor + + enum_values = data.get('enum') + if enum_values is not None: + self.__enum_values = [ControlEnum(enum) for enum in enum_values] + + size = self.__data.get('size') + if size is not None: + if len(size) == 0: + raise RuntimeError(f'Control `{self.__name}` size must have at least one dimension') + + # Compute the total number of elements in the array. If any of the + # array dimension is a string, the array is variable-sized. + num_elems = 1 + for dim in size: + if type(dim) is str: + num_elems = 0 + break + + dim = int(dim) + if dim <= 0: + raise RuntimeError(f'Control `{self.__name}` size must have positive values only') + + num_elems *= dim + + self.__size = num_elems + + @property + def description(self): + """The control description""" + return self.__data.get('description') + + @property + def enum_values(self): + """The enum values, if the control is an enumeration""" + if self.__enum_values is None: + return + for enum in self.__enum_values: + yield enum + + @property + def enum_values_count(self): + """The number of enum values, if the control is an enumeration""" + if self.__enum_values is None: + return 0 + return len(self.__enum_values) + + @property + def is_enum(self): + """Is the control an enumeration""" + return self.__enum_values is not None + + @property + def vendor(self): + """The vendor string, or None""" + return self.__vendor + + @property + def name(self): + """The control name (CamelCase)""" + return self.__name + + @property + def type(self): + typ = self.__data.get('type') + size = self.__data.get('size') + + if typ == 'string': + return 'std::string' + + if self.__size is None: + return typ + + if self.__size: + return f"Span" + else: + return f"Span" diff --git a/utils/codegen/gen-controls.py b/utils/codegen/gen-controls.py index 685ef7a00d5f..2968eb9a5d4e 100755 --- a/utils/codegen/gen-controls.py +++ b/utils/codegen/gen-controls.py @@ -12,110 +12,7 @@ import os import sys import yaml - -class ControlEnum(object): - def __init__(self, data): - self.__data = data - - @property - def description(self): - """The enum description""" - return self.__data.get('description') - - @property - def name(self): - """The enum name""" - return self.__data.get('name') - - @property - def value(self): - """The enum value""" - return self.__data.get('value') - - -class Control(object): - def __init__(self, name, data, vendor): - self.__name = name - self.__data = data - self.__enum_values = None - self.__size = None - self.__vendor = vendor - - enum_values = data.get('enum') - if enum_values is not None: - self.__enum_values = [ControlEnum(enum) for enum in enum_values] - - size = self.__data.get('size') - if size is not None: - if len(size) == 0: - raise RuntimeError(f'Control `{self.__name}` size must have at least one dimension') - - # Compute the total number of elements in the array. If any of the - # array dimension is a string, the array is variable-sized. - num_elems = 1 - for dim in size: - if type(dim) is str: - num_elems = 0 - break - - dim = int(dim) - if dim <= 0: - raise RuntimeError(f'Control `{self.__name}` size must have positive values only') - - num_elems *= dim - - self.__size = num_elems - - @property - def description(self): - """The control description""" - return self.__data.get('description') - - @property - def enum_values(self): - """The enum values, if the control is an enumeration""" - if self.__enum_values is None: - return - for enum in self.__enum_values: - yield enum - - @property - def enum_values_count(self): - """The number of enum values, if the control is an enumeration""" - if self.__enum_values is None: - return 0 - return len(self.__enum_values) - - @property - def is_enum(self): - """Is the control an enumeration""" - return self.__enum_values is not None - - @property - def vendor(self): - """The vendor string, or None""" - return self.__vendor - - @property - def name(self): - """The control name (CamelCase)""" - return self.__name - - @property - def type(self): - typ = self.__data.get('type') - size = self.__data.get('size') - - if typ == 'string': - return 'std::string' - - if self.__size is None: - return typ - - if self.__size: - return f"Span" - else: - return f"Span" +from controls import Control def snake_case(s):