File: compile.ml

package info (click to toggle)
js-of-ocaml 6.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,932 kB
  • sloc: ml: 135,957; javascript: 58,364; ansic: 437; makefile: 422; sh: 12; perl: 4
file content (715 lines) | stat: -rw-r--r-- 24,610 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
(* Wasm_of_ocaml compiler
 * http://www.ocsigen.org/js_of_ocaml/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, with linking exception;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *)

open! Js_of_ocaml_compiler.Stdlib
open Js_of_ocaml_compiler
open Wasm_of_ocaml_compiler

let times = Debug.find "times"

let binaryen_times = Debug.find "binaryen-times"

let debug_mem = Debug.find "mem"

let debug_wat = Debug.find "wat"

let () = Sys.catch_break true

let update_sourcemap ~sourcemap_root ~sourcemap_don't_inline_content sourcemap_file =
  if Option.is_some sourcemap_root || not sourcemap_don't_inline_content
  then (
    let open Source_map in
    let source_map =
      match Source_map.of_file sourcemap_file with
      | Index _ -> assert false
      | Standard sm -> sm
    in
    assert (List.is_empty (Option.value source_map.sources_content ~default:[]));
    (* Add source file contents to source map *)
    let sources_content =
      if sourcemap_don't_inline_content
      then None
      else
        Some
          (List.map source_map.sources ~f:(fun file ->
               if String.equal file Wasm_source_map.blackbox_filename
               then
                 Some (Source_map.Source_content.create Wasm_source_map.blackbox_contents)
               else if Sys.file_exists file && not (Sys.is_directory file)
               then Some (Source_map.Source_content.create (Fs.read_file file))
               else None))
    in
    let source_map =
      { source_map with
        sources_content
      ; sourceroot =
          (if Option.is_some sourcemap_root then sourcemap_root else source_map.sourceroot)
      ; ignore_list =
          (if
             List.mem
               ~eq:String.equal
               Wasm_source_map.blackbox_filename
               source_map.sources
           then [ Wasm_source_map.blackbox_filename ]
           else [])
      }
    in
    Source_map.to_file (Standard source_map) sourcemap_file)

let opt_with action x f =
  match x with
  | None -> f None
  | Some x -> action x (fun y -> f (Some y))

let preprocessor_variables () =
  (* Keep this variables in sync with gen/gen.ml *)
  [ ( "effects"
    , Wat_preprocess.String
        (match Config.effects () with
        | `Disabled | `Jspi -> "jspi"
        | `Cps -> "cps"
        | `Double_translation -> assert false) )
  ]

let with_runtime_files ~runtime_wasm_files f =
  let inputs =
    List.map
      ~f:(fun file -> { Wat_preprocess.module_name = "env"; file; source = File })
      runtime_wasm_files
  in
  Wat_preprocess.with_preprocessed_files ~variables:(preprocessor_variables ()) ~inputs f

let build_runtime ~runtime_file =
  let variables = preprocessor_variables () in
  match
    List.find_opt Runtime_files.precompiled_runtimes ~f:(fun (flags, _) ->
        assert (List.length flags = List.length variables);
        List.equal
          ~eq:(fun (k1, v1) (k2, v2) ->
            assert (String.equal k1 k2);
            Wat_preprocess.value_equal v1 v2)
          flags
          variables)
  with
  | Some (_, contents) -> Fs.write_file ~name:runtime_file ~contents
  | None ->
      let inputs =
        List.map
          ~f:(fun (module_name, contents) ->
            { Wat_preprocess.module_name
            ; file = module_name ^ ".wat"
            ; source = Contents contents
            })
          Runtime_files.wat_files
      in
      Runtime.build
        ~link_options:[ "-g" ]
        ~opt_options:[ "-g"; "-O2" ]
        ~variables
        ~allowed_imports:
          (Some
             [ "bindings"
             ; "Math"
             ; "js"
             ; "wasm:js-string"
             ; "wasm:text-encoder"
             ; "wasm:text-decoder"
             ])
        ~inputs
        ~output_file:runtime_file

let link_and_optimize
    ~profile
    ~sourcemap_root
    ~sourcemap_don't_inline_content
    ~opt_sourcemap
    runtime_wasm_files
    wat_files
    output_file =
  let opt_sourcemap_file =
    (* Check that Binaryen supports the necessary sourcemaps options (requires
       version >= 118) *)
    match opt_sourcemap with
    | Some _ when Sys.command "wasm-merge -osm foo 2> /dev/null" <> 0 -> None
    | Some _ | None -> opt_sourcemap
  in
  let enable_source_maps = Option.is_some opt_sourcemap_file in
  Fs.with_intermediate_file (Filename.temp_file "runtime" ".wasm")
  @@ fun runtime_file ->
  build_runtime ~runtime_file;
  Fs.with_intermediate_file (Filename.temp_file "wasm-merged" ".wasm")
  @@ fun temp_file ->
  opt_with
    Fs.with_intermediate_file
    (if enable_source_maps
     then Some (Filename.temp_file "wasm-merged" ".wasm.map")
     else None)
  @@ fun opt_temp_sourcemap ->
  (with_runtime_files ~runtime_wasm_files
  @@ fun runtime_inputs ->
  let t = Timer.make ~get_time:Unix.time () in
  Binaryen.link
    ~inputs:
      ({ Binaryen.module_name = "env"; file = runtime_file; source_map_file = None }
       :: runtime_inputs
      @ List.map
          ~f:(fun (file, source_map_file) ->
            { Binaryen.module_name = "OCaml"; file; source_map_file })
          wat_files)
    ~opt_output_sourcemap:opt_temp_sourcemap
    ~output_file:temp_file
    ();
  if binaryen_times () then Format.eprintf "  binaryen link: %a@." Timer.print t);

  Fs.with_intermediate_file (Filename.temp_file "wasm-dce" ".wasm")
  @@ fun temp_file' ->
  opt_with
    Fs.with_intermediate_file
    (if enable_source_maps then Some (Filename.temp_file "wasm-dce" ".wasm.map") else None)
  @@ fun opt_temp_sourcemap' ->
  let t = Timer.make ~get_time:Unix.time () in
  let primitives =
    Binaryen.dead_code_elimination
      ~dependencies:Runtime_files.dependencies
      ~opt_input_sourcemap:opt_temp_sourcemap
      ~opt_output_sourcemap:opt_temp_sourcemap'
      ~input_file:temp_file
      ~output_file:temp_file'
  in
  if binaryen_times () then Format.eprintf "  binaryen dce: %a@." Timer.print t;
  let t = Timer.make ~get_time:Unix.time () in
  Binaryen.optimize
    ~profile
    ~opt_input_sourcemap:opt_temp_sourcemap'
    ~opt_output_sourcemap:opt_sourcemap
    ~input_file:temp_file'
    ~output_file
    ();
  if binaryen_times () then Format.eprintf "  binaryen opt: %a@." Timer.print t;
  Option.iter
    ~f:(update_sourcemap ~sourcemap_root ~sourcemap_don't_inline_content)
    opt_sourcemap_file;
  primitives

let link_runtime ~profile runtime_wasm_files output_file =
  if List.is_empty runtime_wasm_files
  then build_runtime ~runtime_file:output_file
  else
    Fs.with_intermediate_file (Filename.temp_file "extra_runtime" ".wasm")
    @@ fun extra_runtime ->
    Fs.with_intermediate_file (Filename.temp_file "merged_runtime" ".wasm")
    @@ fun temp_file ->
    (with_runtime_files ~runtime_wasm_files
    @@ fun runtime_inputs ->
    Binaryen.link
      ~opt_output_sourcemap:None
      ~inputs:runtime_inputs
      ~output_file:temp_file
      ());
    Binaryen.optimize
      ~profile
      ~opt_input_sourcemap:None
      ~opt_output_sourcemap:None
      ~input_file:temp_file
      ~output_file:extra_runtime
      ();
    Fs.with_intermediate_file (Filename.temp_file "runtime" ".wasm")
    @@ fun runtime_file ->
    build_runtime ~runtime_file;
    Binaryen.link
      ~opt_output_sourcemap:None
      ~inputs:
        (List.map
           ~f:(fun file -> { Binaryen.module_name = "env"; file; source_map_file = None })
           [ runtime_file; extra_runtime ])
      ~output_file
      ()

let generate_prelude ~out_file =
  Filename.gen_file out_file
  @@ fun ch ->
  let code, uinfo = Parse_bytecode.predefined_exceptions () in
  let profile = Profile.O1 in
  let ( Driver.
          { program
          ; variable_uses
          ; in_cps
          ; deadcode_sentinal
          ; shapes = _
          ; trampolined_calls = _
          }
      , global_flow_data ) =
    Driver.optimize_for_wasm ~profile ~shapes:false code
  in
  let context = Generate.start () in
  let _ =
    Generate.f
      ~context
      ~unit_name:(Some "prelude")
      ~live_vars:variable_uses
      ~in_cps
      ~deadcode_sentinal
      ~global_flow_data
      program
  in
  Generate.wasm_output ch ~opt_source_map_file:None ~context;
  uinfo.provides

let build_prelude z =
  Fs.with_intermediate_file (Filename.temp_file "prelude" ".wasm")
  @@ fun prelude_file ->
  let predefined_exceptions = generate_prelude ~out_file:prelude_file in
  Zip.add_file z ~name:"prelude.wasm" ~file:prelude_file;
  predefined_exceptions

let build_js_runtime ~primitives ?runtime_arguments () =
  let always_required_js, primitives =
    let l =
      StringSet.fold
        (fun nm l ->
          let id = Utf8_string.of_string_exn nm in
          Javascript.Property (PNI id, EVar (S { name = id; var = None; loc = N })) :: l)
        primitives
        []
    in
    match
      List.split_last
      @@ Driver.link_and_pack
           ~link:`Needed
           [ Javascript.Return_statement (Some (EObj l), N), N ]
    with
    | Some x -> x
    | None -> assert false
  in
  let primitives =
    match primitives with
    | Javascript.Expression_statement e, N -> e
    | _ -> assert false
  in
  let init_fun =
    match Parse_js.parse (Parse_js.Lexer.of_string Runtime_files.js_runtime) with
    | [ (Expression_statement f, _) ] -> f
    | _ -> assert false
  in
  let launcher =
    let js = Javascript.call init_fun [ primitives ] N in
    let js =
      match runtime_arguments with
      | None -> js
      | Some runtime_arguments -> Javascript.call js [ runtime_arguments ] N
    in
    [ Javascript.Expression_statement js, Javascript.N ]
  in
  Link.output_js (always_required_js @ launcher)

let add_source_map sourcemap_don't_inline_content z opt_source_map =
  let sm =
    match opt_source_map with
    | `File opt_file -> Option.map ~f:Source_map.of_file opt_file
    | `Source_map sm -> Some sm
  in
  Option.iter sm ~f:(fun sm ->
      Zip.add_entry z ~name:"source_map.map" ~contents:(Source_map.to_string sm);
      if not sourcemap_don't_inline_content
      then
        Wasm_source_map.iter_sources sm (fun i j file ->
            if Sys.file_exists file && not (Sys.is_directory file)
            then
              let sm = Fs.read_file file in
              Zip.add_entry
                z
                ~name:(Link.source_name i j file)
                ~contents:(Yojson.Basic.to_string (`String sm))))

