diff --git a/sycl/include/sycl/detail/os_util.hpp b/sycl/include/sycl/detail/os_util.hpp index a0c3a8483373e..5063c06e6e5d6 100644 --- a/sycl/include/sycl/detail/os_util.hpp +++ b/sycl/include/sycl/detail/os_util.hpp @@ -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; diff --git a/sycl/source/detail/os_util.cpp b/sycl/source/detail/os_util.cpp index 8c4c0bff1293d..427da3c7ffcff 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,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; }