File: link_wasm.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 (126 lines) | stat: -rw-r--r-- 3,768 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
(* 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 Cmdliner
open Js_of_ocaml_compiler.Stdlib
open Wasm_of_ocaml_compiler

type binaryen_options =
  { common : string list
  ; opt : string list
  ; merge : string list
  }

type options =
  { input_modules : (string * string) list
  ; output_file : string
  ; variables : Preprocess.variables
  ; allowed_imports : string list option
  ; binaryen_options : binaryen_options
  }

let options =
  let input_modules =
    let doc =
      "Specify an input module with name $(i,NAME) in Wasm text file $(i,FILE)."
    in
    Arg.(
      value
      & pos_right 0 (pair ~sep:':' string string) []
      & info [] ~docv:"NAME:FILE" ~doc)
  in
  let output_file =
    let doc = "Specify the Wasm binary output file $(docv)." in
    Arg.(required & pos 0 (some string) None & info [] ~docv:"WASM_FILE" ~doc)
  in
  let allowed_imports =
    let doc = "List of modules we expect to import from." in
    Arg.(
      value
      & opt_all (list ~sep:',' string) []
      & info [ "allowed-imports" ] ~docv:"IMPORT" ~doc)
  in
  let binaryen_options =
    let doc = "Pass option $(docv) to binaryen tools" in
    Arg.(value & opt_all string [] & info [ "binaryen" ] ~docv:"OPT" ~doc)
  in
  let opt_options =
    let doc = "Pass option $(docv) to $(b,wasm-opt)" in
    Arg.(value & opt_all string [] & info [ "binaryen-opt" ] ~docv:"OPT" ~doc)
  in
  let merge_options =
    let doc = "Pass option $(docv) to $(b,wasm-merge)" in
    Arg.(value & opt_all string [] & info [ "binaryen-merge" ] ~docv:"OPT" ~doc)
  in
  let build_t input_modules output_file variables allowed_imports common opt merge =
    let allowed_imports =
      if List.is_empty allowed_imports then None else Some (List.concat allowed_imports)
    in
    `Ok
      { input_modules
      ; output_file
      ; variables
      ; allowed_imports
      ; binaryen_options = { common; opt; merge }
      }
  in
  let t =
    Term.(
      const build_t
      $ input_modules
      $ output_file
      $ Preprocess.variable_options
      $ allowed_imports
      $ binaryen_options
      $ opt_options
      $ merge_options)
  in
  Term.ret t

let link
    { input_modules
    ; output_file
    ; variables
    ; allowed_imports
    ; binaryen_options = { common; merge; opt }
    } =
  let inputs =
    List.map
      ~f:(fun (module_name, file) -> { Wat_preprocess.module_name; file; source = File })
      input_modules
  in
  Runtime.build
    ~allowed_imports
    ~link_options:(common @ merge)
    ~opt_options:(common @ opt)
    ~variables:(Preprocess.set_variables variables)
    ~inputs
    ~output_file

let info =
  Info.make
    ~name:"link-wasm"
    ~doc:"Wasm linker"
    ~description:
      "$(b,wasmoo_link_wasm) is a Wasm linker. It takes as input a list of Wasm text \
       files, preprocesses them, links them together, and outputs a single Wasm binary \
       module"

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

let command = Cmdliner.Cmd.v info term