diff options
Diffstat (limited to 'src/support/command-line.cpp')
-rw-r--r-- | src/support/command-line.cpp | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/src/support/command-line.cpp b/src/support/command-line.cpp index 23ded0346..2b1b0b10b 100644 --- a/src/support/command-line.cpp +++ b/src/support/command-line.cpp @@ -17,6 +17,15 @@ #include "support/command-line.h" #include "config.h" #include "support/debug.h" +#include "support/path.h" + +#ifdef USE_WSTRING_PATHS +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include "windows.h" +#include "shellapi.h" +#endif using namespace wasm; @@ -163,6 +172,23 @@ Options& Options::add_positional(const std::string& name, } void Options::parse(int argc, const char* argv[]) { + +// On Windows, get the wide char version of the command line flags, and convert +// each one to std::string with UTF-8 manually. This means that all paths in +// Binaryen are stored this way on all platforms right up until a library call +// is made to open a file (at which point we use Path::to_path to convert back) +// so that it works with the underlying Win32 APIs. +// Only argList (and not argv) should be used below. +#ifdef USE_WSTRING_PATHS + LPWSTR* argListW = CommandLineToArgvW(GetCommandLineW(), &argc); + std::vector<std::string> argList; + for (size_t i = 0, e = argc; i < e; ++i) { + argList.push_back(wasm::Path::wstring_to_string(argListW[i])); + } +#else + const char** argList = argv; +#endif + assert(argc > 0 && "expect at least program name as an argument"); size_t positionalsSeen = 0; auto dashes = [](const std::string& s) { @@ -174,7 +200,7 @@ void Options::parse(int argc, const char* argv[]) { return s.size(); }; for (size_t i = 1, e = argc; i != e; ++i) { - std::string currentOption = argv[i]; + std::string currentOption = argList[i]; // "-" alone is a positional option if (dashes(currentOption) == 0 || currentOption == "-") { @@ -241,7 +267,7 @@ void Options::parse(int argc, const char* argv[]) { << currentOption << "'\n"; exit(EXIT_FAILURE); } - argument = argv[++i]; + argument = argList[++i]; } break; case Arguments::Optional: |