def main(args): parser = argparse.ArgumentParser() parser.add_argument('-o', '--out-dir', metavar='PATH', help='output directory for files.') parser.add_argument('-P', '--prefix', metavar='PATH', help='prefix file.', default=os.path.join(SCRIPT_DIR, 'spec-wasm2c-prefix.c')) parser.add_argument('--bindir', metavar='PATH', default=find_exe.GetDefaultPath(), help='directory to search for all executables.') parser.add_argument('--wasmrt-dir', metavar='PATH', help='directory with wasm-rt files', default=WASM2C_DIR) parser.add_argument('--cc', metavar='PATH', help='the path to the C compiler', default='cc') parser.add_argument('--cflags', metavar='FLAGS', help='additional flags for C compiler.', action='append', default=[]) parser.add_argument('--compile', help='compile the C code (default)', dest='compile', action='store_true') parser.add_argument('--no-compile', help='don\'t compile the C code', dest='compile', action='store_false') parser.set_defaults(compile=True) parser.add_argument('--no-run', help='don\'t run the compiled executable', dest='run', action='store_false') parser.add_argument('-v', '--verbose', help='print more diagnotic messages.', action='store_true') parser.add_argument('--no-error-cmdline', help='don\'t display the subprocess\'s commandline when ' 'an error occurs', dest='error_cmdline', action='store_false') parser.add_argument('-p', '--print-cmd', help='print the commands that are run.', action='store_true') parser.add_argument('file', help='wast file.') options = parser.parse_args(args) with utils.TempDirectory(options.out_dir, 'run-spec-wasm2c-') as out_dir: # Parse JSON file and generate main .c file with calls to test functions. wast2json = utils.Executable( find_exe.GetWast2JsonExecutable(options.bindir), error_cmdline=options.error_cmdline) wast2json.AppendOptionalArgs({'-v': options.verbose}) json_file_path = utils.ChangeDir( utils.ChangeExt(options.file, '.json'), out_dir) wast2json.RunWithArgs(options.file, '-o', json_file_path) wasm2c = utils.Executable( find_exe.GetWasm2CExecutable(options.bindir), error_cmdline=options.error_cmdline) cc = utils.Executable(options.cc, *options.cflags) with open(json_file_path) as json_file: spec_json = json.load(json_file) prefix = '' if options.prefix: with open(options.prefix) as prefix_file: prefix = prefix_file.read() + '\n' output = io.StringIO() cwriter = CWriter(spec_json, prefix, output, out_dir) cwriter.Write() main_filename = utils.ChangeExt(json_file_path, '-main.c') with open(main_filename, 'w') as out_main_file: out_main_file.write(output.getvalue()) o_filenames = [] includes = '-I%s' % options.wasmrt_dir # Compile wasm-rt-impl. wasm_rt_impl_c = os.path.join(options.wasmrt_dir, 'wasm-rt-impl.c') o_filenames.append(Compile(cc, wasm_rt_impl_c, out_dir, includes)) for i, wasm_filename in enumerate(cwriter.GetModuleFilenames()): c_filename = utils.ChangeExt(wasm_filename, '.c') wasm2c.RunWithArgs(wasm_filename, '-o', c_filename, cwd=out_dir) if options.compile: defines = '-DWASM_RT_MODULE_PREFIX=%s' % cwriter.GetModulePrefix(i) o_filenames.append(Compile(cc, c_filename, out_dir, includes, defines)) if options.compile: main_c = os.path.basename(main_filename) o_filenames.append(Compile(cc, main_c, out_dir, includes, defines)) main_exe = os.path.basename(utils.ChangeExt(json_file_path, '')) Link(cc, o_filenames, main_exe, out_dir, '-lm') if options.compile and options.run: utils.Executable(os.path.join(out_dir, main_exe), forward_stdout=True).RunWithArgs() return 0
def main(args): default_compiler = 'cc' if IS_WINDOWS: default_compiler = 'cl.exe' parser = argparse.ArgumentParser() parser.add_argument('-o', '--out-dir', metavar='PATH', help='output directory for files.') parser.add_argument('-P', '--prefix', metavar='PATH', help='prefix file.', default=os.path.join(SCRIPT_DIR, 'spec-wasm2c-prefix.c')) parser.add_argument('--bindir', metavar='PATH', default=find_exe.GetDefaultPath(), help='directory to search for all executables.') parser.add_argument('--wasmrt-dir', metavar='PATH', help='directory with wasm-rt files', default=WASM2C_DIR) parser.add_argument('--cc', metavar='PATH', help='the path to the C compiler', default=default_compiler) parser.add_argument('--cflags', metavar='FLAGS', help='additional flags for C compiler.', action='append', default=[]) parser.add_argument('--compile', help='compile the C code (default)', dest='compile', action='store_true') parser.add_argument('--no-compile', help='don\'t compile the C code', dest='compile', action='store_false') parser.set_defaults(compile=True) parser.add_argument('--no-run', help='don\'t run the compiled executable', dest='run', action='store_false') parser.add_argument('-v', '--verbose', help='print more diagnotic messages.', action='store_true') parser.add_argument('--no-error-cmdline', help='don\'t display the subprocess\'s commandline when ' 'an error occurs', dest='error_cmdline', action='store_false') parser.add_argument('-p', '--print-cmd', help='print the commands that are run.', action='store_true') parser.add_argument('file', help='wast file.') parser.add_argument('--enable-multi-memory', action='store_true') parser.add_argument('--disable-bulk-memory', action='store_true') parser.add_argument('--disable-reference-types', action='store_true') options = parser.parse_args(args) with utils.TempDirectory(options.out_dir, 'run-spec-wasm2c-') as out_dir: # Parse JSON file and generate main .c file with calls to test functions. wast2json = utils.Executable( find_exe.GetWast2JsonExecutable(options.bindir), error_cmdline=options.error_cmdline) wast2json.verbose = options.print_cmd wast2json.AppendOptionalArgs({ '-v': options.verbose, '--enable-multi-memory': options.enable_multi_memory, '--disable-bulk-memory': options.disable_bulk_memory, '--disable-reference-types': options.disable_reference_types}) json_file_path = utils.ChangeDir( utils.ChangeExt(options.file, '.json'), out_dir) wast2json.RunWithArgs(options.file, '-o', json_file_path) wasm2c = utils.Executable( find_exe.GetWasm2CExecutable(options.bindir), error_cmdline=options.error_cmdline) wasm2c.verbose = options.print_cmd wasm2c.AppendOptionalArgs({ '--enable-multi-memory': options.enable_multi_memory}) options.cflags += shlex.split(os.environ.get('WASM2C_CFLAGS', '')) cc = utils.Executable(options.cc, *options.cflags, forward_stderr=True, forward_stdout=False) cc.verbose = options.print_cmd with open(json_file_path, encoding='utf-8') as json_file: spec_json = json.load(json_file) prefix = '' if options.prefix: with open(options.prefix) as prefix_file: prefix = prefix_file.read() + '\n' output = io.StringIO() cwriter = CWriter(spec_json, prefix, output, out_dir) cwriter.Write() main_filename = utils.ChangeExt(json_file_path, '-main.c') with open(main_filename, 'w') as out_main_file: out_main_file.write(output.getvalue()) o_filenames = [] includes = '-I%s' % options.wasmrt_dir for i, wasm_filename in enumerate(cwriter.GetModuleFilenames()): wasm_filename = os.path.join(out_dir, wasm_filename) c_filename = utils.ChangeExt(wasm_filename, '.c') args = ['-n', cwriter.GetModulePrefixUnmangled(i)] wasm2c.RunWithArgs(wasm_filename, '-o', c_filename, *args) if options.compile: o_filenames.append(Compile(cc, c_filename, out_dir, includes)) if options.compile: # Compile wasm-rt-impl. wasm_rt_impl_c = os.path.join(options.wasmrt_dir, 'wasm-rt-impl.c') o_filenames.append(Compile(cc, wasm_rt_impl_c, out_dir, includes)) # Compile and link -main test run entry point o_filenames.append(Compile(cc, main_filename, out_dir, includes)) if IS_WINDOWS: exe_ext = '.exe' libs = [] else: exe_ext = '' libs = ['-lm'] main_exe = utils.ChangeExt(json_file_path, exe_ext) Link(cc, o_filenames, main_exe, *libs) # Run the resulting binary if options.run: utils.Executable(main_exe, forward_stdout=True).RunWithArgs() return 0