let merge_shape a b =
  StringMap.union (fun _name s1 s2 -> if Shape.equal s1 s2 then Some s1 else None) a b

let sexp_of_shapes s =
  StringMap.bindings s
  |> List.map ~f:(fun (name, shape) ->
         Sexp.List [ Atom name; Atom (Shape.to_string shape) ])

let string_of_shapes s = Sexp.List (sexp_of_shapes s) |> Sexp.to_string

let run
    { Cmd_arg.common
    ; profile
    ; runtime_only
    ; runtime_files
    ; input_file
    ; output_file
    ; enable_source_maps
    ; params
    ; include_dirs
    ; sourcemap_root
    ; sourcemap_don't_inline_content
    ; effects
    ; shape_files
    } =
  Config.set_target `Wasm;
  Jsoo_cmdline.Arg.eval common;
  Config.set_effects_backend effects;
  Generate.init ();
  List.iter shape_files ~f:(fun s ->
      let z = Zip.open_in s in
      if Zip.has_entry z ~name:"shapes.sexp"
      then
        let s = Zip.read_entry z ~name:"shapes.sexp" in
        match Sexp.from_string s with
        | List l ->
            List.iter l ~f:(function
              | Sexp.List [ Atom name; Atom shape ] ->
                  Shape.Store.set ~name (Shape.of_string shape)
              | _ -> ())
        | _ -> ());
  let output_file = fst output_file in
  if debug_mem () then Debug.start_profiling output_file;
  List.iter params ~f:(fun (s, v) -> Config.Param.set s v);
  let t = Timer.make () in
  let include_dirs =
    List.filter_map (include_dirs @ [ "+stdlib/" ]) ~f:(fun d -> Findlib.find [] d)
  in
  let runtime_wasm_files, runtime_js_files =
    List.partition runtime_files ~f:(fun name ->
        List.exists
          ~f:(fun s -> Filename.check_suffix name s)
          [ ".wasm"; ".wat"; ".wast" ])
  in
  let runtime_js_files, builtin =
    List.partition_map runtime_js_files ~f:(fun name ->
        match Builtins.find name with
        | Some t -> Right t
        | None -> Left name)
  in
  let t1 = Timer.make () in
  let builtin = Js_of_ocaml_compiler_runtime_files.runtime @ builtin in
  List.iter builtin ~f:(fun t ->
      let filename = Builtins.File.name t in
      let runtimes = Linker.Fragment.parse_builtin t in
      Linker.load_fragments ~target_env:Target_env.Isomorphic ~filename runtimes);
  Linker.load_files ~target_env:Target_env.Isomorphic runtime_js_files;
  Linker.check_deps ();
  if times () then Format.eprintf "  parsing js: %a@." Timer.print t1;
  if times () then Format.eprintf "Start parsing...@.";
  let need_debug = enable_source_maps || Config.Flag.debuginfo () in
  let check_debug (one : Parse_bytecode.one) =
    if
      (not runtime_only)
      && enable_source_maps
      && Parse_bytecode.Debug.is_empty one.debug
      && not (Code.is_empty one.code)
    then
      Warning.warn
        `Missing_debug_event
        "'--source-map' is enabled but the bytecode program was compiled with no \
         debugging information.\n\
         Warning: Consider passing '-g' option to ocamlc.\n\
         %!"
  in
  let profile =
    match profile with
    | Some p -> p
    | None -> Profile.O1
  in
  let output (one : Parse_bytecode.one) ~unit_name ~wat_file ~file ~opt_source_map_file =
    check_debug one;
    let code = one.code in
    let standalone = Option.is_none unit_name in
    let ( Driver.
            { program
            ; variable_uses
            ; in_cps
            ; deadcode_sentinal
            ; shapes
            ; trampolined_calls = _
            }
        , global_flow_data ) =
      Driver.optimize_for_wasm ~profile ~shapes:true code
    in
    StringMap.iter (fun name shape -> Shape.Store.set ~name shape) shapes;
    let context = Generate.start () in
    let toplevel_name, generated_js =
      Generate.f
        ~context
        ~unit_name
        ~live_vars:variable_uses
        ~in_cps
        ~deadcode_sentinal
        ~global_flow_data
        program
    in
    if standalone then Generate.add_start_function ~context toplevel_name;
    let ch = open_out_bin file in
    Generate.wasm_output ch ~opt_source_map_file ~context;
    close_out ch;
    if debug_wat ()
    then (
      let ch = open_out_bin wat_file in
      Generate.output ch ~context;
      close_out ch);
    if times () then Format.eprintf "compilation: %a@." Timer.print t;
    generated_js, shapes
  in
  (if runtime_only
   then (
     Fs.gen_file output_file
     @@ fun tmp_output_file ->
     Fs.with_intermediate_file (Filename.temp_file "wasm" ".wasm")
     @@ fun tmp_wasm_file ->
     link_runtime ~profile runtime_wasm_files tmp_wasm_file;
     let primitives =
       tmp_wasm_file
       |> (fun file -> Link.Wasm_binary.read_imports ~file)
       |> List.filter_map ~f:(fun { Link.Wasm_binary.module_; name; _ } ->
              if String.equal module_ "js" then Some name else None)
       |> StringSet.of_list
     in
     let js_runtime = build_js_runtime ~primitives () in
     let z = Zip.open_out tmp_output_file in
     Zip.add_file z ~name:"runtime.wasm" ~file:tmp_wasm_file;
     Zip.add_entry z ~name:"runtime.js" ~contents:js_runtime;
     let predefined_exceptions = build_prelude z in
     Link.add_info
       z
       ~predefined_exceptions
       ~build_info:(Build_info.create `Runtime)
       ~unit_data:[]
       ();
     Zip.close_out z)
   else
     let kind, ic, close_ic, include_dirs =
       let input_file =
         match input_file with
         | None -> assert false
         | Some f -> f
       in
       let ch = open_in_bin input_file in
       let res = Parse_bytecode.from_channel ch in
       let include_dirs = Filename.dirname input_file :: include_dirs in
       res, ch, (fun () -> close_in ch), include_dirs
     in
     let compile_cmo cmo cont =
       let t1 = Timer.make () in
       let code =
         Parse_bytecode.from_cmo ~includes:include_dirs ~debug:need_debug cmo ic
       in
       let unit_info = Unit_info.of_cmo cmo in
       let unit_name = Ocaml_compiler.Cmo_format.name cmo in
       if times () then Format.eprintf "  parsing: %a (%s)@." Timer.print t1 unit_name;
       Fs.with_intermediate_file (Filename.temp_file unit_name ".wasm")
       @@ fun tmp_wasm_file ->
       opt_with
         Fs.with_intermediate_file
         (if enable_source_maps
          then Some (Filename.temp_file unit_name ".wasm.map")
          else None)
       @@ fun opt_tmp_map_file ->
       let unit_data, shapes =
         Fs.with_intermediate_file (Filename.temp_file unit_name ".wasm")
         @@ fun input_file ->
         opt_with
           Fs.with_intermediate_file
           (if enable_source_maps
            then Some (Filename.temp_file unit_name ".wasm.map")
            else None)
         @@ fun opt_input_sourcemap ->
         let fragments, shapes =
           output
             code
             ~wat_file:
               (Filename.concat (Filename.dirname output_file) (unit_name ^ ".wat"))
             ~unit_name:(Some unit_name)
             ~file:input_file
             ~opt_source_map_file:opt_input_sourcemap
         in
         Binaryen.optimize
           ~profile
           ~opt_input_sourcemap
           ~opt_output_sourcemap:opt_tmp_map_file
           ~input_file
           ~output_file:tmp_wasm_file
           ();
         { Link.unit_name; unit_info; fragments }, shapes
       in
       cont unit_data unit_name tmp_wasm_file opt_tmp_map_file shapes
     in
     (match kind with
     | `Exe ->
         let t1 = Timer.make () in
         let code =
           Parse_bytecode.from_exe
             ~includes:include_dirs
             ~include_cmis:false
             ~link_info:false
             ~linkall:false
             ~debug:need_debug
             ic
         in
         if times () then Format.eprintf "  parsing: %a@." Timer.print t1;
         Fs.with_intermediate_file (Filename.temp_file "code" ".wasm")
         @@ fun input_wasm_file ->
         let dir = Filename.chop_extension output_file ^ ".assets" in
         Link.gen_dir dir
         @@ fun tmp_dir ->
         Sys.mkdir tmp_dir 0o777;
         let opt_sourcemap =
           if enable_source_maps
           then Some (Filename.concat tmp_dir "code.wasm.map")
           else None
         in
         let opt_source_map_file =
           if enable_source_maps
           then Some (Filename.temp_file "code" ".wasm.map")
           else None
         in
         let generated_js, _shapes =
           output
             code
             ~unit_name:None
             ~wat_file:(Filename.chop_extension output_file ^ ".wat")
             ~file:input_wasm_file
             ~opt_source_map_file
         in
         let tmp_wasm_file = Filename.concat tmp_dir "code.wasm" in
         let t2 = Timer.make ~get_time:Unix.time () in
         let primitives =
           link_and_optimize
             ~profile
             ~sourcemap_root
             ~sourcemap_don't_inline_content
             ~opt_sourcemap
             runtime_wasm_files
             [ input_wasm_file, opt_source_map_file ]
             tmp_wasm_file
         in
         if binaryen_times ()
         then Format.eprintf " link_and_optimize: %a@." Timer.print t2;
         let wasm_name =
           Printf.sprintf
             "code-%s"
             (String.sub (Digest.to_hex (Digest.file tmp_wasm_file)) ~pos:0 ~len:20)
         in
         let tmp_wasm_file' = Filename.concat tmp_dir (wasm_name ^ ".wasm") in
         Sys.rename tmp_wasm_file tmp_wasm_file';
         if enable_source_maps
         then (
           Sys.rename (Filename.concat tmp_dir "code.wasm.map") (tmp_wasm_file' ^ ".map");
           Link.Wasm_binary.append_source_map_section
             ~file:tmp_wasm_file'
             ~url:(wasm_name ^ ".wasm.map"));
         if times () then Format.eprintf "Start building js runtime@.";
         let js_runtime =
           let missing_primitives =
             let l = Link.Wasm_binary.read_imports ~file:tmp_wasm_file' in
             List.filter_map
               ~f:(fun { Link.Wasm_binary.module_; name; _ } ->
                 if String.equal module_ "env" then Some name else None)
               l
           in
           build_js_runtime
             ~primitives
             ~runtime_arguments:
               (Link.build_runtime_arguments
                  ~missing_primitives
                  ~wasm_dir:dir
                  ~link_spec:[ wasm_name, None ]
                  ~separate_compilation:false
                  ~generated_js:[ None, generated_js ]
                  ())
             ()
         in
         Fs.gen_file output_file
         @@ fun tmp_output_file ->
         Fs.write_file ~name:tmp_output_file ~contents:js_runtime
     | `Cmo cmo ->
         Fs.gen_file output_file
         @@ fun tmp_output_file ->
         let z = Zip.open_out tmp_output_file in
         let compile_cmo' z cmo =
           compile_cmo cmo (fun unit_data _ tmp_wasm_file opt_tmp_map_file shapes ->
               Zip.add_file z ~name:"code.wasm" ~file:tmp_wasm_file;
               Zip.add_entry z ~name:"shapes.sexp" ~contents:(string_of_shapes shapes);
               add_source_map sourcemap_don't_inline_content z (`File opt_tmp_map_file);
               unit_data)
         in
         let unit_data = [ compile_cmo' z cmo ] in
         Link.add_info z ~build_info:(Build_info.create `Cmo) ~unit_data ();
         Zip.close_out z
     | `Cma cma ->
         Fs.gen_file output_file
         @@ fun tmp_output_file ->
         let z = Zip.open_out tmp_output_file in
         let unit_data =
           let tmp_buf = Buffer.create 10000 in
           List.fold_right
             ~f:(fun cmo cont l ->
               compile_cmo cmo
               @@ fun unit_data unit_name tmp_wasm_file opt_tmp_map_file shapes ->
               cont ((unit_data, unit_name, tmp_wasm_file, opt_tmp_map_file, shapes) :: l))
             cma.lib_units
             ~init:(fun l ->
               Fs.with_intermediate_file (Filename.temp_file "wasm" ".wasm")
               @@ fun tmp_wasm_file ->
               let l = List.rev l in
               let source_map =
                 Wasm_link.f
                   (List.map
                      ~f:(fun (_, _, file, opt_source_map, _) ->
                        { Wasm_link.module_name = "OCaml"
                        ; file
                        ; code = None
                        ; opt_source_map =
                            Option.map
                              ~f:(fun f -> Source_map.Standard.of_file ~tmp_buf f)
                              opt_source_map
                        })
                      l)
                   ~output_file:tmp_wasm_file
               in
               Zip.add_file z ~name:"code.wasm" ~file:tmp_wasm_file;
               let shapes =
                 List.fold_left
                   ~init:StringMap.empty
                   ~f:(fun acc (_, _, _, _, shapes) -> merge_shape acc shapes)
                   l
               in
               Zip.add_entry z ~name:"shapes.sexp" ~contents:(string_of_shapes shapes);
               if enable_source_maps
               then
                 add_source_map sourcemap_don't_inline_content z (`Source_map source_map);
               List.map ~f:(fun (unit_data, _, _, _, _) -> unit_data) l)
             []
         in
         Link.add_info z ~build_info:(Build_info.create `Cma) ~unit_data ();
         Zip.close_out z);
     close_ic ());
  Debug.stop_profiling ()

let info name =
  Info.make
    ~name
    ~doc:"Wasm_of_ocaml compiler"
    ~description:"Wasm_of_ocaml is a compiler from OCaml bytecode to WebAssembly."

let options = Cmd_arg.options ()

let term = Cmdliner.Term.(const run $ options)

let command =
  let t = Cmdliner.Term.(const run $ options) in
  Cmdliner.Cmd.v (info "compile") t