Skip to content

Commit 8e47ba1

Browse files
committed
Wat file preprocessor
1 parent 28f0ca6 commit 8e47ba1

File tree

14 files changed

+953
-41
lines changed

14 files changed

+953
-41
lines changed

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ let output_gen output_file f =
7373
Code.Var.set_stable (Config.Flag.stable_var ());
7474
Filename.gen_file output_file f
7575

76+
let with_runtime_files ~runtime_wasm_files f =
77+
let inputs =
78+
List.map
79+
~f:(fun file ->
80+
{ Wat_preprocess.module_name = "env"
81+
; file
82+
; source =
83+
(if Link.Wasm_binary.check_file ~file
84+
then File
85+
else Contents (Js_of_ocaml_compiler.Fs.read_file file))
86+
})
87+
runtime_wasm_files
88+
in
89+
Wat_preprocess.with_preprocessed_files ~variables:[] ~inputs f
90+
7691
let link_and_optimize
7792
~profile
7893
~sourcemap_root
@@ -100,15 +115,15 @@ let link_and_optimize
100115
then Some (Filename.temp_file "wasm-merged" ".wasm.map")
101116
else None)
102117
@@ fun opt_temp_sourcemap ->
118+
(with_runtime_files ~runtime_wasm_files
119+
@@ fun runtime_inputs ->
103120
Binaryen.link
104121
~inputs:
105-
(List.map
106-
~f:(fun file -> { Binaryen.module_name = "env"; file })
107-
(runtime_file :: runtime_wasm_files)
122+
(({ Binaryen.module_name = "env"; file = runtime_file } :: runtime_inputs)
108123
@ List.map ~f:(fun file -> { Binaryen.module_name = "OCaml"; file }) wat_files)
109124
~opt_output_sourcemap:opt_temp_sourcemap
110125
~output_file:temp_file
111-
();
126+
());
112127
Fs.with_intermediate_file (Filename.temp_file "wasm-dce" ".wasm")
113128
@@ fun temp_file' ->
114129
opt_with
@@ -141,12 +156,11 @@ let link_runtime ~profile runtime_wasm_files output_file =
141156
Fs.write_file ~name:runtime_file ~contents:Runtime_files.wasm_runtime;
142157
Fs.with_intermediate_file (Filename.temp_file "wasm-merged" ".wasm")
143158
@@ fun temp_file ->
159+
with_runtime_files ~runtime_wasm_files
160+
@@ fun runtime_inputs ->
144161
Binaryen.link
145162
~opt_output_sourcemap:None
146-
~inputs:
147-
(List.map
148-
~f:(fun file -> { Binaryen.module_name = "env"; file })
149-
(runtime_file :: runtime_wasm_files))
163+
~inputs:({ Binaryen.module_name = "env"; file = runtime_file } :: runtime_inputs)
150164
~output_file:temp_file
151165
();
152166
Binaryen.optimize

compiler/bin-wasmoo_util/cmd_arg.ml

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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)"

compiler/bin-wasmoo_util/cmd_arg.mli

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
type variables =
20+
{ enable : string list
21+
; disable : string list
22+
; set : (string * string) list
23+
}
24+
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+
}
47+
48+
val link_options : link_options Cmdliner.Term.t
49+
50+
val link_info : Cmdliner.Cmd.info
51+
52+
val info : Cmdliner.Cmd.info

compiler/bin-wasmoo_util/dune

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
(executable
2+
(name wasmoo_util)
3+
(public_name wasmoo_util)
4+
(package wasm_of_ocaml-compiler)
5+
(libraries wasm_of_ocaml-compiler jsoo_cmdline cmdliner))
6+
7+
(rule
8+
(targets wasmoo_util.1)
9+
(action
10+
(with-stdout-to
11+
%{targets}
12+
(run %{bin:wasmoo_util} --help=groff))))
13+
14+
(install
15+
(section man)
16+
(package wasm_of_ocaml-compiler)
17+
(files wasmoo_util.1))

0 commit comments

Comments
 (0)