@@ -101,6 +101,8 @@ module Line_writer : sig
101101
102102 val write : ?source : Line_reader .t -> t -> string -> unit
103103
104+ val write_lines : ?source : Line_reader .t -> t -> string -> unit
105+
104106 val lnum : t -> int
105107end = struct
106108 type t =
@@ -134,6 +136,16 @@ end = struct
134136 t.source < - source;
135137 t.lnum < - t.lnum + lnum_off
136138
139+ let write_lines ?source t lines =
140+ let l = String. split_on_char ~sep: '\n' lines in
141+ let rec w = function
142+ | [ " " ] | [] -> ()
143+ | s :: xs ->
144+ write ?source t s;
145+ w xs
146+ in
147+ w l
148+
137149 let lnum t = t.lnum
138150end
139151
@@ -231,7 +243,10 @@ end = struct
231243 build_info, units
232244end
233245
234- let link ~output ~linkall ~files ~resolve_sourcemap_url ~source_map =
246+ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source_map =
247+ (* we currently don't do anything with [toplevel]. It could be used
248+ to conditionally include link_info ?*)
249+ ignore (toplevel : bool );
235250 let t = Timer. make () in
236251 let oc = Line_writer. of_channel output in
237252 let warn_effects = ref false in
@@ -255,6 +270,7 @@ let link ~output ~linkall ~files ~resolve_sourcemap_url ~source_map =
255270 ~f: (fun (info : Unit_info.t ) (requires , to_link , all ) ->
256271 let all = StringSet. union all info.provides in
257272 if (not (Config.Flag. auto_link () ))
273+ || mklib
258274 || cmo_file
259275 || linkall
260276 || info.force_link
@@ -266,7 +282,7 @@ let link ~output ~linkall ~files ~resolve_sourcemap_url ~source_map =
266282 else requires, to_link, all))
267283 in
268284 let skip = StringSet. diff all to_link in
269- if not (StringSet. is_empty missing)
285+ if ( not (StringSet. is_empty missing)) && not mklib
270286 then
271287 failwith
272288 (Printf. sprintf
@@ -290,6 +306,8 @@ let link ~output ~linkall ~files ~resolve_sourcemap_url ~source_map =
290306 : int );
291307 sym_js := s :: ! sym_js)
292308 u.Unit_info. provides));
309+
310+ let build_info_emitted = ref false in
293311 List. iter files ~f: (fun (file , (build_info_for_file , units )) ->
294312 let sm_for_file = ref None in
295313 let ic = Line_reader. open_ file in
@@ -311,7 +329,14 @@ let link ~output ~linkall ~files ~resolve_sourcemap_url ~source_map =
311329 file
312330 line
313331 with
314- | Keep | Build_info _ -> copy ic oc
332+ | Keep -> copy ic oc
333+ | Build_info bi ->
334+ skip ic;
335+ if not ! build_info_emitted
336+ then (
337+ let bi = if mklib then Build_info. with_kind bi `Cma else bi in
338+ Line_writer. write_lines oc (Build_info. to_string bi);
339+ build_info_emitted := true )
315340 | Drop -> skip ic
316341 | Unit ->
317342 let u = Units. read ic Unit_info. empty in
@@ -327,7 +352,11 @@ let link ~output ~linkall ~files ~resolve_sourcemap_url ~source_map =
327352 then
328353 Format. eprintf
329354 " Copy %s@."
330- (String. concat ~sep: " ," (StringSet. elements u.provides)))
355+ (String. concat ~sep: " ," (StringSet. elements u.provides));
356+ if mklib
357+ then
358+ let u = if linkall then { u with force_link = true } else u in
359+ Line_writer. write_lines oc (Unit_info. to_string u))
331360 else (
332361 if debug ()
333362 then
@@ -345,7 +374,7 @@ let link ~output ~linkall ~files ~resolve_sourcemap_url ~source_map =
345374 skip ic
346375 done )
347376 | Source_map x ->
348- Line_reader. drop ic;
377+ skip ic;
349378 sm_for_file := Some x);
350379 read ()
351380 in
@@ -380,7 +409,7 @@ let link ~output ~linkall ~files ~resolve_sourcemap_url ~source_map =
380409 (Parse_bytecode.Debug. create ~include_cmis: false false )
381410 code;
382411 let content = Buffer. contents b in
383- String. split_on_char ~sep: '\n' content |> List. iter ~f: ( Line_writer. write oc) );
412+ Line_writer. write_lines oc content );
384413 (match ! sm_for_file with
385414 | None -> ()
386415 | Some x -> sm := (x, ! reloc) :: ! sm);
@@ -424,8 +453,8 @@ let link ~output ~linkall ~files ~resolve_sourcemap_url ~source_map =
424453 Line_writer. write oc s));
425454 if times () then Format. eprintf " sourcemap: %a@." Timer. print t
426455
427- let link ~output ~linkall ~files ~resolve_sourcemap_url ~source_map =
428- try link ~output ~linkall ~files ~resolve_sourcemap_url ~source_map
456+ let link ~output ~linkall ~mklib ~ toplevel ~ files ~resolve_sourcemap_url ~source_map =
457+ try link ~output ~linkall ~toplevel ~mklib ~ files ~resolve_sourcemap_url ~source_map
429458 with Build_info. Incompatible_build_info { key; first = f1 , v1 ; second = f2 , v2 } ->
430459 let string_of_v = function
431460 | None -> " <empty>"
0 commit comments