{"id":21474,"url":"https://patchwork.libcamera.org/api/1.1/patches/21474/?format=json","web_url":"https://patchwork.libcamera.org/patch/21474/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20241002161933.247091-4-stefan.klug@ideasonboard.com>","date":"2024-10-02T16:19:21","name":"[v1,3/8] utils: Add script to generate control_ids_debug.yaml","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"dabe466b1986203a8a7523e0b917debe97524b81","submitter":{"id":184,"url":"https://patchwork.libcamera.org/api/1.1/people/184/?format=json","name":"Stefan Klug","email":"stefan.klug@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/21474/mbox/","series":[{"id":4648,"url":"https://patchwork.libcamera.org/api/1.1/series/4648/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4648","date":"2024-10-02T16:19:18","name":"Add support for IPA debugging metadata","version":1,"mbox":"https://patchwork.libcamera.org/series/4648/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/21474/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/21474/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 4ADA6BD80A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  2 Oct 2024 16:20:22 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id DED5C63522;\n\tWed,  2 Oct 2024 18:20:21 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 801DE63522\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  2 Oct 2024 18:20:20 +0200 (CEST)","from ideasonboard.com (unknown\n\t[IPv6:2a00:6020:448c:6c00:b246:b33f:c662:8ae1])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 87EF510D8;\n\tWed,  2 Oct 2024 18:18:47 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"p6JsRqk0\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1727885927;\n\tbh=h5CKhhbUcXkFYXZAwzQvEqeLYChhaFYFrU/ITQRA9TE=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=p6JsRqk0vjLJLsDm0VLQf6CEqSV4mJDb3IhU+qik0KGnkNhHcPTWobGWiUtTnJ+I9\n\tROc/8zOcDwYTpKG6APQ46bC+eyXW6laHZbYqQT6Lwk1uxhOHUTc1xso/VZKavibQCI\n\tcXrv7zMOM/EyYmbNLXN1bcPk5fKa+yLEByq5YMd8=","From":"Stefan Klug <stefan.klug@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Stefan Klug <stefan.klug@ideasonboard.com>","Subject":"[PATCH v1 3/8] utils: Add script to generate control_ids_debug.yaml","Date":"Wed,  2 Oct 2024 18:19:21 +0200","Message-ID":"<20241002161933.247091-4-stefan.klug@ideasonboard.com>","X-Mailer":"git-send-email 2.43.0","In-Reply-To":"<20241002161933.247091-1-stefan.klug@ideasonboard.com>","References":"<20241002161933.247091-1-stefan.klug@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"For flexible debugging it is helpful to minimize the roundtrip time.\nThis scipts parses the sourcetree and loos for usages of\n\nset<type>(controls::debug::Something, ...)\n\nand adds (or removes) the controls as necessary from the yaml\ndescription.\n\nSigned-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\n---\n utils/gen-debug-controls.py | 161 ++++++++++++++++++++++++++++++++++++\n 1 file changed, 161 insertions(+)\n create mode 100755 utils/gen-debug-controls.py","diff":"diff --git a/utils/gen-debug-controls.py b/utils/gen-debug-controls.py\nnew file mode 100755\nindex 000000000000..6cb087e1e0ae\n--- /dev/null\n+++ b/utils/gen-debug-controls.py\n@@ -0,0 +1,161 @@\n+#!/usr/bin/env python3\n+# SPDX-License-Identifier: GPL-2.0-or-later\n+# Copyright (C) 2024, Ideas on Board Inc.\n+#\n+# Author: Stefan Klug <stefan.klug@ideasonboard.com>\n+#\n+# This script looks for occurrences of the debug metadata controls in the source\n+# tree and updates src/libcamera/control_ids_debug.yaml accordingly\n+\n+import argparse\n+import logging\n+import os\n+import re\n+import sys\n+from dataclasses import dataclass\n+from pathlib import Path\n+\n+logger = logging.getLogger(__name__)\n+logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')\n+\n+try:\n+    import ruyaml\n+except:\n+    logger.error(\n+        f'Failed to import ruyaml. Please install the ruyaml package.')\n+    sys.exit(1)\n+\n+\n+@dataclass\n+class FoundMatch:\n+    file: os.PathLike\n+    whole_match: str\n+    line: int\n+    type: str\n+    name: str\n+    size: str = None\n+\n+\n+def get_control_name(control):\n+    k = list(control.keys())\n+    if len(k) != 1:\n+        raise Exception(f\"Can't handle control entry with {len(k)} keys\")\n+    return k[0]\n+\n+\n+def find_debug_controls(dir):\n+    extensions = ['.cpp', '.h']\n+    files = [p for p in dir.rglob('*') if p.suffix in extensions]\n+\n+    # The following regex was tested on\n+    # set<Span<type>>( controls::debug::something , static_cast<type>(var) )\n+    # set<>( controls::debug::something , static_cast<type>(var) )\n+    # set( controls::debug::something , static_cast<type> (var) )\n+    exp = re.compile(r'set'  # set function\n+                     # possibly followed by template param\n+                     r'(?:\\<((?:[^)(])*)\\>)?'\n+                     # referencing a debug control\n+                     r'\\(\\s*controls::debug::(\\w+)\\s*,.*\\)'\n+                     )\n+    matches = []\n+    for p in files:\n+        with p.open('r') as f:\n+            for idx, line in enumerate(f):\n+                match = exp.search(line)\n+                if match:\n+                    m = FoundMatch(file=p, line=idx, type=match.group(1),\n+                                   name=match.group(2), whole_match=match.group(0))\n+                    if m.type is not None and m.type.startswith('Span'):\n+                        # simple span type detection treating the last word inside <> as type\n+                        r = re.match(r'Span<(?:.*\\s+)(.*)>', m.type)\n+                        m.type = r.group(1)\n+                        m.size = '[n]'\n+                    matches.append(m)\n+    return matches\n+\n+\n+def main(argv):\n+    parser = argparse.ArgumentParser(\n+        description='Automatically updates control_ids_debug.yaml',)\n+    args = parser.parse_args(argv[1:])\n+\n+    yaml = ruyaml.YAML()\n+    root_dir = Path(__file__).resolve().parent.parent\n+    output = root_dir.joinpath('src/libcamera/control_ids_debug.yaml')\n+\n+    matches = find_debug_controls(root_dir.joinpath('src'))\n+\n+    doc = yaml.load(output)\n+\n+    controls = doc['controls']\n+\n+    # create a map of names in the existing yaml for easier updating\n+    controls_map = {}\n+    for control in controls:\n+        for k, v in control.items():\n+            controls_map[k] = v\n+\n+    obsolete_names = list(controls_map.keys())\n+\n+    for m in matches:\n+        if not m.type:\n+            p = m.file.relative_to(Path.cwd(), walk_up=True)\n+            logger.warning(\n+                f'{p}:{m.line + 1}: Failed to deduce type from {m.whole_match} ... skipping')\n+            continue\n+\n+        p = m.file.relative_to(root_dir)\n+        desc = {'type': m.type,\n+                'description': f'Debug control {m.name} found in {p}:{m.line}'}\n+        if m.size is not None:\n+            desc['size'] = m.size\n+\n+        if m.name in controls_map:\n+            # Can't use == for modified check because of the special yaml dicts.\n+            update_needed = False\n+            if list(controls_map[m.name].keys()) != list(desc.keys()):\n+                update_needed = True\n+            else:\n+                for k, v in controls_map[m.name].items():\n+                    if v != desc[k]:\n+                        update_needed = True\n+                        break\n+\n+            if update_needed:\n+                logger.info(f\"Update control '{m.name}'\")\n+                controls_map[m.name].clear()\n+                controls_map[m.name].update(desc)\n+\n+            obsolete_names.remove(m.name)\n+        else:\n+            logger.info(f\"Add control '{m.name}'\")\n+            insert_before = len(controls)\n+            for idx, control in enumerate(controls):\n+                if get_control_name(control).lower() > m.name.lower():\n+                    insert_before = idx\n+                    break\n+            controls.insert(insert_before, {m.name: desc})\n+\n+    # Remove elements from controls without recreating the list (to keep comments etc.)\n+    idx = 0\n+    while idx < len(controls):\n+        name = get_control_name(controls[idx])\n+        if name in obsolete_names:\n+            logger.info(f\"Remove control '{name}'\")\n+            controls.pop(idx)\n+        else:\n+            idx += 1\n+\n+    with output.open('w') as f:\n+        # ruyaml looses the copyright header\n+        f.write((\"# SPDX-License-Identifier: LGPL-2.1-or-later\\n\"\n+                 \"#\\n\"\n+                 \"# Copyright (C) 2024, Ideas on Board Oy\\n\"\n+                 \"#\\n\"))\n+        yaml.dump(doc, f)\n+\n+    return 0\n+\n+\n+if __name__ == '__main__':\n+    sys.exit(main(sys.argv))\n","prefixes":["v1","3/8"]}