From f53aacaea7af9c7919841c5e83dabd76dac0dc27 Mon Sep 17 00:00:00 2001 From: "Agarwal, Udit" Date: Fri, 20 Dec 2024 09:52:26 -0800 Subject: [PATCH 1/5] Refactor OSUtils to use std::filesystem --- sycl/include/sycl/detail/os_util.hpp | 13 +---- sycl/source/detail/os_util.cpp | 73 ++++++---------------------- 2 files changed, 18 insertions(+), 68 deletions(-) diff --git a/sycl/include/sycl/detail/os_util.hpp b/sycl/include/sycl/detail/os_util.hpp index a0c3a8483373e..759cd8d9b6c36 100644 --- a/sycl/include/sycl/detail/os_util.hpp +++ b/sycl/include/sycl/detail/os_util.hpp @@ -14,8 +14,7 @@ #include // for size_t #include -#include // for string -#include // for stat +#include // for string #ifdef _WIN32 #define __SYCL_RT_OS_WINDOWS @@ -80,15 +79,7 @@ class __SYCL_EXPORT OSUtil { static int makeDir(const char *Dir); /// Checks if specified path is present - static bool isPathPresent(const std::string &Path) { -#ifdef __SYCL_RT_OS_WINDOWS - struct _stat Stat; - return !_stat(Path.c_str(), &Stat); -#else - struct stat Stat; - return !stat(Path.c_str(), &Stat); -#endif - } + static bool isPathPresent(const std::string &Path); }; // These functions are not a part of OSUtils class to prevent diff --git a/sycl/source/detail/os_util.cpp b/sycl/source/detail/os_util.cpp index 8c4c0bff1293d..ff6cf62b6aced 100644 --- a/sycl/source/detail/os_util.cpp +++ b/sycl/source/detail/os_util.cpp @@ -36,7 +36,6 @@ namespace fs = std::experimental::filesystem; #include // for dirname #include #include // for PATH_MAX -#include #include #elif defined(__SYCL_RT_OS_WINDOWS) @@ -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; @@ -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(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) @@ -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. @@ -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(&getCurrentDSODir); @@ -210,7 +188,6 @@ std::string OSUtil::getCurrentDSODir() { return Path.substr(0, LastSlashPos); } - #endif // __SYCL_RT_OS size_t OSUtil::getOSMemSize() { @@ -254,35 +231,17 @@ 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; } +bool OSUtil::isPathPresent(const std::string &Path) { return fs::exists(Path); } + // Get size of file in bytes. size_t getFileSize(const std::string &Path) { return static_cast(fs::file_size(Path)); From b82709234353d4e014c6537f7861506a846e8001 Mon Sep 17 00:00:00 2001 From: "Agarwal, Udit" Date: Thu, 2 Jan 2025 11:57:18 -0800 Subject: [PATCH 2/5] Fix isPathPresent ABI issue --- sycl/include/sycl/detail/os_util.hpp | 12 ++++++++++-- sycl/source/detail/os_util.cpp | 2 -- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/sycl/include/sycl/detail/os_util.hpp b/sycl/include/sycl/detail/os_util.hpp index 759cd8d9b6c36..d20fbfd7618f2 100644 --- a/sycl/include/sycl/detail/os_util.hpp +++ b/sycl/include/sycl/detail/os_util.hpp @@ -78,8 +78,16 @@ 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 - static bool isPathPresent(const std::string &Path); + /// Checks if specified path is present. + static bool isPathPresent(const std::string &Path) { +#ifdef __SYCL_RT_OS_WINDOWS + struct _stat Stat; + return !_stat(Path.c_str(), &Stat); +#else + struct stat Stat; + return !stat(Path.c_str(), &Stat); +#endif + } }; // These functions are not a part of OSUtils class to prevent diff --git a/sycl/source/detail/os_util.cpp b/sycl/source/detail/os_util.cpp index ff6cf62b6aced..427da3c7ffcff 100644 --- a/sycl/source/detail/os_util.cpp +++ b/sycl/source/detail/os_util.cpp @@ -240,8 +240,6 @@ int OSUtil::makeDir(const char *Dir) { return 0; } -bool OSUtil::isPathPresent(const std::string &Path) { return fs::exists(Path); } - // Get size of file in bytes. size_t getFileSize(const std::string &Path) { return static_cast(fs::file_size(Path)); From b9ecde8071f52d65f7d71abd7c4005d3b9185bda Mon Sep 17 00:00:00 2001 From: Udit Kumar Agarwal Date: Thu, 2 Jan 2025 16:01:41 -0800 Subject: [PATCH 3/5] Add missing header file --- sycl/include/sycl/detail/os_util.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/sycl/include/sycl/detail/os_util.hpp b/sycl/include/sycl/detail/os_util.hpp index d20fbfd7618f2..72566b67d8e4f 100644 --- a/sycl/include/sycl/detail/os_util.hpp +++ b/sycl/include/sycl/detail/os_util.hpp @@ -14,6 +14,7 @@ #include // for size_t #include +#include // for stat #include // for string #ifdef _WIN32 From 7455683ee4b288eb89ab924d6bcf98649087757b Mon Sep 17 00:00:00 2001 From: Udit Kumar Agarwal Date: Thu, 2 Jan 2025 16:02:15 -0800 Subject: [PATCH 4/5] clang format --- sycl/include/sycl/detail/os_util.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/include/sycl/detail/os_util.hpp b/sycl/include/sycl/detail/os_util.hpp index 72566b67d8e4f..4ed63091e5537 100644 --- a/sycl/include/sycl/detail/os_util.hpp +++ b/sycl/include/sycl/detail/os_util.hpp @@ -14,8 +14,8 @@ #include // for size_t #include -#include // for stat #include // for string +#include // for stat #ifdef _WIN32 #define __SYCL_RT_OS_WINDOWS From cb64cf1c0a7be5020192b24d6ec08e5442c4f4ac Mon Sep 17 00:00:00 2001 From: Udit Kumar Agarwal Date: Thu, 2 Jan 2025 16:31:07 -0800 Subject: [PATCH 5/5] More changes. --- sycl/include/sycl/detail/os_util.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/include/sycl/detail/os_util.hpp b/sycl/include/sycl/detail/os_util.hpp index 4ed63091e5537..5063c06e6e5d6 100644 --- a/sycl/include/sycl/detail/os_util.hpp +++ b/sycl/include/sycl/detail/os_util.hpp @@ -14,7 +14,7 @@ #include // for size_t #include -#include // for string +#include // for string #include // for stat #ifdef _WIN32