Skip to content

Commit 57dd853

Browse files
committed
address tex, make wstrings more universal too
1 parent 2915a8d commit 57dd853

File tree

3 files changed

+71
-85
lines changed

3 files changed

+71
-85
lines changed

include/dxc/Support/dxcapi.extval.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ class DxcDllExtValidationSupport : public dxc::DxcDllSupport {
88
protected:
99
dxc::DxcDllSupport DxilSupport;
1010

11-
std::string DxilDllPath;
11+
std::wstring DxilDllPath;
1212

1313
// override DxcDllSupport's implementation of InitializeInternal,
1414
// adding the environment variable value check for a path to a dxil.dll
1515
HRESULT InitializeInternal(LPCSTR dllName, LPCSTR fnName) override;
1616

1717
public:
18-
std::string GetDxilDllPath() { return DxilDllPath; }
18+
std::wstring GetDxilDllPath() { return DxilDllPath; }
1919
bool DxilDllFailedToLoad() {
2020
return !DxilDllPath.empty() && !DxilSupport.IsEnabled();
2121
}

lib/DxcSupport/dxcapi.extval.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,30 @@
22
#include <dxc/Support/Global.h> // for hresult handling with DXC_FAILED
33
#include <filesystem> // C++17 and later
44
// WinIncludes must come before dxcapi.extval.h
5+
#include "dxc/Support/Unicode.h" // for wstring conversions like WideToUtf8String
56
#include "dxc/Support/dxcapi.extval.h"
67

78
HRESULT DxcDllExtValidationSupport::InitializeInternal(LPCSTR dllName,
89
LPCSTR fnName) {
910
// Load dxcompiler.dll
10-
HRESULT result = DxcDllSupport::InitializeInternal(dllName, fnName);
11+
HRESULT Result = DxcDllSupport::InitializeInternal(dllName, fnName);
1112
// if dxcompiler.dll fails to load, return the failed HRESULT
12-
if (DXC_FAILED(result)) {
13-
return result;
13+
if (DXC_FAILED(Result)) {
14+
return Result;
1415
}
1516

1617
// now handle external dxil.dll
17-
const char *envVal = std::getenv("DXC_DXIL_DLL_PATH");
18-
if (!envVal || std::string(envVal).empty()) {
18+
const wchar_t *EnvVarValue = _wgetenv(L"DXC_DXIL_DLL_PATH");
19+
if (!EnvVarValue || std::wstring(EnvVarValue).empty()) {
1920
return S_OK;
2021
}
2122

22-
std::string DllPathStr(envVal);
23-
DxilDllPath = DllPathStr;
24-
std::filesystem::path DllPath(DllPathStr);
23+
std::wstring DllPathWStr(EnvVarValue);
24+
std::string DllPathStr;
25+
Unicode::WideToUTF8String(DllPathWStr.data(), &DllPathStr);
26+
27+
DxilDllPath = DllPathWStr;
28+
std::filesystem::path DllPath(DllPathWStr);
2529

2630
// Check if path is absolute and exists
2731
if (!DllPath.is_absolute() || !std::filesystem::exists(DllPath)) {
@@ -31,6 +35,7 @@ HRESULT DxcDllExtValidationSupport::InitializeInternal(LPCSTR dllName,
3135
// to see if dxil.dll is successfully loaded.
3236
// the CheckDxilDLLLoaded function can determine whether there were any
3337
// problems loading dxil.dll or not
38+
3439
DxilSupport.InitializeForDll(DllPathStr.data(), fnName);
3540

3641
return S_OK;

tools/clang/unittests/HLSL/ValidationTest.cpp

Lines changed: 56 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4211,46 +4211,31 @@ TEST_F(ValidationTest, ValidateWithHash) {
42114211
VERIFY_ARE_EQUAL(memcmp(Result, pHeader->Hash.Digest, sizeof(Result)), 0);
42124212
}
42134213

4214-
std::wstring GetEnvVarW(const std::wstring &varName) {
4214+
std::wstring GetEnvVarW(const std::wstring &VarName) {
42154215
#ifdef _WIN32
4216-
DWORD size = GetEnvironmentVariableW(varName.c_str(), nullptr, 0);
4217-
if (size == 0) {
4218-
return L""; // Not found or empty
4219-
}
4220-
4221-
std::wstring buffer(size - 1, '\0'); // size includes null terminator
4222-
GetEnvironmentVariableW(varName.c_str(), &buffer[0], size);
4223-
return buffer;
4224-
#else
4225-
const char *result = std::getenv(varName.c_str());
4226-
return result ? std::string(result) : std::string();
4227-
#endif
4228-
}
4229-
4230-
void SetEnvVarW(const std::wstring &varName, const std::wstring &varValue) {
4231-
#ifdef _WIN32
4232-
VERIFY_IS_TRUE(SetEnvironmentVariableW(varName.c_str(), varValue.c_str()));
4233-
// also update the CRT environment
4234-
std::string varNameStr;
4235-
std::string varValueStr;
4236-
Unicode::WideToUTF8String(varName.c_str(), &varNameStr);
4237-
Unicode::WideToUTF8String(varValue.c_str(), &varValueStr);
4238-
_putenv_s(varNameStr.c_str(), varValueStr.c_str());
4216+
if (const wchar_t *Result = _wgetenv(VarName.c_str()))
4217+
return std::wstring(Result);
42394218
#else
4240-
std::string name_utf8 = wstring_to_utf8(varName);
4241-
std::string value_utf8 = wstring_to_utf8(varValue);
4242-
setenv(name_utf8.c_str(), value_utf8.c_str(), 1);
4219+
std::string NameUtf8;
4220+
WideToUTF8String(Name.c_str, &NameUtf8);
4221+
if (const char *Result = std::getenv(NameUtf8.c_str())) {
4222+
std::wstring ResultWide;
4223+
Unicode::UTF8ToWideString(Result.c_str(), &ResultWide);
4224+
return std::wstring(ResultWide);
4225+
}
42434226
#endif
4227+
return std::wstring();
42444228
}
42454229

4246-
void ClearEnvVarW(const std::wstring &varName) {
4247-
std::string varNameStr;
4248-
Unicode::WideToUTF8String(varName.c_str(), &varNameStr);
4230+
void SetEnvVarW(const std::wstring &VarName, const std::wstring &VarValue) {
42494231
#ifdef _WIN32
4250-
SetEnvironmentVariableW(varName.c_str(), nullptr);
4251-
_putenv_s(varNameStr.c_str(), "");
4232+
_wputenv_s(VarName.c_str(), VarValue.c_str());
42524233
#else
4253-
unsetenv(varNameStr.c_str());
4234+
std::string VarNameUtf8;
4235+
std::string VarValueUtf8;
4236+
Unicode::WideToUTF8String(VarName.c_str(), &VarNameUtf8);
4237+
Unicode::WideToUTF8String(VarValue.c_str(), &VarValueUtf8);
4238+
setenv(VarNameUtf8.c_str(), VarValueUtf8.c_str(), 1);
42544239
#endif
42554240
}
42564241

@@ -4266,70 +4251,66 @@ void ClearEnvVarW(const std::wstring &varName) {
42664251
// works as intended.
42674252

42684253
TEST_F(ValidationTest, UnitTestExtValidationSupport) {
4269-
DxcDllExtValidationSupport m_dllExtSupport1;
4270-
DxcDllExtValidationSupport m_dllExtSupport2;
4254+
DxcDllExtValidationSupport ExtSupportEmpty;
4255+
DxcDllExtValidationSupport ExtSupportBogus;
42714256

42724257
// capture any existing value in the environment variable,
42734258
// so that it can be restored after the test
4274-
std::wstring oldEnvVal = GetEnvVarW(L"DXC_DXIL_DLL_PATH");
4259+
std::wstring OldEnvVal = GetEnvVarW(L"DXC_DXIL_DLL_PATH");
42754260

42764261
// 1. with no env var set, test GetDxilDllPath() and DxilDllFailedToLoad()
42774262

42784263
// make sure the variable is cleared, in case other tests may have set it
42794264
SetEnvVarW(L"DXC_DXIL_DLL_PATH", L"");
42804265

42814266
// empty initialization should succeed
4282-
VERIFY_SUCCEEDED(m_dllExtSupport1.Initialize());
4267+
VERIFY_SUCCEEDED(ExtSupportEmpty.Initialize());
42834268

4284-
VERIFY_IS_FALSE(m_dllExtSupport1.DxilDllFailedToLoad());
4285-
VERIFY_ARE_EQUAL(m_dllExtSupport1.GetDxilDllPath(), "");
4269+
VERIFY_IS_FALSE(ExtSupportEmpty.DxilDllFailedToLoad());
4270+
VERIFY_ARE_EQUAL_WSTR(ExtSupportEmpty.GetDxilDllPath().c_str(), L"");
42864271

42874272
// 2. Test with a bogus path in the environment variable
42884273
SetEnvVarW(L"DXC_DXIL_DLL_PATH", L"bogus");
42894274

4290-
if (!m_dllExtSupport2.IsEnabled()) {
4291-
VERIFY_SUCCEEDED(m_dllExtSupport2.Initialize());
4275+
if (!ExtSupportBogus.IsEnabled()) {
4276+
VERIFY_SUCCEEDED(ExtSupportBogus.Initialize());
42924277
}
42934278

42944279
// validate that m_dllExtSupport2 was able to capture the environment
42954280
// variable's value, and that loading the bogus path was unsuccessful
4296-
std::string extPath("bogus");
4297-
VERIFY_ARE_EQUAL(m_dllExtSupport2.GetDxilDllPath(), extPath);
4298-
VERIFY_IS_TRUE(m_dllExtSupport2.DxilDllFailedToLoad());
4281+
VERIFY_ARE_EQUAL_WSTR(ExtSupportBogus.GetDxilDllPath().c_str(), L"bogus");
4282+
VERIFY_IS_TRUE(ExtSupportBogus.DxilDllFailedToLoad());
42994283

43004284
// 3. Test production of class IDs CLSID_DxcCompiler, CLSID_DxcLinker,
43014285
// and CLSID_DxcValidator through DxcDllExtValidationSupport.
4302-
CComPtr<IDxcCompiler> pCompiler;
4303-
CComPtr<IDxcLinker> pLinker;
4304-
CComPtr<IDxcValidator> pValidator;
4305-
CComPtr<IDxcOperationResult> pResult;
4306-
4307-
VERIFY_SUCCEEDED(m_dllExtSupport2.CreateInstance(
4308-
CLSID_DxcCompiler, __uuidof(IDxcCompiler), (IUnknown **)&pCompiler));
4309-
VERIFY_SUCCEEDED(m_dllExtSupport2.CreateInstance(
4310-
CLSID_DxcLinker, __uuidof(IDxcLinker), (IUnknown **)&pLinker));
4311-
VERIFY_SUCCEEDED(m_dllExtSupport2.CreateInstance(
4312-
CLSID_DxcValidator, __uuidof(IDxcValidator), (IUnknown **)&pValidator));
4313-
CComPtr<IMalloc> pMalloc;
4314-
CComPtr<IDxcCompiler2> pCompiler2;
4315-
pLinker.Release();
4316-
pValidator.Release();
4317-
VERIFY_SUCCEEDED(DxcCoGetMalloc(1, &pMalloc));
4318-
VERIFY_SUCCEEDED(m_dllExtSupport2.CreateInstance2(pMalloc, CLSID_DxcCompiler,
4319-
__uuidof(IDxcCompiler),
4320-
(IUnknown **)&pCompiler2));
4321-
VERIFY_SUCCEEDED(m_dllExtSupport2.CreateInstance2(
4322-
pMalloc, CLSID_DxcLinker, __uuidof(IDxcLinker), (IUnknown **)&pLinker));
4323-
VERIFY_SUCCEEDED(m_dllExtSupport2.CreateInstance2(pMalloc, CLSID_DxcValidator,
4324-
__uuidof(IDxcValidator),
4325-
(IUnknown **)&pValidator));
4326-
4327-
// reset the environment variable to its previous value, if it had one.
4328-
if (!oldEnvVal.empty()) {
4329-
SetEnvVarW(L"DXC_DXIL_DLL_PATH", oldEnvVal);
4330-
} else {
4331-
ClearEnvVarW(L"DXC_DXIL_DLL_PATH");
4332-
}
4286+
CComPtr<IDxcCompiler> Compiler;
4287+
CComPtr<IDxcLinker> Linker;
4288+
CComPtr<IDxcValidator> Validator;
4289+
4290+
VERIFY_SUCCEEDED(ExtSupportBogus.CreateInstance(
4291+
CLSID_DxcCompiler, __uuidof(IDxcCompiler), (IUnknown **)&Compiler));
4292+
VERIFY_SUCCEEDED(ExtSupportBogus.CreateInstance(
4293+
CLSID_DxcLinker, __uuidof(IDxcLinker), (IUnknown **)&Linker));
4294+
VERIFY_SUCCEEDED(ExtSupportBogus.CreateInstance(
4295+
CLSID_DxcValidator, __uuidof(IDxcValidator), (IUnknown **)&Validator));
4296+
4297+
CComPtr<IMalloc> Malloc;
4298+
CComPtr<IDxcCompiler2> Compiler2;
4299+
Linker.Release();
4300+
Validator.Release();
4301+
VERIFY_SUCCEEDED(DxcCoGetMalloc(1, &Malloc));
4302+
VERIFY_SUCCEEDED(ExtSupportBogus.CreateInstance2(Malloc, CLSID_DxcCompiler,
4303+
__uuidof(IDxcCompiler),
4304+
(IUnknown **)&Compiler2));
4305+
VERIFY_SUCCEEDED(ExtSupportBogus.CreateInstance2(
4306+
Malloc, CLSID_DxcLinker, __uuidof(IDxcLinker), (IUnknown **)&Linker));
4307+
VERIFY_SUCCEEDED(ExtSupportBogus.CreateInstance2(Malloc, CLSID_DxcValidator,
4308+
__uuidof(IDxcValidator),
4309+
(IUnknown **)&Validator));
4310+
4311+
// reset the environment variable to its previous value,
4312+
// or the empty string if there was no previous value
4313+
SetEnvVarW(L"DXC_DXIL_DLL_PATH", OldEnvVal);
43334314
}
43344315

43354316
TEST_F(ValidationTest, ValidatePreviewBypassHash) {

0 commit comments

Comments
 (0)