diff --git a/example/README.md b/example/README.md index 7e4d20092..9608622fd 100644 --- a/example/README.md +++ b/example/README.md @@ -53,11 +53,11 @@ COMMANDS validate [--debug] [OPTION]… [ARG]… Validate a module - wasm2wat [--emit-file] [OPTION]… [ARG]… + wasm2wat [--emit-file] [--output=FILE] [OPTION]… ARG Generate a text format file (.wat) from a binary format file (.wasm) - wat2wasm [OPTION]… [ARG]… + wat2wasm [OPTION]… ARG Generate a binary format file (.wasm) from a text format file (.wat) diff --git a/example/wasm2wat/README.md b/example/wasm2wat/README.md index 655f7ebc0..352f60c9a 100644 --- a/example/wasm2wat/README.md +++ b/example/wasm2wat/README.md @@ -28,12 +28,15 @@ NAME file (.wasm) SYNOPSIS - owi wasm2wat [--emit-file] [OPTION]… [ARG]… + owi wasm2wat [--emit-file] [--output=FILE] [OPTION]… ARG OPTIONS --emit-file emit (.wat) files from corresponding (.wasm) files + -o FILE, --output=FILE + wasm + COMMON OPTIONS --help[=FMT] (default=auto) Show this help in format FMT. The value FMT must be one of auto, diff --git a/example/wat2wasm/README.md b/example/wat2wasm/README.md index caf044b72..3877b02fb 100644 --- a/example/wat2wasm/README.md +++ b/example/wat2wasm/README.md @@ -34,12 +34,15 @@ NAME format file (.wat) SYNOPSIS - owi wat2wasm [OPTION]… [ARG]… + owi wat2wasm [OPTION]… ARG OPTIONS -d, --debug debug mode + -o FILE, --output=FILE + wat + --optimize optimize mode diff --git a/src/ast/binary_encoder.ml b/src/ast/binary_encoder.ml index e15f83ec7..ac5136d16 100644 --- a/src/ast/binary_encoder.ml +++ b/src/ast/binary_encoder.ml @@ -759,18 +759,17 @@ let encode encode_section buf '\x0B' encode_datas data; Buffer.contents buf -let write_file filename content = +let write_file outfile filename content = let _dir, filename = Fpath.split_base filename in - let filename, _ext = Fpath.split_ext filename in - let filename = Fpath.add_ext ".wasm" filename in - let fullpath = Fpath.add_seg _dir (Fpath.to_string filename) in - let filename = Fpath.to_string fullpath in + let filename = Fpath.set_ext "wasm" filename in + let filename = match outfile with Some name -> Fpath.v name | None -> filename in + let filename = Fpath.to_string filename in let oc = Out_channel.open_bin filename in Out_channel.output_string oc content; Out_channel.close oc -let convert (filename : Fpath.t) ~unsafe ~optimize m = +let convert (outfile: string option) (filename : Fpath.t) ~unsafe ~optimize m = Log.debug0 "bin encoding ...@\n"; let+ m = Compile.Text.until_optimize ~unsafe ~optimize m in let content = encode m in - write_file filename content + write_file outfile filename content diff --git a/src/ast/binary_encoder.mli b/src/ast/binary_encoder.mli index d1481b2c5..2c020b45a 100644 --- a/src/ast/binary_encoder.mli +++ b/src/ast/binary_encoder.mli @@ -3,7 +3,8 @@ (* Written by the Owi programmers *) val convert : - Fpath.t + string option + -> Fpath.t -> unsafe:bool -> optimize:bool -> Text.modul diff --git a/src/bin/owi.ml b/src/bin/owi.ml index 56cd947d1..7d501f794 100644 --- a/src/bin/owi.ml +++ b/src/bin/owi.ml @@ -39,6 +39,16 @@ let files = let f = existing_non_dir_file in Cmdliner.Arg.(value & pos_all f [] (info [] ~doc)) +let sourcefile ty = + let doc = Fmt.str "%s source file" ty in + let f = existing_non_dir_file in + Cmdliner.Arg.(required & pos 0 (some f) None (info [] ~doc)) + +let outfile ty = + let doc = Fmt.str "%s" ty in + Cmdliner.Arg.( + value & opt (some string) None & info [ "o"; "output" ] ~docv:"FILE" ~doc ) + let emit_files = let doc = "emit (.wat) files from corresponding (.wasm) files" in Cmdliner.Arg.(value & flag & info [ "emit-file" ] ~doc) @@ -249,7 +259,8 @@ let wasm2wat_cmd = let man = [] @ shared_man in Cmd.info "wasm2wat" ~version ~doc ~sdocs ~man in - Cmd.v info Term.(const Cmd_wasm2wat.cmd $ files $ emit_files) + Cmd.v info + Term.(const Cmd_wasm2wat.cmd $ sourcefile "wasm" $ emit_files $ outfile "wasm") let wat2wasm_cmd = let open Cmdliner in @@ -261,7 +272,9 @@ let wat2wasm_cmd = Cmd.info "wat2wasm" ~version ~doc ~sdocs ~man in Cmd.v info - Term.(const Cmd_wat2wasm.cmd $ profiling $ debug $ unsafe $ optimize $ files) + Term.( + const Cmd_wat2wasm.cmd $ profiling $ debug $ unsafe $ optimize + $ outfile "wat" $ sourcefile "wat" ) let cli = let open Cmdliner in diff --git a/src/cmd/cmd_wasm2wat.ml b/src/cmd/cmd_wasm2wat.ml index 046eb422b..4d0316357 100644 --- a/src/cmd/cmd_wasm2wat.ml +++ b/src/cmd/cmd_wasm2wat.ml @@ -5,16 +5,24 @@ open Syntax (** Utility function to handle writing to a file or printing to stdout *) -let cmd_one emitfile file = +let cmd_one emitfile outfile file = let ext = Fpath.get_ext file in - let wat_file = Fpath.set_ext "wat" file in + let _dir, wat_file = Fpath.split_base file in + let wat_file = Fpath.set_ext "wat" wat_file in match ext with | ".wasm" -> let* m = Parse.Binary.Module.from_file file in let m = Binary_to_text.modul m in - if emitfile then Bos.OS.File.writef wat_file "%a@\n" Text.pp_modul m + let outname, output = + begin + match outfile with + | Some name -> Fpath.v name, true + | None -> wat_file, false + end + in + if emitfile || output then Bos.OS.File.writef outname "%a@\n" Text.pp_modul m else Ok (Fmt.pr "%a@\n" Text.pp_modul m) | ext -> Error (`Unsupported_file_extension ext) -let cmd files emitfile = list_iter (cmd_one emitfile) files +let cmd file emitfile outfile = cmd_one emitfile outfile file diff --git a/src/cmd/cmd_wasm2wat.mli b/src/cmd/cmd_wasm2wat.mli index 08e7444ae..2c7245d19 100644 --- a/src/cmd/cmd_wasm2wat.mli +++ b/src/cmd/cmd_wasm2wat.mli @@ -2,4 +2,4 @@ (* Copyright © 2021-2024 OCamlPro *) (* Written by the Owi programmers *) -val cmd : Fpath.t list -> bool -> unit Result.t +val cmd : Fpath.t -> bool -> string option -> unit Result.t diff --git a/src/cmd/cmd_wat2wasm.ml b/src/cmd/cmd_wat2wasm.ml index 4192eaeb4..e820eadb2 100644 --- a/src/cmd/cmd_wat2wasm.ml +++ b/src/cmd/cmd_wat2wasm.ml @@ -4,15 +4,15 @@ open Syntax -let cmd_one ~unsafe ~optimize file = +let cmd_one ~unsafe ~optimize outfile file = let ext = Fpath.get_ext file in match ext with | ".wat" -> let* modul = Parse.Text.Module.from_file file in - Binary_encoder.convert file ~unsafe ~optimize modul + Binary_encoder.convert outfile file ~unsafe ~optimize modul | ext -> Error (`Unsupported_file_extension ext) -let cmd profiling debug unsafe optimize files = +let cmd profiling debug unsafe optimize outfile file = if profiling then Log.profiling_on := true; if debug then Log.debug_on := true; - list_iter (cmd_one ~unsafe ~optimize) files + cmd_one ~unsafe ~optimize outfile file diff --git a/src/cmd/cmd_wat2wasm.mli b/src/cmd/cmd_wat2wasm.mli index 5cef2e0c3..585196f7c 100644 --- a/src/cmd/cmd_wat2wasm.mli +++ b/src/cmd/cmd_wat2wasm.mli @@ -2,4 +2,4 @@ (* Copyright © 2021-2024 OCamlPro *) (* Written by the Owi programmers *) -val cmd : bool -> bool -> bool -> bool -> Fpath.t list -> unit Result.t +val cmd : bool -> bool -> bool -> bool -> string option -> Fpath.t -> unit Result.t diff --git a/test/help/help.t b/test/help/help.t index 2f5505199..2ff577330 100644 --- a/test/help/help.t +++ b/test/help/help.t @@ -31,11 +31,11 @@ no subcommand should print help validate [--debug] [OPTION]… [ARG]… Validate a module - wasm2wat [--emit-file] [OPTION]… [ARG]… + wasm2wat [--emit-file] [--output=FILE] [OPTION]… ARG Generate a text format file (.wat) from a binary format file (.wasm) - wat2wasm [OPTION]… [ARG]… + wat2wasm [OPTION]… ARG Generate a binary format file (.wasm) from a text format file (.wat) diff --git a/test/wasm2wat/not_exists.t b/test/wasm2wat/not_exists.t index ca3bc617b..6cc6a083e 100644 --- a/test/wasm2wat/not_exists.t +++ b/test/wasm2wat/not_exists.t @@ -1,6 +1,6 @@ $ owi wasm2wat idontexist.wat owi: no file 'idontexist.wat' - Usage: owi wasm2wat [--emit-file] [OPTION]… [ARG]… + Usage: owi wasm2wat [--emit-file] [--output=FILE] [OPTION]… ARG Try 'owi wasm2wat --help' or 'owi --help' for more information. [124] $ owi wasm2wat bad.ext diff --git a/test/wat2wasm/not_exists.t b/test/wat2wasm/not_exists.t index b09f42d15..400c2f5ef 100644 --- a/test/wat2wasm/not_exists.t +++ b/test/wat2wasm/not_exists.t @@ -1,6 +1,6 @@ $ owi wat2wasm not_exists.wat owi: no file 'not_exists.wat' - Usage: owi wat2wasm [OPTION]… [ARG]… + Usage: owi wat2wasm [OPTION]… ARG Try 'owi wat2wasm --help' or 'owi --help' for more information. [124] $ owi wat2wasm bad.ext