Source file build_info.ml
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
open! Stdlib
type kind =
  [ `Runtime
  | `Exe
  | `Cmo
  | `Cma
  | `Unknown
  ]
let all = [ `Runtime; `Exe; `Cmo; `Cma; `Unknown ]
let string_of_kind = function
  | `Runtime -> "runtime"
  | `Exe -> "exe"
  | `Cmo -> "cmo"
  | `Cma -> "cma"
  | `Unknown -> "unknown"
let string_of_effects_backend : Config.effects_backend -> string = function
  | `Disabled -> "disabled"
  | `Cps -> "cps"
  | `Double_translation -> "double-translation"
  | `Jspi -> "jspi"
let effects_backend_of_string = function
  | "disabled" -> `Disabled
  | "cps" -> `Cps
  | "double-translation" -> `Double_translation
  | "jspi" -> `Jspi
  | _ -> invalid_arg "effects_backend_of_string"
let kind_of_string s =
  match List.find_opt all ~f:(fun k -> String.equal s (string_of_kind k)) with
  | None -> `Unknown
  | Some k -> k
type t = string StringMap.t
let kind t =
  match StringMap.find "kind" t with
  | exception Not_found -> `Unknown
  | s -> kind_of_string s
let create kind =
  let version =
    match Compiler_version.git_version with
    | "" -> Compiler_version.s
    | v -> Printf.sprintf "%s+%s" Compiler_version.s v
  in
  [ "use-js-string", string_of_bool (Config.Flag.use_js_string ())
  ; "effects", string_of_effects_backend (Config.effects ())
  ; "version", version
  ; "kind", string_of_kind kind
  ]
  |> List.fold_left ~init:StringMap.empty ~f:(fun acc (k, v) -> StringMap.add k v acc)
let with_kind t kind = StringMap.add "kind" (string_of_kind kind) t
let prefix = "//# buildInfo:"
let to_string info =
  let str =
    StringMap.bindings info
    |> List.map ~f:(fun (k, v) -> Printf.sprintf "%s=%s" k v)
    |> String.concat ~sep:", "
  in
  Printf.sprintf "%s%s\n" prefix str
let parse s =
  match String.drop_prefix ~prefix s with
  | None -> None
  | Some suffix ->
      let t =
        suffix
        |> String.split_on_char ~sep:','
        |> List.map ~f:String.trim
        |> List.map ~f:(fun s ->
               match String.lsplit2 ~on:'=' s with
               | None -> s, ""
               | Some (k, v) -> k, v)
        |> List.fold_left ~init:StringMap.empty ~f:(fun acc (k, v) ->
               StringMap.add k v acc)
      in
      Some t
let to_map : t -> string StringMap.t = Fun.id
let of_map : string StringMap.t -> t = Fun.id
exception
  Incompatible_build_info of
    { key : string
    ; first : (string * string option)
    ; second : (string * string option)
    }
let merge fname1 info1 fname2 info2 =
  if String.equal fname1 fname2
  then
    StringMap.merge
      (fun k v1 v2 ->
        match v1, v2 with
        | Some v1, Some v2 when String.equal v1 v2 -> Some v1
        | Some v1, Some v2 ->
            failwith
              (Printf.sprintf
                 "%s: Duplicated build info with incompatible value. key=%s, v1=%s, v2=%s"
                 fname1
                 k
                 v1
                 v2)
        | Some x, None | None, Some x -> Some x
        | None, None -> assert false)
      info1
      info2
  else
    StringMap.merge
      (fun k v1 v2 ->
        match k, v1, v2 with
        | "kind", v1, v2 ->
            if Option.equal String.equal v1 v2 then v1 else Some (string_of_kind `Unknown)
        | ("effects" | "use-js-string" | "version"), Some v1, Some v2
          when String.equal v1 v2 -> Some v1
        | (("effects" | "use-js-string" | "version") as key), v1, v2 ->
            raise
              (Incompatible_build_info { key; first = fname1, v1; second = fname2, v2 })
        | _, Some v1, Some v2 when String.equal v1 v2 -> Some v1
        
        | _, Some _, Some _ -> None
        | _, None, Some _ | _, Some _, None -> None
        | _, None, None -> assert false)
      info1
      info2
let configure t =
  StringMap.iter
    (fun k v ->
      match k with
      | "use-js-string" -> Config.Flag.set k (bool_of_string v)
      | "effects" -> Config.set_effects_backend (effects_backend_of_string v)
      | _ -> ())
    t