{"id":24222,"url":"https://patchwork.libcamera.org/api/1.1/patches/24222/?format=json","web_url":"https://patchwork.libcamera.org/patch/24222/","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":"<20250825114219.562831-2-paul.elder@ideasonboard.com>","date":"2025-08-25T11:42:03","name":"[v4,1/8] libcamera: ipa_manager: Factor out .so file searching","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"7fba8fb65d50ab4f718a491fb90d85c337256a58","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/1.1/people/17/?format=json","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/24222/mbox/","series":[{"id":5401,"url":"https://patchwork.libcamera.org/api/1.1/series/5401/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5401","date":"2025-08-25T11:42:02","name":"Add Layers support","version":4,"mbox":"https://patchwork.libcamera.org/series/5401/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/24222/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/24222/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 4575EBEFBE\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 25 Aug 2025 11:42:37 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E3CF5692EF;\n\tMon, 25 Aug 2025 13:42:36 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 04394692E3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 25 Aug 2025 13:42:34 +0200 (CEST)","from neptunite.infra.iob (unknown\n\t[IPv6:2404:7a81:160:2100:39d7:37aa:64a2:5533])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 2B55E4B30;\n\tMon, 25 Aug 2025 13:41:29 +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=\"gSsG2dv/\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1756122092;\n\tbh=CF2U4clHc7PCkGP283JseUVVynyJz91EOvIVuGKWLy0=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=gSsG2dv/9AYiZ0ejIusB39tdF2v1ZGv8PRavxHFxb6R/QX+SVwynFoY986EJWaGNY\n\tpZffUjmxwqzV69EolmP+tAYdvrkK71ss+RXTBvpgIA67jHbVw3vUoM/xVzmDeR8wjP\n\t4wxXLWc8et2tt1jvdPMS0WchlnpVYGeSdRznyuaw=","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Paul Elder <paul.elder@ideasonboard.com>, kieran.bingham@ideasonboard.com,\n\tstefan.klug@ideasonboard.com, barnabas.pocze@ideasonboard.com","Subject":"[PATCH v4 1/8] libcamera: ipa_manager: Factor out .so file searching","Date":"Mon, 25 Aug 2025 20:42:03 +0900","Message-ID":"<20250825114219.562831-2-paul.elder@ideasonboard.com>","X-Mailer":"git-send-email 2.47.2","In-Reply-To":"<20250825114219.562831-1-paul.elder@ideasonboard.com>","References":"<20250825114219.562831-1-paul.elder@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":"In the near future we will add support for layers/shoes on top of\nlibcamera, which will need to search for shared object files similar to\nhow IPAManager does. To avoid code duplication, move this code out of\nIPAManager into internal utils.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\n\n---\nNo change in v4\n\nChanges in v3:\n- rename addDir to findSharedObjects\n- make parseDir static\n\nNo change in v2\n---\n include/libcamera/internal/ipa_manager.h |   4 -\n include/libcamera/internal/meson.build   |   1 +\n include/libcamera/internal/utils.h       |  23 +++++\n src/libcamera/ipa_manager.cpp            | 108 ++++-------------------\n src/libcamera/meson.build                |   1 +\n src/libcamera/utils.cpp                  | 107 ++++++++++++++++++++++\n 6 files changed, 151 insertions(+), 93 deletions(-)\n create mode 100644 include/libcamera/internal/utils.h\n create mode 100644 src/libcamera/utils.cpp","diff":"diff --git a/include/libcamera/internal/ipa_manager.h b/include/libcamera/internal/ipa_manager.h\nindex a0d448cf9862..6b0ba4b5477f 100644\n--- a/include/libcamera/internal/ipa_manager.h\n+++ b/include/libcamera/internal/ipa_manager.h\n@@ -59,10 +59,6 @@ public:\n #endif\n \n private:\n-\tvoid parseDir(const char *libDir, unsigned int maxDepth,\n-\t\t      std::vector<std::string> &files);\n-\tunsigned int addDir(const char *libDir, unsigned int maxDepth = 0);\n-\n \tIPAModule *module(PipelineHandler *pipe, uint32_t minVersion,\n \t\t\t  uint32_t maxVersion);\n \ndiff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build\nindex 5c80a28c4cbe..690f5c5ec9f6 100644\n--- a/include/libcamera/internal/meson.build\n+++ b/include/libcamera/internal/meson.build\n@@ -41,6 +41,7 @@ libcamera_internal_headers = files([\n     'shared_mem_object.h',\n     'source_paths.h',\n     'sysfs.h',\n+    'utils.h',\n     'v4l2_device.h',\n     'v4l2_pixelformat.h',\n     'v4l2_subdevice.h',\ndiff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h\nnew file mode 100644\nindex 000000000000..5b7957b5d77e\n--- /dev/null\n+++ b/include/libcamera/internal/utils.h\n@@ -0,0 +1,23 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2018, Google Inc.\n+ *\n+ * Miscellaneous utility functions\n+ */\n+\n+#pragma once\n+\n+#include <functional>\n+#include <string>\n+#include <vector>\n+\n+namespace libcamera {\n+\n+namespace utils {\n+\n+unsigned int findSharedObjects(const char *libDir, unsigned int maxDepth,\n+\t\t\t       std::function<int(const std::string &)> func);\n+\n+} /* namespace utils */\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/ipa_manager.cpp b/src/libcamera/ipa_manager.cpp\nindex 830750dcc0fb..1d4de71722df 100644\n--- a/src/libcamera/ipa_manager.cpp\n+++ b/src/libcamera/ipa_manager.cpp\n@@ -8,9 +8,8 @@\n #include \"libcamera/internal/ipa_manager.h\"\n \n #include <algorithm>\n-#include <dirent.h>\n+#include <functional>\n #include <string.h>\n-#include <sys/types.h>\n \n #include <libcamera/base/file.h>\n #include <libcamera/base/log.h>\n@@ -19,6 +18,7 @@\n #include \"libcamera/internal/ipa_module.h\"\n #include \"libcamera/internal/ipa_proxy.h\"\n #include \"libcamera/internal/pipeline_handler.h\"\n+#include \"libcamera/internal/utils.h\"\n \n /**\n  * \\file ipa_manager.h\n@@ -110,6 +110,20 @@ IPAManager::IPAManager()\n \n \tunsigned int ipaCount = 0;\n \n+\tauto &modules = modules_;\n+\tstd::function<int(const std::string &)> soHandler =\n+\t[&modules](const std::string &file) {\n+\t\tauto ipaModule = std::make_unique<IPAModule>(file);\n+\t\tif (!ipaModule->isValid())\n+\t\t\treturn 0;\n+\n+\t\tLOG(IPAManager, Debug) << \"Loaded IPA module '\" << file << \"'\";\n+\n+\t\tmodules.push_back(std::move(ipaModule));\n+\n+\t\treturn 1;\n+\t};\n+\n \t/* User-specified paths take precedence. */\n \tconst char *modulePaths = utils::secure_getenv(\"LIBCAMERA_IPA_MODULE_PATH\");\n \tif (modulePaths) {\n@@ -117,7 +131,7 @@ IPAManager::IPAManager()\n \t\t\tif (dir.empty())\n \t\t\t\tcontinue;\n \n-\t\t\tipaCount += addDir(dir.c_str());\n+\t\t\tipaCount += utils::findSharedObjects(dir.c_str(), 0, soHandler);\n \t\t}\n \n \t\tif (!ipaCount)\n@@ -138,11 +152,11 @@ IPAManager::IPAManager()\n \t\t\t<< \"libcamera is not installed. Adding '\"\n \t\t\t<< ipaBuildPath << \"' to the IPA search path\";\n \n-\t\tipaCount += addDir(ipaBuildPath.c_str(), maxDepth);\n+\t\tipaCount += utils::findSharedObjects(ipaBuildPath.c_str(), maxDepth, soHandler);\n \t}\n \n \t/* Finally try to load IPAs from the installed system path. */\n-\tipaCount += addDir(IPA_MODULE_DIR);\n+\tipaCount += utils::findSharedObjects(IPA_MODULE_DIR, 0, soHandler);\n \n \tif (!ipaCount)\n \t\tLOG(IPAManager, Warning)\n@@ -151,90 +165,6 @@ IPAManager::IPAManager()\n \n IPAManager::~IPAManager() = default;\n \n-/**\n- * \\brief Identify shared library objects within a directory\n- * \\param[in] libDir The directory to search for shared objects\n- * \\param[in] maxDepth The maximum depth of sub-directories to parse\n- * \\param[out] files A vector of paths to shared object library files\n- *\n- * Search a directory for .so files, allowing recursion down to sub-directories\n- * no further than the depth specified by \\a maxDepth.\n- *\n- * Discovered shared objects are added to the \\a files vector.\n- */\n-void IPAManager::parseDir(const char *libDir, unsigned int maxDepth,\n-\t\t\t  std::vector<std::string> &files)\n-{\n-\tstruct dirent *ent;\n-\tDIR *dir;\n-\n-\tdir = opendir(libDir);\n-\tif (!dir)\n-\t\treturn;\n-\n-\twhile ((ent = readdir(dir)) != nullptr) {\n-\t\tif (ent->d_type == DT_DIR && maxDepth) {\n-\t\t\tif (strcmp(ent->d_name, \".\") == 0 ||\n-\t\t\t    strcmp(ent->d_name, \"..\") == 0)\n-\t\t\t\tcontinue;\n-\n-\t\t\tstd::string subdir = std::string(libDir) + \"/\" + ent->d_name;\n-\n-\t\t\t/* Recursion is limited to maxDepth. */\n-\t\t\tparseDir(subdir.c_str(), maxDepth - 1, files);\n-\n-\t\t\tcontinue;\n-\t\t}\n-\n-\t\tint offset = strlen(ent->d_name) - 3;\n-\t\tif (offset < 0)\n-\t\t\tcontinue;\n-\t\tif (strcmp(&ent->d_name[offset], \".so\"))\n-\t\t\tcontinue;\n-\n-\t\tfiles.push_back(std::string(libDir) + \"/\" + ent->d_name);\n-\t}\n-\n-\tclosedir(dir);\n-}\n-\n-/**\n- * \\brief Load IPA modules from a directory\n- * \\param[in] libDir The directory to search for IPA modules\n- * \\param[in] maxDepth The maximum depth of sub-directories to search\n- *\n- * This function tries to create an IPAModule instance for every shared object\n- * found in \\a libDir, and skips invalid IPA modules.\n- *\n- * Sub-directories are searched up to a depth of \\a maxDepth. A \\a maxDepth\n- * value of 0 only searches the directory specified in \\a libDir.\n- *\n- * \\return Number of modules loaded by this call\n- */\n-unsigned int IPAManager::addDir(const char *libDir, unsigned int maxDepth)\n-{\n-\tstd::vector<std::string> files;\n-\n-\tparseDir(libDir, maxDepth, files);\n-\n-\t/* Ensure a stable ordering of modules. */\n-\tstd::sort(files.begin(), files.end());\n-\n-\tunsigned int count = 0;\n-\tfor (const std::string &file : files) {\n-\t\tauto ipaModule = std::make_unique<IPAModule>(file);\n-\t\tif (!ipaModule->isValid())\n-\t\t\tcontinue;\n-\n-\t\tLOG(IPAManager, Debug) << \"Loaded IPA module '\" << file << \"'\";\n-\n-\t\tmodules_.push_back(std::move(ipaModule));\n-\t\tcount++;\n-\t}\n-\n-\treturn count;\n-}\n-\n /**\n  * \\brief Retrieve an IPA module that matches a given pipeline handler\n  * \\param[in] pipe The pipeline handler\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex b3ca27f217da..4382d4fa8d38 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -51,6 +51,7 @@ libcamera_internal_sources = files([\n     'shared_mem_object.cpp',\n     'source_paths.cpp',\n     'sysfs.cpp',\n+    'utils.cpp',\n     'v4l2_device.cpp',\n     'v4l2_pixelformat.cpp',\n     'v4l2_subdevice.cpp',\ndiff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp\nnew file mode 100644\nindex 000000000000..2ce62f236cea\n--- /dev/null\n+++ b/src/libcamera/utils.cpp\n@@ -0,0 +1,107 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * Miscellaneous utility functions (internal)\n+ */\n+\n+#include \"libcamera/internal/utils.h\"\n+\n+#include <algorithm>\n+#include <dirent.h>\n+#include <functional>\n+#include <string.h>\n+#include <string>\n+#include <sys/types.h>\n+#include <vector>\n+\n+/**\n+ * \\file internal/utils.h\n+ * \\brief Miscellaneous utility functions (internal)\n+ */\n+\n+namespace libcamera {\n+\n+namespace utils {\n+\n+/**\n+ * \\brief Identify shared library objects within a directory\n+ * \\param[in] libDir The directory to search for shared objects\n+ * \\param[in] maxDepth The maximum depth of sub-directories to parse\n+ * \\param[out] files A vector of paths to shared object library files\n+ *\n+ * Search a directory for .so files, allowing recursion down to sub-directories\n+ * no further than the depth specified by \\a maxDepth.\n+ *\n+ * Discovered shared objects are added to the \\a files vector.\n+ */\n+static void parseDir(const char *libDir, unsigned int maxDepth,\n+\t\t     std::vector<std::string> &files)\n+{\n+\tstruct dirent *ent;\n+\tDIR *dir;\n+\n+\tdir = opendir(libDir);\n+\tif (!dir)\n+\t\treturn;\n+\n+\twhile ((ent = readdir(dir)) != nullptr) {\n+\t\tif (ent->d_type == DT_DIR && maxDepth) {\n+\t\t\tif (strcmp(ent->d_name, \".\") == 0 ||\n+\t\t\t    strcmp(ent->d_name, \"..\") == 0)\n+\t\t\t\tcontinue;\n+\n+\t\t\tstd::string subdir = std::string(libDir) + \"/\" + ent->d_name;\n+\n+\t\t\t/* Recursion is limited to maxDepth. */\n+\t\t\tparseDir(subdir.c_str(), maxDepth - 1, files);\n+\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tint offset = strlen(ent->d_name) - 3;\n+\t\tif (offset < 0)\n+\t\t\tcontinue;\n+\t\tif (strcmp(&ent->d_name[offset], \".so\"))\n+\t\t\tcontinue;\n+\n+\t\tfiles.push_back(std::string(libDir) + \"/\" + ent->d_name);\n+\t}\n+\n+\tclosedir(dir);\n+}\n+\n+/**\n+ * \\brief Execute some function on shared object files from a directory\n+ * \\param[in] libDir The directory to search for shared objects\n+ * \\param[in] maxDepth The maximum depth of sub-directories to search\n+ * \\param[in] func The function to execute on every shared object\n+ *\n+ * This function tries to execute the given function \\a func for every shared\n+ * object found in \\a libDir.\n+ *\n+ * Sub-directories are searched up to a depth of \\a maxDepth. A \\a maxDepth\n+ * value of 0 only searches the directory specified in \\a libDir.\n+ *\n+ * \\return Number of shared objects loaded by this call\n+ */\n+unsigned int findSharedObjects(const char *libDir, unsigned int maxDepth,\n+\t\t\t       std::function<int(const std::string &)> func)\n+{\n+\tstd::vector<std::string> files;\n+\n+\tparseDir(libDir, maxDepth, files);\n+\n+\t/* Ensure a stable ordering of modules. */\n+\tstd::sort(files.begin(), files.end());\n+\n+\tunsigned int count = 0;\n+\tfor (const std::string &file : files)\n+\t\tcount += func(file);\n+\n+\treturn count;\n+}\n+\n+} /* namespace utils */\n+\n+} /* namespace libcamera */\n","prefixes":["v4","1/8"]}