Skip to content

Commit d255089

Browse files
authored
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).
1 parent 83f6145 commit d255089

3 files changed

Lines changed: 37 additions & 9 deletions

File tree

src/support/path.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,32 @@ namespace wasm {
2424

2525
namespace Path {
2626

27-
std::string getPathSeparator() {
27+
char getPathSeparator() {
2828
// TODO: use c++17's path separator
2929
// http://en.cppreference.com/w/cpp/experimental/fs/path
3030
#if defined(WIN32) || defined(_WIN32)
31-
return "\\";
31+
return '\\';
3232
#else
33-
return "/";
33+
return '/';
3434
#endif
3535
}
3636

37+
std::string getDirName(const std::string& path) {
38+
auto sep = path.rfind(getPathSeparator());
39+
if (sep == std::string::npos) {
40+
return "";
41+
}
42+
return path.substr(0, sep);
43+
}
44+
45+
std::string getBaseName(const std::string& path) {
46+
auto sep = path.rfind(getPathSeparator());
47+
if (sep == std::string::npos) {
48+
return path;
49+
}
50+
return path.substr(sep + 1);
51+
}
52+
3753
std::string getBinaryenRoot() {
3854
auto* envVar = getenv("BINARYEN_ROOT");
3955
if (envVar) {
@@ -52,10 +68,15 @@ std::string getBinaryenBinDir() {
5268
}
5369
}
5470

55-
void setBinaryenBinDir(std::string dir) { binDir = dir; }
71+
void setBinaryenBinDir(const std::string& dir) {
72+
binDir = dir;
73+
if (binDir.back() != getPathSeparator()) {
74+
binDir += getPathSeparator();
75+
}
76+
}
5677

5778
// Gets the path to a binaryen binary tool, like wasm-opt
58-
std::string getBinaryenBinaryTool(std::string name) {
79+
std::string getBinaryenBinaryTool(const std::string& name) {
5980
return getBinaryenBinDir() + name;
6081
}
6182

src/support/path.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ namespace wasm {
2828

2929
namespace Path {
3030

31-
std::string getPathSeparator();
31+
char getPathSeparator();
32+
std::string getDirName(const std::string& path);
33+
std::string getBaseName(const std::string& path);
3234

3335
// Get the binaryen root dor.
3436
std::string getBinaryenRoot();
@@ -37,10 +39,10 @@ std::string getBinaryenRoot();
3739
std::string getBinaryenBinDir();
3840

3941
// Set the binaryen bin dir (allows tools to change it based on user input).
40-
void setBinaryenBinDir(std::string dir);
42+
void setBinaryenBinDir(const std::string& dir);
4143

4244
// Gets the path to a binaryen binary tool, like wasm-opt.
43-
std::string getBinaryenBinaryTool(std::string name);
45+
std::string getBinaryenBinaryTool(const std::string& name);
4446

4547
} // namespace Path
4648

src/tools/wasm-reduce.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,8 @@ struct Reducer
10331033

10341034
int main(int argc, const char* argv[]) {
10351035
std::string input, test, working, command;
1036+
// By default, look for binaries alongside our own binary.
1037+
std::string binDir = Path::getDirName(argv[0]);
10361038
bool binary = true, deNan = false, verbose = false, debugInfo = false,
10371039
force = false;
10381040
Options options("wasm-reduce",
@@ -1066,7 +1068,7 @@ int main(int argc, const char* argv[]) {
10661068
Options::Arguments::One,
10671069
[&](Options* o, const std::string& argument) {
10681070
// Add separator just in case
1069-
Path::setBinaryenBinDir(argument + Path::getPathSeparator());
1071+
binDir = argument + Path::getPathSeparator();
10701072
})
10711073
.add("--text",
10721074
"-S",
@@ -1121,10 +1123,13 @@ int main(int argc, const char* argv[]) {
11211123
Colors::setEnabled(false);
11221124
}
11231125

1126+
Path::setBinaryenBinDir(binDir);
1127+
11241128
std::cerr << "|wasm-reduce\n";
11251129
std::cerr << "|input: " << input << '\n';
11261130
std::cerr << "|test: " << test << '\n';
11271131
std::cerr << "|working: " << working << '\n';
1132+
std::cerr << "|bin dir: " << binDir << '\n';
11281133

11291134
// get the expected output
11301135
copy_file(input, test);

0 commit comments

Comments
 (0)