swr/rast: add CreateDirectoryPath to recursively create directories
authorTim Rowley <timothy.o.rowley@intel.com>
Wed, 26 Apr 2017 18:46:31 +0000 (13:46 -0500)
committerTim Rowley <timothy.o.rowley@intel.com>
Tue, 30 May 2017 22:20:33 +0000 (17:20 -0500)
Reviewed-by: Bruce Cherniak <bruce.cherniak@intel.com>
src/gallium/drivers/swr/rasterizer/common/os.cpp
src/gallium/drivers/swr/rasterizer/common/os.h
src/gallium/drivers/swr/rasterizer/jitter/JitManager.cpp

index 295556a5678ff182c73f489c01f7ed3548a76a20..27ad5e90b66b66ff18cfc1bc781d33345133f98f 100644 (file)
 ****************************************************************************/
 
 #include "common/os.h"
+#include <vector>
+#include <sstream>
 
-#if defined(FORCE_LINUX) || defined(__linux__) || defined(__gnu_linux__)
+#if defined(_WIN32)
+#include <shlobj.h>
+#endif // Windows
+
+#if defined(__APPLE__) || defined(FORCE_LINUX) || defined(__linux__) || defined(__gnu_linux__)
 #include <pthread.h>
 #endif // Linux
 
@@ -105,3 +111,43 @@ void SWR_API SetCurrentThreadName(const char* pThreadName)
     pthread_setname_np(pthread_self(), pThreadName);
 #endif // Linux
 }
+
+static void SplitString(std::vector<std::string>& out_segments, const std::string& input, char splitToken)
+{
+    out_segments.clear();
+
+    std::istringstream f(input);
+    std::string s;
+    while (std::getline(f, s, splitToken))
+    {
+        if (s.size())
+        {
+            out_segments.push_back(s);
+        }
+    }
+}
+
+void SWR_API CreateDirectoryPath(const std::string& path)
+{
+#if defined(_WIN32)
+    SHCreateDirectoryExA(nullptr, path.c_str(), nullptr);
+#endif // Windows
+
+#if defined(__APPLE__) || defined(FORCE_LINUX) || defined(__linux__) || defined(__gnu_linux__)
+    std::vector<std::string> pathSegments;
+    SplitString(pathSegments, path, '/');
+
+    std::string tmpPath;
+    for (auto const& segment : pathSegments)
+    {
+        tmpPath.push_back('/');
+        tmpPath += segment;
+
+        int result = mkdir(tmpPath.c_str(), 0777);
+        if (result == -1 && errno != EEXIST)
+        {
+            break;
+        }
+    }
+#endif // Unix
+}
index f9b6ccaee82adcd0c5ecbc3e6deff3ccba6672f6..6e4d98f13fc00975dc8ab0ee3314917f158d294c 100644 (file)
@@ -234,8 +234,6 @@ void AlignedFree(void* p)
 pid_t gettid(void);
 #define GetCurrentThreadId gettid
 
-#define CreateDirectory(name, pSecurity) mkdir(name, 0777)
-
 #define InterlockedCompareExchange(Dest, Exchange, Comparand) __sync_val_compare_and_swap(Dest, Comparand, Exchange)
 #define InterlockedExchangeAdd(Addend, Value) __sync_fetch_and_add(Addend, Value)
 #define InterlockedDecrement(Append) __sync_sub_and_fetch(Append, 1)
@@ -281,5 +279,6 @@ typedef MEGABYTE    GIGABYTE[1024];
 
 // Defined in os.cpp
 void SWR_API SetCurrentThreadName(const char* pThreadName);
+void SWR_API CreateDirectoryPath(const std::string& path);
 
 #endif//__SWR_OS_H__
index 5d8ad273d36b2915e9a0b68e03e0598e26960bd5..2009db09febd2beab1b3dc4d81d5941c964bf5f0 100644 (file)
@@ -159,9 +159,9 @@ JitManager::JitManager(uint32_t simdWidth, const char *arch, const char* core)
 #if defined(_WIN32)
     if (KNOB_DUMP_SHADER_IR)
     {
-        CreateDirectory(INTEL_OUTPUT_DIR, NULL);
-        CreateDirectory(SWR_OUTPUT_DIR, NULL);
-        CreateDirectory(JITTER_OUTPUT_DIR, NULL);
+        CreateDirectoryPath(INTEL_OUTPUT_DIR);
+        CreateDirectoryPath(SWR_OUTPUT_DIR);
+        CreateDirectoryPath(JITTER_OUTPUT_DIR);
     }
 #endif
 }
@@ -204,7 +204,7 @@ void JitManager::DumpAsm(Function* pFunction, const char* fileName)
         const char* pBaseName = strrchr(procname, '\\');
         std::stringstream outDir;
         outDir << JITTER_OUTPUT_DIR << pBaseName << "_" << pid << std::ends;
-        CreateDirectory(outDir.str().c_str(), NULL);
+        CreateDirectoryPath(outDir.str().c_str());
 #endif
 
         std::error_code EC;
@@ -242,7 +242,7 @@ void JitManager::DumpToFile(Function *f, const char *fileName)
         const char* pBaseName = strrchr(procname, '\\');
         std::stringstream outDir;
         outDir << JITTER_OUTPUT_DIR << pBaseName << "_" << pid << std::ends;
-        CreateDirectory(outDir.str().c_str(), NULL);
+        CreateDirectoryPath(outDir.str().c_str());
 #endif
 
         std::error_code EC;