summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2020-08-17 17:09:45 -0700
committerGitHub <noreply@github.com>2020-08-17 17:09:45 -0700
commitf5a8bfd25c83dc05d48cbc525c5e8ec2deb246b3 (patch)
treeb60ba1ba7c528ed081ab2f9c0cb107e44683947e
parent8139c9d99d4639a2173e51828a6e7bbae397fc2c (diff)
downloadbinaryen-f5a8bfd25c83dc05d48cbc525c5e8ec2deb246b3.tar.gz
binaryen-f5a8bfd25c83dc05d48cbc525c5e8ec2deb246b3.tar.bz2
binaryen-f5a8bfd25c83dc05d48cbc525c5e8ec2deb246b3.zip
Make wasm-emscripten-finalize's output optional (#3055)
This helps towards the goal of allowing emscripten to not always modify the wasm during link. Until now wasm-emscripten-finalize always wrote an output, while with this PR it only does so if it was asked to, either by giving it an output filename, or asking for text output. The only noticeable change from this should be to make what was an error before (not specify an output or ask for text) into a non-error (run and print metadata, but do not write the wasm).
-rw-r--r--src/tools/wasm-emscripten-finalize.cpp40
-rw-r--r--test/unit/input/empty_lld.wat9
-rw-r--r--test/unit/test_finalize.py24
3 files changed, 53 insertions, 20 deletions
diff --git a/src/tools/wasm-emscripten-finalize.cpp b/src/tools/wasm-emscripten-finalize.cpp
index 7bb8ed310..ce1da4c9e 100644
--- a/src/tools/wasm-emscripten-finalize.cpp
+++ b/src/tools/wasm-emscripten-finalize.cpp
@@ -80,7 +80,8 @@ int main(int argc, const char* argv[]) {
[&DWARF](Options*, const std::string&) { DWARF = true; })
.add("--emit-text",
"-S",
- "Emit text instead of binary for the output file",
+ "Emit text instead of binary for the output file. "
+ "In this mode if no output file is specified, we write to stdout.",
Options::Arguments::Zero,
[&emitBinary](Options*, const std::string&) { emitBinary = false; })
.add("--global-base",
@@ -172,9 +173,6 @@ int main(int argc, const char* argv[]) {
if (infile == "") {
Fatal() << "Need to specify an infile\n";
}
- if (outfile == "" && emitBinary) {
- Fatal() << "Need to specify an outfile, or use text output\n";
- }
Module wasm;
ModuleReader reader;
@@ -335,23 +333,29 @@ int main(int argc, const char* argv[]) {
BYN_DEBUG_WITH_TYPE("emscripten-dump",
WasmPrinter::printModule(&wasm, std::cerr));
- Output output(outfile, emitBinary ? Flags::Binary : Flags::Text);
- ModuleWriter writer;
- writer.setDebugInfo(debugInfo);
- // writer.setSymbolMap(symbolMap);
- writer.setBinary(emitBinary);
- if (outputSourceMapFilename.size()) {
- writer.setSourceMapFilename(outputSourceMapFilename);
- writer.setSourceMapUrl(outputSourceMapUrl);
+ // Write the modified wasm if the user asked us to, either by specifying an
+ // output file, or requesting text output (which goes to stdout by default).
+ if (outfile.size() > 0 || !emitBinary) {
+ Output output(outfile, emitBinary ? Flags::Binary : Flags::Text);
+ ModuleWriter writer;
+ writer.setDebugInfo(debugInfo);
+ // writer.setSymbolMap(symbolMap);
+ writer.setBinary(emitBinary);
+ if (outputSourceMapFilename.size()) {
+ writer.setSourceMapFilename(outputSourceMapFilename);
+ writer.setSourceMapUrl(outputSourceMapUrl);
+ }
+ writer.write(wasm, output);
+ if (!emitBinary) {
+ output << "(;\n";
+ output << "--BEGIN METADATA --\n" << metadata << "-- END METADATA --\n";
+ output << ";)\n";
+ }
}
- writer.write(wasm, output);
+ // If we emit text then we emitted the metadata together with that text
+ // earlier. Otherwise emit it to stdout.
if (emitBinary) {
std::cout << metadata;
- } else {
- output << "(;\n";
- output << "--BEGIN METADATA --\n" << metadata << "-- END METADATA --\n";
- output << ";)\n";
}
-
return 0;
}
diff --git a/test/unit/input/empty_lld.wat b/test/unit/input/empty_lld.wat
new file mode 100644
index 000000000..978caab81
--- /dev/null
+++ b/test/unit/input/empty_lld.wat
@@ -0,0 +1,9 @@
+(module
+ (global $global$0 (mut i32) (i32.const 66192))
+ (global $global$1 i32 (i32.const 652))
+ (export "__data_end" (global $global$1))
+ (export "main" (func $main))
+ (func $main (param $0 i32) (param $1 i32) (result i32)
+ (i32.const 0)
+ )
+)
diff --git a/test/unit/test_finalize.py b/test/unit/test_finalize.py
index ff4eebd9c..b393177bd 100644
--- a/test/unit/test_finalize.py
+++ b/test/unit/test_finalize.py
@@ -6,10 +6,30 @@ from . import utils
class EmscriptenFinalizeTest(utils.BinaryenTestCase):
def test_em_asm_mangled_string(self):
- input_dir = os.path.dirname(__file__)
p = shared.run_process(shared.WASM_EMSCRIPTEN_FINALIZE + [
- os.path.join(input_dir, 'input', 'em_asm_mangled_string.wat'), '-o', os.devnull, '--global-base=1024'
+ self.input_path('em_asm_mangled_string.wat'), '-o', os.devnull, '--global-base=1024'
], check=False, capture_output=True)
self.assertNotEqual(p.returncode, 0)
self.assertIn('Fatal: local.get of unknown in arg0 of call to emscripten_asm_const_int (used by EM_ASM* macros) in function main.', p.stderr)
self.assertIn('This might be caused by aggressive compiler transformations. Consider using EM_JS instead.', p.stderr)
+
+ def do_output_test(self, args):
+ # without any output file specified, don't error, don't write the wasm,
+ # but do emit metadata
+ p = shared.run_process(shared.WASM_EMSCRIPTEN_FINALIZE + [
+ self.input_path('empty_lld.wat'), '--global-base=1024'
+ ] + args, capture_output=True)
+ # metadata is always present
+ self.assertIn('{', p.stdout)
+ self.assertIn('}', p.stdout)
+ return p.stdout
+
+ def test_no_output(self):
+ stdout = self.do_output_test([])
+ # module is not present
+ self.assertNotIn('(module', stdout)
+
+ def test_text_output(self):
+ stdout = self.do_output_test(['-S'])
+ # module is present
+ self.assertIn('(module', stdout)