Skip to content

[NFC][SYCL] Refactor OSUtils to use std::filesystem #16447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sycl/include/sycl/detail/os_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class __SYCL_EXPORT OSUtil {
/// Make all directories on the path, throws on error.
static int makeDir(const char *Dir);

/// Checks if specified path is present
/// Checks if specified path is present.
static bool isPathPresent(const std::string &Path) {
#ifdef __SYCL_RT_OS_WINDOWS
struct _stat Stat;
Expand Down
71 changes: 14 additions & 57 deletions sycl/source/detail/os_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ namespace fs = std::experimental::filesystem;
#include <libgen.h> // for dirname
#include <link.h>
#include <linux/limits.h> // for PATH_MAX
#include <sys/stat.h>
#include <sys/sysinfo.h>

#elif defined(__SYCL_RT_OS_WINDOWS)
Expand All @@ -59,6 +58,15 @@ namespace sycl {
inline namespace _V1 {
namespace detail {

#if defined(__INTEL_PREVIEW_BREAKING_CHANGES)
static std::string getDirName(const char *Path)
#else
std::string OSUtil::getDirName(const char *Path)
#endif
{
return fs::path(Path).parent_path().string();
}

#if defined(__SYCL_RT_OS_LINUX)
bool procMapsAddressInRange(std::istream &Stream, uintptr_t Addr) {
uintptr_t Start = 0, End = 0;
Expand All @@ -75,20 +83,6 @@ bool procMapsAddressInRange(std::istream &Stream, uintptr_t Addr) {
return Addr >= Start && Addr < End;
}

#if defined(__INTEL_PREVIEW_BREAKING_CHANGES)
static std::string getDirName(const char *Path)
#else
std::string OSUtil::getDirName(const char *Path)
#endif
{
std::string Tmp(Path);
// dirname(3) needs a writable C string: a null-terminator is written where a
// path should split.
size_t TruncatedSize = strlen(dirname(const_cast<char *>(Tmp.c_str())));
Tmp.resize(TruncatedSize);
return Tmp;
}

/// Returns an absolute path to a directory where the object was found.
std::string OSUtil::getCurrentDSODir() {
// Examine /proc/self/maps and find where this function (getCurrendDSODir)
Expand Down Expand Up @@ -157,7 +151,6 @@ std::string OSUtil::getCurrentDSODir() {
}

#elif defined(__SYCL_RT_OS_WINDOWS)

/// Returns an absolute path where the object was found.
// ur_win_proxy_loader.dll and sycl-jit.dll use this same logic. If it is
// changed significantly, it might be wise to change it there too.
Expand All @@ -180,21 +173,6 @@ std::string OSUtil::getCurrentDSODir() {
return Path;
}

#if !defined(__INTEL_PREVIEW_BREAKING_CHANGES)
std::string OSUtil::getDirName(const char *Path) {
std::string Tmp(Path);
// Remove trailing directory separators
Tmp.erase(Tmp.find_last_not_of("/\\") + 1, std::string::npos);

size_t pos = Tmp.find_last_of("/\\");
if (pos != std::string::npos)
return Tmp.substr(0, pos);

// If no directory separator is present return initial path like dirname does
return Tmp;
}
#endif

#elif defined(__SYCL_RT_OS_DARWIN)
std::string OSUtil::getCurrentDSODir() {
auto CurrentFunc = reinterpret_cast<const void *>(&getCurrentDSODir);
Expand All @@ -210,7 +188,6 @@ std::string OSUtil::getCurrentDSODir() {

return Path.substr(0, LastSlashPos);
}

#endif // __SYCL_RT_OS

size_t OSUtil::getOSMemSize() {
Expand Down Expand Up @@ -254,32 +231,12 @@ void OSUtil::alignedFree(void *Ptr) {
// Make all directories on the path, throws on error.
int OSUtil::makeDir(const char *Dir) {
assert((Dir != nullptr) && "Passed null-pointer as directory name.");
if (isPathPresent(Dir))
return 0;

// older GCC doesn't have full C++ 17 support.
#if __GNUC__ && __GNUC__ < 8
std::string Path{Dir}, CurPath;
size_t pos = 0;

do {
pos = Path.find_first_of("/\\", ++pos);
CurPath = Path.substr(0, pos);
#if defined(__SYCL_RT_OS_POSIX_SUPPORT)
auto Res = mkdir(CurPath.c_str(), 0777);
#else
auto Res = _mkdir(CurPath.c_str());
#endif
if (Res && errno != EEXIST)
throw std::runtime_error("Failed to mkdir: " + CurPath + " (" +
std::strerror(errno) + ")");

} while (pos != std::string::npos);
#else
// using filesystem is simpler, more reliable, works better on Win
std::filesystem::path path(Dir);
std::filesystem::create_directories(path.make_preferred());
#endif
if (!isPathPresent(Dir)) {
fs::path path(Dir);
fs::create_directories(path.make_preferred());
}

return 0;
}

Expand Down
Loading