Source file omod_ocamlc.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
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
let strf = Format.asprintf
let failwithf fmt = Format.kasprintf (fun s -> failwith s) fmt
let failwith_magic_mismatch ~kind ~found ~expected =
failwithf "not a %s file, found magic %S, expected %S"
kind found expected
let pp_cmt_format_error ppf = function
| Cmt_format.Not_a_typedtree s -> Format.fprintf ppf "not a typed tree: %s" s
let error_msgf fmt = Format.kasprintf (fun s -> Error s) fmt
let exn_to_error f v = try f v with
| Sys_error e | Failure e -> error_msgf "%s" e
| End_of_file -> error_msgf "Unexpected end of file"
| Cmi_format.Error e -> error_msgf "%a" Cmi_format.report_error e
| Cmt_format.Error e -> error_msgf "%a" pp_cmt_format_error e
let prefix_path_on_error file r = match r with
| Ok _ as v -> v
| Error e -> error_msgf "%s: %s" file e
let with_ic fpath f v =
exn_to_error begin fun () ->
let ic = open_in fpath in
try let r = f ic v in close_in ic; Ok r
with e -> (try ignore (close_in ic) with e -> ()); raise e
end ()
let name_of_fpath fpath =
let basename = Filename.basename fpath in
match Omod.Private.String.rev_cut ~sep:'.' basename with
| None -> basename
| Some (name, _ext) -> name
let split_dep name deps =
let rec loop digest acc = function
| [] ->
begin match digest with
| None -> failwithf "self-digest for %s not found" name
| Some digest -> (digest, List.rev acc)
end
| (n, digest') :: deps when n = name ->
begin match digest with
| None -> loop digest' acc deps
| Some d ->
begin match digest' with
| None ->
loop digest acc deps
| Some d' when d = d' ->
loop digest acc deps
| Some d' ->
failwithf "multiple self-digest for %s (%s and %s)"
name (Digest.to_hex d) (Digest.to_hex d')
end
end
| (n, _ as dep) :: deps -> loop digest (dep :: acc) deps
in
loop None [] deps
let read_magic ~kind ~magic ic =
let len = String.length magic in
let found = really_input_string ic len in
if not (String.equal found magic)
then failwith_magic_mismatch ~kind ~found ~expected:magic;
()
let seek_data ~kind ~magic ic =
read_magic ~kind ~magic ic;
seek_in ic (input_binary_int ic);
()
module type DOBJ = sig
type t
val read : Omod.fpath -> (t, string) result
val name : t -> string
val iface_digest : t -> Digest.t
val iface_deps : t -> (string * Digest.t option) list
end
type dobj =
{ name : string;
iface_digest : Digest.t;
iface_deps : (string * Digest.t option) list; }
module Dobj = struct
let name cmi = cmi.name
let iface_digest cmi = cmi.iface_digest
let iface_deps cmi = cmi.iface_deps
end
module Cmi = struct
type t = dobj
let read cmi =
exn_to_error begin fun () ->
let info = Cmi_format.read_cmi cmi in
let name = info.Cmi_format.cmi_name in
let iface_digest, iface_deps = split_dep name info.Cmi_format.cmi_crcs in
Ok { name; iface_digest; iface_deps; }
end ()
|> prefix_path_on_error cmi
include Dobj
end
module Cmti = struct
type t = dobj
let read cmti =
exn_to_error begin fun () ->
let info = Cmt_format.read_cmi cmti in
let name = info.Cmi_format.cmi_name in
let iface_digest, iface_deps = split_dep name info.Cmi_format.cmi_crcs in
Ok { name; iface_digest; iface_deps; }
end ()
|> prefix_path_on_error cmti
include Dobj
end
module Cmo = struct
type t = dobj
let of_compilation_unit cu =
let name = Omod_cu.name cu in
let iface_digest, iface_deps = split_dep name cu.Cmo_format.cu_imports in
{ name; iface_digest; iface_deps }
let of_in_channel ic () =
seek_data ~kind:"cmo" ~magic:Config.cmo_magic_number ic;
of_compilation_unit (input_value ic : Cmo_format.compilation_unit)
let read cmo =
exn_to_error (with_ic cmo of_in_channel) ()
|> prefix_path_on_error cmo
include Dobj
end
module Cmt = struct
type t = dobj
let read cmt =
exn_to_error begin fun () ->
let info = Cmt_format.read_cmt cmt in
let name = info.Cmt_format.cmt_modname in
let iface_digest, iface_deps =
split_dep name info.Cmt_format.cmt_imports
in
Ok { name; iface_digest; iface_deps }
end ()
|> prefix_path_on_error cmt
include Dobj
end
module Cma = struct
type t =
{ name : string;
cmos : Cmo.t list;
custom : bool;
custom_cobjs : string list;
custom_copts : string list;
dllibs : string list; }
let of_library fpath l =
let name = name_of_fpath fpath in
let cmos = List.map Cmo.of_compilation_unit l.Cmo_format.lib_units in
let custom = l.Cmo_format.lib_custom in
let custom_cobjs = l.Cmo_format.lib_ccobjs in
let custom_copts = l.Cmo_format.lib_ccopts in
let dllibs =
match l.Cmo_format.lib_dllibs with
| [] -> []
| lib :: _ ->
if Obj.tag (Obj.repr lib) = Obj.string_tag then
(Obj.magic (l.Cmo_format.lib_dllibs) : string list)
else
List.map snd (Obj.magic l.Cmo_format.lib_dllibs : (_ * string) list)
in
{ name; cmos; custom; custom_cobjs; custom_copts; dllibs }
let cma_of_in_channel ic fpath =
seek_data ~kind:"cma" ~magic:Config.cma_magic_number ic;
of_library fpath (input_value ic : Cmo_format.library)
let read cma =
exn_to_error (with_ic cma cma_of_in_channel) cma
|> prefix_path_on_error cma
let name cma = cma.name
let cmos cma = cma.cmos
let custom cma = cma.custom
let custom_cobjs cma = cma.custom_cobjs
let custom_copts cma = cma.custom_copts
let dllibs cma = cma.dllibs
end
module Cmx = struct
type t =
{ name : string;
digest : Digest.t;
iface_digest : Digest.t;
iface_deps : (string * Digest.t option) list;
cmx_deps : (string * Digest.t option) list; }
let of_compilation_unit (cu, digest) =
let name = cu.Cmx_format.ui_name in
let iface_digest, iface_deps =
split_dep name cu.Cmx_format.ui_imports_cmi
in
let cmx_deps = cu.Cmx_format.ui_imports_cmx in
{ name; digest; iface_digest; iface_deps; cmx_deps }
let of_in_channel ic () =
read_magic ~kind:"cmx" ~magic:Config.cmx_magic_number ic;
let cu = (input_value ic : Cmx_format.unit_infos) in
let digest = Digest.input ic in
of_compilation_unit (cu, digest)
let read cmx =
exn_to_error (with_ic cmx of_in_channel) ()
|> prefix_path_on_error cmx
let name cmx = cmx.name
let digest cmx = cmx.digest
let iface_digest cmx = cmx.iface_digest
let iface_deps cmx = cmx.iface_deps
let cmx_deps cmx = cmx.cmx_deps
end
module Cmxa = struct
type t =
{ name : string;
cmxs : Cmx.t list;
cobjs : string list;
copts : string list; }
let of_library fpath l =
let name = name_of_fpath fpath in
let cmxs = List.map Cmx.of_compilation_unit l.Cmx_format.lib_units in
let cobjs = l.Cmx_format.lib_ccobjs in
let copts = l.Cmx_format.lib_ccopts in
{ name; cmxs; cobjs; copts; }
let of_in_channel ic fpath =
read_magic ~kind:"cmxa" ~magic:Config.cmxa_magic_number ic;
of_library fpath (input_value ic : Cmx_format.library_infos)
let read cmxa =
exn_to_error (with_ic cmxa of_in_channel) cmxa
|> prefix_path_on_error cmxa
let name cmxa = cmxa.name
let cmxs cmxa = cmxa.cmxs
let cobjs cmxa = cmxa.cobjs
let copts cmxa = cmxa.copts
end
module Cmxs = struct
type t = { name : string; }
let of_library fpath () =
let name = name_of_fpath fpath in
{ name }
let read cmxa =
let exists = try Ok (Sys.file_exists cmxa) with
| Sys_error e -> Error e
in
match exists with
| Error _ as e -> e
| Ok true -> Ok (of_library cmxa ())
| Ok false -> error_msgf "%s: No such file" cmxa
let name cmxs = cmxs.name
end