Skip to content

Commit

Permalink
Cleanup windows specific code and restore mingw compatibility
Browse files Browse the repository at this point in the history
Check the ``_WIN32`` macro rather than ``_MSC_VER`` to detect if we're targeting Windows, rather than checking if we're compiling using Visual Studio.
Add ``function_cast`` to properly convert the function pointer retrieved from GetProcAddress when building under mingw.
  • Loading branch information
edo9300 authored and DyXel committed Dec 27, 2023
1 parent bc363e5 commit 32063ad
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions source/reflect_load_metafunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
#include <utility>
#include "cpp2util.h"

#ifdef _MSC_VER
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#ifdef _WIN32
#include <libloaderapi.h>
#else
#include <dlfcn.h>
#endif // _MSC_VER
#endif // _WIN32

namespace cpp2::meta {

Expand All @@ -19,23 +18,23 @@ class dll
public:
dll(std::string const& path)
{
#ifdef _MSC_VER
#ifdef _WIN32
handle_ = static_cast<void*>(LoadLibraryA(path.c_str()));
#else
handle_ = static_cast<void*>(dlopen(path.c_str(), RTLD_NOW|RTLD_LOCAL));
#endif // _MSC_VER
#endif // _WIN32
// TODO: log if the dll could not be open?
}

~dll() noexcept
{
if(handle_ == nullptr);
return;
#ifdef _MSC_VER
FreeLibrary(static_cast<HMODULE>(handle));
#ifdef _WIN32
FreeLibrary(static_cast<HMODULE>(handle_));
#else
dlclose(handle_);
#endif // _MSC_VER
#endif // _WIN32
}

// Uncopyable
Expand Down Expand Up @@ -72,12 +71,19 @@ class dll
auto const us_name = "_" + name;
symbol = dlsym(handle_, us_name.c_str());
}
#endif // _MSC_VER
#endif // _WIN32
// TODO: log if the symbol is not found?
return reinterpret_cast<T*>(symbol);
return function_cast<T*>(symbol);
}
private:
void* handle_{nullptr};

template<typename T>
static T function_cast(auto ptr) {
using generic_function_ptr = void (*)(void);
return reinterpret_cast<T>(reinterpret_cast<generic_function_ptr>(ptr));
}

};


Expand All @@ -88,6 +94,10 @@ class dll
// 'CPPFRONT_METAFUNCTION_LIBRARIES'
auto load_metafunction(std::string const& name) -> std::function<void(type_declaration&)>
{
// FIXME: On Windows, using this approach with the system apis not set to utf8, will
// break if a metafunction library contains unicode codepoints in its name, a proper
// way to handle this would be to use _wgetenv and use wchar_t strings for the dll opening
// function
auto cpp1_libraries_cstr = std::getenv("CPPFRONT_METAFUNCTION_LIBRARIES");
if (!cpp1_libraries_cstr) {
return {};
Expand Down

0 comments on commit 32063ad

Please sign in to comment.