diff options
author | Alon Zakai <azakai@google.com> | 2019-10-17 17:29:32 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-17 17:29:32 -0700 |
commit | d2550891e41ad0215b1cae46fa711bc1e264166a (patch) | |
tree | 7d102696ad01ff5eb76de33cf7a89eb3068d9350 | |
parent | 83f61457fd10feffe8cdcc6c278bc79cfb1667aa (diff) | |
download | binaryen-d2550891e41ad0215b1cae46fa711bc1e264166a.tar.gz binaryen-d2550891e41ad0215b1cae46fa711bc1e264166a.tar.bz2 binaryen-d2550891e41ad0215b1cae46fa711bc1e264166a.zip |
Fix autoreducing when not in the binaryen directory (#2390)
This uses argv[0] as the default way to find the location
of the wasm binaries (wasm-reduce needs to call wasm-opt).
-rw-r--r-- | src/support/path.cpp | 31 | ||||
-rw-r--r-- | src/support/path.h | 8 | ||||
-rw-r--r-- | src/tools/wasm-reduce.cpp | 7 |
3 files changed, 37 insertions, 9 deletions
diff --git a/src/support/path.cpp b/src/support/path.cpp index e9817aca9..2501c636c 100644 --- a/src/support/path.cpp +++ b/src/support/path.cpp @@ -24,16 +24,32 @@ namespace wasm { namespace Path { -std::string getPathSeparator() { +char getPathSeparator() { // TODO: use c++17's path separator // http://en.cppreference.com/w/cpp/experimental/fs/path #if defined(WIN32) || defined(_WIN32) - return "\\"; + return '\\'; #else - return "/"; + return '/'; #endif } +std::string getDirName(const std::string& path) { + auto sep = path.rfind(getPathSeparator()); + if (sep == std::string::npos) { + return ""; + } + return path.substr(0, sep); +} + +std::string getBaseName(const std::string& path) { + auto sep = path.rfind(getPathSeparator()); + if (sep == std::string::npos) { + return path; + } + return path.substr(sep + 1); +} + std::string getBinaryenRoot() { auto* envVar = getenv("BINARYEN_ROOT"); if (envVar) { @@ -52,10 +68,15 @@ std::string getBinaryenBinDir() { } } -void setBinaryenBinDir(std::string dir) { binDir = dir; } +void setBinaryenBinDir(const std::string& dir) { + binDir = dir; + if (binDir.back() != getPathSeparator()) { + binDir += getPathSeparator(); + } +} // Gets the path to a binaryen binary tool, like wasm-opt -std::string getBinaryenBinaryTool(std::string name) { +std::string getBinaryenBinaryTool(const std::string& name) { return getBinaryenBinDir() + name; } diff --git a/src/support/path.h b/src/support/path.h index 505ba0d88..e9f675142 100644 --- a/src/support/path.h +++ b/src/support/path.h @@ -28,7 +28,9 @@ namespace wasm { namespace Path { -std::string getPathSeparator(); +char getPathSeparator(); +std::string getDirName(const std::string& path); +std::string getBaseName(const std::string& path); // Get the binaryen root dor. std::string getBinaryenRoot(); @@ -37,10 +39,10 @@ std::string getBinaryenRoot(); std::string getBinaryenBinDir(); // Set the binaryen bin dir (allows tools to change it based on user input). -void setBinaryenBinDir(std::string dir); +void setBinaryenBinDir(const std::string& dir); // Gets the path to a binaryen binary tool, like wasm-opt. -std::string getBinaryenBinaryTool(std::string name); +std::string getBinaryenBinaryTool(const std::string& name); } // namespace Path diff --git a/src/tools/wasm-reduce.cpp b/src/tools/wasm-reduce.cpp index 8923b5eec..ba9f10264 100644 --- a/src/tools/wasm-reduce.cpp +++ b/src/tools/wasm-reduce.cpp @@ -1033,6 +1033,8 @@ struct Reducer int main(int argc, const char* argv[]) { std::string input, test, working, command; + // By default, look for binaries alongside our own binary. + std::string binDir = Path::getDirName(argv[0]); bool binary = true, deNan = false, verbose = false, debugInfo = false, force = false; Options options("wasm-reduce", @@ -1066,7 +1068,7 @@ int main(int argc, const char* argv[]) { Options::Arguments::One, [&](Options* o, const std::string& argument) { // Add separator just in case - Path::setBinaryenBinDir(argument + Path::getPathSeparator()); + binDir = argument + Path::getPathSeparator(); }) .add("--text", "-S", @@ -1121,10 +1123,13 @@ int main(int argc, const char* argv[]) { Colors::setEnabled(false); } + Path::setBinaryenBinDir(binDir); + std::cerr << "|wasm-reduce\n"; std::cerr << "|input: " << input << '\n'; std::cerr << "|test: " << test << '\n'; std::cerr << "|working: " << working << '\n'; + std::cerr << "|bin dir: " << binDir << '\n'; // get the expected output copy_file(input, test); |