Skip to content

Commit 6cf7343

Browse files
authored
Fix wasm-reduce testing out of tree (#1284)
* fix wasm-reduce when out-of-tree: do not use a hardcoded bin/wasm-opt, instead add a Path namespace with utilities to get the proper path, and use BINARYEN_ROOT which our test setup code ensures
1 parent eedcc29 commit 6cf7343

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

scripts/test/shared.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ def warn(text):
102102
else:
103103
options.binaryen_bin = 'bin'
104104

105+
# ensure BINARYEN_ROOT is set up
106+
os.environ['BINARYEN_ROOT'] = os.path.dirname(os.path.abspath(
107+
options.binaryen_bin))
108+
105109
options.binaryen_bin = os.path.normpath(options.binaryen_bin)
106110

107111
wasm_dis_filenames = ['wasm-dis', 'wasm-dis.exe']

src/support/path.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2015 WebAssembly Community Group participants
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
//
18+
// Command line helpers.
19+
//
20+
21+
#ifndef wasm_support_path_h
22+
#define wasm_support_path_h
23+
24+
#include <cstdlib>
25+
#include <string>
26+
27+
namespace wasm {
28+
29+
namespace Path {
30+
31+
inline std::string getPathSeparator() {
32+
// TODO: use c++17's path separator
33+
// http://en.cppreference.com/w/cpp/experimental/fs/path
34+
#if defined(WIN32) || defined(_WIN32)
35+
return "\\";
36+
#else
37+
return "/";
38+
#endif
39+
}
40+
41+
inline std::string getBinaryenRoot() {
42+
auto* envVar = getenv("BINARYEN_ROOT");
43+
if (envVar) return envVar;
44+
return ".";
45+
}
46+
47+
// Gets the path to a binaryen binary tool, like wasm-opt
48+
inline std::string getBinaryenBinaryTool(std::string name) {
49+
return getBinaryenRoot() + getPathSeparator() +
50+
"bin" + getPathSeparator() +
51+
name;
52+
}
53+
54+
} // namespace Path
55+
56+
} // namespace wasm
57+
58+
#endif // wasm_support_path_h

src/tools/wasm-reduce.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "pass.h"
3131
#include "support/command-line.h"
3232
#include "support/file.h"
33+
#include "support/path.h"
3334
#include "wasm-io.h"
3435
#include "wasm-builder.h"
3536
#include "ir/literal-utils.h"
@@ -137,7 +138,7 @@ struct Reducer : public WalkerPass<PostWalker<Reducer, UnifiedExpressionVisitor<
137138
// try both combining with a generic shrink (so minor pass overhead is compensated for), and without
138139
for (auto shrinking : { false, true }) {
139140
for (auto pass : passes) {
140-
std::string currCommand = "bin/wasm-opt ";
141+
std::string currCommand = Path::getBinaryenBinaryTool("wasm-opt") + " ";
141142
if (shrinking) currCommand += " --dce --vacuum ";
142143
currCommand += working + " -o " + test + " " + pass;
143144
if (debugInfo) currCommand += " -g ";
@@ -576,7 +577,7 @@ int main(int argc, const char* argv[]) {
576577
std::cerr << "|checking that command has expected behavior on canonicalized (read-written) binary\n";
577578
{
578579
// read and write it
579-
ProgramResult readWrite(std::string("bin/wasm-opt ") + input + " -o " + test);
580+
ProgramResult readWrite(Path::getBinaryenBinaryTool("wasm-opt") + " " + input + " -o " + test);
580581
if (readWrite.failed()) {
581582
stopIfNotForced("failed to read and write the binary", readWrite);
582583
} else {

0 commit comments

Comments
 (0)