Skip to content

Commit cd3fa9b

Browse files
committed
Preprocessor: move tools as subcommands inside wasm_of_ocaml
1 parent 5d9553a commit cd3fa9b

File tree

18 files changed

+412
-396
lines changed

18 files changed

+412
-396
lines changed

compiler/bin-wasm_of_ocaml/dune

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
(executable
2-
(name wasm_of_ocaml)
3-
(public_name wasm_of_ocaml)
1+
(executables
2+
(names wasm_of_ocaml wasmoo_link_wasm)
3+
(public_names wasm_of_ocaml -)
44
(package wasm_of_ocaml-compiler)
55
(libraries
66
jsoo_cmdline

compiler/bin-wasm_of_ocaml/info.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ let make ~name ~doc ~description =
3232
; `S "AUTHORS"
3333
; `P "Jerome Vouillon, Hugo Heuzard."
3434
; `S "LICENSE"
35-
; `P "Copyright (C) 2010-2024."
35+
; `P "Copyright (C) 2010-2025."
3636
; `P
37-
"js_of_ocaml is free software, you can redistribute it and/or modify it under \
37+
"wasm_of_ocaml is free software, you can redistribute it and/or modify it under \
3838
the terms of the GNU Lesser General Public License as published by the Free \
3939
Software Foundation, with linking exception; either version 2.1 of the License, \
4040
or (at your option) any later version."
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
type binaryen_options =
24+
{ common : string list
25+
; opt : string list
26+
; merge : string list
27+
}
28+
29+
type options =
30+
{ input_modules : (string * string) list
31+
; output_file : string
32+
; variables : Preprocess.variables
33+
; binaryen_options : binaryen_options
34+
}
35+
36+
let options =
37+
let input_modules =
38+
let doc =
39+
"Specify an input module with name $(i,NAME) in Wasm text file $(i,FILE)."
40+
in
41+
Arg.(
42+
value
43+
& pos_right 0 (pair ~sep:':' string string) []
44+
& info [] ~docv:"NAME:FILE" ~doc)
45+
in
46+
let output_file =
47+
let doc = "Specify the Wasm binary output file $(docv)." in
48+
Arg.(required & pos 0 (some string) None & info [] ~docv:"WASM_FILE" ~doc)
49+
in
50+
let binaryen_options =
51+
let doc = "Pass option $(docv) to binaryen tools" in
52+
Arg.(value & opt_all string [] & info [ "binaryen" ] ~docv:"OPT" ~doc)
53+
in
54+
let opt_options =
55+
let doc = "Pass option $(docv) to $(b,wasm-opt)" in
56+
Arg.(value & opt_all string [] & info [ "binaryen-opt" ] ~docv:"OPT" ~doc)
57+
in
58+
let merge_options =
59+
let doc = "Pass option $(docv) to $(b,wasm-merge)" in
60+
Arg.(value & opt_all string [] & info [ "binaryen-merge" ] ~docv:"OPT" ~doc)
61+
in
62+
let build_t input_modules output_file variables common opt merge =
63+
`Ok
64+
{ input_modules; output_file; variables; binaryen_options = { common; opt; merge } }
65+
in
66+
let t =
67+
Term.(
68+
const build_t
69+
$ input_modules
70+
$ output_file
71+
$ Preprocess.variable_options
72+
$ binaryen_options
73+
$ opt_options
74+
$ merge_options)
75+
in
76+
Term.ret t
77+
78+
let link
79+
{ input_modules; output_file; variables; binaryen_options = { common; merge; opt } } =
80+
let inputs =
81+
List.map
82+
~f:(fun (module_name, file) ->
83+
{ Wat_preprocess.module_name
84+
; file
85+
; source =
86+
(if Link.Wasm_binary.check_file ~file
87+
then File
88+
else Contents (Js_of_ocaml_compiler.Fs.read_file file))
89+
})
90+
input_modules
91+
in
92+
Runtime.build
93+
~link_options:(common @ merge)
94+
~opt_options:(common @ opt)
95+
~variables:(Preprocess.set_variables variables)
96+
~inputs
97+
~output_file
98+
99+
let info =
100+
Info.make
101+
~name:"link-wasm"
102+
~doc:"Wasm linker"
103+
~description:
104+
"$(b,wasmoo_link_wasm) is a Wasm linker. It takes as input a list of Wasm text \
105+
files, preprocesses them, links them together, and outputs a single Wasm binary \
106+
module"
107+
108+
let term = Cmdliner.Term.(const link $ options)
109+
110+
let command = Cmdliner.Cmd.v info term
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
val command : unit Cmdliner.Cmd.t
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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"

compiler/bin-wasmoo_util/cmd_arg.mli renamed to compiler/bin-wasm_of_ocaml/preprocess.mli

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,11 @@ type variables =
2222
; set : (string * string) list
2323
}
2424

25-
type preprocess_options =
26-
{ input_file : string option
27-
; output_file : string option
28-
; variables : variables
29-
}
30-
31-
val preprocess_options : preprocess_options Cmdliner.Term.t
32-
33-
val preprocess_info : Cmdliner.Cmd.info
34-
35-
type binaryen_options =
36-
{ common : string list
37-
; opt : string list
38-
; merge : string list
39-
}
40-
41-
type link_options =
42-
{ input_modules : (string * string) list
43-
; output_file : string
44-
; variables : variables
45-
; binaryen_options : binaryen_options
46-
}
25+
val variable_options : variables Cmdliner.Term.t
4726

48-
val link_options : link_options Cmdliner.Term.t
27+
val set_variables :
28+
variables -> (string * Wasm_of_ocaml_compiler.Wat_preprocess.value) list
4929

50-
val link_info : Cmdliner.Cmd.info
30+
val command : unit Cmdliner.Cmd.t
5131

52-
val info : Cmdliner.Cmd.info
32+
val command_alias : unit Cmdliner.Cmd.t

compiler/bin-wasm_of_ocaml/wasm_of_ocaml.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ let () =
4848
(Cmdliner.Cmd.group
4949
~default:Compile.term
5050
(Compile.info "wasm_of_ocaml")
51-
[ Link.command; Build_runtime.command; Compile.command ])
51+
[ Link.command
52+
; Build_runtime.command
53+
; Compile.command
54+
; Preprocess.command
55+
; Preprocess.command_alias
56+
; Link_wasm.command
57+
])
5258
with
5359
| Ok (`Ok () | `Help | `Version) ->
5460
if !warnings > 0 && !werror

0 commit comments

Comments
 (0)