Preserve Unicode Windows arguments when loading native models
This commit is contained in:
1 parent
da2a8e6cbb
commit
6e758c5cca
7 files changed
+184
-5
No files matched your search
@@ -67,6 +67,10 @@ set(MTMD_VIDEO OFF CACHE BOOL "Frames decoded by bounded media adapter" FORCE)
|
||||
set(LLAMA_OPENSSL OFF CACHE BOOL "No network runtime dependencies" FORCE)
|
||||
add_subdirectory("${LLAMA_CPP_DIR}" llama-build)
|
||||
add_executable(searchi-gemma-worker worker.cpp)
|
||||
if(MINGW)
|
||||
# Windows argument conversion uses wmain; keep the same wide CRT entry.
|
||||
target_link_options(searchi-gemma-worker PRIVATE -municode)
|
||||
endif()
|
||||
target_include_directories(searchi-gemma-worker PRIVATE "${LLAMA_CPP_DIR}/vendor")
|
||||
target_link_libraries(searchi-gemma-worker PRIVATE llama mtmd ${CMAKE_DL_LIBS})
|
||||
if(SEARCHI_CPU_DISPATCH)
|
||||
|
||||
@@ -15,16 +15,17 @@
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#ifdef SEARCHI_CPU_DISPATCH
|
||||
#include <filesystem>
|
||||
#ifdef _WIN32
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define NOMINMAX
|
||||
#include <windows.h>
|
||||
#elif defined(__linux__)
|
||||
#endif
|
||||
#ifdef SEARCHI_CPU_DISPATCH
|
||||
#include <filesystem>
|
||||
#if defined(__linux__)
|
||||
#include <dlfcn.h>
|
||||
#include <unistd.h>
|
||||
#else
|
||||
#elif !defined(_WIN32)
|
||||
#error Unsupported platform for packaged CPU dispatch
|
||||
#endif
|
||||
#endif
|
||||
@@ -384,7 +385,7 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
static int worker_main(int argc, const char * const * argv) {
|
||||
try {
|
||||
std::string path, device = "auto", role = "indexing"; int threads = 1;
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
@@ -447,3 +448,35 @@ int main(int argc, char ** argv) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
// The narrow CRT argv uses the system ANSI code page (commonly1252), whereas
|
||||
// ggml_fopen requires UTF-8. Preserve Windows UTF-16 arguments before parsing;
|
||||
// model/runtime directories may contain any valid Unicode user/folder name.
|
||||
int wmain(int argc, wchar_t ** argv) {
|
||||
try {
|
||||
std::vector<std::string> utf8;
|
||||
utf8.reserve(size_t(argc));
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
int bytes = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, argv[i], -1, nullptr, 0, nullptr, nullptr);
|
||||
require(bytes > 0, "Worker argument contains invalid Windows Unicode");
|
||||
std::string argument(size_t(bytes), '\0');
|
||||
require(WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, argv[i], -1, argument.data(), bytes, nullptr, nullptr) == bytes,
|
||||
"Cannot convert worker argument to UTF-8");
|
||||
argument.pop_back(); // Conversion includes the terminating NUL.
|
||||
utf8.push_back(std::move(argument));
|
||||
}
|
||||
std::vector<const char *> arguments;
|
||||
arguments.reserve(utf8.size());
|
||||
for (const auto & argument : utf8) arguments.push_back(argument.c_str());
|
||||
return worker_main(argc, arguments.data());
|
||||
} catch (const std::exception & error) {
|
||||
std::cerr << "Searchi Gemma worker: " << error.what() << '\n';
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
#else
|
||||
int main(int argc, char ** argv) {
|
||||
return worker_main(argc, argv);
|
||||
}
|
||||
#endif
|
||||
Reference in new issue
Block a user