[libcamera-devel,v2] libcamera: utils: merge isLibcamerainstalled with libcameraPath

Message ID 20200319115908.GA30421@kaaira-HP-Pavilion-Notebook
State Superseded
Headers show
Series
  • [libcamera-devel,v2] libcamera: utils: merge isLibcamerainstalled with libcameraPath
Related show

Commit Message

Kaaira Gupta March 19, 2020, 11:59 a.m. UTC
The two callers of functions libcameraPath() and isLibcameraInstalled()
end up using the same process and finally use the path with
libcamera.so. Hence write a function libcameraBuildPath() which
combines their functions and returns the root of the libcamera sources,
thereby making call sites simpler.

Make changes in the call sites accordingly.

Signed-off-by: Kaaira Gupta <kgupta@es.iitr.ac.in>
---

Changes since v1:
	- rename function to libcameraBuildPath() instead, and return
	  the root of libcamera source, and not the source directory
	  root.
	- Return empty string instead of nullptr when ret==0

 src/libcamera/include/utils.h |  2 +-
 src/libcamera/ipa_manager.cpp |  5 +++--
 src/libcamera/ipa_proxy.cpp   |  6 +++---
 src/libcamera/utils.cpp       | 17 +++++++++--------
 4 files changed, 16 insertions(+), 14 deletions(-)

Comments

Kieran Bingham March 19, 2020, 12:25 p.m. UTC | #1
Hi Kaaira

I think we need to adjust the Subject a little:

 libcamera: utils: merge isLibcamerainstalled with libcameraPath

We aren't merging them, we keep the two functions separate, we are
adapting libcameraPath() to a new function to suit the goals of existing
call sites:

Perhaps:
 libcamera: utils: Rework and rename libcameraPath to match use cases ?


On 19/03/2020 11:59, Kaaira Gupta wrote:
> The two callers of functions libcameraPath() and isLibcameraInstalled()
> end up using the same process and finally use the path with
> libcamera.so. Hence write a function libcameraBuildPath() which
> combines their functions and returns the root of the libcamera sources,

It returns the root of the build when the library has not been
installed, not the root of the sources, which is potentially an
important distinction to make (they are different directories).

> thereby making call sites simpler.

It could be worth highlighting here for documentation sake,

"When the library is installed, libcameraBuildPath() will return an
empty string."


