Skip to content

Commit 1242b31

Browse files
committed
Compiler: new include-partial-runtime flag
1 parent 3d09f2b commit 1242b31

File tree

12 files changed

+176
-88
lines changed

12 files changed

+176
-88
lines changed

CHANGES.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
# dev
1+
# 5.8.1 (2024-05-??) - ?
22

33
## Features/Changes
44

55
* Library: new Typed_array.Bytes module.
66

7+
## Bug fixes
8+
9+
* Compiler: fix #1509
10+
711
# 5.8.0 (2024-04-20) - Lille
812

913
## Features/Changes

compiler/bin-js_of_ocaml/build_fs.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ function jsoo_create_file_extern(name,content){
7777
Driver.f
7878
~standalone:true
7979
~wrap_with_fun:`Iife
80+
~link:`Needed
8081
pfs_fmt
8182
(Parse_bytecode.Debug.create ~include_cmis:false false)
8283
code

compiler/bin-js_of_ocaml/cmd_arg.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type t =
4646
; source_map : (string option * Source_map.t) option
4747
; runtime_files : string list
4848
; no_runtime : bool
49+
; include_partial_runtime : bool
4950
; runtime_only : bool
5051
; output_file : [ `Name of string | `Stdout ] * bool
5152
; input_file : string option
@@ -122,6 +123,12 @@ let options =
122123
let doc = "Do not include the standard runtime." in
123124
Arg.(value & flag & info [ "noruntime"; "no-runtime" ] ~doc)
124125
in
126+
let include_partial_runtime =
127+
let doc =
128+
"Include (partial) runtime when compiling cmo and cma files to JavaScript."
129+
in
130+
Arg.(value & flag & info [ "include-partial-runtime" ] ~doc)
131+
in
125132
let no_sourcemap =
126133
let doc =
127134
"Don't generate source map. All other source map related flags will be be ignored."
@@ -263,6 +270,7 @@ let options =
263270
no_cmis
264271
profile
265272
no_runtime
273+
include_partial_runtime
266274
no_sourcemap
267275
sourcemap
268276
sourcemap_inline_in_js
@@ -338,6 +346,7 @@ let options =
338346
; include_dirs
339347
; runtime_files
340348
; no_runtime
349+
; include_partial_runtime
341350
; runtime_only = false
342351
; fs_files
343352
; fs_output
@@ -367,6 +376,7 @@ let options =
367376
$ no_cmis
368377
$ profile
369378
$ noruntime
379+
$ include_partial_runtime
370380
$ no_sourcemap
371381
$ sourcemap
372382
$ sourcemap_inline_in_js
@@ -576,6 +586,7 @@ let options_runtime_only =
576586
; include_dirs
577587
; runtime_files
578588
; no_runtime
589+
; include_partial_runtime = false
579590
; runtime_only = true
580591
; fs_files
581592
; fs_output

compiler/bin-js_of_ocaml/cmd_arg.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type t =
2626
; source_map : (string option * Source_map.t) option
2727
; runtime_files : string list
2828
; no_runtime : bool
29+
; include_partial_runtime : bool
2930
; runtime_only : bool
3031
; output_file : [ `Name of string | `Stdout ] * bool
3132
; input_file : string option

compiler/bin-js_of_ocaml/compile.ml

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ let run
6868
{ Cmd_arg.common
6969
; profile
7070
; source_map
71-
; runtime_files
71+
; runtime_files = runtime_files_from_cmdline
7272
; no_runtime
7373
; input_file
7474
; output_file
@@ -87,6 +87,7 @@ let run
8787
; fs_external
8888
; export_file
8989
; keep_unit_names
90+
; include_partial_runtime
9091
} =
9192
let include_cmis = toplevel && not no_cmis in
9293
let custom_header = common.Jsoo_cmdline.Arg.custom_header in
@@ -122,8 +123,10 @@ let run
122123
if (not no_runtime) && (toplevel || dynlink)
123124
then
124125
let add_if_absent x l = if List.mem x ~set:l then l else x :: l in
125-
runtime_files |> add_if_absent "+toplevel.js" |> add_if_absent "+dynlink.js"
126-
else runtime_files
126+
runtime_files_from_cmdline
127+
|> add_if_absent "+toplevel.js"
128+
|> add_if_absent "+dynlink.js"
129+
else runtime_files_from_cmdline
127130
in
128131
let runtime_files, builtin =
129132
List.partition_map runtime_files ~f:(fun name ->
@@ -175,7 +178,7 @@ let run
175178
, noloc )
176179
])
177180
in
178-
let output (one : Parse_bytecode.one) ~standalone ~source_map ~linkall output_file =
181+
let output (one : Parse_bytecode.one) ~standalone ~source_map ~link output_file =
179182
check_debug one;
180183
let init_pseudo_fs = fs_external && standalone in
181184
let sm =
@@ -192,7 +195,7 @@ let run
192195
Driver.f
193196
~standalone
194197
?profile
195-
~linkall
198+
~link
196199
~wrap_with_fun
197200
?source_map
198201
fmt
@@ -216,7 +219,7 @@ let run
216219
Driver.f
217220
~standalone
218221
?profile
219-
~linkall
222+
~link
220223
~wrap_with_fun
221224
?source_map
222225
fmt
@@ -228,7 +231,14 @@ let run
228231
let instr = fs_instr2 in
229232
let code = Code.prepend Code.empty instr in
230233
let pfs_fmt = Pretty_print.to_out_channel chan in
231-
Driver.f' ~standalone ?profile ~wrap_with_fun pfs_fmt one.debug code));
234+
Driver.f'
235+
~standalone
236+
~link:`Needed
237+
?profile
238+
~wrap_with_fun
239+
pfs_fmt
240+
one.debug
241+
code));
232242
res
233243
in
234244
if times () then Format.eprintf "compilation: %a@." Timer.print t;
@@ -244,11 +254,14 @@ let run
244254
let uinfo = Unit_info.of_cmo cmo in
245255
Pretty_print.string fmt "\n";
246256
Pretty_print.string fmt (Unit_info.to_string uinfo);
247-
output code ~source_map ~standalone ~linkall:false output_file
257+
output code ~source_map ~standalone ~link:`No output_file
248258
in
249-
let output_runtime ~standalone ~source_map ((_, fmt) as output_file) =
259+
let output_partial_runtime ~standalone ~source_map ((_, fmt) as output_file) =
250260
assert (not standalone);
251-
let uinfo = Unit_info.of_primitives (Linker.list_all () |> StringSet.elements) in
261+
let uinfo =
262+
Unit_info.of_primitives
263+
(Linker.list_all ~from:runtime_files_from_cmdline () |> StringSet.elements)
264+
in
252265
Pretty_print.string fmt "\n";
253266
Pretty_print.string fmt (Unit_info.to_string uinfo);
254267
let code =
@@ -257,7 +270,12 @@ let run
257270
; debug = Parse_bytecode.Debug.create ~include_cmis:false false
258271
}
259272
in
260-
output code ~source_map ~standalone ~linkall:true output_file
273+
output
274+
code
275+
~source_map
276+
~standalone
277+
~link:(`All_from runtime_files_from_cmdline)
278+
output_file
261279
in
262280
(if runtime_only
263281
then (
@@ -280,7 +298,7 @@ let run
280298
(fun ~standalone ~source_map ((_, fmt) as output_file) ->
281299
Pretty_print.string fmt "\n";
282300
Pretty_print.string fmt (Unit_info.to_string uinfo);
283-
output code ~source_map ~standalone ~linkall:true output_file))
301+
output code ~source_map ~standalone ~link:`All output_file))
284302
else
285303
let kind, ic, close_ic, include_dirs =
286304
match input_file with
@@ -318,7 +336,7 @@ let run
318336
~build_info:(Build_info.create `Exe)
319337
~source_map
320338
(fst output_file)
321-
(output code ~linkall)
339+
(output code ~link:(if linkall then `All else `Needed))
322340
| `Cmo cmo ->
323341
let output_file =
324342
match output_file, keep_unit_names with
@@ -341,7 +359,6 @@ let run
341359
cmo
342360
ic
343361
in
344-
let linkall = linkall || toplevel || dynlink in
345362
if times () then Format.eprintf " parsing: %a@." Timer.print t1;
346363
output_gen
347364
~standalone:false
@@ -351,12 +368,33 @@ let run
351368
output_file
352369
(fun ~standalone ~source_map output ->
353370
let source_map =
354-
if linkall
355-
then output_runtime ~standalone ~source_map output
356-
else source_map
371+
if not include_partial_runtime
372+
then source_map
373+
else output_partial_runtime ~standalone ~source_map output
357374
in
358375
output_partial cmo code ~standalone ~source_map output)
359376
| `Cma cma when keep_unit_names ->
377+
(if include_partial_runtime
378+
then
379+
let output_file =
380+
let gen dir = Filename.concat dir "runtime.js" in
381+
match output_file with
382+
| `Stdout, false -> gen "./"
383+
| `Name x, false -> gen (Filename.dirname x)
384+
| `Name x, true
385+
when String.length x > 0 && Char.equal x.[String.length x - 1] '/' ->
386+
gen x
387+
| `Stdout, true | `Name _, true ->
388+
failwith "use [-o dirname/] or remove [--keep-unit-names]"
389+
in
390+
output_gen
391+
~standalone:false
392+
~custom_header
393+
~build_info:(Build_info.create `Runtime)
394+
~source_map
395+
(`Name output_file)
396+
(fun ~standalone ~source_map output ->
397+
output_partial_runtime ~standalone ~source_map output));
360398
List.iter cma.lib_units ~f:(fun cmo ->
361399
let output_file =
362400
match output_file with
@@ -392,10 +430,11 @@ let run
392430
(`Name output_file)
393431
(output_partial cmo code))
394432
| `Cma cma ->
395-
let linkall = linkall || toplevel || dynlink in
396433
let f ~standalone ~source_map output =
397434
let source_map =
398-
if linkall then output_runtime ~standalone ~source_map output else source_map
435+
if not include_partial_runtime
436+
then source_map
437+
else output_partial_runtime ~standalone ~source_map output
399438
in
400439
List.fold_left cma.lib_units ~init:source_map ~f:(fun source_map cmo ->
401440
let t1 = Timer.make () in

0 commit comments

Comments
 (0)