0018-use-std-filesystem-path-instead-of-wstring.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: jmorganca <jmorganca@gmail.com>
  3. Date: Sun, 16 Feb 2025 20:00:22 -0500
  4. Subject: [PATCH] use std::filesystem::path instead of wstring
  5. ---
  6. ggml/src/ggml-backend-reg.cpp | 116 ++++++++++++----------------------
  7. 1 file changed, 40 insertions(+), 76 deletions(-)
  8. diff --git a/ggml/src/ggml-backend-reg.cpp b/ggml/src/ggml-backend-reg.cpp
  9. index 84b21dd8..de78feae 100644
  10. --- a/ggml/src/ggml-backend-reg.cpp
  11. +++ b/ggml/src/ggml-backend-reg.cpp
  12. @@ -72,16 +72,6 @@
  13. # pragma clang diagnostic ignored "-Wdeprecated-declarations"
  14. #endif
  15. -static std::wstring utf8_to_utf16(const std::string & str) {
  16. - std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
  17. - return converter.from_bytes(str);
  18. -}
  19. -
  20. -static std::string utf16_to_utf8(const std::wstring & str) {
  21. - std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
  22. - return converter.to_bytes(str);
  23. -}
  24. -
  25. #if defined(__clang__)
  26. # pragma clang diagnostic pop
  27. #endif
  28. @@ -96,12 +86,12 @@ struct dl_handle_deleter {
  29. }
  30. };
  31. -static dl_handle * dl_load_library(const std::wstring & path) {
  32. +static dl_handle * dl_load_library(const std::filesystem::path & path) {
  33. // suppress error dialogs for missing DLLs
  34. DWORD old_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
  35. SetErrorMode(old_mode | SEM_FAILCRITICALERRORS);
  36. - HMODULE handle = LoadLibraryW(path.c_str());
  37. + HMODULE handle = LoadLibraryW(path.wstring().c_str());
  38. SetErrorMode(old_mode);
  39. @@ -129,8 +119,8 @@ struct dl_handle_deleter {
  40. }
  41. };
  42. -static void * dl_load_library(const std::wstring & path) {
  43. - dl_handle * handle = dlopen(utf16_to_utf8(path).c_str(), RTLD_NOW | RTLD_LOCAL);
  44. +static void * dl_load_library(const std::filesystem::path & path) {
  45. + dl_handle * handle = dlopen(path.string().c_str(), RTLD_NOW | RTLD_LOCAL);
  46. return handle;
  47. }
  48. @@ -222,11 +212,11 @@ struct ggml_backend_registry {
  49. );
  50. }
  51. - ggml_backend_reg_t load_backend(const std::wstring & path, bool silent) {
  52. + ggml_backend_reg_t load_backend(const std::filesystem::path & path, bool silent) {
  53. dl_handle_ptr handle { dl_load_library(path) };
  54. if (!handle) {
  55. if (!silent) {
  56. - GGML_LOG_ERROR("%s: failed to load %s\n", __func__, utf16_to_utf8(path).c_str());
  57. + GGML_LOG_ERROR("%s: failed to load %s\n", __func__, path.string().c_str());
  58. }
  59. return nullptr;
  60. }
  61. @@ -234,7 +224,7 @@ struct ggml_backend_registry {
  62. auto score_fn = (ggml_backend_score_t) dl_get_sym(handle.get(), "ggml_backend_score");
  63. if (score_fn && score_fn() == 0) {
  64. if (!silent) {
  65. - GGML_LOG_INFO("%s: backend %s is not supported on this system\n", __func__, utf16_to_utf8(path).c_str());
  66. + GGML_LOG_INFO("%s: backend %s is not supported on this system\n", __func__, path.string().c_str());
  67. }
  68. return nullptr;
  69. }
  70. @@ -242,7 +232,7 @@ struct ggml_backend_registry {
  71. auto backend_init_fn = (ggml_backend_init_t) dl_get_sym(handle.get(), "ggml_backend_init");
  72. if (!backend_init_fn) {
  73. if (!silent) {
  74. - GGML_LOG_ERROR("%s: failed to find ggml_backend_init in %s\n", __func__, utf16_to_utf8(path).c_str());
  75. + GGML_LOG_ERROR("%s: failed to find ggml_backend_init in %s\n", __func__, path.string().c_str());
  76. }
  77. return nullptr;
  78. }
  79. @@ -251,16 +241,16 @@ struct ggml_backend_registry {
  80. if (!reg || reg->api_version != GGML_BACKEND_API_VERSION) {
  81. if (!silent) {
  82. if (!reg) {
  83. - GGML_LOG_ERROR("%s: failed to initialize backend from %s: ggml_backend_init returned NULL\n", __func__, utf16_to_utf8(path).c_str());
  84. + GGML_LOG_ERROR("%s: failed to initialize backend from %s: ggml_backend_init returned NULL\n", __func__, path.string().c_str());
  85. } else {
  86. GGML_LOG_ERROR("%s: failed to initialize backend from %s: incompatible API version (backend: %d, current: %d)\n",
  87. - __func__, utf16_to_utf8(path).c_str(), reg->api_version, GGML_BACKEND_API_VERSION);
  88. + __func__, path.string().c_str(), reg->api_version, GGML_BACKEND_API_VERSION);
  89. }
  90. }
  91. return nullptr;
  92. }
  93. - GGML_LOG_INFO("%s: loaded %s backend from %s\n", __func__, ggml_backend_reg_name(reg), utf16_to_utf8(path).c_str());
  94. + GGML_LOG_INFO("%s: loaded %s backend from %s\n", __func__, ggml_backend_reg_name(reg), path.string().c_str());
  95. register_backend(reg, score_fn ? score_fn() : -1, std::move(handle));
  96. @@ -396,14 +386,14 @@ ggml_backend_t ggml_backend_init_best(void) {
  97. // Dynamic loading
  98. ggml_backend_reg_t ggml_backend_load(const char * path) {
  99. - return get_reg().load_backend(utf8_to_utf16(path), false);
  100. + return get_reg().load_backend(path, false);
  101. }
  102. void ggml_backend_unload(ggml_backend_reg_t reg) {
  103. get_reg().unload_backend(reg, true);
  104. }
  105. -static std::wstring get_executable_path() {
  106. +static std::filesystem::path get_executable_path() {
  107. #if defined(__APPLE__)
  108. // get executable path
  109. std::vector<char> path;
  110. @@ -415,15 +405,9 @@ static std::wstring get_executable_path() {
  111. }
  112. path.resize(size);
  113. }
  114. - std::string base_path(path.data(), size);
  115. - // remove executable name
  116. - auto last_slash = base_path.find_last_of('/');
  117. - if (last_slash != std::string::npos) {
  118. - base_path = base_path.substr(0, last_slash);
  119. - }
  120. - return utf8_to_utf16(base_path + "/");
  121. +
  122. + return std::filesystem::path(path.data()).parent_path();
  123. #elif defined(__linux__) || defined(__FreeBSD__)
  124. - std::string base_path = ".";
  125. std::vector<char> path(1024);
  126. while (true) {
  127. // get executable path
  128. @@ -436,76 +420,56 @@ static std::wstring get_executable_path() {
  129. break;
  130. }
  131. if (len < (ssize_t) path.size()) {
  132. - base_path = std::string(path.data(), len);
  133. - // remove executable name
  134. - auto last_slash = base_path.find_last_of('/');
  135. - if (last_slash != std::string::npos) {
  136. - base_path = base_path.substr(0, last_slash);
  137. - }
  138. - break;
  139. + return std::filesystem::path(path.data()).parent_path();
  140. }
  141. path.resize(path.size() * 2);
  142. }
  143. -
  144. - return utf8_to_utf16(base_path + "/");
  145. #elif defined(_WIN32)
  146. std::vector<wchar_t> path(MAX_PATH);
  147. DWORD len = GetModuleFileNameW(NULL, path.data(), path.size());
  148. if (len == 0) {
  149. return {};
  150. }
  151. - std::wstring base_path(path.data(), len);
  152. - // remove executable name
  153. - auto last_slash = base_path.find_last_of('\\');
  154. - if (last_slash != std::string::npos) {
  155. - base_path = base_path.substr(0, last_slash);
  156. - }
  157. - return base_path + L"\\";
  158. -#else
  159. - return {};
  160. -#endif
  161. -}
  162. -static std::wstring backend_filename_prefix() {
  163. -#ifdef _WIN32
  164. - return L"ggml-";
  165. + return std::filesystem::path(path.data()).parent_path();
  166. #else
  167. - return L"libggml-";
  168. + return {};
  169. #endif
  170. }
  171. -static std::wstring backend_filename_suffix() {
  172. +static std::string backend_filename_prefix() {
  173. #ifdef _WIN32
  174. - return L".dll";
  175. + return "ggml-";
  176. #else
  177. - return L".so";
  178. + return "libggml-";
  179. #endif
  180. }
  181. -static std::wstring path_separator() {
  182. +static std::string backend_filename_suffix() {
  183. #ifdef _WIN32
  184. - return L"\\";
  185. + return ".dll";
  186. #else
  187. - return L"/";
  188. + return ".so";
  189. #endif
  190. }
  191. static ggml_backend_reg_t ggml_backend_load_best(const char * name, bool silent, const char * user_search_path) {
  192. // enumerate all the files that match [lib]ggml-name-*.[so|dll] in the search paths
  193. // TODO: search system paths
  194. - std::wstring file_prefix = backend_filename_prefix() + utf8_to_utf16(name) + L"-";
  195. - std::vector<std::wstring> search_paths;
  196. + namespace fs = std::filesystem;
  197. + std::string file_prefix = backend_filename_prefix() + name + "-";
  198. + std::vector<fs::path> search_paths;
  199. +
  200. if (user_search_path == nullptr) {
  201. - search_paths.push_back(L"." + path_separator());
  202. + search_paths.push_back(fs::current_path());
  203. search_paths.push_back(get_executable_path());
  204. } else {
  205. - search_paths.push_back(utf8_to_utf16(user_search_path) + path_separator());
  206. + search_paths.push_back(fs::u8path(user_search_path));
  207. }
  208. int best_score = 0;
  209. - std::wstring best_path;
  210. + fs::path best_path;
  211. - namespace fs = std::filesystem;
  212. for (const auto & search_path : search_paths) {
  213. if (!fs::exists(search_path)) {
  214. continue;
  215. @@ -514,31 +478,31 @@ static ggml_backend_reg_t ggml_backend_load_best(const char * name, bool silent,
  216. for (const auto & entry : dir_it) {
  217. try {
  218. if (entry.is_regular_file()) {
  219. - std::wstring filename = entry.path().filename().wstring();
  220. - std::wstring ext = entry.path().extension().wstring();
  221. + std::string filename = entry.path().filename().string();
  222. + std::string ext = entry.path().extension().string();
  223. if (filename.find(file_prefix) == 0 && ext == backend_filename_suffix()) {
  224. - dl_handle_ptr handle { dl_load_library(entry.path().wstring()) };
  225. + dl_handle_ptr handle { dl_load_library(entry.path()) };
  226. if (!handle) {
  227. - GGML_LOG_ERROR("%s: failed to load %s\n", __func__, utf16_to_utf8(entry.path().wstring()).c_str());
  228. + GGML_LOG_ERROR("%s: failed to load %s\n", __func__, entry.path().string().c_str());
  229. continue;
  230. }
  231. auto score_fn = (ggml_backend_score_t) dl_get_sym(handle.get(), "ggml_backend_score");
  232. if (!score_fn) {
  233. - GGML_LOG_DEBUG("%s: failed to find ggml_backend_score in %s\n", __func__, utf16_to_utf8(entry.path().wstring()).c_str());
  234. + GGML_LOG_DEBUG("%s: failed to find ggml_backend_score in %s\n", __func__, entry.path().string().c_str());
  235. continue;
  236. }
  237. int s = score_fn();
  238. - GGML_LOG_DEBUG("%s: %s score: %d\n", __func__, utf16_to_utf8(entry.path().wstring()).c_str(), s);
  239. + GGML_LOG_DEBUG("%s: %s score: %d\n", __func__, entry.path().string().c_str(), s);
  240. if (s > best_score) {
  241. best_score = s;
  242. - best_path = entry.path().wstring();
  243. + best_path = entry.path();
  244. }
  245. }
  246. }
  247. } catch (const std::exception & e) {
  248. - GGML_LOG_ERROR("%s: failed to load %s: %s\n", __func__, utf16_to_utf8(entry.path().wstring()).c_str(), e.what());
  249. + GGML_LOG_ERROR("%s: failed to load %s: %s\n", __func__, entry.path().string().c_str(), e.what());
  250. }
  251. }
  252. }
  253. @@ -546,7 +510,7 @@ static ggml_backend_reg_t ggml_backend_load_best(const char * name, bool silent,
  254. if (best_score == 0) {
  255. // try to load the base backend
  256. for (const auto & search_path : search_paths) {
  257. - std::wstring path = search_path + backend_filename_prefix() + utf8_to_utf16(name) + backend_filename_suffix();
  258. + fs::path path = fs::path(search_path) / (backend_filename_prefix() + name + backend_filename_suffix());
  259. if (fs::exists(path)) {
  260. return get_reg().load_backend(path, silent);
  261. }