> Make changes in the call sites accordingly.
> 
> Signed-off-by: Kaaira Gupta <kgupta@es.iitr.ac.in>
> ---
> 
> Changes since v1:
> 	- rename function to libcameraBuildPath() instead, and return
> 	  the root of libcamera source, and not the source directory
> 	  root.
> 	- Return empty string instead of nullptr when ret==0
> 
>  src/libcamera/include/utils.h |  2 +-
>  src/libcamera/ipa_manager.cpp |  5 +++--
>  src/libcamera/ipa_proxy.cpp   |  6 +++---
>  src/libcamera/utils.cpp       | 17 +++++++++--------
>  4 files changed, 16 insertions(+), 14 deletions(-)
> 
> diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h
> index bc96e79..5dea8d2 100644
> --- a/src/libcamera/include/utils.h
> +++ b/src/libcamera/include/utils.h
> @@ -145,7 +145,7 @@ details::StringSplitter split(const std::string &str, const std::string &delim);
>  
>  bool isLibcameraInstalled();
>  
> -std::string libcameraPath();
> +std::string libcameraBuildPath();
>  
>  } /* namespace utils */
>  
> diff --git a/src/libcamera/ipa_manager.cpp b/src/libcamera/ipa_manager.cpp
> index 0bd280c..4e294c2 100644
> --- a/src/libcamera/ipa_manager.cpp
> +++ b/src/libcamera/ipa_manager.cpp
> @@ -119,8 +119,9 @@ IPAManager::IPAManager()
>  	 * path for the IPA from that point. We need to recurse one level of
>  	 * sub-directories to match the build tree.
>  	 */
> -	if (!utils::isLibcameraInstalled()) {
> -		std::string ipaBuildPath = utils::dirname(utils::libcameraPath()) + "/../ipa";
> +	std::string root = utils::libcameraBuildPath();
> +	if (!root.empty()) {
> +		std::string ipaBuildPath = root + "/src/ipa";

Great, I think that's much clearer intent, and to read...

>  		constexpr int maxDepth = 1;
>  
>  		LOG(IPAManager, Info)
> diff --git a/src/libcamera/ipa_proxy.cpp b/src/libcamera/ipa_proxy.cpp
> index 2f866cc..5fd88a4 100644
> --- a/src/libcamera/ipa_proxy.cpp
> +++ b/src/libcamera/ipa_proxy.cpp
> @@ -97,9 +97,9 @@ std::string IPAProxy::resolvePath(const std::string &file) const
>  	 * This requires identifying the path of the libcamera.so, and
>  	 * referencing a relative path for the proxy workers from that point.
>  	 */
> -	if (!utils::isLibcameraInstalled()) {
> -		std::string ipaProxyDir = utils::dirname(utils::libcameraPath())
> -					  + "/proxy/worker";
> +	std::string root = utils::libcameraBuildPath();
> +	if (!root.empty()) {
> +		std::string ipaProxyDir = root + "src/libcamera/proxy/worker";

And that too seems good to me!

>  
>  		LOG(IPAProxy, Info)
>  			<< "libcamera is not installed. Loading proxy workers from'"
> diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp
> index 7e118fa..eae682b 100644
> --- a/src/libcamera/utils.cpp
> +++ b/src/libcamera/utils.cpp
> @@ -347,16 +347,17 @@ bool isLibcameraInstalled()
>   *
>   * \return A string stating the path
>   */
> -std::string libcameraPath()
> +std::string libcameraBuildPath()
>  {
> -	Dl_info info;
> -
> -	/* Look up our own symbol. */
> -	int ret = dladdr(reinterpret_cast<void *>(libcameraPath), &info);
> -	if (ret == 0)
> -		return nullptr;
> +	if (!isLibcameraInstalled()) {
> +		Dl_info info;

The separating line here was probably worth keeping...


> +		/* Look up our own symbol. */
> +		int ret = dladdr(reinterpret_cast<void *>(libcameraBuildPath), &info);
> +		if (ret)
> +			return dirname(info.dli_fname) + "/../..";

Oh ...

We have:
	root = dirname(info.dli_fname) + "/../..";

and
	std::string ipaBuildPath = root + "/src/ipa";

but
	std::string ipaProxyDir = root + "src/libcamera/proxy/worker";

I think I would prefer the ipaProxyDir variant (without needed to
specify the leading slash).
--
Regards

Kieran


> +	}
>  
> -	return info.dli_fname;
> +	return std::string();
>  }
>  
>  } /* namespace utils */
>

Patch

diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h
index bc96e79..5dea8d2 100644
--- a/src/libcamera/include/utils.h
+++ b/src/libcamera/include/utils.h
@@ -145,7 +145,7 @@  details::StringSplitter split(const std::string &str, const std::string &delim);
 
 bool isLibcameraInstalled();
 
-std::string libcameraPath();
+std::string libcameraBuildPath();
 
 } /* namespace utils */
 
diff --git a/src/libcamera/ipa_manager.cpp b/src/libcamera/ipa_manager.cpp
index 0bd280c..4e294c2 100644
--- a/src/libcamera/ipa_manager.cpp
+++ b/src/libcamera/ipa_manager.cpp
@@ -119,8 +119,9 @@  IPAManager::IPAManager()
 	 * path for the IPA from that point. We need to recurse one level of
 	 * sub-directories to match the build tree.
 	 */
-	if (!utils::isLibcameraInstalled()) {
-		std::string ipaBuildPath = utils::dirname(utils::libcameraPath()) + "/../ipa";
+	std::string root = utils::libcameraBuildPath();
+	if (!root.empty()) {
+		std::string ipaBuildPath = root + "/src/ipa";
 		constexpr int maxDepth = 1;
 
 		LOG(IPAManager, Info)
diff --git a/src/libcamera/ipa_proxy.cpp b/src/libcamera/ipa_proxy.cpp
index 2f866cc..5fd88a4 100644
--- a/src/libcamera/ipa_proxy.cpp
+++ b/src/libcamera/ipa_proxy.cpp
@@ -97,9 +97,9 @@  std::string IPAProxy::resolvePath(const std::string &file) const
 	 * This requires identifying the path of the libcamera.so, and
 	 * referencing a relative path for the proxy workers from that point.
 	 */
-	if (!utils::isLibcameraInstalled()) {
-		std::string ipaProxyDir = utils::dirname(utils::libcameraPath())
-					  + "/proxy/worker";
+	std::string root = utils::libcameraBuildPath();
+	if (!root.empty()) {
+		std::string ipaProxyDir = root + "src/libcamera/proxy/worker";
 
 		LOG(IPAProxy, Info)
 			<< "libcamera is not installed. Loading proxy workers from'"
diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp
index 7e118fa..eae682b 100644
--- a/src/libcamera/utils.cpp
+++ b/src/libcamera/utils.cpp
@@ -347,16 +347,17 @@  bool isLibcameraInstalled()
  *
  * \return A string stating the path
  */
-std::string libcameraPath()
+std::string libcameraBuildPath()
 {
-	Dl_info info;
-
-	/* Look up our own symbol. */
-	int ret = dladdr(reinterpret_cast<void *>(libcameraPath), &info);
-	if (ret == 0)
-		return nullptr;
+	if (!isLibcameraInstalled()) {
+		Dl_info info;
+		/* Look up our own symbol. */
+		int ret = dladdr(reinterpret_cast<void *>(libcameraBuildPath), &info);
+		if (ret)
+			return dirname(info.dli_fname) + "/../..";
+	}
 
-	return info.dli_fname;
+	return std::string();
 }
 
 } /* namespace utils */