summaryrefslogtreecommitdiff
path: root/lldb/packages/Python/lldbsuite/test/make/dylib.h
blob: 50abcdbca9a2fc2cd0b71a04935520a535daaf64 (plain)
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
#ifndef LLDB_TEST_DYLIB_H
#define LLDB_TEST_DYLIB_H

#include <stdio.h>

#ifdef _WIN32
#include <Windows.h>

#define dylib_get_symbol(handle, name) GetProcAddress((HMODULE)handle, name)
#define dylib_close(handle) (!FreeLibrary((HMODULE)handle))
#else
#include <dlfcn.h>

#define dylib_get_symbol(handle, name) dlsym(handle, name)
#define dylib_close(handle) dlclose(handle)
#endif


inline void *dylib_open(const char *name) {
  char dylib_prefix[] =
#ifdef _WIN32
    "";
#else
    "lib";
#endif
  char dylib_suffix[] =
#ifdef _WIN32
    ".dll";
#elif defined(__APPLE__)
    ".dylib";
#else
    ".so";
#endif
  char fullname[1024];
  snprintf(fullname, sizeof(fullname), "%s%s%s", dylib_prefix, name, dylib_suffix);
#ifdef _WIN32
  return LoadLibraryA(fullname);
#else
  return dlopen(fullname, RTLD_NOW);
#endif
}

inline const char *dylib_last_error() {
#ifndef _WIN32
  return dlerror();
#else
  DWORD err = GetLastError();
  char *msg;
  FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
      NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char *)&msg, 0, NULL);
  return msg;
#endif
}

#endif