Skip to content

Commit

Permalink
[Windows] default paths to user and system db files on Windows (#2365)
Browse files Browse the repository at this point in the history
  • Loading branch information
apwojcik authored Dec 27, 2023
1 parent 94ed5b1 commit 9e666c0
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 13 deletions.
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,13 @@ if(BUILD_DEV)
set(MIOPEN_CACHE_DIR "" CACHE STRING "")
else()
set(MIOPEN_BUILD_DEV 0)
set(MIOPEN_USER_DB_PATH "~/.config/miopen/" CACHE STRING "Default path of user db files")
set(MIOPEN_CACHE_DIR "~/.cache/miopen/" CACHE STRING "")
if(WIN32)
set(MIOPEN_USER_DB_PATH "$USERPROFILE\\\\.miopen\\\\db\\\\" CACHE STRING "Default path to user db files")
set(MIOPEN_CACHE_DIR "$USERPROFILE\\\\.miopen\\\\cache\\\\" CACHE STRING "")
else()
set(MIOPEN_USER_DB_PATH "~/.config/miopen/" CACHE STRING "Default path of user db files")
set(MIOPEN_CACHE_DIR "~/.cache/miopen/" CACHE STRING "")
endif()
set(MIOPEN_USER_DB_SUFFIX "${MIOPEN_BACKEND}.${MIOpen_VERSION_MAJOR}_${MIOpen_VERSION_MINOR}_${MIOpen_VERSION_PATCH}_${MIOpen_VERSION_TWEAK}" CACHE PATH "Filename suffix for the user find-db files")
endif()
set(MIOPEN_SYSTEM_FIND_DB_SUFFIX "${MIOPEN_BACKEND}" CACHE PATH "Filename suffix for the system find-db files")
Expand Down
10 changes: 5 additions & 5 deletions src/db_path.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ MIOPEN_DECLARE_ENV_VAR_STR(MIOPEN_USER_DB_PATH)

namespace miopen {

#ifdef __linux__
boost::filesystem::path GetLibPath()
{
boost::filesystem::path path = {""};
#ifdef __linux__
Dl_info info;

if(dladdr(reinterpret_cast<void*>(miopenCreate), &info) != 0)
Expand All @@ -58,17 +58,17 @@ boost::filesystem::path GetLibPath()
path = path.parent_path();
}
return path;
#else
MIOPEN_THROW(miopenStatusNotImplemented);
#endif
}
#endif

std::string GetSystemDbPath()
{
auto p = GetStringEnv(ENV(MIOPEN_SYSTEM_DB_PATH));
if(p.empty())
#if MIOPEN_BUILD_DEV
#if MIOPEN_BUILD_DEV || defined(_WIN32)
{
// Here, by default, MIOPEN_SYSTEM_DB_PATH is an empty path on Windows.
// MIOpen.dll will be searching for System DB files in local directory.
return "${MIOPEN_SYSTEM_DB_PATH}";
}
#else
Expand Down
77 changes: 71 additions & 6 deletions src/expanduser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
#include <boost/filesystem.hpp>

#include <string>
#ifdef _WIN32
#include <optional>
#include <boost/algorithm/string/replace.hpp>
#endif

#ifdef __linux__
#include <errno.h>
Expand Down Expand Up @@ -131,11 +135,9 @@ bool IsNetworked(unsigned long ft)
} // namespace

#undef CASE_RET_STRING
#endif // __linux__

bool IsNetworkedFilesystem(const boost::filesystem::path& path_)
{
#ifdef __linux__
// Non-DEV builds put user databases in ~/.config/miopen by default; the binary cache is placed
// in ~/.cache/miopen. If these directories do not exist, this is not a problem, because the
// library creates them as needed.
Expand Down Expand Up @@ -172,10 +174,6 @@ bool IsNetworkedFilesystem(const boost::filesystem::path& path_)
MIOPEN_LOG_NQI("Filesystem type at '" << path.string() << "' is: 0x" << std::hex << stat.f_type
<< " '" << Stringize(stat.f_type) << '\'');
return IsNetworked(stat.f_type);
#else
std::ignore = path_;
return false;
#endif // __linux__
}

namespace {
Expand All @@ -200,4 +198,71 @@ boost::filesystem::path ExpandUser(const std::string& path)
return {ReplaceString(path, "~", home_dir)};
}

#else

namespace {
std::optional<std::string> GetEnvironmentVariable(const std::string_view name)
{
std::size_t required_size;
getenv_s(&required_size, nullptr, 0, name.data());
if(required_size == 0)
{
return std::nullopt;
}
// getenv_s returns the required size of a string including '\0' character.
std::string result(required_size - 1, 'A');
getenv_s(&required_size, result.data(), required_size, name.data());
return {result};
}

std::optional<std::pair<std::string::size_type, std::string>>
ReplaceVariable(const std::string& path, std::string_view name, std::size_t offset = 0)
{
std::vector<std::string> variables{
"$" + std::string{name}, "$env:" + std::string{name}, "%" + std::string{name} + "%"};
for(auto& variable : variables)
{
auto pos{path.find(variable, offset)};
if(pos != std::string::npos)
{
auto result{path};
auto value{GetEnvironmentVariable(name)};
if(!value)
{
// TODO: log warning message that the name used does not
// correspond to an environment variable.
value = boost::filesystem::temp_directory_path().string();
}
result.replace(pos, variable.length(), *value);
return {{pos, result}};
}
}
return std::nullopt;
}
} // namespace

boost::filesystem::path ExpandUser(const std::string& path)
{
auto result{ReplaceVariable(path, "USERPROFILE")};
if(!result)
{
result = ReplaceVariable(path, "HOME");
if(!result)
{
result = ReplaceVariable(path, "HOMEDRIVE");
if(result)
{
result = ReplaceVariable(std::get<1>(*result), "HOMEPATH", std::get<0>(*result));
// TODO: if (not result): log warning message that
// HOMEDRIVE and HOMEPATH work in conjunction, respectively.
}
}
}
return {!result ? path : std::get<1>(*result)};
}

bool IsNetworkedFilesystem(const boost::filesystem::path&) { return false; }

#endif

} // namespace miopen

0 comments on commit 9e666c0

Please sign in to comment.