From patchwork Mon Apr 27 12:18:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 3567 Return-Path: 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 EEDE1603F9 for ; Mon, 27 Apr 2020 14:18:52 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="N8DLQM4q"; dkim-atps=neutral Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 5ACD572C for ; Mon, 27 Apr 2020 14:18:52 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1587989932; bh=GHMBE45760CiUMLJq0HZwj6/Wos+ZEReX2gJe2KaI7A=; h=From:To:Subject:Date:In-Reply-To:References:From; b=N8DLQM4q+WAniHowmczheZKOPmEkCvpj4c+l0tsNAxS4f9Phy7s2qJwzD2AEwZUaG Etc7FzGQMij9zwnjFtI8DKiJemljoAitGABq26XTZjLhe7AidzJWXRHvjIViwFY/Ma 04JwpZ9VA+dxVJbi6JZTihDVJc6iYa5MqHMtS120= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Mon, 27 Apr 2020 15:18:34 +0300 Message-Id: <20200427121834.15930-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.25.3 In-Reply-To: <20200427031713.14013-3-laurent.pinchart@ideasonboard.com> References: <20200427031713.14013-3-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v1.1 02/11] libcamera: utils: Add a function to retrieve the libcamera source tree 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: , X-List-Received-Date: Mon, 27 Apr 2020 12:18:53 -0000 Similarly to libcameraBuildPath(), there's a need to locate resources within the source tree when running libcamera without installing it. Support this use case with a new utils::libcameraSourcePath() function. The implementation uses a symlink from the build root to the source root in order to avoid hardcoding the path to the source root in the libcamera.so binary. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- Changes since v1: - Use a symlink to the source tree to avoid hardcoding the path in the libcamera.so binary --- meson.build | 6 +++++ src/libcamera/include/utils.h | 1 + src/libcamera/utils.cpp | 49 ++++++++++++++++++++++++++++++++--- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/meson.build b/meson.build index c6e6a934e54e..ec5bc970f5b6 100644 --- a/meson.build +++ b/meson.build @@ -93,6 +93,12 @@ if get_option('test') subdir('test') endif +# Create a symlink from the build root to the source root. This is used when +# running libcamera from the build directory to locate resources in the source +# directory (such as IPA configuration files). +run_command('ln', '-fs', meson.source_root(), + join_paths(meson.build_root(), 'source')) + configure_file(output : 'config.h', configuration : config_h) pkg_mod = import('pkgconfig') diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h index 242eeded9d50..3334ff16613d 100644 --- a/src/libcamera/include/utils.h +++ b/src/libcamera/include/utils.h @@ -188,6 +188,7 @@ private: details::StringSplitter split(const std::string &str, const std::string &delim); std::string libcameraBuildPath(); +std::string libcameraSourcePath(); } /* namespace utils */ diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp index 97f9b632e45b..a96ca7f40cbd 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -10,10 +10,13 @@ #include #include #include +#include #include #include #include #include +#include +#include #include /** @@ -360,9 +363,10 @@ bool isLibcameraInstalled() * * During development, it is useful to run libcamera binaries directly from the * build directory without installing them. This function helps components that - * need to locate resources, such as IPA modules or IPA proxy workers, by - * providing them with the path to the root of the build directory. Callers can - * then use it to complement or override searches in system-wide directories. + * need to locate resources in the build tree, such as IPA modules or IPA proxy + * workers, by providing them with the path to the root of the build directory. + * Callers can then use it to complement or override searches in system-wide + * directories. * * If libcamera has been installed, the build directory path is not available * and this function returns an empty string. @@ -385,6 +389,45 @@ std::string libcameraBuildPath() return dirname(info.dli_fname) + "/../../"; } +/** + * \brief Retrieve the path to the source directory + * + * During development, it is useful to run libcamera binaries directly from the + * build directory without installing them. This function helps components that + * need to locate resources in the source tree, such as IPA configuration + * files, by providing them with the path to the root of the source directory. + * Callers can then use it to complement or override searches in system-wide + * directories. + * + * If libcamera has been installed, the source directory path is not available + * and this function returns an empty string. + * + * \return The path to the source directory if running from a build directory, + * or an empty string otherwise + */ +std::string libcameraSourcePath() +{ + std::string path = libcameraBuildPath(); + if (path.empty()) + return std::string(); + + path += "source"; + + char *real = realpath(path.c_str(), nullptr); + if (!real) + return std::string(); + + path = real; + free(real); + + struct stat statbuf; + int ret = stat(path.c_str(), &statbuf); + if (ret < 0 || (statbuf.st_mode & S_IFMT) != S_IFDIR) + return std::string(); + + return path + "/"; +} + } /* namespace utils */ } /* namespace libcamera */