33
44#include < dlfcn.h>
55
6+ #include " Utility/Env.h"
67#include < stdexcept>
78#include < string>
89
@@ -44,13 +45,14 @@ namespace proton {
4445
4546struct ExternLibBase {
4647 using RetType = int ; // Generic type, can be overridden in derived structs
47- static constexpr const char *name = " " ; // Placeholder
48- static constexpr RetType success = 0 ; // Placeholder
48+ static constexpr const char *name = " " ; // Placeholder
49+ static constexpr const char *symbolName{}; // Placeholder
50+ static constexpr const char *pathEnv{}; // Placeholder
51+ static constexpr RetType success = 0 ; // Placeholder
4952 ExternLibBase () = delete ;
5053 ExternLibBase (const ExternLibBase &) = delete ;
5154 ExternLibBase &operator =(const ExternLibBase &) = delete ;
5255 static inline void *lib{nullptr };
53- static inline std::string defaultDir{" " };
5456};
5557
5658template <typename ExternLib> class Dispatch {
@@ -60,8 +62,9 @@ template <typename ExternLib> class Dispatch {
6062 static void init (const char *name, void **lib) {
6163 if (*lib == nullptr ) {
6264 // If not found, try to load it from the default path
63- auto dir = std::string (ExternLib::defaultDir);
64- if (dir.length () > 0 ) {
65+ auto dir =
66+ ExternLib::pathEnv == nullptr ? " " : getStrEnv (ExternLib::pathEnv);
67+ if (!dir.empty ()) {
6568 auto fullPath = dir + " /" + name;
6669 *lib = dlopen (fullPath.c_str (), RTLD_LOCAL | RTLD_LAZY);
6770 } else {
@@ -105,6 +108,25 @@ template <typename ExternLib> class Dispatch {
105108 }
106109 return ret;
107110 }
111+
112+ static std::string getLibPath () {
113+ if (ExternLib::lib == nullptr ) {
114+ // Force initialization
115+ Dispatch<ExternLib>::init (ExternLib::name, &ExternLib::lib);
116+ if (ExternLib::lib == nullptr ) {
117+ return " " ;
118+ }
119+ }
120+ if (ExternLib::lib != nullptr ) {
121+ void *sym = dlsym (ExternLib::lib,
122+ ExternLib::symbolName); // pick any known symbol
123+ Dl_info info;
124+ if (dladdr (sym, &info)) {
125+ return info.dli_fname ;
126+ }
127+ }
128+ return " " ;
129+ }
108130};
109131
110132} // namespace proton
0 commit comments