Skip to content

Commit f6f5d7c

Browse files
[NFC][SYCL] Refactor OSUtils to use std::filesystem (#16447)
For older gcc versions (< 8), use `std::experimental::filesystem`.
1 parent 075fe28 commit f6f5d7c

File tree

2 files changed

+15
-58
lines changed

2 files changed

+15
-58
lines changed

sycl/include/sycl/detail/os_util.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class __SYCL_EXPORT OSUtil {
7979
/// Make all directories on the path, throws on error.
8080
static int makeDir(const char *Dir);
8181

82-
/// Checks if specified path is present
82+
/// Checks if specified path is present.
8383
static bool isPathPresent(const std::string &Path) {
8484
#ifdef __SYCL_RT_OS_WINDOWS
8585
struct _stat Stat;

sycl/source/detail/os_util.cpp

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ namespace fs = std::experimental::filesystem;
3636
#include <libgen.h> // for dirname
3737
#include <link.h>
3838
#include <linux/limits.h> // for PATH_MAX
39-
#include <sys/stat.h>
4039
#include <sys/sysinfo.h>
4140

4241
#elif defined(__SYCL_RT_OS_WINDOWS)
@@ -59,6 +58,15 @@ namespace sycl {
5958
inline namespace _V1 {
6059
namespace detail {
6160

61+
#if defined(__INTEL_PREVIEW_BREAKING_CHANGES)
62+
static std::string getDirName(const char *Path)
63+
#else
64+
std::string OSUtil::getDirName(const char *Path)
65+
#endif
66+
{
67+
return fs::path(Path).parent_path().string();
68+
}
69+
6270
#if defined(__SYCL_RT_OS_LINUX)
6371
bool procMapsAddressInRange(std::istream &Stream, uintptr_t Addr) {
6472
uintptr_t Start = 0, End = 0;
@@ -75,20 +83,6 @@ bool procMapsAddressInRange(std::istream &Stream, uintptr_t Addr) {
7583
return Addr >= Start && Addr < End;
7684
}
7785

78-
#if defined(__INTEL_PREVIEW_BREAKING_CHANGES)
79-
static std::string getDirName(const char *Path)
80-
#else
81-
std::string OSUtil::getDirName(const char *Path)
82-
#endif
83-
{
84-
std::string Tmp(Path);
85-
// dirname(3) needs a writable C string: a null-terminator is written where a
86-
// path should split.
87-
size_t TruncatedSize = strlen(dirname(const_cast<char *>(Tmp.c_str())));
88-
Tmp.resize(TruncatedSize);
89-
return Tmp;
90-
}
91-
9286
/// Returns an absolute path to a directory where the object was found.
9387
std::string OSUtil::getCurrentDSODir() {
9488
// Examine /proc/self/maps and find where this function (getCurrendDSODir)
@@ -157,7 +151,6 @@ std::string OSUtil::getCurrentDSODir() {
157151
}
158152

159153
#elif defined(__SYCL_RT_OS_WINDOWS)
160-
161154
/// Returns an absolute path where the object was found.
162155
// ur_win_proxy_loader.dll and sycl-jit.dll use this same logic. If it is
163156
// changed significantly, it might be wise to change it there too.
@@ -180,21 +173,6 @@ std::string OSUtil::getCurrentDSODir() {
180173
return Path;
181174
}
182175

183-
#if !defined(__INTEL_PREVIEW_BREAKING_CHANGES)
184-
std::string OSUtil::getDirName(const char *Path) {
185-
std::string Tmp(Path);
186-
// Remove trailing directory separators
187-
Tmp.erase(Tmp.find_last_not_of("/\\") + 1, std::string::npos);
188-
189-
size_t pos = Tmp.find_last_of("/\\");
190-
if (pos != std::string::npos)
191-
return Tmp.substr(0, pos);
192-
193-
// If no directory separator is present return initial path like dirname does
194-
return Tmp;
195-
}
196-
#endif
197-
198176
#elif defined(__SYCL_RT_OS_DARWIN)
199177
std::string OSUtil::getCurrentDSODir() {
200178
auto CurrentFunc = reinterpret_cast<const void *>(&getCurrentDSODir);
@@ -210,7 +188,6 @@ std::string OSUtil::getCurrentDSODir() {
210188

211189
return Path.substr(0, LastSlashPos);
212190
}
213-
214191
#endif // __SYCL_RT_OS
215192

216193
size_t OSUtil::getOSMemSize() {
@@ -254,32 +231,12 @@ void OSUtil::alignedFree(void *Ptr) {
254231
// Make all directories on the path, throws on error.
255232
int OSUtil::makeDir(const char *Dir) {
256233
assert((Dir != nullptr) && "Passed null-pointer as directory name.");
257-
if (isPathPresent(Dir))
258-
return 0;
259-
260-
// older GCC doesn't have full C++ 17 support.
261-
#if __GNUC__ && __GNUC__ < 8
262-
std::string Path{Dir}, CurPath;
263-
size_t pos = 0;
264-
265-
do {
266-
pos = Path.find_first_of("/\\", ++pos);
267-
CurPath = Path.substr(0, pos);
268-
#if defined(__SYCL_RT_OS_POSIX_SUPPORT)
269-
auto Res = mkdir(CurPath.c_str(), 0777);
270-
#else
271-
auto Res = _mkdir(CurPath.c_str());
272-
#endif
273-
if (Res && errno != EEXIST)
274-
throw std::runtime_error("Failed to mkdir: " + CurPath + " (" +
275-
std::strerror(errno) + ")");
276234

277-
} while (pos != std::string::npos);
278-
#else
279-
// using filesystem is simpler, more reliable, works better on Win
280-
std::filesystem::path path(Dir);
281-
std::filesystem::create_directories(path.make_preferred());
282-
#endif
235+
if (!isPathPresent(Dir)) {
236+
fs::path path(Dir);
237+
fs::create_directories(path.make_preferred());
238+
}
239+
283240
return 0;
284241
}
285242

0 commit comments

Comments
 (0)