Skip to content

Commit 6d480fd

Browse files
author
Greg Roth
committed
Allow libdxcompiler.so and dxc to find libdxil.so (#5004)
When present in the library search path, linux builds of libdxcompiler.so and the dxc executable should load libdxil.so and use it for validation and signing. However, the code to do that was still searching for dxil.dll even on these platforms. This change creates appropriate names for the dxcompiler and dxil libraries for each platform and uses them where appropriate. In addition, this changes some of the internal interfaces from wide chars to simple chars as the wide interface wasn't useful here. (cherry picked from commit ae51624)
1 parent d751bd8 commit 6d480fd

14 files changed

Lines changed: 68 additions & 36 deletions

File tree

include/dxc/Support/dxcapi.use.h

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,23 @@
1616

1717
namespace dxc {
1818

19+
extern const char* kDxCompilerLib;
20+
extern const char* kDxilLib;
21+
1922
// Helper class to dynamically load the dxcompiler or a compatible libraries.
2023
class DxcDllSupport {
2124
protected:
2225
HMODULE m_dll;
2326
DxcCreateInstanceProc m_createFn;
2427
DxcCreateInstance2Proc m_createFn2;
2528

26-
HRESULT InitializeInternal(LPCWSTR dllName, LPCSTR fnName) {
29+
HRESULT InitializeInternal(LPCSTR dllName, LPCSTR fnName) {
2730
if (m_dll != nullptr) return S_OK;
2831

2932
#ifdef _WIN32
30-
m_dll = LoadLibraryW(dllName);
33+
m_dll = LoadLibraryA(dllName);
3134
#else
32-
char nameStr[256];
33-
std::wcstombs(nameStr, dllName, 256);
34-
m_dll = ::dlopen(nameStr, RTLD_LAZY);
35+
m_dll = ::dlopen(dllName, RTLD_LAZY);
3536
#endif
3637

3738
if (m_dll == nullptr) return HRESULT_FROM_WIN32(GetLastError());
@@ -86,16 +87,10 @@ class DxcDllSupport {
8687
}
8788

8889
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
90+
return InitializeInternal(kDxCompilerLib, "DxcCreateInstance");
9691
}
9792

98-
HRESULT InitializeForDll(_In_z_ const wchar_t* dll, _In_z_ const char* entryPoint) {
93+
HRESULT InitializeForDll(_In_z_ LPCSTR dll, _In_z_ LPCSTR entryPoint) {
9994
return InitializeInternal(dll, entryPoint);
10095
}
10196

lib/DxcSupport/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@ add_llvm_library(LLVMDxcSupport
1111
WinFunctions.cpp
1212
)
1313

14+
#generate header with platform-specific library name
15+
configure_file(
16+
${LLVM_MAIN_SRC_DIR}/lib/DxcSupport/SharedLibAffix.inc
17+
SharedLibAffix.h
18+
)
19+
20+
include_directories( ${LLVM_INCLUDE_DIR}/DxcSupport) #needed to find the generated header
21+
1422
add_dependencies(LLVMDxcSupport TablegenHLSLOptions)

lib/DxcSupport/HLSLOptions.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,9 +1183,8 @@ int SetupDxcDllSupport(const DxcOpts &opts, dxc::DxcDllSupport &dxcSupport,
11831183
llvm::raw_ostream &errors) {
11841184
if (!opts.ExternalLib.empty()) {
11851185
DXASSERT(!opts.ExternalFn.empty(), "else ReadDxcOpts should have failed");
1186-
StringRefWide externalLib(opts.ExternalLib);
11871186
HRESULT hrLoad =
1188-
dxcSupport.InitializeForDll(externalLib, opts.ExternalFn.data());
1187+
dxcSupport.InitializeForDll(opts.ExternalLib.data(), opts.ExternalFn.data());
11891188
if (DXC_FAILED(hrLoad)) {
11901189
errors << "Unable to load support for external DLL " << opts.ExternalLib
11911190
<< " with function " << opts.ExternalFn << " - error 0x";

lib/DxcSupport/SharedLibAffix.inc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
///////////////////////////////////////////////////////////////////////////////
2+
// //
3+
// SharedLibAffix.inc //
4+
// Copyright (C) Microsoft Corporation. All rights reserved. //
5+
// This file is distributed under the University of Illinois Open Source //
6+
// License. See LICENSE.TXT for details. //
7+
// //
8+
// Defines shared library prefixes and suffixes for the build platform. //
9+
// //
10+
///////////////////////////////////////////////////////////////////////////////
11+
12+
13+
#pragma once
14+
15+
#cmakedefine CMAKE_SHARED_LIBRARY_PREFIX "@CMAKE_SHARED_LIBRARY_PREFIX@"
16+
#cmakedefine CMAKE_SHARED_LIBRARY_SUFFIX "@CMAKE_SHARED_LIBRARY_SUFFIX@"
17+
18+
#ifndef CMAKE_SHARED_LIBRARY_PREFIX
19+
#define CMAKE_SHARED_LIBRARY_PREFIX
20+
#endif
21+
22+
#ifndef CMAKE_SHARED_LIBRARY_SUFFIX
23+
#define CMAKE_SHARED_LIBRARY_SUFFIX
24+
#endif

lib/DxcSupport/dxcapi.use.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
#include "dxc/Support/Unicode.h"
1616
#include "dxc/Support/FileIOHelper.h"
1717
#include "dxc/Support/WinFunctions.h"
18+
#include "SharedLibAffix.h"
1819

1920
namespace dxc {
2021

22+
const char* kDxCompilerLib = CMAKE_SHARED_LIBRARY_PREFIX "dxcompiler" CMAKE_SHARED_LIBRARY_SUFFIX;
23+
const char* kDxilLib = CMAKE_SHARED_LIBRARY_PREFIX "dxil" CMAKE_SHARED_LIBRARY_SUFFIX;
24+
2125
#ifdef _WIN32
2226
static void TrimEOL(_Inout_z_ char *pMsg) {
2327
char *pEnd = pMsg + strlen(pMsg);

projects/dxilconv/unittests/DxilConvTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class DxilConvTest {
166166

167167
bool DxilConvTest::InitSupport() {
168168
if (!m_dllSupport.IsEnabled()) {
169-
VERIFY_SUCCEEDED(m_dllSupport.InitializeForDll(L"dxilconv.dll", "DxcCreateInstance"));
169+
VERIFY_SUCCEEDED(m_dllSupport.InitializeForDll("dxilconv.dll", "DxcCreateInstance"));
170170
}
171171

172172
if (!FindToolInBinDir("%dxbc2dxil", "dxbc2dxil.exe")) {

tools/clang/tools/dxclib/dxc.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ void WriteDxCompilerVersionInfo(llvm::raw_ostream &OS,
11891189
CComPtr<IDxcVersionInfo2> VerInfo2;
11901190
#endif // SUPPORT_QUERY_GIT_COMMIT_INFO
11911191

1192-
const char *dllName = !ExternalLib ? "dxcompiler.dll" : ExternalLib;
1192+
const char *dllName = !ExternalLib ? kDxCompilerLib : ExternalLib;
11931193
std::string compilerName(dllName);
11941194
if (ExternalFn)
11951195
compilerName = compilerName + "!" + ExternalFn;
@@ -1244,17 +1244,17 @@ void WriteDXILVersionInfo(llvm::raw_ostream &OS,
12441244
UINT32 validatorMajor, validatorMinor = 0;
12451245
VerInfo->GetVersion(&validatorMajor, &validatorMinor);
12461246
OS << "; "
1247-
<< "dxil.dll"
1247+
<< kDxilLib
12481248
<< ": " << validatorMajor << "." << validatorMinor;
12491249

12501250
}
12511251
// dxil.dll 1.0 did not support IdxcVersionInfo
12521252
else {
12531253
OS << "; "
1254-
<< "dxil.dll: " << 1 << "." << 0;
1254+
<< kDxilLib << ": " << 1 << "." << 0;
12551255
}
12561256
unsigned int version[4];
1257-
if (GetDLLFileVersionInfo("dxil.dll", version)) {
1257+
if (GetDLLFileVersionInfo(kDxilLib, version)) {
12581258
OS << "(" << version[0] << "." << version[1] << "." << version[2] << "."
12591259
<< version[3] << ")";
12601260
}
@@ -1272,7 +1272,7 @@ void DxcContext::GetCompilerVersionInfo(llvm::raw_string_ostream &OS) {
12721272

12731273
// Print validator if exists
12741274
DxcDllSupport DxilSupport;
1275-
DxilSupport.InitializeForDll(L"dxil.dll", "DxcCreateInstance");
1275+
DxilSupport.InitializeForDll(kDxilLib, "DxcCreateInstance");
12761276
WriteDXILVersionInfo(OS, DxilSupport);
12771277
}
12781278

tools/clang/tools/dxcompiler/dxcutil.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ HRESULT ValidateAndAssembleToContainer(AssembleInputs &inputs) {
176176
if (inputs.pDiag) {
177177
unsigned diagID =
178178
inputs.pDiag->getCustomDiagID(clang::DiagnosticsEngine::Level::Warning,
179-
"DXIL.dll not found. Resulting DXIL will not be "
179+
"DXIL signing library (dxil.dll,libdxil.so) not found. Resulting DXIL will not be "
180180
"signed for use in release environments.\r\n");
181181
inputs.pDiag->Report(diagID);
182182
}

tools/clang/tools/dxcompiler/dxillib.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@ static llvm::sys::Mutex *cs = nullptr;
2525
// This function is to prevent multiple attempts to load dxil.dll
2626
HRESULT DxilLibInitialize() {
2727
cs = new llvm::sys::Mutex;
28-
#if LLVM_ON_WIN32
2928
cs->lock();
30-
g_DllLibResult = g_DllSupport.InitializeForDll(L"dxil.dll", "DxcCreateInstance");
29+
g_DllLibResult = g_DllSupport.InitializeForDll(kDxilLib, "DxcCreateInstance");
3130
cs->unlock();
32-
#endif
3331
return S_OK;
3432
}
3533

@@ -53,19 +51,14 @@ HRESULT DxilLibCleanup(DxilLibCleanUpType type) {
5351
// If we fail to load dxil.dll, set g_DllLibResult to E_FAIL so that we don't
5452
// have multiple attempts to load dxil.dll
5553
bool DxilLibIsEnabled() {
56-
#if LLVM_ON_WIN32
5754
cs->lock();
5855
if (SUCCEEDED(g_DllLibResult)) {
5956
if (!g_DllSupport.IsEnabled()) {
60-
g_DllLibResult = g_DllSupport.InitializeForDll(L"dxil.dll", "DxcCreateInstance");
57+
g_DllLibResult = g_DllSupport.InitializeForDll(kDxilLib, "DxcCreateInstance");
6158
}
6259
}
6360
cs->unlock();
6461
return SUCCEEDED(g_DllLibResult);
65-
#else
66-
g_DllLibResult = (HRESULT)-1;
67-
return false;
68-
#endif
6962
}
7063

7164

tools/clang/tools/dxopt/dxopt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ int __cdecl wmain(int argc, const wchar_t **argv_) {
300300

301301
if (externalLib) {
302302
CW2A externalFnA(externalFn, CP_UTF8);
303-
IFT(g_DxcSupport.InitializeForDll(externalLib, externalFnA));
303+
CW2A externalLibA(externalFn, CP_UTF8);
304+
IFT(g_DxcSupport.InitializeForDll(externalLibA, externalFnA));
304305
}
305306
else {
306307
IFT(g_DxcSupport.Initialize());

0 commit comments

Comments
 (0)