#include "dawn/common/SystemUtils.h"
#include "dawn/common/Assert.h"
#include "dawn/common/Log.h"
#if DAWN_PLATFORM_IS(WINDOWS)
#include <Windows.h>
#include <vector>
#elif DAWN_PLATFORM_IS(LINUX)
#include <dlfcn.h>
#include <limits.h>
#include <unistd.h>
#include <cstdlib>
#elif DAWN_PLATFORM_IS(MACOS) || DAWN_PLATFORM_IS(IOS)
#include <dlfcn.h>
#include <mach-o/dyld.h>
#include <vector>
#endif
#include <array>
namespace dawn {
#if DAWN_PLATFORM_IS(WINDOWS)
const char* GetPathSeparator() {
return "\\";
}
std::pair<std::string, bool> GetEnvironmentVar(const char* variableName) {
DWORD sizeWithNullTerminator = GetEnvironmentVariableA(variableName, nullptr, 0);
if (sizeWithNullTerminator == 0) {
DWORD err = GetLastError();
if (err != ERROR_ENVVAR_NOT_FOUND) {
dawn::WarningLog() << "GetEnvironmentVariableA failed with code " << err;
}
return std::make_pair(std::string(), false);
}
std::vector<char> buffer(sizeWithNullTerminator);
DWORD sizeStored =
GetEnvironmentVariableA(variableName, buffer.data(), static_cast<DWORD>(buffer.size()));
if (sizeStored + 1 != sizeWithNullTerminator) {
DWORD err = GetLastError();
if (err) {
dawn::WarningLog() << "GetEnvironmentVariableA failed with code " << err;
}
return std::make_pair(std::string(), false);
}
return std::make_pair(std::string(buffer.data(), sizeStored), true);
}
bool SetEnvironmentVar(const char* variableName, const char* value) {
return SetEnvironmentVariableA(variableName, value) == TRUE;
}
#elif DAWN_PLATFORM_IS(POSIX)
const char* GetPathSeparator() { … }
std::pair<std::string, bool> GetEnvironmentVar(const char* variableName) { … }
bool SetEnvironmentVar(const char* variableName, const char* value) { … }
#else
#error "Implement Get/SetEnvironmentVar for your platform."
#endif
#if DAWN_PLATFORM_IS(WINDOWS)
std::optional<std::string> GetHModulePath(HMODULE module) {
std::array<char, MAX_PATH> executableFileBuf;
DWORD executablePathLen = GetModuleFileNameA(nullptr, executableFileBuf.data(),
static_cast<DWORD>(executableFileBuf.size()));
if (executablePathLen == 0) {
return {};
}
return executableFileBuf.data();
}
std::optional<std::string> GetExecutablePath() {
return GetHModulePath(nullptr);
}
#elif DAWN_PLATFORM_IS(LINUX)
std::optional<std::string> GetExecutablePath() { … }
#elif DAWN_PLATFORM_IS(MACOS) || DAWN_PLATFORM_IS(IOS)
std::optional<std::string> GetExecutablePath() {
uint32_t size = 0;
_NSGetExecutablePath(nullptr, &size);
std::vector<char> buffer(size + 1);
if (_NSGetExecutablePath(buffer.data(), &size) != 0) {
return {};
}
buffer[size] = '\0';
return buffer.data();
}
#elif DAWN_PLATFORM_IS(FUCHSIA)
std::optional<std::string> GetExecutablePath() {
return {};
}
#elif DAWN_PLATFORM_IS(EMSCRIPTEN)
std::optional<std::string> GetExecutablePath() {
return {};
}
#else
#error "Implement GetExecutablePath for your platform."
#endif
std::optional<std::string> GetExecutableDirectory() { … }
#if DAWN_PLATFORM_IS(LINUX) || DAWN_PLATFORM_IS(MACOS) || DAWN_PLATFORM_IS(IOS)
std::optional<std::string> GetModulePath() { … }
#elif DAWN_PLATFORM_IS(WINDOWS)
std::optional<std::string> GetModulePath() {
static int placeholderSymbol = 0;
HMODULE module = nullptr;
#if defined(DAWN_IS_WINUWP)
return {};
#else
if (!GetModuleHandleExA(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCSTR>(&placeholderSymbol), &module)) {
return {};
}
#endif
return GetHModulePath(module);
}
#elif DAWN_PLATFORM_IS(FUCHSIA)
std::optional<std::string> GetModulePath() {
return {};
}
#elif DAWN_PLATFORM_IS(EMSCRIPTEN)
std::optional<std::string> GetModulePath() {
return {};
}
#else
#error "Implement GetModulePath for your platform."
#endif
std::optional<std::string> GetModuleDirectory() { … }
ScopedEnvironmentVar::ScopedEnvironmentVar() = default;
ScopedEnvironmentVar::ScopedEnvironmentVar(const char* variableName, const char* value)
: … { … }
ScopedEnvironmentVar::~ScopedEnvironmentVar() { … }
bool ScopedEnvironmentVar::Set(const char* variableName, const char* value) { … }
}