|
| 1 | +(* Wasm_of_ocaml compiler |
| 2 | + * http://www.ocsigen.org/js_of_ocaml/ |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Lesser General Public License as published by |
| 6 | + * the Free Software Foundation, with linking exception; |
| 7 | + * either version 2.1 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | + *) |
| 18 | + |
| 19 | +open Cmdliner |
| 20 | +open Js_of_ocaml_compiler.Stdlib |
| 21 | +open Wasm_of_ocaml_compiler |
| 22 | + |
| 23 | +let () = Sys.catch_break true |
| 24 | + |
| 25 | +let read_contents ch = |
| 26 | + let buf = Buffer.create 65536 in |
| 27 | + let b = Bytes.create 65536 in |
| 28 | + let rec read () = |
| 29 | + let n = input ch b 0 (Bytes.length b) in |
| 30 | + if n > 0 |
| 31 | + then ( |
| 32 | + Buffer.add_subbytes buf b 0 n; |
| 33 | + read ()) |
| 34 | + in |
| 35 | + read (); |
| 36 | + Buffer.contents buf |
| 37 | + |
| 38 | +type variables = |
| 39 | + { enable : string list |
| 40 | + ; disable : string list |
| 41 | + ; set : (string * string) list |
| 42 | + } |
| 43 | + |
| 44 | +type options = |
| 45 | + { input_file : string option |
| 46 | + ; output_file : string option |
| 47 | + ; variables : variables |
| 48 | + } |
| 49 | + |
| 50 | +let variable_options = |
| 51 | + let enable = |
| 52 | + let doc = "Set preprocessor variable $(docv) to true." in |
| 53 | + let arg = |
| 54 | + Arg.(value & opt_all (list string) [] & info [ "enable" ] ~docv:"VAR" ~doc) |
| 55 | + in |
| 56 | + Term.(const List.flatten $ arg) |
| 57 | + in |
| 58 | + let disable = |
| 59 | + let doc = "Set preprocessor variable $(docv) to false." in |
| 60 | + let arg = |
| 61 | + Arg.(value & opt_all (list string) [] & info [ "disable" ] ~docv:"VAR" ~doc) |
| 62 | + in |
| 63 | + Term.(const List.flatten $ arg) |
| 64 | + in |
| 65 | + let set = |
| 66 | + let doc = "Set preprocessor variable $(i,VAR) to value $(i,VALUE)." in |
| 67 | + let arg = |
| 68 | + Arg.( |
| 69 | + value |
| 70 | + & opt_all (list (pair ~sep:'=' string string)) [] |
| 71 | + & info [ "set" ] ~docv:"VAR=VALUE" ~doc) |
| 72 | + in |
| 73 | + Term.(const List.flatten $ arg) |
| 74 | + in |
| 75 | + let build_t enable disable set = { enable; disable; set } in |
| 76 | + Term.(const build_t $ enable $ disable $ set) |
| 77 | + |
| 78 | +let options = |
| 79 | + let input_file = |
| 80 | + let doc = |
| 81 | + "Use the Wasm text file $(docv) as input (default to the standard input)." |
| 82 | + in |
| 83 | + Arg.(value & pos 0 (some string) None & info [] ~docv:"INPUT_FILE" ~doc) |
| 84 | + in |
| 85 | + let output_file = |
| 86 | + let doc = "Specify the output file $(docv) (default to the standard output)." in |
| 87 | + Arg.(value & opt (some string) None & info [ "o" ] ~docv:"OUTPUT_FILE" ~doc) |
| 88 | + in |
| 89 | + let build_t input_file output_file variables = |
| 90 | + `Ok { input_file; output_file; variables } |
| 91 | + in |
| 92 | + let t = Term.(const build_t $ input_file $ output_file $ variable_options) in |
| 93 | + Term.ret t |
| 94 | + |
| 95 | +let set_variables { enable; disable; set } = |
| 96 | + List.map ~f:(fun nm -> nm, Wat_preprocess.Bool true) enable |
| 97 | + @ List.map ~f:(fun nm -> nm, Wat_preprocess.Bool false) disable |
| 98 | + @ List.map ~f:(fun (nm, v) -> nm, Wat_preprocess.String v) set |
| 99 | + |
| 100 | +let preprocess { input_file; output_file; variables } = |
| 101 | + let with_input f = |
| 102 | + match input_file with |
| 103 | + | None -> f stdin |
| 104 | + | Some file -> |
| 105 | + let ch = open_in file in |
| 106 | + let res = f ch in |
| 107 | + close_in ch; |
| 108 | + res |
| 109 | + in |
| 110 | + let with_output f = |
| 111 | + match output_file with |
| 112 | + | Some "-" | None -> f stdout |
| 113 | + | Some file -> Filename.gen_file file f |
| 114 | + in |
| 115 | + let contents = with_input read_contents in |
| 116 | + let res = |
| 117 | + Wat_preprocess.f |
| 118 | + ~filename:(Option.value ~default:"-" input_file) |
| 119 | + ~contents |
| 120 | + ~variables:(set_variables variables) |
| 121 | + in |
| 122 | + with_output (fun ch -> output_string ch res) |
| 123 | + |
| 124 | +let term = Cmdliner.Term.(const preprocess $ options) |
| 125 | + |
| 126 | +let info = |
| 127 | + Info.make |
| 128 | + ~name:"preprocess" |
| 129 | + ~doc:"Wasm text file preprocessor" |
| 130 | + ~description:"$(b,wasmoo_util pp) is a Wasm text file preprocessor." |
| 131 | + |
| 132 | +let command = Cmdliner.Cmd.v info term |
| 133 | + |
| 134 | +(* Adapted from |
| 135 | + https://github.com/ocaml/opam/blob/fbbe93c3f67034da62d28c8666ec6b05e0a9b17c/s |
| 136 | +rc/client/opamArg.ml#L759 *) |
| 137 | +let alias_command ?orig_name cmd term name = |
| 138 | + let orig = |
| 139 | + match orig_name with |
| 140 | + | Some s -> s |
| 141 | + | None -> Cmd.name cmd |
| 142 | + in |
| 143 | + let doc = Printf.sprintf "An alias for $(b,%s)." orig in |
| 144 | + let man = |
| 145 | + [ `S "DESCRIPTION" |
| 146 | + ; `P (Printf.sprintf "$(mname)$(b, %s) is an alias for $(mname)$(b, %s)." name orig) |
| 147 | + ; `P (Printf.sprintf "See $(mname)$(b, %s --help) for details." orig) |
| 148 | + ] |
| 149 | + in |
| 150 | + Cmd.v (Cmd.info name ~docs:"COMMAND ALIASES" ~doc ~man) term |
| 151 | + |
| 152 | +let command_alias = alias_command ~orig_name:"preprocess" command term "pp" |
0 commit comments