10#ifndef CPPINTEROP_DYNAMIC_LIBRARY_MANAGER_H
11#define CPPINTEROP_DYNAMIC_LIBRARY_MANAGER_H
13#include "llvm/ADT/ArrayRef.h"
14#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/ADT/StringSet.h"
17#include "llvm/Support/Debug.h"
18#include "llvm/Support/Path.h"
22class InterpreterCallbacks;
55 typedef void* DyLibHandle;
56 typedef llvm::DenseMap<DyLibHandle, std::string> DyLibs;
60 llvm::StringSet<> m_LoadedLibraries;
66 InterpreterCallbacks* m_Callbacks =
nullptr;
68 Dyld* m_Dyld =
nullptr;
83 lookupLibInPaths(llvm::StringRef libStem,
84 llvm::SmallVector<llvm::StringRef, 2> RPath = {},
85 llvm::SmallVector<llvm::StringRef, 2> RunPath = {},
86 llvm::StringRef libLoader =
"")
const;
103 lookupLibMaybeAddExt(llvm::StringRef filename,
104 llvm::SmallVector<llvm::StringRef, 2> RPath = {},
105 llvm::SmallVector<llvm::StringRef, 2> RunPath = {},
106 llvm::StringRef libLoader =
"")
const;
111 static std::string getSymbolLocation(
void* func);
120 const InterpreterCallbacks*
getCallbacks()
const {
return m_Callbacks; }
130 bool prepend =
false) {
132 for (
auto& item : m_SearchPaths)
133 if (dir == item.Path)
135 auto pos = prepend ? m_SearchPaths.begin() : m_SearchPaths.end();
155 llvm::SmallVector<llvm::StringRef, 2> RPath = {},
156 llvm::SmallVector<llvm::StringRef, 2> RunPath = {},
157 llvm::StringRef libLoader =
"",
158 bool variateLibStem =
true)
const;
172 bool resolved =
false);
188 initializeDyld(std::function<
bool(llvm::StringRef)> shouldPermanentlyIgnore);
198 bool searchSystem =
true)
const;
200 void dump(llvm::raw_ostream* S =
nullptr)
const;
206 static_assert(std::is_pointer<T>::value,
"Must be a function pointer!");
207 return getSymbolLocation(
reinterpret_cast<void*
>(func));
220 bool* exists =
nullptr);
A helper class managing dynamic shared objects.
void unloadLibrary(llvm::StringRef libStem)
LoadLibResult loadLibrary(llvm::StringRef, bool permanent, bool resolved=false)
Loads a shared library.
static std::string normalizePath(llvm::StringRef path)
InterpreterCallbacks * getCallbacks()
DynamicLibraryManager & operator=(const DynamicLibraryManager &)=delete
static bool isSharedLibrary(llvm::StringRef libFullPath, bool *exists=nullptr)
Returns true if file is a shared library.
DynamicLibraryManager(const DynamicLibraryManager &)=delete
LoadLibResult
Describes the result of loading a library.
@ kLoadLibNotFound
library was not found
@ kLoadLibAlreadyLoaded
library was already loaded
@ kLoadLibLoadError
loading the library failed
@ kLoadLibSuccess
library loaded successfully
void addSearchPath(llvm::StringRef dir, bool isUser=true, bool prepend=false)
const InterpreterCallbacks * getCallbacks() const
void setCallbacks(InterpreterCallbacks *C)
void dump(llvm::raw_ostream *S=nullptr) const
const SearchPathInfos & getSearchPaths() const
Returns the system include paths.
std::string searchLibrariesForSymbol(llvm::StringRef mangledName, bool searchSystem=true) const
Find the first not-yet-loaded shared object that contains the symbol.
bool isLibraryLoaded(llvm::StringRef fullPath) const
Returns true if the file was a dynamic library and it was already loaded.
std::string lookupLibrary(llvm::StringRef libStem, llvm::SmallVector< llvm::StringRef, 2 > RPath={}, llvm::SmallVector< llvm::StringRef, 2 > RunPath={}, llvm::StringRef libLoader="", bool variateLibStem=true) const
Looks up a library taking into account the current include paths and the system include paths.
llvm::SmallVector< SearchPathInfo, 32 > SearchPathInfos
static std::string getSymbolLocation(T func)
On a success returns to full path to a shared object that holds the symbol pointed by func.
void initializeDyld(std::function< bool(llvm::StringRef)> shouldPermanentlyIgnore)
Initialize the dyld.
Describes the library search paths.
bool IsUser
True if the Path is on the LD_LIBRARY_PATH.
std::string Path
The search path.
bool operator==(const SearchPathInfo &Other) const