|
24 | 24 | #include <libgen.h> // for dirname |
25 | 25 | #include <link.h> |
26 | 26 | #include <linux/limits.h> // for PATH_MAX |
| 27 | +#include <sys/stat.h> |
27 | 28 | #include <sys/sysinfo.h> |
28 | 29 |
|
29 | 30 | #elif defined(__SYCL_RT_OS_WINDOWS) |
30 | 31 |
|
31 | 32 | #include <Windows.h> |
| 33 | +#include <direct.h> |
32 | 34 | #include <malloc.h> |
33 | 35 | #include <shlwapi.h> |
34 | 36 |
|
@@ -211,6 +213,19 @@ std::string OSUtil::getCurrentDSODir() { |
211 | 213 | return Path; |
212 | 214 | } |
213 | 215 |
|
| 216 | +std::string OSUtil::getDirName(const char *Path) { |
| 217 | + std::string Tmp(Path); |
| 218 | + // Remove trailing directory separators |
| 219 | + Tmp.erase(Tmp.find_last_not_of("/\\") + 1, std::string::npos); |
| 220 | + |
| 221 | + int pos = Tmp.find_last_of("/\\"); |
| 222 | + if (pos != std::string::npos) |
| 223 | + return Tmp.substr(0, pos); |
| 224 | + |
| 225 | + // If no directory separator is present return initial path like dirname does |
| 226 | + return Tmp; |
| 227 | +} |
| 228 | + |
214 | 229 | #elif defined(__SYCL_RT_OS_DARWIN) |
215 | 230 | OSModuleHandle OSUtil::getOSModuleHandle(const void *VirtAddr) { |
216 | 231 | Dl_info Res; |
@@ -258,6 +273,32 @@ void OSUtil::alignedFree(void *Ptr) { |
258 | 273 | #endif |
259 | 274 | } |
260 | 275 |
|
| 276 | +/* This is temporary solution until std::filesystem is available when SYCL RT |
| 277 | + * is moved to c++17 standard*/ |
| 278 | + |
| 279 | +/* Create directory recursively and return non zero code on success*/ |
| 280 | +int OSUtil::makeDir(const char *Dir) { |
| 281 | + assert((Dir != nullptr) && "Passed null-pointer as directory name."); |
| 282 | + if (isPathPresent(Dir)) |
| 283 | + return 0; |
| 284 | + |
| 285 | + std::string Path{Dir}, CurPath; |
| 286 | + size_t pos = 0; |
| 287 | + |
| 288 | + do { |
| 289 | + pos = Path.find_first_of("/\\", ++pos); |
| 290 | + CurPath = Path.substr(0, pos); |
| 291 | +#if defined(__SYCL_RT_OS_LINUX) |
| 292 | + auto Res = mkdir(CurPath.c_str(), 0777); |
| 293 | +#else |
| 294 | + auto Res = _mkdir(CurPath.c_str()); |
| 295 | +#endif |
| 296 | + if (Res && errno != EEXIST) |
| 297 | + return Res; |
| 298 | + } while (pos != std::string::npos); |
| 299 | + return 0; |
| 300 | +} |
| 301 | + |
261 | 302 | } // namespace detail |
262 | 303 | } // namespace sycl |
263 | 304 | } // __SYCL_INLINE_NAMESPACE(cl) |
0 commit comments