2019-03-09 13:31:33 +00:00
|
|
|
// Copyright 2019 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-03-09 13:31:33 +00:00
|
|
|
|
|
|
|
#include "Common/DynamicLibrary.h"
|
2019-06-14 14:53:46 +00:00
|
|
|
|
2019-03-09 13:31:33 +00:00
|
|
|
#include <cstring>
|
2019-06-14 14:53:46 +00:00
|
|
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2019-03-09 13:31:33 +00:00
|
|
|
#include "Common/Assert.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
|
|
|
#else
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace Common
|
|
|
|
{
|
|
|
|
DynamicLibrary::DynamicLibrary() = default;
|
|
|
|
|
|
|
|
DynamicLibrary::DynamicLibrary(const char* filename)
|
|
|
|
{
|
|
|
|
Open(filename);
|
|
|
|
}
|
|
|
|
|
2023-05-31 22:05:06 +00:00
|
|
|
DynamicLibrary::DynamicLibrary(void* handle)
|
|
|
|
{
|
|
|
|
m_handle = handle;
|
|
|
|
}
|
|
|
|
|
2019-03-09 13:31:33 +00:00
|
|
|
DynamicLibrary::~DynamicLibrary()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2023-05-31 22:05:06 +00:00
|
|
|
DynamicLibrary& DynamicLibrary::operator=(void* handle)
|
|
|
|
{
|
|
|
|
m_handle = handle;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-03-09 13:31:33 +00:00
|
|
|
std::string DynamicLibrary::GetUnprefixedFilename(const char* filename)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32)
|
|
|
|
return std::string(filename) + ".dll";
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
return std::string(filename) + ".dylib";
|
|
|
|
#else
|
|
|
|
return std::string(filename) + ".so";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DynamicLibrary::GetVersionedFilename(const char* libname, int major, int minor)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32)
|
|
|
|
if (major >= 0 && minor >= 0)
|
2019-06-14 14:53:46 +00:00
|
|
|
return fmt::format("{}-{}-{}.dll", libname, major, minor);
|
2019-03-09 13:31:33 +00:00
|
|
|
else if (major >= 0)
|
2019-06-14 14:53:46 +00:00
|
|
|
return fmt::format("{}-{}.dll", libname, major);
|
2019-03-09 13:31:33 +00:00
|
|
|
else
|
2019-06-14 14:53:46 +00:00
|
|
|
return fmt::format("{}.dll", libname);
|
2019-03-09 13:31:33 +00:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
const char* prefix = std::strncmp(libname, "lib", 3) ? "lib" : "";
|
|
|
|
if (major >= 0 && minor >= 0)
|
2019-06-14 14:53:46 +00:00
|
|
|
return fmt::format("{}{}.{}.{}.dylib", prefix, libname, major, minor);
|
2019-03-09 13:31:33 +00:00
|
|
|
else if (major >= 0)
|
2019-06-14 14:53:46 +00:00
|
|
|
return fmt::format("{}{}.{}.dylib", prefix, libname, major);
|
2019-03-09 13:31:33 +00:00
|
|
|
else
|
2019-06-14 14:53:46 +00:00
|
|
|
return fmt::format("{}{}.dylib", prefix, libname);
|
2019-03-09 13:31:33 +00:00
|
|
|
#else
|
|
|
|
const char* prefix = std::strncmp(libname, "lib", 3) ? "lib" : "";
|
|
|
|
if (major >= 0 && minor >= 0)
|
2019-06-14 14:53:46 +00:00
|
|
|
return fmt::format("{}{}.so.{}.{}", prefix, libname, major, minor);
|
2019-03-09 13:31:33 +00:00
|
|
|
else if (major >= 0)
|
2019-06-14 14:53:46 +00:00
|
|
|
return fmt::format("{}{}.so.{}", prefix, libname, major);
|
2019-03-09 13:31:33 +00:00
|
|
|
else
|
2019-06-14 14:53:46 +00:00
|
|
|
return fmt::format("{}{}.so", prefix, libname);
|
2019-03-09 13:31:33 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DynamicLibrary::Open(const char* filename)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
m_handle = reinterpret_cast<void*>(LoadLibraryA(filename));
|
|
|
|
#else
|
|
|
|
m_handle = dlopen(filename, RTLD_NOW);
|
|
|
|
#endif
|
|
|
|
return m_handle != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DynamicLibrary::Close()
|
|
|
|
{
|
|
|
|
if (!IsOpen())
|
|
|
|
return;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
FreeLibrary(reinterpret_cast<HMODULE>(m_handle));
|
|
|
|
#else
|
|
|
|
dlclose(m_handle);
|
|
|
|
#endif
|
|
|
|
m_handle = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* DynamicLibrary::GetSymbolAddress(const char* name) const
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return reinterpret_cast<void*>(GetProcAddress(reinterpret_cast<HMODULE>(m_handle), name));
|
|
|
|
#else
|
|
|
|
return reinterpret_cast<void*>(dlsym(m_handle, name));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} // namespace Common
|