Skip to content

Commit

Permalink
Added MacOS support
Browse files Browse the repository at this point in the history
  • Loading branch information
bridgerrholt authored and maddouri committed Jan 29, 2021
1 parent a69019a commit 7348d7f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/dynalo/detail/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#define DYNALO_HAS_LINUX
#elif defined(_WIN32) || defined(_WIN64)
#define DYNALO_HAS_WINDOWS
#elif defined(__APPLE__)
#define DYNALO_HAS_MACOS
#else
#error "dynalo/detail/config.hpp OS Not Supported"
#endif
Expand Down
70 changes: 70 additions & 0 deletions include/dynalo/detail/macos/dynalo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#pragma once

#include <string>
#include <stdexcept>

#include <dlfcn.h>

namespace dynalo { namespace detail
{

inline
std::string last_error()
{
return std::string(::dlerror());
}

namespace native
{

using handle = void*;

inline handle invalid_handle() { return nullptr; }

namespace name
{

inline std::string prefix() { return std::string("lib"); }
inline std::string suffix() { return std::string(); }
inline std::string extension() { return std::string("dylib"); }

}

}

inline
native::handle open(const std::string& dyn_lib_path)
{
native::handle lib_handle = ::dlopen(dyn_lib_path.c_str(), RTLD_LAZY);
if (lib_handle == nullptr)
{
throw std::runtime_error(std::string("Failed to open [dyn_lib_path:") + dyn_lib_path + "]: " + last_error());
}

return lib_handle;
}

inline
void close(native::handle lib_handle)
{
const int rc = ::dlclose(lib_handle);
if (rc != 0)
{
throw std::runtime_error(std::string("Failed to close the dynamic library: ") + last_error());
}
}

template <typename FunctionSignature>
inline
FunctionSignature* get_function(native::handle lib_handle, const std::string& func_name)
{
void* func_ptr = ::dlsym(lib_handle, func_name.c_str());
if (func_ptr == nullptr)
{
throw std::runtime_error(std::string("Failed to get [func_name:") + func_name + "]: " + last_error());
}

return reinterpret_cast<FunctionSignature*>(func_ptr);
}

}}
2 changes: 2 additions & 0 deletions include/dynalo/dynalo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "detail/linux/dynalo.hpp"
#elif defined(DYNALO_HAS_WINDOWS)
#include "detail/windows/dynalo.hpp"
#elif defined(DYNALO_HAS_MACOS)
#include "detail/macos/dynalo.hpp"
#endif

/// DYNAmic LOading of shared libraries and access to their exported functions
Expand Down

0 comments on commit 7348d7f

Please sign in to comment.