MLLIF
a MLIR-based Language to Language Interoperability Flyover
Loading...
Searching...
No Matches
BridgeStub.cxx
Go to the documentation of this file.
1#include "library.h"
2
3#include <cstdlib>
4#include <coreclr_delegates.h>
5#include <hostfxr.h>
6#include <nethost.h>
7
8#if _WIN32
9# include <libloaderapi.h>
10# define c_dlopen LoadLibraryW
11# define c_dlclose FreeLibrary
12# define c_dlsym GetProcAddress
13#else
14# include <dlfcn.h>
15# define c_dlopen(f) dlopen(f, RTLD_LAZY | RTLD_LOCAL)
16# define c_dlclose dlclose
17# define c_dlsym dlsym
18#endif
19
20static get_function_pointer_fn s_get_function_pointer;
21static void *s_dl;
22static hostfxr_handle s_hostfxr;
23
24int mllif::init(const char_t *runtimeConfigPath, const char_t *assemblyPath) {
25 static char_t buffer[8192];
26 int r = 0;
27
28 size_t size = sizeof buffer;
29 if (r = get_hostfxr_path(buffer, &size, nullptr)) {
30 return r;
31 }
32
33 s_dl = c_dlopen(buffer);
34
35 auto initr = reinterpret_cast<hostfxr_initialize_for_runtime_config_fn>(c_dlsym(s_dl, "hostfxr_initialize_for_runtime_config"));
36 if (!initr) {
37 return -1;
38 }
39
40 auto get_delegate = reinterpret_cast<hostfxr_get_runtime_delegate_fn>(c_dlsym(s_dl, "hostfxr_get_runtime_delegate"));
41 if (!get_delegate) {
42 return -1;
43 }
44
45 if (r = initr(runtimeConfigPath, nullptr, &s_hostfxr)) {
46 return r;
47 }
48
49 load_assembly_fn load_assembly = nullptr;
50 if (r = get_delegate(s_hostfxr, hdt_load_assembly, (void **)&load_assembly)) {
51 return r;
52 }
53 if (r = load_assembly(assemblyPath, nullptr, nullptr)) {
54 return r;
55 }
56 if (r = get_delegate(s_hostfxr, hdt_get_function_pointer, (void **)&s_get_function_pointer)) {
57 return r;
58 }
59
60 return 0;
61}
62
64 //hostfxr_close(s_hostfxr);
65 c_dlclose(s_dl);
66}
#define c_dlsym
#define c_dlopen(f)
#define c_dlclose
int init(const char *runtimeConfigPath, const char *assemblyPath)
void close()