This commit is contained in:
Catherine 2026-08-05 21:50:58 +00:00 committed by GitHub
commit 80a075c4fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 33 additions and 20 deletions

View File

@ -130,26 +130,16 @@ std::string proc_self_dirname()
#elif defined(_WIN32) #elif defined(_WIN32)
std::string proc_self_dirname() std::string proc_self_dirname()
{ {
int i = 0; std::wstring wbinpath(4096, L'\0');
#ifdef _WIN32 if (!GetModuleFileNameW(0, &wbinpath[0], wbinpath.size()))
char longpath[MAX_PATH + 1]; fprintf(stderr, "GetModuleFileNameW() failed.\n");
char shortpath[MAX_PATH + 1]; wbinpath.resize(wbinpath.rfind(L'\\') + 1); // remove filename
#else std::string ubinpath;
WCHAR longpath[MAX_PATH + 1]; ubinpath.resize(WideCharToMultiByte(CP_UTF8, 0, wbinpath.data(), wbinpath.size(), NULL, 0, NULL, NULL));
TCHAR shortpath[MAX_PATH + 1]; if (WideCharToMultiByte(CP_UTF8, 0, wbinpath.data(), wbinpath.size(), &ubinpath[0], ubinpath.size(), NULL, NULL) == 0)
#endif fprintf(stderr, "WideCharToMultiByte() failed.\n");
if (!GetModuleFileNameA(0, longpath, MAX_PATH + 1)) return ubinpath;
log_error("GetModuleFileNameA() failed.\n");
if (!GetShortPathNameA(longpath, shortpath, MAX_PATH + 1))
log_error("GetShortPathNameA() failed.\n");
while (shortpath[i] != 0)
i++;
while (i > 0 && shortpath[i - 1] != '/' && shortpath[i - 1] != '\\')
shortpath[--i] = 0;
std::string path;
for (i = 0; shortpath[i]; i++)
path += char(shortpath[i]);
return path;
} }
#elif defined(EMSCRIPTEN) || defined(__wasm) #elif defined(EMSCRIPTEN) || defined(__wasm)
std::string proc_self_dirname() { return "/"; } std::string proc_self_dirname() { return "/"; }
@ -242,6 +232,20 @@ bool CommandHandler::parseOptions()
{ {
options.add(getGeneralOptions()).add(getArchOptions()); options.add(getGeneralOptions()).add(getArchOptions());
try { try {
#ifdef _WIN32
int argc = 0;
LPWSTR *wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
std::vector<char*> uargs;
for (int i = 0; i < argc; i++) {
int usize = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL);
char *uarg = (char *)malloc(usize);
WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, uarg, usize, NULL, NULL);
uargs.push_back(uarg);
}
uargs.push_back(NULL);
LocalFree(wargv);
char **argv = uargs.data();
#endif
po::parsed_options parsed = po::parsed_options parsed =
po::command_line_parser(argc, argv) po::command_line_parser(argc, argv)
.style(po::command_line_style::default_style ^ po::command_line_style::allow_guessing) .style(po::command_line_style::default_style ^ po::command_line_style::allow_guessing)
@ -718,6 +722,15 @@ void CommandHandler::printFooter()
int CommandHandler::exec() int CommandHandler::exec()
{ {
#ifdef _WIN32
// Configure ASCII functions like fopen() to use UTF-8 filenames.
// See https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale?view=msvc-170#utf-8-support
setlocale(LC_ALL, ".UTF-8");
// Configure the Win32 console to use UTF-8.
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
#endif
try { try {
if (!parseOptions()) if (!parseOptions())
return 125; return 125;