aboutsummaryrefslogtreecommitdiff
path: root/src/common/dynamic_library.cpp
blob: 11003e1d635ea69c84dc2021305151f8ba653aeb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2019 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <string>
#include <utility>

#include <fmt/format.h>

#include "common/dynamic_library.h"

#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif

namespace Common {

DynamicLibrary::DynamicLibrary() = default;

DynamicLibrary::DynamicLibrary(const char* filename) {
    void(Open(filename));
}

DynamicLibrary::DynamicLibrary(DynamicLibrary&& rhs) noexcept
    : handle{std::exchange(rhs.handle, nullptr)} {}

DynamicLibrary& DynamicLibrary::operator=(DynamicLibrary&& rhs) noexcept {
    Close();
    handle = std::exchange(rhs.handle, nullptr);
    return *this;
}

DynamicLibrary::~DynamicLibrary() {
    Close();
}

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)
        return fmt::format("{}-{}-{}.dll", libname, major, minor);
    else if (major >= 0)
        return fmt::format("{}-{}.dll", libname, major);
    else
        return fmt::format("{}.dll", libname);
#elif defined(__APPLE__)
    const char* prefix = std::strncmp(libname, "lib", 3) ? "lib" : "";
    if (major >= 0 && minor >= 0)
        return fmt::format("{}{}.{}.{}.dylib", prefix, libname, major, minor);
    else if (major >= 0)
        return fmt::format("{}{}.{}.dylib", prefix, libname, major);
    else
        return fmt::format("{}{}.dylib", prefix, libname);
#else
    const char* prefix = std::strncmp(libname, "lib", 3) ? "lib" : "";
    if (major >= 0 && minor >= 0)
        return fmt::format("{}{}.so.{}.{}", prefix, libname, major, minor);
    else if (major >= 0)
        return fmt::format("{}{}.so.{}", prefix, libname, major);
    else
        return fmt::format("{}{}.so", prefix, libname);
#endif
}

bool DynamicLibrary::Open(const char* filename) {
#ifdef _WIN32
    handle = reinterpret_cast<void*>(LoadLibraryA(filename));
#else
    handle = dlopen(filename, RTLD_NOW);
#endif
    return handle != nullptr;
}

void DynamicLibrary::Close() {
    if (!IsOpen())
        return;

#ifdef _WIN32
    FreeLibrary(reinterpret_cast<HMODULE>(handle));
#else
    dlclose(handle);
#endif
    handle = nullptr;
}

void* DynamicLibrary::GetSymbolAddress(const char* name) const {
#ifdef _WIN32
    return reinterpret_cast<void*>(GetProcAddress(reinterpret_cast<HMODULE>(handle), name));
#else
    return reinterpret_cast<void*>(dlsym(handle, name));
#endif
}

} // namespace Common