@@ -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