|
14 | 14 |
|
15 | 15 | #pragma once
|
16 | 16 |
|
| 17 | +#include <cstdio> |
17 | 18 | #include <stdexcept>
|
| 19 | + |
| 20 | +#include <memory> |
18 | 21 | #include <string>
|
19 | 22 |
|
| 23 | +#define GLOG_NO_ABBREVIATED_SEVERITIES // msvc conflict logging with windows.h |
| 24 | +#include "glog/logging.h" |
| 25 | + |
20 | 26 | #if !defined(_WIN32)
|
21 |
| -#include <dlfcn.h> // for dladdr |
22 |
| -#include <execinfo.h> // for backtrace |
| 27 | +#define UNUSED __attribute__((unused)) |
| 28 | +#include <dlfcn.h> // dladdr |
| 29 | +#include <execinfo.h> // backtrace |
| 30 | +#include <sys/stat.h> |
| 31 | +#include <algorithm> // std::accumulate |
23 | 32 | #else
|
24 |
| -#include <Shlwapi.h> |
25 |
| -#include <Windows.h> |
| 33 | +#include <io.h> // _popen, _pclose |
| 34 | +#include <windows.h> |
| 35 | +#if defined(_WIN32) |
| 36 | +#include <numeric> // std::accumulate in msvc |
| 37 | +#endif |
| 38 | +// windows version of __attribute__((unused)) |
| 39 | +#define UNUSED __pragma(warning(suppress : 4100)) |
26 | 40 |
|
27 |
| -static void* dlsym(void* handle, const char* symbol_name) { |
| 41 | +#ifndef S_ISDIR // windows port for sys/stat.h |
| 42 | +#define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR) |
| 43 | +#endif // S_ISDIR |
| 44 | + |
| 45 | +static void *dlsym(void *handle, const char *symbol_name) { |
28 | 46 | FARPROC found_symbol;
|
29 | 47 | found_symbol = GetProcAddress((HMODULE)handle, symbol_name);
|
30 | 48 |
|
31 | 49 | if (found_symbol == NULL) {
|
32 | 50 | throw std::runtime_error(std::string(symbol_name) + " not found.");
|
33 | 51 | }
|
34 |
| - return reinterpret_cast<void*>(found_symbol); |
| 52 | + return reinterpret_cast<void *>(found_symbol); |
35 | 53 | }
|
36 | 54 |
|
37 |
| -#endif |
| 55 | +static void *dlopen(const char *filename, int flag) { |
| 56 | + std::string file_name(filename); |
| 57 | + file_name.replace(0, file_name.size() - 1, '/', '\\'); |
| 58 | + HMODULE hModule = LoadLibrary(file_name.c_str()); |
| 59 | + if (!hModule) { |
| 60 | + throw std::runtime_error(file_name + " not found."); |
| 61 | + } |
| 62 | + return reinterpret_cast<void *>(hModule); |
| 63 | +} |
| 64 | + |
| 65 | +#endif // !_WIN32 |
| 66 | + |
| 67 | +static void ExecShellCommand(const std::string &cmd, std::string *message) { |
| 68 | + char buffer[128]; |
| 69 | +#if !defined(_WIN32) |
| 70 | + std::shared_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose); |
| 71 | +#else |
| 72 | + std::shared_ptr<FILE> pipe(_popen(cmd.c_str(), "r"), _pclose); |
| 73 | +#endif // _WIN32 |
| 74 | + if (!pipe) { |
| 75 | + LOG(ERROR) << "error running command: " << cmd; |
| 76 | + return; |
| 77 | + } |
| 78 | + while (!feof(pipe.get())) { |
| 79 | + if (fgets(buffer, 128, pipe.get()) != nullptr) { |
| 80 | + *message += buffer; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +static bool PathExists(const std::string &path) { |
| 86 | +#if !defined(_WIN32) |
| 87 | + struct stat statbuf; |
| 88 | + if (stat(path.c_str(), &statbuf) != -1) { |
| 89 | + if (S_ISDIR(statbuf.st_mode)) { |
| 90 | + return true; |
| 91 | + } |
| 92 | + } |
| 93 | +#else |
| 94 | + struct _stat statbuf; |
| 95 | + if (_stat(path.c_str(), &statbuf) != -1) { |
| 96 | + if (S_ISDIR(statbuf.st_mode)) { |
| 97 | + return true; |
| 98 | + } |
| 99 | + } |
| 100 | +#endif // !_WIN32 |
| 101 | + return false; |
| 102 | +} |
| 103 | + |
| 104 | +// TODO(yuyang18): If the functions below are needed by other files, move them |
| 105 | +// to paddle::filesystem namespace. |
| 106 | +#if !defined(_WIN32) |
| 107 | +constexpr char kSEP = '/'; |
| 108 | +#else |
| 109 | +constexpr char kSEP = '\\'; |
| 110 | +#endif // _WIN32 |
| 111 | + |
| 112 | +static bool FileExists(const std::string &filepath) { |
| 113 | +#if !defined(_WIN32) |
| 114 | + struct stat buffer; |
| 115 | + return (stat(filepath.c_str(), &buffer) == 0); |
| 116 | +#else |
| 117 | + struct _stat buffer; |
| 118 | + return (_stat(filepath.c_str(), &buffer) == 0); |
| 119 | +#endif // !_WIN32 |
| 120 | +} |
| 121 | + |
| 122 | +static std::string DirName(const std::string &filepath) { |
| 123 | + auto pos = filepath.rfind(kSEP); |
| 124 | + if (pos == std::string::npos) { |
| 125 | + return ""; |
| 126 | + } |
| 127 | + return filepath.substr(0, pos); |
| 128 | +} |
| 129 | + |
| 130 | +static void MkDir(const char *path) { |
| 131 | + std::string path_error(path); |
| 132 | + path_error += " mkdir failed!"; |
| 133 | +#if !defined(_WIN32) |
| 134 | + if (mkdir(path, 0755)) { |
| 135 | + if (errno != EEXIST) { |
| 136 | + throw std::runtime_error(path_error); |
| 137 | + } |
| 138 | + } |
| 139 | +#else |
| 140 | + CreateDirectory(path, NULL); |
| 141 | + auto errorno = GetLastError(); |
| 142 | + if (errorno != ERROR_ALREADY_EXISTS) { |
| 143 | + throw std::runtime_error(path_error); |
| 144 | + } |
| 145 | +#endif // !_WIN32 |
| 146 | +} |
| 147 | + |
| 148 | +static void MkDirRecursively(const char *fullpath) { |
| 149 | + if (*fullpath == '\0') return; // empty string |
| 150 | + if (FileExists(fullpath)) return; |
| 151 | + |
| 152 | + MkDirRecursively(DirName(fullpath).c_str()); |
| 153 | + MkDir(fullpath); |
| 154 | +} |
0 commit comments