Skip to content

Commit 28f0ca6

Browse files
committed
More flexible API to call binaryen tools
1 parent 8d84e4d commit 28f0ca6

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,14 @@ let link_and_optimize
101101
else None)
102102
@@ fun opt_temp_sourcemap ->
103103
Binaryen.link
104-
~runtime_files:(runtime_file :: runtime_wasm_files)
105-
~input_files:wat_files
104+
~inputs:
105+
(List.map
106+
~f:(fun file -> { Binaryen.module_name = "env"; file })
107+
(runtime_file :: runtime_wasm_files)
108+
@ List.map ~f:(fun file -> { Binaryen.module_name = "OCaml"; file }) wat_files)
106109
~opt_output_sourcemap:opt_temp_sourcemap
107-
~output_file:temp_file;
110+
~output_file:temp_file
111+
();
108112
Fs.with_intermediate_file (Filename.temp_file "wasm-dce" ".wasm")
109113
@@ fun temp_file' ->
110114
opt_with
@@ -124,7 +128,8 @@ let link_and_optimize
124128
~opt_input_sourcemap:opt_temp_sourcemap'
125129
~opt_output_sourcemap:opt_sourcemap
126130
~input_file:temp_file'
127-
~output_file;
131+
~output_file
132+
();
128133
Option.iter
129134
~f:(update_sourcemap ~sourcemap_root ~sourcemap_don't_inline_content)
130135
opt_sourcemap_file;
@@ -138,15 +143,19 @@ let link_runtime ~profile runtime_wasm_files output_file =
138143
@@ fun temp_file ->
139144
Binaryen.link
140145
~opt_output_sourcemap:None
141-
~runtime_files:(runtime_file :: runtime_wasm_files)
142-
~input_files:[]
143-
~output_file:temp_file;
146+
~inputs:
147+
(List.map
148+
~f:(fun file -> { Binaryen.module_name = "env"; file })
149+
(runtime_file :: runtime_wasm_files))
150+
~output_file:temp_file
151+
();
144152
Binaryen.optimize
145153
~profile
146154
~opt_input_sourcemap:None
147155
~opt_output_sourcemap:None
148156
~input_file:temp_file
149157
~output_file
158+
()
150159

151160
let generate_prelude ~out_file =
152161
Filename.gen_file out_file
@@ -186,7 +195,8 @@ let build_prelude z =
186195
~input_file:prelude_file
187196
~output_file:tmp_prelude_file
188197
~opt_input_sourcemap:None
189-
~opt_output_sourcemap:None;
198+
~opt_output_sourcemap:None
199+
();
190200
Zip.add_file z ~name:"prelude.wasm" ~file:tmp_prelude_file;
191201
predefined_exceptions
192202

@@ -413,7 +423,8 @@ let run
413423
~opt_input_sourcemap:None
414424
~opt_output_sourcemap:opt_tmp_map_file
415425
~input_file:wat_file
416-
~output_file:tmp_wasm_file;
426+
~output_file:tmp_wasm_file
427+
();
417428
{ Link.unit_name; unit_info; strings; fragments }
418429
in
419430
cont unit_data unit_name tmp_wasm_file opt_tmp_map_file

compiler/lib-wasm/binaryen.ml

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,20 @@ let opt_flag flag v =
4545
| None -> []
4646
| Some v -> [ flag; Filename.quote v ]
4747

48-
let link ~runtime_files ~input_files ~opt_output_sourcemap ~output_file =
48+
type link_input =
49+
{ module_name : string
50+
; file : string
51+
}
52+
53+
let link ?options ~inputs ~opt_output_sourcemap ~output_file () =
4954
command
5055
("wasm-merge"
5156
:: (common_options ()
57+
@ Option.value ~default:[] options
5258
@ List.flatten
5359
(List.map
54-
~f:(fun runtime_file -> [ Filename.quote runtime_file; "env" ])
55-
runtime_files)
56-
@ List.flatten
57-
(List.map
58-
~f:(fun input_file -> [ Filename.quote input_file; "OCaml" ])
59-
input_files)
60+
~f:(fun { file; module_name } -> [ Filename.quote file; module_name ])
61+
inputs)
6062
@ [ "-o"; Filename.quote output_file ]
6163
@ opt_flag "--output-source-map" opt_output_sourcemap))
6264

@@ -114,8 +116,14 @@ let optimization_options =
114116
; [ "-O3"; "--skip-pass=inlining-optimizing"; "--traps-never-happen" ]
115117
|]
116118

117-
let optimize ~profile ~opt_input_sourcemap ~input_file ~opt_output_sourcemap ~output_file
118-
=
119+
let optimize
120+
~profile
121+
?options
122+
~opt_input_sourcemap
123+
~input_file
124+
~opt_output_sourcemap
125+
~output_file
126+
() =
119127
let level =
120128
match profile with
121129
| None -> 1
@@ -124,7 +132,7 @@ let optimize ~profile ~opt_input_sourcemap ~input_file ~opt_output_sourcemap ~ou
124132
command
125133
("wasm-opt"
126134
:: (common_options ()
127-
@ optimization_options.(level - 1)
135+
@ Option.value ~default:optimization_options.(level - 1) options
128136
@ [ Filename.quote input_file; "-o"; Filename.quote output_file ])
129137
@ opt_flag "--input-source-map" opt_input_sourcemap
130138
@ opt_flag "--output-source-map" opt_output_sourcemap)

compiler/lib-wasm/binaryen.mli

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@
1616
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1717
*)
1818

19+
type link_input =
20+
{ module_name : string
21+
; file : string
22+
}
23+
1924
val link :
20-
runtime_files:string list
21-
-> input_files:string list
25+
?options:string list
26+
-> inputs:link_input list
2227
-> opt_output_sourcemap:string option
2328
-> output_file:string
2429
-> unit
30+
-> unit
2531

2632
val dead_code_elimination :
2733
dependencies:string
@@ -33,8 +39,10 @@ val dead_code_elimination :
3339

3440
val optimize :
3541
profile:Driver.profile option
42+
-> ?options:string list
3643
-> opt_input_sourcemap:string option
3744
-> input_file:string
3845
-> opt_output_sourcemap:string option
3946
-> output_file:string
4047
-> unit
48+
-> unit

0 commit comments

Comments
 (0)