Skip to content

Commit 7ce5cd1

Browse files
committed
remove Boost.DLL dependency by doing it ourselves
1 parent 568294b commit 7ce5cd1

6 files changed

+131
-74
lines changed

source/reflect.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ class compiler_services_data;
4444
// Reflection and meta
4545
//===========================================================================
4646

47-
#ifndef CPPFRONT_LOAD_METAFUNCTION_IMPL_HEADER
48-
#define CPPFRONT_LOAD_METAFUNCTION_IMPL_HEADER "reflect_load_metafunction_never.h"
49-
#endif
50-
51-
#include CPPFRONT_LOAD_METAFUNCTION_IMPL_HEADER
47+
#include "reflect_load_metafunction.h"
5248

5349
#include "parse.h"
5450

source/reflect.h2

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
// Reflection and meta
1616
//===========================================================================
1717

18-
#ifndef CPPFRONT_LOAD_METAFUNCTION_IMPL_HEADER
19-
#define CPPFRONT_LOAD_METAFUNCTION_IMPL_HEADER "reflect_load_metafunction_never.h"
20-
#endif
21-
22-
#include CPPFRONT_LOAD_METAFUNCTION_IMPL_HEADER
18+
#include "reflect_load_metafunction.h"
2319

2420
#include "parse.h"
2521

source/reflect_load_metafunction.h

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#include <cstdlib>
2+
#include <functional>
3+
#include <string>
4+
#include <string_view>
5+
#include <utility>
6+
#include "cpp2util.h"
7+
8+
#ifdef _MSC_VER
9+
#define WIN32_LEAN_AND_MEAN
10+
#include <windows.h>
11+
#else
12+
#include <dlfcn.h>
13+
#endif // _MSC_VER
14+
15+
namespace cpp2::meta {
16+
17+
class dll
18+
{
19+
public:
20+
dll(std::string_view path)
21+
{
22+
#ifdef _MSC_VER
23+
handle_ = static_cast<void*>(LoadLibraryA(path.data()));
24+
#else
25+
handle_ = static_cast<void*>(dlopen(path.data(), RTLD_NOW|RTLD_LOCAL));
26+
#endif // _MSC_VER
27+
// TODO: log if the dll could not be open?
28+
}
29+
30+
~dll() noexcept
31+
{
32+
if(handle_ == nullptr);
33+
return;
34+
#ifdef _MSC_VER
35+
FreeLibrary(static_cast<HMODULE>(handle));
36+
#else
37+
dlclose(handle_);
38+
#endif // _MSC_VER
39+
}
40+
41+
// Uncopyable
42+
dll(dll&) = delete;
43+
dll(dll const&) = delete;
44+
auto operator=(dll const&) -> dll& = delete;
45+
46+
// Movable
47+
dll(dll&& from) noexcept
48+
{
49+
handle_ = from.handle_;
50+
from.handle_ = nullptr;
51+
}
52+
53+
auto operator=(dll&& from) noexcept -> dll&
54+
{
55+
handle_ = from.handle_;
56+
from.handle_ = nullptr;
57+
return *this;
58+
}
59+
60+
auto is_open() noexcept -> bool { return handle_ != nullptr; }
61+
62+
template<typename T>
63+
auto get_alias(std::string_view name) noexcept -> T*
64+
{
65+
void* symbol = nullptr;
66+
#ifdef _MSC_VER
67+
symbol = static_cast<void*>(GetProcAddress(static_cast<HMODULE>(handle_), name.data()));
68+
#else
69+
symbol = dlsym(handle_, name.data());
70+
if(symbol == nullptr)
71+
{
72+
// Some platforms export with additional underscore, so try that.
73+
auto const us_name = std::string("_") + name.data();
74+
symbol = dlsym(handle_, us_name.c_str());
75+
}
76+
#endif // _MSC_VER
77+
// TODO: log if the symbol is not found?
78+
return reinterpret_cast<T*>(symbol);
79+
}
80+
private:
81+
void* handle_{nullptr};
82+
};
83+
84+
85+
// Load metafunction using Boost.DLL
86+
//
87+
// The ':'-separated library paths
88+
// are read from the environment variable
89+
// 'CPPFRONT_METAFUNCTION_LIBRARIES'
90+
auto load_metafunction(std::string const& name) -> std::function<void(type_declaration&)>
91+
{
92+
auto cpp1_libraries_cstr = std::getenv("CPPFRONT_METAFUNCTION_LIBRARIES");
93+
if (!cpp1_libraries_cstr) {
94+
return {};
95+
}
96+
97+
auto cpp1_libraries = std::string_view{cpp1_libraries_cstr};
98+
auto cpp1_name = "cpp2_metafunction_" + name;
99+
100+
while (!cpp1_libraries.empty())
101+
{
102+
auto colon = cpp1_libraries.find(':');
103+
auto lib_path = cpp1_libraries.substr(0, colon);
104+
cpp1_libraries.remove_prefix(lib_path.size() + unsigned(colon != lib_path.npos));
105+
106+
auto lib = std::make_shared<dll>(lib_path);
107+
if(!lib->is_open())
108+
continue;
109+
110+
if (auto* fun = lib->get_alias<void(void*)>(cpp1_name); fun != nullptr)
111+
{
112+
return [
113+
fun = fun,
114+
lib = lib
115+
](type_declaration& t)
116+
{
117+
fun(static_cast<void*>(&t));
118+
};
119+
}
120+
}
121+
122+
return {};
123+
}
124+
125+
}

source/reflect_load_metafunction_boost_dll.h

Lines changed: 0 additions & 48 deletions
This file was deleted.

source/reflect_load_metafunction_never.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

source/to_cpp1.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6448,11 +6448,12 @@ class cppfront
64486448
{
64496449
auto identifier = print_to_string(*n.identifier);
64506450
printer.print_extra(
6451-
"\nextern \"C\" constexpr auto cpp2_metafunction_"
6451+
"\nextern \"C\" void cpp2_metafunction_"
64526452
+ identifier
6453-
+ " = &"
6453+
+ "(void* t) { "
64546454
+ identifier
6455-
+ ";"
6455+
+ "(*static_cast<cpp2::meta::type_declaration*>(t));"
6456+
+ " }"
64566457
);
64576458
}
64586459
}

0 commit comments

Comments
 (0)