[libcamera-devel,v2,3/6] libcamera: utils: add Libcamera installed & path

Message ID 20200318001009.32512-4-kgupta@es.iitr.ac.in
State Superseded
Headers show
Series
  • libcamera: determine IPA_PROXY_PATH at runtime
Related show

Commit Message

Kaaira Gupta March 18, 2020, 12:10 a.m. UTC
Add a global functions 'isLibcameraInstalled' to check if libcamera is
installed or not, and another global function 'libcameraPath' to return
the path of libcamera.so in utils.

Signed-off-by: Kaaira Gupta <kgupta@es.iitr.ac.in>
---
 src/libcamera/utils.cpp | 48 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

Patch

diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp
index f566e88..1f6ec60 100644
--- a/src/libcamera/utils.cpp
+++ b/src/libcamera/utils.cpp
@@ -12,6 +12,9 @@ 
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <dlfcn.h>
+#include <elf.h>
+#include <link.h>
 
 /**
  * \file utils.h
@@ -310,6 +313,51 @@  details::StringSplitter split(const std::string &str, const std::string &delim)
 	return details::StringSplitter(str, delim);
 }
 
+/**
+ * \brief Checks if Libcamera is installed or not
+ *
+ * Utilises the build_rpath dynamic tag which is stripped out by meson at
+ * install time to determine at runtime if the library currently executing
+ * has been installed or not.
+ *
+ * \return A bool stating if libcamera is installed or not
+ */
+bool isLibcameraInstalled()
+{
+	/* musl doesn't declare _DYNAMIC in link.h, declare it manually. */
+	extern ElfW(Dyn) _DYNAMIC[];
+
+	/*
+	 * DT_RUNPATH (DT_RPATH when the linker uses old dtags) is removed on
+	 * install.
+	 */
+	for (const ElfW(Dyn) *dyn = _DYNAMIC; dyn->d_tag != DT_NULL; ++dyn) {
+		if (dyn->d_tag == DT_RUNPATH || dyn->d_tag == DT_RPATH)
+			return false;
+	}
+
+	return true;
+}
+
+/**
+ * \brief Identifies libcamera path
+ *
+ * Identifies the location by finding the path of the active libcamera.so itself.
+ *
+ * \return A string stating the path.
+ */
+std::string libcameraPath()
+{
+	Dl_info info;
+
+	/* Look up our own symbol. */
+	int ret = dladdr(reinterpret_cast<void *>(libcameraPath), &info);
+	if (ret == 0)
+		return nullptr;
+
+	return info.dli_fname;
+}
+
 } /* namespace utils */
 
 } /* namespace libcamera */