From 530e7d720f1898f6512179311b44418263d115f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Vouillon?= Date: Tue, 4 Feb 2025 15:49:58 +0100 Subject: [PATCH] Preprocessor: move tools as subcommands inside wasm_of_ocaml --- compiler/bin-wasm_of_ocaml/dune | 6 +- compiler/bin-wasm_of_ocaml/info.ml | 4 +- compiler/bin-wasm_of_ocaml/link_wasm.ml | 110 +++++++++++ compiler/bin-wasm_of_ocaml/link_wasm.mli | 19 ++ compiler/bin-wasm_of_ocaml/preprocess.ml | 152 +++++++++++++++ .../preprocess.mli} | 30 +-- compiler/bin-wasm_of_ocaml/wasm_of_ocaml.ml | 8 +- .../bin-wasm_of_ocaml/wasmoo_link_wasm.ml | 43 +++++ .../bin-wasm_of_ocaml/wasmoo_link_wasm.mli | 17 ++ compiler/bin-wasmoo_util/cmd_arg.ml | 181 ------------------ compiler/bin-wasmoo_util/dune | 17 -- compiler/bin-wasmoo_util/wasmoo_util.ml | 121 ------------ .../preprocess}/cram.t | 64 +++---- .../preprocess}/dune | 4 +- .../preprocess}/tests.expected | 0 .../preprocess}/tests.txt | 0 runtime/wasm/dune | 3 +- 17 files changed, 393 insertions(+), 386 deletions(-) create mode 100644 compiler/bin-wasm_of_ocaml/link_wasm.ml create mode 100644 compiler/bin-wasm_of_ocaml/link_wasm.mli create mode 100644 compiler/bin-wasm_of_ocaml/preprocess.ml rename compiler/{bin-wasmoo_util/cmd_arg.mli => bin-wasm_of_ocaml/preprocess.mli} (61%) create mode 100644 compiler/bin-wasm_of_ocaml/wasmoo_link_wasm.ml create mode 100644 compiler/bin-wasm_of_ocaml/wasmoo_link_wasm.mli delete mode 100644 compiler/bin-wasmoo_util/cmd_arg.ml delete mode 100644 compiler/bin-wasmoo_util/dune delete mode 100644 compiler/bin-wasmoo_util/wasmoo_util.ml rename compiler/{bin-wasmoo_util/tests => tests-wasm_of_ocaml/preprocess}/cram.t (64%) rename compiler/{bin-wasmoo_util/tests => tests-wasm_of_ocaml/preprocess}/dune (50%) rename compiler/{bin-wasmoo_util/tests => tests-wasm_of_ocaml/preprocess}/tests.expected (100%) rename compiler/{bin-wasmoo_util/tests => tests-wasm_of_ocaml/preprocess}/tests.txt (100%) diff --git a/compiler/bin-wasm_of_ocaml/dune b/compiler/bin-wasm_of_ocaml/dune index 19598ca764..48619f0fe4 100644 --- a/compiler/bin-wasm_of_ocaml/dune +++ b/compiler/bin-wasm_of_ocaml/dune @@ -1,6 +1,6 @@ -(executable - (name wasm_of_ocaml) - (public_name wasm_of_ocaml) +(executables + (names wasm_of_ocaml wasmoo_link_wasm) + (public_names wasm_of_ocaml -) (package wasm_of_ocaml-compiler) (libraries jsoo_cmdline diff --git a/compiler/bin-wasm_of_ocaml/info.ml b/compiler/bin-wasm_of_ocaml/info.ml index c297de5b6c..9a440d48c9 100644 --- a/compiler/bin-wasm_of_ocaml/info.ml +++ b/compiler/bin-wasm_of_ocaml/info.ml @@ -32,9 +32,9 @@ let make ~name ~doc ~description = ; `S "AUTHORS" ; `P "Jerome Vouillon, Hugo Heuzard." ; `S "LICENSE" - ; `P "Copyright (C) 2010-2024." + ; `P "Copyright (C) 2010-2025." ; `P - "js_of_ocaml is free software, you can redistribute it and/or modify it under \ + "wasm_of_ocaml is free software, you can redistribute it and/or modify it under \ the terms of the GNU Lesser General Public License as published by the Free \ Software Foundation, with linking exception; either version 2.1 of the License, \ or (at your option) any later version." diff --git a/compiler/bin-wasm_of_ocaml/link_wasm.ml b/compiler/bin-wasm_of_ocaml/link_wasm.ml new file mode 100644 index 0000000000..7f04ab68e1 --- /dev/null +++ b/compiler/bin-wasm_of_ocaml/link_wasm.ml @@ -0,0 +1,110 @@ +(* Wasm_of_ocaml compiler + * http://www.ocsigen.org/js_of_ocaml/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, with linking exception; + * either version 2.1 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *) + +open Cmdliner +open Js_of_ocaml_compiler.Stdlib +open Wasm_of_ocaml_compiler + +type binaryen_options = + { common : string list + ; opt : string list + ; merge : string list + } + +type options = + { input_modules : (string * string) list + ; output_file : string + ; variables : Preprocess.variables + ; binaryen_options : binaryen_options + } + +let options = + let input_modules = + let doc = + "Specify an input module with name $(i,NAME) in Wasm text file $(i,FILE)." + in + Arg.( + value + & pos_right 0 (pair ~sep:':' string string) [] + & info [] ~docv:"NAME:FILE" ~doc) + in + let output_file = + let doc = "Specify the Wasm binary output file $(docv)." in + Arg.(required & pos 0 (some string) None & info [] ~docv:"WASM_FILE" ~doc) + in + let binaryen_options = + let doc = "Pass option $(docv) to binaryen tools" in + Arg.(value & opt_all string [] & info [ "binaryen" ] ~docv:"OPT" ~doc) + in + let opt_options = + let doc = "Pass option $(docv) to $(b,wasm-opt)" in + Arg.(value & opt_all string [] & info [ "binaryen-opt" ] ~docv:"OPT" ~doc) + in + let merge_options = + let doc = "Pass option $(docv) to $(b,wasm-merge)" in + Arg.(value & opt_all string [] & info [ "binaryen-merge" ] ~docv:"OPT" ~doc) + in + let build_t input_modules output_file variables common opt merge = + `Ok + { input_modules; output_file; variables; binaryen_options = { common; opt; merge } } + in + let t = + Term.( + const build_t + $ input_modules + $ output_file + $ Preprocess.variable_options + $ binaryen_options + $ opt_options + $ merge_options) + in + Term.ret t + +let link + { input_modules; output_file; variables; binaryen_options = { common; merge; opt } } = + let inputs = + List.map + ~f:(fun (module_name, file) -> + { Wat_preprocess.module_name + ; file + ; source = + (if Link.Wasm_binary.check_file ~file + then File + else Contents (Js_of_ocaml_compiler.Fs.read_file file)) + }) + input_modules + in + Runtime.build + ~link_options:(common @ merge) + ~opt_options:(common @ opt) + ~variables:(Preprocess.set_variables variables) + ~inputs + ~output_file + +let info = + Info.make + ~name:"link-wasm" + ~doc:"Wasm linker" + ~description: + "$(b,wasmoo_util link) is a Wasm linker. It takes as input a list of Wasm text \ + files, preprocesses them, links them together, and outputs a single Wasm binary \ + module" + +let term = Cmdliner.Term.(const link $ options) + +let command = Cmdliner.Cmd.v info term diff --git a/compiler/bin-wasm_of_ocaml/link_wasm.mli b/compiler/bin-wasm_of_ocaml/link_wasm.mli new file mode 100644 index 0000000000..952975461c --- /dev/null +++ b/compiler/bin-wasm_of_ocaml/link_wasm.mli @@ -0,0 +1,19 @@ +(* Wasm_of_ocaml compiler + * http://www.ocsigen.org/js_of_ocaml/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, with linking exception; + * either version 2.1 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *) + +val command : unit Cmdliner.Cmd.t diff --git a/compiler/bin-wasm_of_ocaml/preprocess.ml b/compiler/bin-wasm_of_ocaml/preprocess.ml new file mode 100644 index 0000000000..277527ff64 --- /dev/null +++ b/compiler/bin-wasm_of_ocaml/preprocess.ml @@ -0,0 +1,152 @@ +(* Wasm_of_ocaml compiler + * http://www.ocsigen.org/js_of_ocaml/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, with linking exception; + * either version 2.1 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *) + +open Cmdliner +open Js_of_ocaml_compiler.Stdlib +open Wasm_of_ocaml_compiler + +let () = Sys.catch_break true + +let read_contents ch = + let buf = Buffer.create 65536 in + let b = Bytes.create 65536 in + let rec read () = + let n = input ch b 0 (Bytes.length b) in + if n > 0 + then ( + Buffer.add_subbytes buf b 0 n; + read ()) + in + read (); + Buffer.contents buf + +type variables = + { enable : string list + ; disable : string list + ; set : (string * string) list + } + +type options = + { input_file : string option + ; output_file : string option + ; variables : variables + } + +let variable_options = + let enable = + let doc = "Set preprocessor variable $(docv) to true." in + let arg = + Arg.(value & opt_all (list string) [] & info [ "enable" ] ~docv:"VAR" ~doc) + in + Term.(const List.flatten $ arg) + in + let disable = + let doc = "Set preprocessor variable $(docv) to false." in + let arg = + Arg.(value & opt_all (list string) [] & info [ "disable" ] ~docv:"VAR" ~doc) + in + Term.(const List.flatten $ arg) + in + let set = + let doc = "Set preprocessor variable $(i,VAR) to value $(i,VALUE)." in + let arg = + Arg.( + value + & opt_all (list (pair ~sep:'=' string string)) [] + & info [ "set" ] ~docv:"VAR=VALUE" ~doc) + in + Term.(const List.flatten $ arg) + in + let build_t enable disable set = { enable; disable; set } in + Term.(const build_t $ enable $ disable $ set) + +let options = + let input_file = + let doc = + "Use the Wasm text file $(docv) as input (default to the standard input)." + in + Arg.(value & pos 0 (some string) None & info [] ~docv:"INPUT_FILE" ~doc) + in + let output_file = + let doc = "Specify the output file $(docv) (default to the standard output)." in + Arg.(value & opt (some string) None & info [ "o" ] ~docv:"OUTPUT_FILE" ~doc) + in + let build_t input_file output_file variables = + `Ok { input_file; output_file; variables } + in + let t = Term.(const build_t $ input_file $ output_file $ variable_options) in + Term.ret t + +let set_variables { enable; disable; set } = + List.map ~f:(fun nm -> nm, Wat_preprocess.Bool true) enable + @ List.map ~f:(fun nm -> nm, Wat_preprocess.Bool false) disable + @ List.map ~f:(fun (nm, v) -> nm, Wat_preprocess.String v) set + +let preprocess { input_file; output_file; variables } = + let with_input f = + match input_file with + | None -> f stdin + | Some file -> + let ch = open_in file in + let res = f ch in + close_in ch; + res + in + let with_output f = + match output_file with + | Some "-" | None -> f stdout + | Some file -> Filename.gen_file file f + in + let contents = with_input read_contents in + let res = + Wat_preprocess.f + ~filename:(Option.value ~default:"-" input_file) + ~contents + ~variables:(set_variables variables) + in + with_output (fun ch -> output_string ch res) + +let term = Cmdliner.Term.(const preprocess $ options) + +let info = + Info.make + ~name:"preprocess" + ~doc:"Wasm text file preprocessor" + ~description:"$(b,wasmoo_util pp) is a Wasm text file preprocessor." + +let command = Cmdliner.Cmd.v info term + +(* Adapted from + https://github.com/ocaml/opam/blob/fbbe93c3f67034da62d28c8666ec6b05e0a9b17c/s +rc/client/opamArg.ml#L759 *) +let alias_command ?orig_name cmd term name = + let orig = + match orig_name with + | Some s -> s + | None -> Cmd.name cmd + in + let doc = Printf.sprintf "An alias for $(b,%s)." orig in + let man = + [ `S "DESCRIPTION" + ; `P (Printf.sprintf "$(mname)$(b, %s) is an alias for $(mname)$(b, %s)." name orig) + ; `P (Printf.sprintf "See $(mname)$(b, %s --help) for details." orig) + ] + in + Cmd.v (Cmd.info name ~docs:"COMMAND ALIASES" ~doc ~man) term + +let command_alias = alias_command ~orig_name:"preprocess" command term "pp" diff --git a/compiler/bin-wasmoo_util/cmd_arg.mli b/compiler/bin-wasm_of_ocaml/preprocess.mli similarity index 61% rename from compiler/bin-wasmoo_util/cmd_arg.mli rename to compiler/bin-wasm_of_ocaml/preprocess.mli index e23e53c35e..9ad1de2fff 100644 --- a/compiler/bin-wasmoo_util/cmd_arg.mli +++ b/compiler/bin-wasm_of_ocaml/preprocess.mli @@ -22,31 +22,11 @@ type variables = ; set : (string * string) list } -type preprocess_options = - { input_file : string option - ; output_file : string option - ; variables : variables - } - -val preprocess_options : preprocess_options Cmdliner.Term.t - -val preprocess_info : Cmdliner.Cmd.info - -type binaryen_options = - { common : string list - ; opt : string list - ; merge : string list - } - -type link_options = - { input_modules : (string * string) list - ; output_file : string - ; variables : variables - ; binaryen_options : binaryen_options - } +val variable_options : variables Cmdliner.Term.t -val link_options : link_options Cmdliner.Term.t +val set_variables : + variables -> (string * Wasm_of_ocaml_compiler.Wat_preprocess.value) list -val link_info : Cmdliner.Cmd.info +val command : unit Cmdliner.Cmd.t -val info : Cmdliner.Cmd.info +val command_alias : unit Cmdliner.Cmd.t diff --git a/compiler/bin-wasm_of_ocaml/wasm_of_ocaml.ml b/compiler/bin-wasm_of_ocaml/wasm_of_ocaml.ml index fdb2df384e..1ffcba57c5 100644 --- a/compiler/bin-wasm_of_ocaml/wasm_of_ocaml.ml +++ b/compiler/bin-wasm_of_ocaml/wasm_of_ocaml.ml @@ -48,7 +48,13 @@ let () = (Cmdliner.Cmd.group ~default:Compile.term (Compile.info "wasm_of_ocaml") - [ Link.command; Build_runtime.command; Compile.command ]) + [ Link.command + ; Build_runtime.command + ; Compile.command + ; Preprocess.command + ; Preprocess.command_alias + ; Link_wasm.command + ]) with | Ok (`Ok () | `Help | `Version) -> if !warnings > 0 && !werror diff --git a/compiler/bin-wasm_of_ocaml/wasmoo_link_wasm.ml b/compiler/bin-wasm_of_ocaml/wasmoo_link_wasm.ml new file mode 100644 index 0000000000..d9f7a24766 --- /dev/null +++ b/compiler/bin-wasm_of_ocaml/wasmoo_link_wasm.ml @@ -0,0 +1,43 @@ +(* Js_of_ocaml compiler + * http://www.ocsigen.org/js_of_ocaml/ + * Copyright (C) 2013 Hugo Heuzard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, with linking exception; + * either version 2.1 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *) + +open Js_of_ocaml_compiler.Stdlib + +let (_ : int) = + try + Cmdliner.Cmd.eval + ~catch:false + ~argv:(Jsoo_cmdline.normalize_argv ~warn:(warn "%s") Sys.argv) + Link_wasm.command + with + | (Match_failure _ | Assert_failure _ | Not_found) as exc -> + let backtrace = Printexc.get_backtrace () in + Format.eprintf + "%s: You found a bug. Please report it at \ + https://github.com/ocsigen/js_of_ocaml/issues :@." + Sys.argv.(0); + Format.eprintf "Error: %s@." (Printexc.to_string exc); + prerr_string backtrace; + exit 1 + | Failure s -> + Format.eprintf "%s: Error: %s@." Sys.argv.(0) s; + exit 1 + | exc -> + Format.eprintf "%s: Error: %s@." Sys.argv.(0) (Printexc.to_string exc); + exit 1 diff --git a/compiler/bin-wasm_of_ocaml/wasmoo_link_wasm.mli b/compiler/bin-wasm_of_ocaml/wasmoo_link_wasm.mli new file mode 100644 index 0000000000..cc6700682b --- /dev/null +++ b/compiler/bin-wasm_of_ocaml/wasmoo_link_wasm.mli @@ -0,0 +1,17 @@ +(* Wasm_of_ocaml compiler + * http://www.ocsigen.org/js_of_ocaml/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, with linking exception; + * either version 2.1 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *) diff --git a/compiler/bin-wasmoo_util/cmd_arg.ml b/compiler/bin-wasmoo_util/cmd_arg.ml deleted file mode 100644 index d9c6d01bdd..0000000000 --- a/compiler/bin-wasmoo_util/cmd_arg.ml +++ /dev/null @@ -1,181 +0,0 @@ -(* Js_of_ocaml compiler - * http://www.ocsigen.org/js_of_ocaml/ - * Copyright (C) 2014 Hugo Heuzard - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, with linking exception; - * either version 2.1 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - *) - -open Cmdliner - -type variables = - { enable : string list - ; disable : string list - ; set : (string * string) list - } - -type preprocess_options = - { input_file : string option - ; output_file : string option - ; variables : variables - } - -let variable_options = - let enable = - let doc = "Set preprocessor variable $(docv) to true." in - let arg = - Arg.(value & opt_all (list string) [] & info [ "enable" ] ~docv:"VAR" ~doc) - in - Term.(const List.flatten $ arg) - in - let disable = - let doc = "Set preprocessor variable $(docv) to false." in - let arg = - Arg.(value & opt_all (list string) [] & info [ "disable" ] ~docv:"VAR" ~doc) - in - Term.(const List.flatten $ arg) - in - let set = - let doc = "Set preprocessor variable $(i,VAR) to value $(i,VALUE)." in - let arg = - Arg.( - value - & opt_all (list (pair ~sep:'=' string string)) [] - & info [ "set" ] ~docv:"VAR=VALUE" ~doc) - in - Term.(const List.flatten $ arg) - in - let build_t enable disable set = { enable; disable; set } in - Term.(const build_t $ enable $ disable $ set) - -let preprocess_options = - let input_file = - let doc = - "Use the Wasm text file $(docv) as input (default to the standard input)." - in - Arg.(value & pos 0 (some string) None & info [] ~docv:"INPUT_FILE" ~doc) - in - let output_file = - let doc = "Specify the output file $(docv) (default to the standard output)." in - Arg.(value & opt (some string) None & info [ "o" ] ~docv:"OUTPUT_FILE" ~doc) - in - let build_t input_file output_file variables = - `Ok { input_file; output_file; variables } - in - let t = Term.(const build_t $ input_file $ output_file $ variable_options) in - Term.ret t - -type binaryen_options = - { common : string list - ; opt : string list - ; merge : string list - } - -type link_options = - { input_modules : (string * string) list - ; output_file : string - ; variables : variables - ; binaryen_options : binaryen_options - } - -let link_options = - let input_modules = - let doc = - "Specify an input module with name $(i,NAME) in Wasm text file $(i,FILE)." - in - Arg.( - value - & pos_right 0 (pair ~sep:':' string string) [] - & info [] ~docv:"NAME:FILE" ~doc) - in - let output_file = - let doc = "Specify the Wasm binary output file $(docv)." in - Arg.(required & pos 0 (some string) None & info [] ~docv:"WASM_FILE" ~doc) - in - let binaryen_options = - let doc = "Pass option $(docv) to binaryen tools" in - Arg.(value & opt_all string [] & info [ "binaryen" ] ~docv:"OPT" ~doc) - in - let opt_options = - let doc = "Pass option $(docv) to $(b,wasm-opt)" in - Arg.(value & opt_all string [] & info [ "binaryen-opt" ] ~docv:"OPT" ~doc) - in - let merge_options = - let doc = "Pass option $(docv) to $(b,wasm-merge)" in - Arg.(value & opt_all string [] & info [ "binaryen-merge" ] ~docv:"OPT" ~doc) - in - let build_t input_modules output_file variables common opt merge = - `Ok - { input_modules; output_file; variables; binaryen_options = { common; opt; merge } } - in - let t = - Term.( - const build_t - $ input_modules - $ output_file - $ variable_options - $ binaryen_options - $ opt_options - $ merge_options) - in - Term.ret t - -let make_info ~name ~doc ~description = - let man = - [ `S "DESCRIPTION" - ; `P description - ; `S "BUGS" - ; `P - "Bugs are tracked on github at \ - $(i,https://github.com/ocsigen/js_of_ocaml/issues)." - ; `S "SEE ALSO" - ; `P "wasm_of_ocaml(1)" - ; `S "AUTHORS" - ; `P "Jerome Vouillon, Hugo Heuzard." - ; `S "LICENSE" - ; `P "Copyright (C) 2010-2025." - ; `P - "wasmoo_util is free software, you can redistribute it and/or modify it under \ - the terms of the GNU Lesser General Public License as published by the Free \ - Software Foundation, with linking exception; either version 2.1 of the License, \ - or (at your option) any later version." - ] - in - let version = - match Js_of_ocaml_compiler.Compiler_version.git_version with - | "" -> Js_of_ocaml_compiler.Compiler_version.s - | v -> Printf.sprintf "%s+%s" Js_of_ocaml_compiler.Compiler_version.s v - in - Cmd.info name ~version ~doc ~man - -let preprocess_info = - make_info - ~name:"pp" - ~doc:"Wasm text file preprocessor" - ~description:"$(b,wasmoo_util pp) is a Wasm text file preprocessor." - -let link_info = - make_info - ~name:"link" - ~doc:"Wasm linker" - ~description: - "$(b,wasmoo_util link) is a Wasm linker. It takes as input a list of Wasm text \ - files, preprocesses them, links them together, and outputs a single Wasm binary \ - module" - -let info = - make_info - ~name:"wasmoo_util" - ~doc:"Wasm utilities" - ~description:"wasmoo_util is a collection of utilities for $(b,wasm_of_ocaml)" diff --git a/compiler/bin-wasmoo_util/dune b/compiler/bin-wasmoo_util/dune deleted file mode 100644 index d09db61954..0000000000 --- a/compiler/bin-wasmoo_util/dune +++ /dev/null @@ -1,17 +0,0 @@ -(executable - (name wasmoo_util) - (public_name wasmoo_util) - (package wasm_of_ocaml-compiler) - (libraries wasm_of_ocaml-compiler jsoo_cmdline cmdliner)) - -(rule - (targets wasmoo_util.1) - (action - (with-stdout-to - %{targets} - (run %{bin:wasmoo_util} --help=groff)))) - -(install - (section man) - (package wasm_of_ocaml-compiler) - (files wasmoo_util.1)) diff --git a/compiler/bin-wasmoo_util/wasmoo_util.ml b/compiler/bin-wasmoo_util/wasmoo_util.ml deleted file mode 100644 index 6f0cc37e29..0000000000 --- a/compiler/bin-wasmoo_util/wasmoo_util.ml +++ /dev/null @@ -1,121 +0,0 @@ -(* Js_of_ocaml compiler - * http://www.ocsigen.org/js_of_ocaml/ - * Copyright (C) 2013 Hugo Heuzard - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, with linking exception; - * either version 2.1 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - *) - -open Js_of_ocaml_compiler.Stdlib -open Wasm_of_ocaml_compiler - -let () = Sys.catch_break true - -let read_contents ch = - let buf = Buffer.create 65536 in - let b = Bytes.create 65536 in - let rec read () = - let n = input ch b 0 (Bytes.length b) in - if n > 0 - then ( - Buffer.add_subbytes buf b 0 n; - read ()) - in - read (); - Buffer.contents buf - -let set_variables { Cmd_arg.enable; disable; set } = - List.map ~f:(fun nm -> nm, Wat_preprocess.Bool true) enable - @ List.map ~f:(fun nm -> nm, Wat_preprocess.Bool false) disable - @ List.map ~f:(fun (nm, v) -> nm, Wat_preprocess.String v) set - -let preprocess { Cmd_arg.input_file; output_file; variables } = - let with_input f = - match input_file with - | None -> f stdin - | Some file -> - let ch = open_in file in - let res = f ch in - close_in ch; - res - in - let with_output f = - match output_file with - | Some "-" | None -> f stdout - | Some file -> Filename.gen_file file f - in - let contents = with_input read_contents in - let res = - Wat_preprocess.f - ~filename:(Option.value ~default:"-" input_file) - ~contents - ~variables:(set_variables variables) - in - with_output (fun ch -> output_string ch res) - -let preprocess_term = Cmdliner.Term.(const preprocess $ Cmd_arg.preprocess_options) - -let preprocess_command = Cmdliner.Cmd.v Cmd_arg.preprocess_info preprocess_term - -let link - { Cmd_arg.input_modules - ; output_file - ; variables - ; binaryen_options = { common; merge; opt } - } = - let inputs = - List.map - ~f:(fun (module_name, file) -> - { Wat_preprocess.module_name - ; file - ; source = - (if Link.Wasm_binary.check_file ~file - then File - else Contents (Js_of_ocaml_compiler.Fs.read_file file)) - }) - input_modules - in - Runtime.build - ~link_options:(common @ merge) - ~opt_options:(common @ opt) - ~variables:(set_variables variables) - ~inputs - ~output_file - -let link_term = Cmdliner.Term.(const link $ Cmd_arg.link_options) - -let link_command = Cmdliner.Cmd.v Cmd_arg.link_info link_term - -let (_ : int) = - try - Cmdliner.Cmd.eval - ~catch:false - ~argv:(Jsoo_cmdline.normalize_argv ~warn:(warn "%s") Sys.argv) - (Cmdliner.Cmd.group Cmd_arg.info [ preprocess_command; link_command ]) - with - | (Match_failure _ | Assert_failure _ | Not_found) as exc -> - let backtrace = Printexc.get_backtrace () in - Format.eprintf - "%s: You found a bug. Please report it at \ - https://github.com/ocsigen/js_of_ocaml/issues :@." - Sys.argv.(0); - Format.eprintf "Error: %s@." (Printexc.to_string exc); - prerr_string backtrace; - exit 1 - | Failure s -> - Format.eprintf "%s: Error: %s@." Sys.argv.(0) s; - exit 1 - | exc -> - Format.eprintf "%s: Error: %s@." Sys.argv.(0) (Printexc.to_string exc); - exit 1 diff --git a/compiler/bin-wasmoo_util/tests/cram.t b/compiler/tests-wasm_of_ocaml/preprocess/cram.t similarity index 64% rename from compiler/bin-wasmoo_util/tests/cram.t rename to compiler/tests-wasm_of_ocaml/preprocess/cram.t index fa98a1eead..2673e15964 100644 --- a/compiler/bin-wasmoo_util/tests/cram.t +++ b/compiler/tests-wasm_of_ocaml/preprocess/cram.t @@ -1,178 +1,178 @@ Too many parentheses - $ echo '())' | wasmoo_util pp + $ echo '())' | wasm_of_ocaml pp File "-", line 1, characters 2-3: Unexpected closing parenthesis. [1] - $ echo '();)' | wasmoo_util pp + $ echo '();)' | wasm_of_ocaml pp File "-", line 1, characters 2-4: Unmatched closing comment. [1] Missing parenthesis - $ echo '(()' | wasmoo_util pp + $ echo '(()' | wasm_of_ocaml pp File "-", line 1, characters 0-1: Unclosed parenthesis. [1] - $ echo '(; ()' | wasmoo_util pp + $ echo '(; ()' | wasm_of_ocaml pp File "-", line 1, characters 0-2: Unclosed comment. [1] - $ echo '(; (; ()' | wasmoo_util pp + $ echo '(; (; ()' | wasm_of_ocaml pp File "-", line 1, characters 3-5: Unclosed comment. [1] Unterminated string (we point at the newline) - $ echo '"abcd' | wasmoo_util pp + $ echo '"abcd' | wasm_of_ocaml pp File "-", line 1, characters 5-5: Malformed string. [1] Bad conditional - $ echo '(@if)' | wasmoo_util pp + $ echo '(@if)' | wasm_of_ocaml pp File "-", line 1, characters 4-5: Expecting condition. [1] - $ echo '(@if a)' | wasmoo_util pp + $ echo '(@if a)' | wasm_of_ocaml pp File "-", line 1, characters 6-7: Expecting @then clause. [1] - $ echo '(@if a xxx)' | wasmoo_util pp + $ echo '(@if a xxx)' | wasm_of_ocaml pp File "-", line 1, characters 7-10: Expecting @then clause. [1] - $ echo '(@if a (@then) xx)' | wasmoo_util pp + $ echo '(@if a (@then) xx)' | wasm_of_ocaml pp File "-", line 1, characters 15-17: Expecting @else clause or closing parenthesis. [1] - $ echo '(@if a (@then) (@else) xx)' | wasmoo_util pp + $ echo '(@if a (@then) (@else) xx)' | wasm_of_ocaml pp File "-", line 1, characters 23-25: Expecting closing parenthesis. [1] Syntax error in condition - $ echo '(@if () (@then))' | wasmoo_util pp + $ echo '(@if () (@then))' | wasm_of_ocaml pp File "-", line 1, characters 5-7: Syntax error. [1] - $ echo '(@if (not) (@then))' | wasmoo_util pp + $ echo '(@if (not) (@then))' | wasm_of_ocaml pp File "-", line 1, characters 5-10: Syntax error. [1] - $ echo '(@if (not (and) (or)) (@then))' | wasmoo_util pp + $ echo '(@if (not (and) (or)) (@then))' | wasm_of_ocaml pp File "-", line 1, characters 5-21: Syntax error. [1] - $ echo '(@if (= "a") (@then))' | wasmoo_util pp + $ echo '(@if (= "a") (@then))' | wasm_of_ocaml pp File "-", line 1, characters 5-12: Syntax error. [1] - $ echo '(@if (= "a" "b" "c") (@then))' | wasmoo_util pp + $ echo '(@if (= "a" "b" "c") (@then))' | wasm_of_ocaml pp File "-", line 1, characters 5-20: Syntax error. [1] Unicode escape sequences are not supported - $ echo '(@if (= "\u{1F600}" "b") (@then))' | wasmoo_util pp + $ echo '(@if (= "\u{1F600}" "b") (@then))' | wasm_of_ocaml pp File "-", line 1, characters 8-19: Unicode escape sequences are not supported. [1] Lonely @then or @else - $ echo '(@then)' | wasmoo_util pp + $ echo '(@then)' | wasm_of_ocaml pp File "-", line 1, characters 1-6: Unexpected @then clause. Maybe you forgot a parenthesis. [1] - $ echo '(@else)' | wasmoo_util pp + $ echo '(@else)' | wasm_of_ocaml pp File "-", line 1, characters 1-6: Unexpected @else clause. Maybe you forgot a parenthesis. [1] - $ echo '(@if (and) (@then (@else)))' | wasmoo_util pp + $ echo '(@if (and) (@then (@else)))' | wasm_of_ocaml pp File "-", line 1, characters 19-24: Unexpected @else clause. Maybe you forgot a parenthesis. [1] Undefined variable - $ echo '(@if a (@then))' | wasmoo_util pp + $ echo '(@if a (@then))' | wasm_of_ocaml pp File "-", line 1, characters 5-6: Unknown variable 'a'. [1] Wrong type - $ echo '(@if "" (@then))' | wasmoo_util pp + $ echo '(@if "" (@then))' | wasm_of_ocaml pp File "-", line 1, characters 5-7: Expected a boolean but this is a string. [1] - $ echo '(@if (not "") (@then))' | wasmoo_util pp + $ echo '(@if (not "") (@then))' | wasm_of_ocaml pp File "-", line 1, characters 10-12: Expected a boolean but this is a string. [1] - $ echo '(@if (and "") (@then))' | wasmoo_util pp + $ echo '(@if (and "") (@then))' | wasm_of_ocaml pp File "-", line 1, characters 10-12: Expected a boolean but this is a string. [1] - $ echo '(@if (or "") (@then))' | wasmoo_util pp + $ echo '(@if (or "") (@then))' | wasm_of_ocaml pp File "-", line 1, characters 9-11: Expected a boolean but this is a string. [1] - $ echo '(@if (= (and) "") (@then))' | wasmoo_util pp + $ echo '(@if (= (and) "") (@then))' | wasm_of_ocaml pp File "-", line 1, characters 14-16: Expected a boolean but this is a string. [1] Bad strings - $ echo '(@string)' | wasmoo_util pp + $ echo '(@string)' | wasm_of_ocaml pp File "-", line 1, characters 8-9: Expecting an id or a string. [1] - $ echo '(@string a "b")' | wasmoo_util pp + $ echo '(@string a "b")' | wasm_of_ocaml pp File "-", line 1, characters 9-10: Expecting an id [1] - $ echo '(@string $a b)' | wasmoo_util pp + $ echo '(@string $a b)' | wasm_of_ocaml pp File "-", line 1, characters 12-13: Expecting a string [1] - $ echo '(@string $bad "\u{1F600}")' | wasmoo_util pp + $ echo '(@string $bad "\u{1F600}")' | wasm_of_ocaml pp File "-", line 1, characters 14-25: Unicode escape sequences are not supported. [1] - $ echo '(@string a)' | wasmoo_util pp + $ echo '(@string a)' | wasm_of_ocaml pp File "-", line 1, characters 9-10: Expecting a string [1] - $ echo '(@string a b c)' | wasmoo_util pp + $ echo '(@string a b c)' | wasm_of_ocaml pp File "-", line 1, characters 13-14: Expecting a closing parenthesis. [1] diff --git a/compiler/bin-wasmoo_util/tests/dune b/compiler/tests-wasm_of_ocaml/preprocess/dune similarity index 50% rename from compiler/bin-wasmoo_util/tests/dune rename to compiler/tests-wasm_of_ocaml/preprocess/dune index efe865bf23..b20088f13d 100644 --- a/compiler/bin-wasmoo_util/tests/dune +++ b/compiler/tests-wasm_of_ocaml/preprocess/dune @@ -1,7 +1,7 @@ (rule (with-stdout-to tests.output - (run wasmoo_util pp --enable a --disable b --set c=1 %{dep:tests.txt}))) + (run %{bin:wasm_of_ocaml} pp --enable a --disable b --set c=1 %{dep:tests.txt}))) (rule (alias runtest) @@ -9,4 +9,4 @@ (diff tests.expected tests.output))) (cram - (deps %{bin:wasmoo_util})) + (deps %{bin:wasm_of_ocaml})) diff --git a/compiler/bin-wasmoo_util/tests/tests.expected b/compiler/tests-wasm_of_ocaml/preprocess/tests.expected similarity index 100% rename from compiler/bin-wasmoo_util/tests/tests.expected rename to compiler/tests-wasm_of_ocaml/preprocess/tests.expected diff --git a/compiler/bin-wasmoo_util/tests/tests.txt b/compiler/tests-wasm_of_ocaml/preprocess/tests.txt similarity index 100% rename from compiler/bin-wasmoo_util/tests/tests.txt rename to compiler/tests-wasm_of_ocaml/preprocess/tests.txt diff --git a/runtime/wasm/dune b/runtime/wasm/dune index c55cb470cd..0df18be889 100644 --- a/runtime/wasm/dune +++ b/runtime/wasm/dune @@ -10,8 +10,7 @@ (glob_files *.wat)) (action (run - wasmoo_util - link + ../../compiler/bin-wasm_of_ocaml/wasmoo_link_wasm.exe --binaryen=-g --binaryen-opt=-O3 %{target}