Source file METAPlugin.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
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
(** META generator
@author Sylvain Le Gall
*)
open OASISGettext
open OASISTypes
open OASISValues
open OASISFindlib
open OASISFileTemplate
open OASISPlugin
open OASISSchema
open Format
type meta_type =
| METALibrary
| METASyntax
type t =
{
enable: bool;
description: string option;
meta_type: meta_type;
requires: (string list) option;
extra_lines: string list;
}
let plugin =
`Extra, "META", Some OASISConf.version_short
let self_id, all_id =
Extra.create plugin
let =
OASISFeatures.create "extra_lines" ~plugin
(OASISFeatures.since_version "0.3")
(fun () ->
s_ "Allow to add extra lines to the generated META.")
let pivot_data =
data_new_property plugin
let generator =
let new_field nm =
new_field OASISLibrary.schema all_id nm
in
let enable =
new_field
"Enable"
~default:true
boolean
(fun () ->
s_ "Enable META generation")
pivot_data (fun _ t -> t.enable)
in
let description =
new_field
"Description"
~default:None
(opt string_not_empty)
(fun () ->
s_ "META package description")
pivot_data (fun _ t -> t.description)
in
let meta_type =
new_field
"Type"
~default:METALibrary
(choices
(fun () ->
s_ "META type")
[
"library", METALibrary;
"syntax", METASyntax;
])
(fun () ->
s_ "Type of META package, set default predicates for archive")
pivot_data (fun _ t -> t.meta_type)
in
let =
new_field
"ExtraLines"
~default:[]
~feature:feature_extra_lines
(newline_separated string_not_empty)
(fun () ->
s_ "Extra lines to add to the META")
pivot_data (fun _ t -> t.extra_lines)
in
let requires =
new_field
"Requires"
~default:None
(opt (comma_separated string))
(fun () ->
s_ "Requires field for META package")
pivot_data (fun _ t -> t.requires)
in
fun data ->
{
enable = enable data;
description = description data;
meta_type = meta_type data;
requires = requires data;
extra_lines = extra_lines data;
}
let pp_print_meta pkg root_t findlib_name_of_library_name fmt grp =
let may f =
function
| Some x -> f x
| None -> ()
in
let replace_chars s =
OASISString.replace_chars
(fun c -> if OASISString.is_whitespace c then ' ' else c)
s
in
let pp_print_field fmt (var, preds, vl) =
fprintf fmt
"@,@[%s(%s) =@ %S@]"
var
(String.concat ", " preds)
(replace_chars vl)
in
let pp_print_sfield fmt (var, vl) =
fprintf fmt "@,@[%s =@ %S@]" var (replace_chars vl)
in
let default_synopsis =
match root_t.description with
| Some txt -> txt
| None -> pkg.synopsis
in
let rec pp_print_library fmt (cs, bs, contents, children) =
let name = cs.cs_name in
let archive_byte, archive_byte_plugin,
archive_native, archive_native_plugin =
match contents with
| `Library _ ->
name^".cma", Some (name^".cma"),
name^".cmxa", Some (name^".cmxs")
| `Object { obj_modules = [ m ] } ->
let dir = OASISHostPath.of_unix bs.bs_path in
let exists fn ext =
let path = Filename.concat dir (fn ^ ext) in
OASISFileUtil.file_exists_case path
in
let m =
List.find
(fun m -> exists m ".mli" || exists m ".ml")
[ OASISString.uncapitalize_ascii m;
OASISString.capitalize_ascii m ]
in
m^".cmo", None,
m^".cmx", None
| `Object _ ->
name^".cmo", None,
name^".cmx", None
in
let t =
generator cs.cs_data
in
pp_print_sfield fmt
("version", (OASISVersion.string_of_version pkg.version));
begin
let txt =
match t.description with
| Some txt -> txt
| None -> default_synopsis
in
pp_print_sfield fmt ("description", txt)
end;
begin
let directory =
match contents with
| `Library l -> l.lib_findlib_directory
| `Object o -> o.obj_findlib_directory
in
match directory with
| Some dir ->
pp_print_sfield fmt ("directory", dir)
| None -> ()
end;
begin
let requires =
match t.requires with
| Some lst ->
lst
| None ->
List.map
(function
| InternalLibrary nm ->
findlib_name_of_library_name nm
| FindlibPackage (fndlb_nm, _) ->
fndlb_nm)
bs.bs_build_depends
in
if requires <> [] then
pp_print_sfield fmt ("requires", String.concat " " requires)
end;
begin
match t.meta_type with
| METALibrary ->
pp_print_field fmt ("archive", ["byte"], archive_byte);
may
(fun x ->
pp_print_field fmt ("archive", ["byte"; "plugin"], x))
archive_byte_plugin;
begin
match bs.bs_compiled_object with
| Best | Native ->
pp_print_field fmt ("archive", ["native"], archive_native);
may
(fun x ->
pp_print_field fmt
("archive", ["native"; "plugin"], x))
archive_native_plugin;
| Byte ->
()
end
| METASyntax ->
pp_print_field fmt
("archive", ["syntax"; "preprocessor"], archive_byte);
pp_print_field fmt
("archive", ["syntax"; "toploop"], archive_byte);
begin match bs.bs_compiled_object with
| Best | Native ->
pp_print_field fmt
("archive", ["syntax"; "preprocessor"; "native"], archive_native);
may
(fun x ->
pp_print_field fmt
("archive", ["syntax"; "preprocessor"; "native"; "plugin"], x))
archive_native_plugin;
| Byte ->
()
end
end;
List.iter (fprintf fmt "@,%s") t.extra_lines;
pp_print_sfield fmt
("exists_if",
if bs.bs_compiled_object = Native then
archive_native
else
archive_byte);
FormatExt.pp_print_list pp_print_group "@," fmt children
and pp_print_group fmt =
function
| Container (fndlb_nm, children) ->
fprintf fmt
"@,@[<v1>@[package %S (@]%a%a@]@,)"
fndlb_nm
pp_print_sfield
("description", "Virtual container")
(FormatExt.pp_print_list pp_print_group "") children
| Package (fndlb_nm, lib_cs, lib_bs, lib, _, children) ->
let t = generator lib_cs.cs_data in
if t.enable then
fprintf fmt "@,@[<v1>@[package %S (@]%a@]@,)"
fndlb_nm
pp_print_library (lib_cs, lib_bs, lib, children)
in
assert(root_t.enable);
pp_open_vbox fmt 0;
fprintf fmt "# OASIS_START";
begin
match grp with
| Container (_, children) ->
FormatExt.pp_print_list pp_print_group "" fmt children
| Package (_, lib_cs, lib_bs, lib, _, children) ->
pp_print_library fmt (lib_cs, lib_bs, lib, children)
end;
fprintf fmt "@,# OASIS_STOP@,";
pp_close_box fmt ();
pp_print_flush fmt ()
let main ctxt pkg =
let group_libs, findlib_name_of_library_name, _ =
findlib_mapping pkg
in
let meta_created = Hashtbl.create 3 in
List.fold_left
(fun ctxt grp ->
let root_cs, root_bs, _ = root_of_group grp in
let root_t =
generator root_cs.cs_data
in
if root_t.enable then
begin
let meta_fn =
OASISUnixPath.concat root_bs.bs_path "META"
in
let buff =
Buffer.create 13
in
if Hashtbl.mem meta_created meta_fn then
OASISUtils.failwithf
(f_ "The file '%s' generated for the library '%s' is \
already used for the library '%s'. You can make \
one a child of the other to solve this \
(field `FindlibParent:`).")
meta_fn root_cs.cs_name
(Hashtbl.find meta_created meta_fn);
Hashtbl.add meta_created meta_fn root_cs.cs_name;
pp_print_meta
pkg
root_t
findlib_name_of_library_name
(Format.formatter_of_buffer buff)
grp;
OASISPlugin.add_file
(template_of_string_list
~ctxt:ctxt.OASISPlugin.ctxt
~template:true
meta_fn
comment_meta
(OASISString.split_newline ~do_trim:false
(Buffer.contents buff)))
ctxt
end
else
ctxt)
ctxt
group_libs
let init () =
register_help
plugin
{(help_default METAData.readme_template_mkd) with
help_order = 40};
Extra.register_act self_id main;
register_generator_section `Library all_id pivot_data generator