|
| 1 | +(* Js_of_ocaml compiler |
| 2 | + * http://www.ocsigen.org/js_of_ocaml/ |
| 3 | + * Copyright (C) 2014 Hugo Heuzard |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Lesser General Public License as published by |
| 7 | + * the Free Software Foundation, with linking exception; |
| 8 | + * either version 2.1 of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public License |
| 16 | + * along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | + *) |
| 19 | + |
| 20 | +open Cmdliner |
| 21 | + |
| 22 | +type variables = |
| 23 | + { enable : string list |
| 24 | + ; disable : string list |
| 25 | + ; set : (string * string) list |
| 26 | + } |
| 27 | + |
| 28 | +type preprocess_options = |
| 29 | + { input_file : string option |
| 30 | + ; output_file : string option |
| 31 | + ; variables : variables |
| 32 | + } |
| 33 | + |
| 34 | +let variable_options = |
| 35 | + let enable = |
| 36 | + let doc = "Set preprocessor variable $(docv) to true." in |
| 37 | + let arg = |
| 38 | + Arg.(value & opt_all (list string) [] & info [ "enable" ] ~docv:"VAR" ~doc) |
| 39 | + in |
| 40 | + Term.(const List.flatten $ arg) |
| 41 | + in |
| 42 | + let disable = |
| 43 | + let doc = "Set preprocessor variable $(docv) to false." in |
| 44 | + let arg = |
| 45 | + Arg.(value & opt_all (list string) [] & info [ "disable" ] ~docv:"VAR" ~doc) |
| 46 | + in |
| 47 | + Term.(const List.flatten $ arg) |
| 48 | + in |
| 49 | + let set = |
| 50 | + let doc = "Set preprocessor variable $(i,VAR) to value $(i,VALUE)." in |
| 51 | + let arg = |
| 52 | + Arg.( |
| 53 | + value |
| 54 | + & opt_all (list (pair ~sep:'=' string string)) [] |
| 55 | + & info [ "set" ] ~docv:"VAR=VALUE" ~doc) |
| 56 | + in |
| 57 | + Term.(const List.flatten $ arg) |
| 58 | + in |
| 59 | + let build_t enable disable set = { enable; disable; set } in |
| 60 | + Term.(const build_t $ enable $ disable $ set) |
| 61 | + |
| 62 | +let preprocess_options = |
| 63 | + let input_file = |
| 64 | + let doc = |
| 65 | + "Use the Wasm text file $(docv) as input (default to the standard input)." |
| 66 | + in |
| 67 | + Arg.(value & pos 0 (some string) None & info [] ~docv:"INPUT_FILE" ~doc) |
| 68 | + in |
| 69 | + let output_file = |
| 70 | + let doc = "Specify the output file $(docv) (default to the standard output)." in |
| 71 | + Arg.(value & opt (some string) None & info [ "o" ] ~docv:"OUTPUT_FILE" ~doc) |
| 72 | + in |
| 73 | + let build_t input_file output_file variables = |
| 74 | + `Ok { input_file; output_file; variables } |
| 75 | + in |
| 76 | + let t = Term.(const build_t $ input_file $ output_file $ variable_options) in |
| 77 | + Term.ret t |
| 78 | + |
| 79 | +type binaryen_options = |
| 80 | + { common : string list |
| 81 | + ; opt : string list |
| 82 | + ; merge : string list |
| 83 | + } |
| 84 | + |
| 85 | +type link_options = |
| 86 | + { input_modules : (string * string) list |
| 87 | + ; output_file : string |
| 88 | + ; variables : variables |
| 89 | + ; binaryen_options : binaryen_options |
| 90 | + } |
| 91 | + |
| 92 | +let link_options = |
| 93 | + let input_modules = |
| 94 | + let doc = |
| 95 | + "Specify an input module with name $(i,NAME) in Wasm text file $(i,FILE)." |
| 96 | + in |
| 97 | + Arg.( |
| 98 | + value |
| 99 | + & pos_right 0 (pair ~sep:':' string string) [] |
| 100 | + & info [] ~docv:"NAME:FILE" ~doc) |
| 101 | + in |
| 102 | + let output_file = |
| 103 | + let doc = "Specify the Wasm binary output file $(docv)." in |
| 104 | + Arg.(required & pos 0 (some string) None & info [] ~docv:"WASM_FILE" ~doc) |
| 105 | + in |
| 106 | + let binaryen_options = |
| 107 | + let doc = "Pass option $(docv) to binaryen tools" in |
| 108 | + Arg.(value & opt_all string [] & info [ "binaryen" ] ~docv:"OPT" ~doc) |
| 109 | + in |
| 110 | + let opt_options = |
| 111 | + let doc = "Pass option $(docv) to $(b,wasm-opt)" in |
| 112 | + Arg.(value & opt_all string [] & info [ "binaryen-opt" ] ~docv:"OPT" ~doc) |
| 113 | + in |
| 114 | + let merge_options = |
| 115 | + let doc = "Pass option $(docv) to $(b,wasm-merge)" in |
| 116 | + Arg.(value & opt_all string [] & info [ "binaryen-merge" ] ~docv:"OPT" ~doc) |
| 117 | + in |
| 118 | + let build_t input_modules output_file variables common opt merge = |
| 119 | + `Ok |
| 120 | + { input_modules; output_file; variables; binaryen_options = { common; opt; merge } } |
| 121 | + in |
| 122 | + let t = |
| 123 | + Term.( |
| 124 | + const build_t |
| 125 | + $ input_modules |
| 126 | + $ output_file |
| 127 | + $ variable_options |
| 128 | + $ binaryen_options |
| 129 | + $ opt_options |
| 130 | + $ merge_options) |
| 131 | + in |
| 132 | + Term.ret t |
| 133 | + |
| 134 | +let make_info ~name ~doc ~description = |
| 135 | + let man = |
| 136 | + [ `S "DESCRIPTION" |
| 137 | + ; `P description |
| 138 | + ; `S "BUGS" |
| 139 | + ; `P |
| 140 | + "Bugs are tracked on github at \ |
| 141 | + $(i,https://github.com/ocsigen/js_of_ocaml/issues)." |
| 142 | + ; `S "SEE ALSO" |
| 143 | + ; `P "wasm_of_ocaml(1)" |
| 144 | + ; `S "AUTHORS" |
| 145 | + ; `P "Jerome Vouillon, Hugo Heuzard." |
| 146 | + ; `S "LICENSE" |
| 147 | + ; `P "Copyright (C) 2010-2025." |
| 148 | + ; `P |
| 149 | + "wasmoo_util is free software, you can redistribute it and/or modify it under \ |
| 150 | + the terms of the GNU Lesser General Public License as published by the Free \ |
| 151 | + Software Foundation, with linking exception; either version 2.1 of the License, \ |
| 152 | + or (at your option) any later version." |
| 153 | + ] |
| 154 | + in |
| 155 | + let version = |
| 156 | + match Js_of_ocaml_compiler.Compiler_version.git_version with |
| 157 | + | "" -> Js_of_ocaml_compiler.Compiler_version.s |
| 158 | + | v -> Printf.sprintf "%s+%s" Js_of_ocaml_compiler.Compiler_version.s v |
| 159 | + in |
| 160 | + Cmd.info name ~version ~doc ~man |
| 161 | + |
| 162 | +let preprocess_info = |
| 163 | + make_info |
| 164 | + ~name:"pp" |
| 165 | + ~doc:"Wasm text file preprocessor" |
| 166 | + ~description:"$(b,wasmoo_util pp) is a Wasm text file preprocessor." |
| 167 | + |
| 168 | +let link_info = |
| 169 | + make_info |
| 170 | + ~name:"link" |
| 171 | + ~doc:"Wasm linker" |
| 172 | + ~description: |
| 173 | + "$(b,wasmoo_util link) is a Wasm linker. It takes as input a list of Wasm text \ |
| 174 | + files, preprocesses them, links them together, and outputs a single Wasm binary \ |
| 175 | + module" |
| 176 | + |
| 177 | +let info = |
| 178 | + make_info |
| 179 | + ~name:"wasmoo_util" |
| 180 | + ~doc:"Wasm utilities" |
| 181 | + ~description:"wasmoo_util is a collection of utilities for $(b,wasm_of_ocaml)" |
0 commit comments