|
16 | 16 |
|
17 | 17 | namespace dxc { |
18 | 18 |
|
| 19 | + extern const char* kDxCompilerLib; |
| 20 | + extern const char* kDxilLib; |
| 21 | + |
19 | 22 | // Helper class to dynamically load the dxcompiler or a compatible libraries. |
20 | 23 | class DxcDllSupport { |
21 | 24 | protected: |
22 | 25 | HMODULE m_dll; |
23 | 26 | DxcCreateInstanceProc m_createFn; |
24 | 27 | DxcCreateInstance2Proc m_createFn2; |
25 | 28 |
|
26 | | - HRESULT InitializeInternal(LPCWSTR dllName, LPCSTR fnName) { |
| 29 | + HRESULT InitializeInternal(LPCSTR dllName, LPCSTR fnName) { |
27 | 30 | if (m_dll != nullptr) return S_OK; |
28 | 31 |
|
29 | 32 | #ifdef _WIN32 |
30 | | - m_dll = LoadLibraryW(dllName); |
31 | | -#else |
32 | | - char nameStr[256]; |
33 | | - std::wcstombs(nameStr, dllName, 256); |
34 | | - m_dll = ::dlopen(nameStr, RTLD_LAZY); |
35 | | -#endif |
36 | | - |
| 33 | + m_dll = LoadLibraryA(dllName); |
37 | 34 | if (m_dll == nullptr) return HRESULT_FROM_WIN32(GetLastError()); |
38 | | - |
39 | | -#ifdef _WIN32 |
40 | 35 | m_createFn = (DxcCreateInstanceProc)GetProcAddress(m_dll, fnName); |
41 | | -#else |
42 | | - m_createFn = (DxcCreateInstanceProc)::dlsym(m_dll, fnName); |
43 | | -#endif |
44 | | - |
| 36 | + |
45 | 37 | if (m_createFn == nullptr) { |
46 | 38 | HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); |
47 | | -#ifdef _WIN32 |
48 | 39 | FreeLibrary(m_dll); |
| 40 | + m_dll = nullptr; |
| 41 | + return hr; |
| 42 | + } |
49 | 43 | #else |
| 44 | + m_dll = ::dlopen(dllName, RTLD_LAZY); |
| 45 | + if (m_dll == nullptr) return E_FAIL; |
| 46 | + m_createFn = (DxcCreateInstanceProc)::dlsym(m_dll, fnName); |
| 47 | + |
| 48 | + if (m_createFn == nullptr) { |
50 | 49 | ::dlclose(m_dll); |
51 | | -#endif |
52 | 50 | m_dll = nullptr; |
53 | | - return hr; |
| 51 | + return E_FAIL; |
54 | 52 | } |
| 53 | +#endif |
55 | 54 |
|
56 | 55 | // Only basic functions used to avoid requiring additional headers. |
57 | 56 | m_createFn2 = nullptr; |
@@ -86,16 +85,10 @@ class DxcDllSupport { |
86 | 85 | } |
87 | 86 |
|
88 | 87 | HRESULT Initialize() { |
89 | | - #ifdef _WIN32 |
90 | | - return InitializeInternal(L"dxcompiler.dll", "DxcCreateInstance"); |
91 | | - #elif __APPLE__ |
92 | | - return InitializeInternal(L"libdxcompiler.dylib", "DxcCreateInstance"); |
93 | | - #else |
94 | | - return InitializeInternal(L"libdxcompiler.so", "DxcCreateInstance"); |
95 | | - #endif |
| 88 | + return InitializeInternal(kDxCompilerLib, "DxcCreateInstance"); |
96 | 89 | } |
97 | 90 |
|
98 | | - HRESULT InitializeForDll(_In_z_ const wchar_t* dll, _In_z_ const char* entryPoint) { |
| 91 | + HRESULT InitializeForDll(_In_z_ LPCSTR dll, _In_z_ LPCSTR entryPoint) { |
99 | 92 | return InitializeInternal(dll, entryPoint); |
100 | 93 | } |
101 | 94 |
|
|
0 commit comments