2626#include " support/file.h"
2727#include " s2wasm.h"
2828#include " wasm-emscripten.h"
29+ #include " wasm-io.h"
2930#include " wasm-linker.h"
3031#include " wasm-printing.h"
3132#include " wasm-validator.h"
@@ -38,7 +39,12 @@ int main(int argc, const char *argv[]) {
3839 bool generateEmscriptenGlue = false ;
3940 bool allowMemoryGrowth = false ;
4041 bool importMemory = false ;
42+ bool emitBinary = false ;
43+ bool debugInfo = false ;
4144 std::string startFunction;
45+ std::string sourceMapFilename;
46+ std::string sourceMapUrl;
47+ std::string symbolMap;
4248 std::vector<std::string> archiveLibraries;
4349 TrapMode trapMode = TrapMode::Allow;
4450 unsigned numReservedFunctionPointers = 0 ;
@@ -61,7 +67,7 @@ int main(int argc, const char *argv[]) {
6167 [&startFunction](Options *, const std::string& argument) {
6268 startFunction = argument.size () ? argument : " main" ;
6369 })
64- .add (" --global-base" , " -g " , " Where to start to place globals" ,
70+ .add (" --global-base" , " " , " Where to start to place globals" ,
6571 Options::Arguments::One,
6672 [](Options *o, const std::string& argument) {
6773 o->extra [" global-base" ] = argument;
@@ -130,12 +136,47 @@ int main(int argc, const char *argv[]) {
130136 const std::string &argument) {
131137 numReservedFunctionPointers = std::stoi (argument);
132138 })
139+ .add (" --emit-binary" , " " ,
140+ " Emit binary instead of text for the output file" ,
141+ Options::Arguments::Zero,
142+ [&emitBinary](Options *, const std::string &) {
143+ emitBinary = true ;
144+ })
145+ .add (" --debuginfo" , " -g" ,
146+ " Emit names section in wasm binary (or full debuginfo in wast)" ,
147+ Options::Arguments::Zero,
148+ [&debugInfo](Options *, const std::string &) {
149+ debugInfo = true ;
150+ })
151+ .add (" --source-map" , " -sm" ,
152+ " Emit source map (if using binary output) to the specified file" ,
153+ Options::Arguments::One,
154+ [&sourceMapFilename](Options *, const std::string& argument) {
155+ sourceMapFilename = argument;
156+ })
157+ .add (" --source-map-url" , " -su" ,
158+ " Use specified string as source map URL" ,
159+ Options::Arguments::One,
160+ [&sourceMapUrl](Options *, const std::string& argument) {
161+ sourceMapUrl = argument;
162+ })
163+ .add (" --symbolmap" , " -s" ,
164+ " Emit a symbol map (indexes => names)" ,
165+ Options::Arguments::One,
166+ [&symbolMap](Options *, const std::string& argument) {
167+ symbolMap = argument;
168+ })
133169 .add_positional (" INFILE" , Options::Arguments::One,
134170 [](Options *o, const std::string& argument) {
135171 o->extra [" infile" ] = argument;
136172 });
137173 options.parse (argc, argv);
138174
175+ if (options.extra [" output" ].size () == 0 ) {
176+ // when no output file is specified, we emit text to stdout
177+ emitBinary = false ;
178+ }
179+
139180 if (allowMemoryGrowth && !generateEmscriptenGlue) {
140181 Fatal () << " Error: adding memory growth code without Emscripten glue. "
141182 " This doesn't do anything.\n " ;
@@ -187,13 +228,13 @@ int main(int argc, const char *argv[]) {
187228 linker.layout ();
188229
189230 std::string metadata;
231+ Module& wasm = linker.getOutput ().wasm ;
190232 if (generateEmscriptenGlue) {
191- Module& wasm = linker.getOutput ().wasm ;
192233 if (options.debug ) {
193234 std::cerr << " Emscripten gluing..." << std::endl;
194235 WasmPrinter::printModule (&wasm, std::cerr);
195236 }
196- metadata = " ;; METADATA: " + emscriptenGlue (
237+ metadata = emscriptenGlue (
197238 wasm,
198239 allowMemoryGrowth,
199240 linker.getStackPointerAddress (),
@@ -204,18 +245,36 @@ int main(int argc, const char *argv[]) {
204245
205246 if (options.extra [" validate" ] != " none" ) {
206247 if (options.debug ) std::cerr << " Validating..." << std::endl;
207- Module* output = &linker.getOutput ().wasm ;
208- if (!wasm::WasmValidator ().validate (*output,
248+ if (!wasm::WasmValidator ().validate (wasm,
209249 WasmValidator::Globally | (options.extra [" validate" ] == " web" ? WasmValidator::Web : 0 ))) {
210- WasmPrinter::printModule (output );
250+ WasmPrinter::printModule (&wasm );
211251 Fatal () << " Error: linked module is not valid.\n " ;
212252 }
213253 }
214254
215255 if (options.debug ) std::cerr << " Printing..." << std::endl;
216- Output output (options.extra [" output" ], Flags::Text, options.debug ? Flags::Debug : Flags::Release);
217- WasmPrinter::printModule (&linker.getOutput ().wasm , output.getStream ());
218- output << metadata;
256+ auto outputDebugFlag = options.debug ? Flags::Debug : Flags::Release;
257+ auto outputBinaryFlag = emitBinary ? Flags::Binary : Flags::Text;
258+ Output output (options.extra [" output" ], outputBinaryFlag, outputDebugFlag);
259+
260+ ModuleWriter writer;
261+ writer.setDebug (options.debug );
262+ writer.setDebugInfo (debugInfo);
263+ writer.setSymbolMap (symbolMap);
264+ writer.setBinary (emitBinary);
265+ if (emitBinary) {
266+ writer.setSourceMapFilename (sourceMapFilename);
267+ writer.setSourceMapUrl (sourceMapUrl);
268+ }
269+ writer.write (wasm, output);
270+
271+ if (generateEmscriptenGlue) {
272+ if (emitBinary) {
273+ std::cout << metadata;
274+ } else {
275+ output << " ;; METADATA: " << metadata;
276+ }
277+ }
219278
220279 if (options.debug ) std::cerr << " Done." << std::endl;
221280 return 0 ;
0 commit comments