mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-16 00:17:03 +00:00
Add ScopedEnvironmentVar class for setting env variables in BackendVk
In the Vulkan backend, we only need to set VK_LAYER_PATH and VK_ICD_FILENAMES while we're gathering the device information. After this, we should unset the environment variables or they will persist and affect loading Vulkan again (from Dawn or another client) in the future. Bug: dawn:406 Change-Id: I30c38b0980e181126fcd7fa911bbf9e8aa35f3b8 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/22021 Reviewed-by: Stephen White <senorblanco@chromium.org> Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Austin Eng <enga@chromium.org>
This commit is contained in:
committed by
Commit Bot service account
parent
f874525de3
commit
295a4177ae
@@ -14,6 +14,8 @@
|
||||
|
||||
#include "common/SystemUtils.h"
|
||||
|
||||
#include "common/Assert.h"
|
||||
|
||||
#if defined(DAWN_PLATFORM_WINDOWS)
|
||||
# include <Windows.h>
|
||||
# include <vector>
|
||||
@@ -115,3 +117,27 @@ std::string GetExecutableDirectory() {
|
||||
size_t lastPathSepLoc = exePath.find_last_of(GetPathSeparator());
|
||||
return lastPathSepLoc != std::string::npos ? exePath.substr(0, lastPathSepLoc + 1) : "";
|
||||
}
|
||||
|
||||
// ScopedEnvironmentVar
|
||||
|
||||
ScopedEnvironmentVar::ScopedEnvironmentVar(const char* variableName, const char* value)
|
||||
: mName(variableName),
|
||||
mOriginalValue(GetEnvironmentVar(variableName)),
|
||||
mIsSet(SetEnvironmentVar(variableName, value)) {
|
||||
}
|
||||
|
||||
ScopedEnvironmentVar::~ScopedEnvironmentVar() {
|
||||
if (mIsSet) {
|
||||
bool success = SetEnvironmentVar(mName.c_str(), mOriginalValue.c_str());
|
||||
// If we set the environment variable in the constructor, we should never fail restoring it.
|
||||
ASSERT(success);
|
||||
}
|
||||
}
|
||||
|
||||
bool ScopedEnvironmentVar::Set(const char* variableName, const char* value) {
|
||||
ASSERT(!mIsSet);
|
||||
mName = variableName;
|
||||
mOriginalValue = GetEnvironmentVar(variableName);
|
||||
mIsSet = SetEnvironmentVar(variableName, value);
|
||||
return mIsSet;
|
||||
}
|
||||
|
||||
@@ -24,4 +24,21 @@ std::string GetEnvironmentVar(const char* variableName);
|
||||
bool SetEnvironmentVar(const char* variableName, const char* value);
|
||||
std::string GetExecutableDirectory();
|
||||
|
||||
class ScopedEnvironmentVar {
|
||||
public:
|
||||
ScopedEnvironmentVar() = default;
|
||||
ScopedEnvironmentVar(const char* variableName, const char* value);
|
||||
~ScopedEnvironmentVar();
|
||||
|
||||
ScopedEnvironmentVar(const ScopedEnvironmentVar& rhs) = delete;
|
||||
ScopedEnvironmentVar& operator=(const ScopedEnvironmentVar& rhs) = delete;
|
||||
|
||||
bool Set(const char* variableName, const char* value);
|
||||
|
||||
private:
|
||||
std::string mName;
|
||||
std::string mOriginalValue;
|
||||
bool mIsSet = false;
|
||||
};
|
||||
|
||||
#endif // COMMON_SYSTEMUTILS_H_
|
||||
|
||||
Reference in New Issue
Block a user