Debian Codegraph @

vendor/opam/src/format/opamFile.ml

raw
1 (**************************************************************************)
2 (* *)
3 (* Copyright 2012-2020 OCamlPro *)
4 (* Copyright 2012 INRIA *)
5 (* *)
6 (* All rights reserved. This file is distributed under the terms of the *)
7 (* GNU Lesser General Public License version 2.1, with the special *)
8 (* exception on linking described in the file LICENSE. *)
9 (* *)
10 (**************************************************************************)
11
12 (** This module contains the handlers for reading and writing all of OPAM files,
13 and defines their internal types (records for most of them).
14
15 We handle three types of files:
16 - raw text files, without lexing
17 - "table" files, i.e. lexing is just cutting into lines and words, returning
18 a string list list. These are mostly used internally
19 - files using the "opam syntax" and lexer, parsed using OpamFormat.Pp.V
20 *)
21
22 open OpamParserTypes.FullPos
23 open OpamTypes
24 open OpamTypesBase
25 open OpamStd.Op
26
27 module OpamParser = OpamParser.FullPos
28 module OpamPrinter = OpamPrinter.FullPos
29
30 module Pp = struct
31 include OpamPp
32 module V = OpamFormat.V
33 module I = OpamFormat.I
34
35 let warn ?pos ?(strict=OpamFormatConfig.(!r.strict)) ?exn fmt =
36 if strict then
37 match exn with
38 | Some e -> raise e
39 | None -> bad_format ?pos fmt
40 else
41 Printf.ksprintf (fun s ->
42 if OpamConsole.verbose () then
43 match exn with
44 | None ->
45 OpamConsole.warning "%s"
46 (OpamPp.string_of_bad_format (Bad_format (pos, s)))
47 | Some e ->
48 OpamConsole.warning "%s" (OpamPp.string_of_bad_format e))
49 fmt
50 end
51
52 open Pp.Op
53
54 type 'a t = filename
55
56 type 'a typed_file = 'a t
57
58 let make f = (f: 'a t)
59 let filename f = (f: 'a t :> filename)
60 let to_string f = OpamFilename.to_string (filename f)
61 let exists f = OpamFilename.exists (filename f)
62
63 module type IO_FILE = sig
64 type t
65 val format_version: OpamVersion.t
66 val empty: t
67 val write: 'a typed_file -> t -> unit
68 val read : 'a typed_file -> t
69 val read_opt: 'a typed_file -> t option
70 val safe_read: 'a typed_file -> t
71 val read_from_channel: ?filename:'a typed_file -> in_channel -> t
72 val read_from_string: ?filename:'a typed_file -> string -> t
73 val write_to_channel: ?filename:'a typed_file -> out_channel -> t -> unit
74 val write_to_string: ?filename:'a typed_file -> t -> string
75 end
76
77 module type IO_Arg = sig
78 val internal : string
79 val atomic : bool
80 type t
81 val empty : t
82 val of_channel : 'a typed_file -> in_channel -> t
83 val to_channel : 'a typed_file -> out_channel -> t -> unit
84 val of_string : 'a typed_file -> string -> t
85 val to_string : 'a typed_file -> t -> string
86 end
87
88 module Stats = struct
89 let read_files = ref []
90 let write_files = ref []
91 let print () =
92 let aux kind = function
93 | [] -> ()
94 | l ->
95 OpamConsole.msg "%d files %s:\n %s\n"
96 (List.length !read_files) kind (String.concat "\n " l)
97 in
98 aux "read" !read_files;
99 aux "write" !write_files
100 end
101
102 let dummy_file = OpamFilename.raw "<none>"
103
104 module MakeIO (F : IO_Arg) = struct
105
106 let log ?level fmt =
107 OpamConsole.log (Printf.sprintf "FILE(%s)" F.internal) ?level fmt
108 let slog = OpamConsole.slog
109
110 let write f v =
111 let filename = OpamFilename.to_string f in
112 let chrono = OpamConsole.timer () in
113 let write =
114 if F.atomic then
115 OpamFilename.with_open_out_bin_atomic
116 else
117 OpamFilename.with_open_out_bin
118 in
119 write f (fun oc -> F.to_channel f oc v);
120 Stats.write_files := filename :: !Stats.write_files;
121 log "Wrote %s%s in %.3fs" filename (if F.atomic then " atomically" else "") (chrono ())
122
123 let read_opt f =
124 let filename = OpamFilename.prettify f in
125 let chrono = OpamConsole.timer () in
126 try
127 let ic = OpamFilename.open_in f in
128 try
129 Unix.lockf (Unix.descr_of_in_channel ic) Unix.F_RLOCK 0;
130 Stats.read_files := filename :: !Stats.read_files;
131 let r = F.of_channel f ic in
132 close_in ic;
133 log ~level:3 "Read %s in %.3fs" filename (chrono ());
134 Some r
135 with e -> OpamStd.Exn.finalise e (fun () -> close_in ic)
136 with
137 | OpamSystem.File_not_found _ ->
138 None
139 | e ->
140 OpamStd.Exn.fatal e;
141 if OpamFormatConfig.(!r.strict) then
142 (OpamConsole.error "%s"
143 (Pp.string_of_bad_format ~file:(OpamFilename.to_string f) e);
144 OpamConsole.error_and_exit `File_error "Strict mode: aborting")
145 else raise e
146
147 let read f =
148 match read_opt f with
149 | Some f -> f
150 | None ->
151 OpamSystem.internal_error "File %s does not exist or can't be read"
152 (OpamFilename.to_string f)
153
154 let safe_read f =
155 try
156 match read_opt f with
157 | Some f -> f
158 | None ->
159 log ~level:2 "Cannot find %a" (slog OpamFilename.to_string) f;
160 F.empty
161 with
162 | (Pp.Bad_version _ | Pp.Bad_format _) as e->
163 OpamConsole.error "%s [skipped]\n"
164 (Pp.string_of_bad_format ~file:(OpamFilename.to_string f) e);
165 F.empty
166
167 let read_from_f f input =
168 try f input with
169 | (Pp.Bad_version _ | Pp.Bad_format _) as e->
170 if OpamFormatConfig.(!r.strict) then
171 (OpamConsole.error "%s" (Pp.string_of_bad_format e);
172 OpamConsole.error_and_exit `File_error "Strict mode: aborting")
173 else raise e
174
175 let read_from_channel ?(filename=dummy_file) ic =
176 read_from_f (F.of_channel filename) ic
177
178 let read_from_string ?(filename=dummy_file) str =
179 read_from_f (F.of_string filename) str
180
181 let write_to_channel ?(filename=dummy_file) oc t =
182 F.to_channel filename oc t
183
184 let write_to_string ?(filename=dummy_file) t =
185 F.to_string filename t
186
187 end
188
189 (** I - Raw text files (no parsing) *)
190
191 (** Compiler and package description opam file fields: one-line title and
192 content. Formerly, (<repo>/packages/.../descr,
193 <repo>/compilers/.../<v>.descr) *)
194
195 module DescrIO = struct
196
197 let internal = "descr"
198 let atomic = false
199 let format_version = OpamVersion.of_string "0"
200
201 type t = string * string
202
203 let empty = "", ""
204
205 let synopsis = fst
206 let body = snd
207
208 let full (x,y) =
209 match y with
210 | "" -> x ^ "\n"
211 | y -> String.concat "" [x; "\n\n"; y; "\n"]
212
213 let of_channel _ ic =
214 let x =
215 try OpamStd.String.strip (input_line ic)
216 with End_of_file | Sys_error _ -> "" in
217 let y =
218 try OpamStd.String.strip (OpamSystem.string_of_channel ic)
219 with End_of_file | Sys_error _ -> ""
220 in
221 x, y
222
223 let to_channel _ oc (x,y) =
224 output_string oc x;
225 output_char oc '\n';
226 if y <> "" then
227 (output_char oc '\n';
228 output_string oc y;
229 output_char oc '\n')
230
231 let create str =
232 let head, tail =
233 match OpamStd.String.cut_at str '\n' with
234 | None -> str, ""
235 | Some (h,t) -> h, t in
236 OpamStd.String.strip head, OpamStd.String.strip tail
237
238 let of_string _ = create
239
240 let to_string _ = full
241
242 end
243 module Descr = struct
244 include DescrIO
245 include MakeIO(DescrIO)
246 end
247
248 (* module Comp_descr = Descr *)
249
250 (** Raw file interface used for variable expansions ( *.in ) *)
251
252 (*
253 module SubstIO = struct
254
255 let internal = "subst"
256
257 type t = string
258
259 let empty = ""
260
261 let of_channel _ ic =
262 OpamSystem.string_of_channel ic
263
264 let to_channel _ oc t =
265 output_string oc t
266
267 let of_string _ str = str
268
269 let to_string _ t = t
270
271 end
272 module Subst = struct
273 include SubstIO
274 include MakeIO(SubstIO)
275 end
276 *)
277
278 (** II - Base word list list parser and associated file types *)
279
280 module LinesBase = struct
281
282 (* Lines of space separated words *)
283 type t = string list list
284
285 let format_version = OpamVersion.of_string "0"
286
287 let empty = []
288
289 let internal = "lines"
290
291 let atomic = false
292
293 let find_escapes s len =
294 let rec aux acc i =
295 if i < 0 then acc else
296 let acc =
297 match s.[i] with
298 | '\\' | ' ' | '\t' | '\n' ->
299 let esc,count = acc in
300 i::esc, count + 1
301 | _ -> acc in
302 aux acc (i-1) in
303 aux ([],0) (len - 1)
304
305 let escape_spaces = function
306 | "" ->
307 "@"
308 | "@" ->
309 "\\@"
310 | str ->
311 let len = String.length str in
312 match find_escapes str len with
313 | [], _ -> str
314 | escapes, n ->
315 let buf = Bytes.create (len + n) in
316 let rec aux i = function
317 | ofs1::(ofs2::_ as r) ->
318 Bytes.blit_string str ofs1 buf (ofs1+i) (ofs2-ofs1);
319 Bytes.set buf (ofs2+i) '\\';
320 aux (i+1) r
321 | [ofs] ->
322 Bytes.blit_string str ofs buf (ofs+i) (len-ofs);
323 buf
324 | [] -> assert false
325 in
326 Bytes.to_string (aux 0 (0::escapes))
327
328 let of_channel (_:filename) ic =
329 OpamLineLexer.main (Lexing.from_channel ic)
330
331 let to_channel (_:filename) oc t =
332 List.iter (function
333 | [] -> ()
334 | w::r ->
335 output_string oc (escape_spaces w);
336 List.iter (fun w ->
337 output_char oc '\t';
338 output_string oc (escape_spaces w))
339 r;
340 output_char oc '\n')
341 t
342
343 let of_string (_:filename) str =
344 OpamLineLexer.main (Lexing.from_string str)
345
346 let to_string (_:filename) (lines: t) =
347 let buf = Buffer.create 1024 in
348 List.iter (fun l ->
349 (match l with
350 | [] -> ()
351 | w::r ->
352 Buffer.add_string buf (escape_spaces w);
353 List.iter (fun w ->
354 Buffer.add_char buf '\t';
355 Buffer.add_string buf (escape_spaces w))
356 r);
357 Buffer.add_string buf "\n"
358 ) lines;
359 Buffer.contents buf
360
361 let file_none = OpamFilename.of_string "<none>"
362
363 let pp_string =
364 Pp.pp
365 (fun ~pos:_ s -> OpamLineLexer.main (Lexing.from_string s))
366 (fun lines -> to_string file_none lines)
367
368 let pp_channel ic oc =
369 Pp.pp
370 (fun ~pos:_ () -> of_channel file_none ic)
371 (to_channel file_none oc)
372
373 end
374
375 module Lines = struct
376 include LinesBase
377 include MakeIO(LinesBase)
378 end
379
380 module type LineFileArg = sig
381 val internal: string
382 val atomic: bool
383 type t
384 val empty: t
385 val pp: (string list list, t) Pp.t
386 end
387
388 module LineFile (X: LineFileArg) = struct
389 module IO = struct
390 include X
391
392 let format_version = OpamVersion.of_string "0"
393
394 let to_channel _ oc t = Pp.print (Lines.pp_channel stdin oc -| pp) t
395
396 let to_string _ t = Pp.print (Lines.pp_string -| pp) t
397
398 let of_channel filename ic =
399 Pp.parse (Lines.pp_channel ic stdout -| pp) ~pos:(pos_file filename) ()
400
401 let of_string filename str =
402 Pp.parse (Lines.pp_string -| pp)
403 ~pos:{ pos_null with filename = OpamFilename.to_string filename }
404 str
405 end
406
407 include IO
408 include MakeIO(IO)
409 end
410
411 (** (1) Internal usage only *)
412
413 (** Compiler aliases definitions (aliases): table
414 <name> <compiler> *)
415
416 module Aliases = LineFile(struct
417
418 let internal = "aliases"
419 let atomic = false
420
421 type t = string switch_map
422
423 let empty = OpamSwitch.Map.empty
424
425 let pp =
426 OpamSwitch.Map.(OpamFormat.lines_map ~empty ~add ~fold) @@
427 Pp.of_module "switch-name" (module OpamSwitch) ^+
428 Pp.last
429
430 end)
431
432 (** Indices of items and their associated source repository: table
433 <fullname> <repo-name> <dir-prefix> *)
434
435 module Repo_index (A : OpamStd.ABSTRACT) = LineFile(struct
436
437 let internal = "repo-index"
438 let atomic = false
439
440 type t = (repository_name * string option) A.Map.t
441
442 let empty = A.Map.empty
443
444 let pp =
445 OpamFormat.lines_map ~empty ~add:A.Map.safe_add ~fold:A.Map.fold @@
446 Pp.of_module "name" (module A) ^+
447 Pp.of_module "repository" (module OpamRepositoryName) ^+
448 Pp.opt Pp.last
449 end)
450
451 module Package_index = Repo_index(OpamPackage)
452
453 (** List of packages (<switch>/installed, <switch>/installed-roots,
454 <switch>/reinstall): table
455 <package> <version> *)
456
457 module PkgList = LineFile (struct
458
459 let internal = "package-version-list"
460 let atomic = false
461
462 type t = package_set
463
464 let empty = OpamPackage.Set.empty
465
466 let pp =
467 OpamPackage.Set.(OpamFormat.lines_set ~empty ~add ~fold) @@
468 (Pp.of_module "pkg-name" (module OpamPackage.Name) ^+
469 Pp.last -| Pp.of_module "pkg-version" (module OpamPackage.Version))
470 -| Pp.pp
471 (fun ~pos:_ (n,v) -> OpamPackage.create n v)
472 (fun nv -> nv.name, nv.version)
473
474 end)
475
476 (** Lists of pinned packages (<switch>/pinned): table
477 <name> <pin-kind> <target>
478
479 Backwards-compatibility code, do not use *)
480 module Pinned_legacy = struct
481 type pin_option =
482 | Version of version
483 | Source of url
484
485 let pp_pin =
486 let looks_like_version_re =
487 Re.(compile @@
488 seq [bos; digit; rep @@ diff any (set "/\\"); eos])
489 in
490 let pin_option_of_string ?kind s =
491 match kind with
492 | Some `version ->
493 Version (OpamPackage.Version.of_string s)
494 | None when Re.execp looks_like_version_re s ->
495 Version (OpamPackage.Version.of_string s)
496 | Some (#OpamUrl.backend as backend) ->
497 Source (OpamUrl.parse ~backend s)
498 | None ->
499 Source (OpamUrl.parse ~handle_suffix:false s)
500 in
501 let string_of_pin_kind = function
502 | `version -> "version"
503 | `rsync -> "path"
504 | #OpamUrl.backend as ub -> OpamUrl.string_of_backend ub
505 in
506 let pin_kind_of_string = function
507 | "version" -> `version
508 | "path" -> `rsync
509 | s -> OpamUrl.backend_of_string s
510 in
511 let string_of_pin_option = function
512 | Version v -> OpamPackage.Version.to_string v
513 | Source url -> OpamUrl.to_string url
514 in
515 let kind_of_pin_option = function
516 | Version _ -> `version
517 | Source url -> (url.OpamUrl.backend :> pin_kind)
518 in
519 Pp.pp
520 ~name:"?pin-kind pin-target"
521 (fun ~pos -> function
522 | [x] -> pin_option_of_string x
523 | [k;x] -> pin_option_of_string ~kind:(pin_kind_of_string k) x
524 | _ -> Pp.bad_format ~pos "Invalid number of fields")
525 (fun x -> [string_of_pin_kind (kind_of_pin_option x);
526 string_of_pin_option x])
527
528 include LineFile(struct
529
530 let internal = "pinned"
531 let atomic = false
532
533 type t = pin_option OpamPackage.Name.Map.t
534
535 let empty = OpamPackage.Name.Map.empty
536
537 let pp =
538 OpamPackage.Name.Map.(OpamFormat.lines_map ~empty ~add:safe_add ~fold) @@
539 Pp.of_module "pkg-name" (module OpamPackage.Name) ^+
540 pp_pin
541
542 end)
543 end
544
545
546 (** Cached environment updates (<switch>/.opam-switch/environment
547 <switch>/.opam-switch/last-env/env-* last env files) *)
548
549 module Environment = LineFile(struct
550
551 let internal = "environment"
552 let atomic = true
553
554 type t = env_update list
555
556 let empty = []
557
558 let pp =
559 (OpamFormat.lines_set ~empty:[] ~add:OpamStd.List.cons ~fold:List.fold_right @@
560 Pp.identity ^+
561 Pp.of_pair "env_update_op"
562 (OpamLexer.FullPos.env_update_op, OpamPrinter.env_update_op_kind) ^+
563 Pp.identity ^+
564 Pp.opt Pp.singleton)
565 -| Pp.pp (fun ~pos:_ -> List.rev) List.rev
566
567
568 let pp =
569 pp -|
570 Pp.map_list
571 (Pp.pp
572 (fun ~pos:_ (a, (b, (c, d))) -> (a, b, c, d))
573 (fun (a, b, c, d) -> (a, (b, (c, d)))))
574
575 end)
576
577 (** (2) Part of the public repository format *)
578
579 (** repository index files ("urls.txt"): table
580 <filename> <md5> <perms> *)
581
582 module File_attributes = LineFile(struct
583
584 let internal = "file_attributes"
585 let atomic = false
586
587 type t = file_attribute_set
588
589 let empty = OpamFilename.Attribute.Set.empty
590
591 let pp =
592 OpamFilename.Attribute.Set.(OpamFormat.lines_set ~empty ~add ~fold) @@
593 (Pp.of_module "file" (module OpamFilename.Base) ^+
594 Pp.of_pair "checksum" OpamHash.(of_string, contents) ^+
595 Pp.opt (Pp.last -| Pp.of_pair "perm" (int_of_string, string_of_int))
596 ) -|
597 Pp.pp
598 (fun ~pos:_ (base,(hash,perm)) ->
599 OpamFilename.Attribute.create base hash perm)
600 (fun att -> OpamFilename.Attribute.(base att, (md5 att, perm att)))
601
602 end)
603
604 (** (3) Available in interface *)
605
606 (** Old Switch export/import format: table
607 <name> <version> <installed-state> [pinning-kind] [pinning-url] *)
608
609 module StateTable = struct
610
611 let internal = "export"
612 let atomic = false
613
614 module M = OpamPackage.Name.Map
615
616 type t = switch_selections
617
618 let empty = {
619 sel_installed = OpamPackage.Set.empty;
620 sel_roots = OpamPackage.Set.empty;
621 sel_compiler = OpamPackage.Set.empty;
622 sel_pinned = OpamPackage.Set.empty;
623 }
624
625 let pp_state =
626 Pp.pp ~name:"pkg-state"
627 (fun ~pos:_ -> function
628 | "compiler" -> `Compiler
629 | "root" -> `Root
630 | "noroot" | "installed" -> `Installed
631 | "uninstalled" -> `Uninstalled
632 | "uninstalled-compiler" -> `Uninstalled_compiler
633 | _ -> Pp.unexpected ())
634 (function
635 | `Compiler -> "compiler"
636 | `Root -> "root"
637 | `Installed -> "installed"
638 | `Uninstalled -> "uninstalled"
639 | `Uninstalled_compiler -> "uninstalled-compiler")
640
641 let pp_lines =
642 M.(OpamFormat.lines_map ~empty ~add:safe_add ~fold) @@
643 Pp.of_module "pkg-name" (module OpamPackage.Name) ^+
644 Pp.of_module "pkg-version" (module OpamPackage.Version) ^+
645 (Pp.opt (pp_state ^+ Pp.opt Pinned_legacy.pp_pin) -|
646 Pp.default (`Root, None))
647
648 (* Convert from one name-map to type t *)
649 let pp =
650 pp_lines -| Pp.pp
651 (fun ~pos:_ map ->
652 M.fold
653 (fun name (version,(state,pin)) t ->
654 let nv = OpamPackage.create name version in
655 {
656 sel_installed = (match state with
657 | `Installed | `Root | `Compiler ->
658 OpamPackage.Set.add nv t.sel_installed
659 | `Uninstalled | `Uninstalled_compiler ->
660 t.sel_installed);
661 sel_roots = (match state with
662 | `Root | `Compiler ->
663 OpamPackage.Set.add nv t.sel_roots
664 | `Installed | `Uninstalled | `Uninstalled_compiler ->
665 t.sel_roots);
666 sel_compiler = (match state with
667 | `Compiler | `Uninstalled_compiler ->
668 OpamPackage.Set.add nv t.sel_compiler
669 | `Root | `Installed | `Uninstalled ->
670 t.sel_compiler);
671 sel_pinned = (match pin with
672 | Some (Pinned_legacy.Version v) ->
673 OpamPackage.Set.add (OpamPackage.create name v)
674 t.sel_pinned
675 | Some _ ->
676 OpamPackage.Set.add (OpamPackage.create name version)
677 t.sel_pinned
678 | None -> t.sel_pinned);
679 })
680 map
681 empty)
682 (fun t ->
683 M.empty |>
684 OpamPackage.Set.fold (fun nv ->
685 M.add nv.name
686 (nv.version, (`Installed, None)))
687 t.sel_installed |>
688 OpamPackage.Set.fold (fun nv ->
689 M.add nv.name
690 (nv.version, (`Root, None)))
691 t.sel_roots |>
692 OpamPackage.Set.fold (fun nv acc ->
693 let name = nv.name in
694 try
695 let (v, _) = M.find name acc in
696 M.add name (v, (`Compiler, None)) acc
697 with Not_found ->
698 M.add name
699 (nv.version, (`Uninstalled_compiler, None))
700 acc)
701 t.sel_compiler |>
702 OpamPackage.Set.fold (fun nv map ->
703 let state =
704 try let _, (state, _) = M.find nv.name map in state
705 with Not_found -> `Uninstalled
706 in
707 (* Incorrect: marks all pins as version. But this is deprecated. *)
708 M.add nv.name
709 (nv.version, (state, Some (Pinned_legacy.Version nv.version))) map)
710 t.sel_pinned)
711
712 end
713
714 module LegacyState = struct
715 type t = switch_selections
716 include (LineFile (StateTable) : IO_FILE with type t := t)
717 end
718
719 (** III - Opam Syntax parser and associated file types *)
720
721
722 module Syntax = struct
723
724 (* Idea: have a [(ic, oc_with_lock * t) pp] that can be used to reading and
725 re-writing files with a guarantee that it hasn't been rewritten in the
726 meantime *)
727
728 let parser_main lexbuf filename =
729 let error msg =
730 let curr = lexbuf.Lexing.lex_curr_p in
731 let start = lexbuf.Lexing.lex_start_p in
732 let pos =
733 { filename = curr.Lexing.pos_fname;
734 start =
735 start.Lexing.pos_lnum,
736 start.Lexing.pos_cnum - start.Lexing.pos_bol;
737 stop = (* XXX here we take current position, where error occurs as end position *)
738 curr.Lexing.pos_lnum,
739 curr.Lexing.pos_cnum - curr.Lexing.pos_bol;
740 }
741 in
742 raise (OpamPp.Bad_format (Some pos, msg))
743 in
744 let filename = OpamFilename.to_string filename in
745 lexbuf.Lexing.lex_curr_p <- { lexbuf.Lexing.lex_curr_p with
746 Lexing.pos_fname = filename };
747 try OpamParser.main OpamLexer.token lexbuf filename with
748 | OpamLexer.Error msg -> error msg
749 | Parsing.Parse_error -> error "Parse error"
750
751
752 let pp_channel filename ic oc =
753 Pp.pp
754 (fun ~pos:_ () ->
755 let lexbuf = Lexing.from_channel ic in
756 parser_main lexbuf filename)
757 (fun file ->
758 let fmt = Format.formatter_of_out_channel oc in
759 OpamPrinter.format_opamfile fmt file)
760
761 let of_channel (filename:filename) (ic:in_channel) =
762 Pp.parse ~pos:(pos_file filename) (pp_channel filename ic stdout) ()
763
764 let to_channel filename oc t =
765 Pp.print (pp_channel filename stdin oc) t
766
767 let of_string (filename:filename) str =
768 let lexbuf = Lexing.from_string str in
769 parser_main lexbuf filename
770
771 let to_string _file_name t =
772 OpamPrinter.opamfile t
773
774 let to_string_with_preserved_format
775 filename ?(format_from=filename) ?format_from_string
776 ~empty ?(sections=[]) ~fields pp t =
777 let current_str_opt =
778 match format_from_string with
779 | Some s -> Some s
780 | None ->
781 try Some (OpamFilename.read format_from)
782 with OpamSystem.File_not_found _ -> None
783 in
784 match current_str_opt with
785 | None -> to_string filename (Pp.print pp (filename, t))
786 | Some str ->
787 let syn_file = of_string filename str in
788 let syn_t = Pp.print pp (filename, t) in
789 let it_ident it = match it.pelem with
790 | Variable (f, _) -> `Var f.pelem
791 | Section ({section_kind = k; section_name = n; _}) ->
792 `Sec (k.pelem, OpamStd.Option.map (fun x -> x.pelem) n)
793 in
794 let lines_index =
795 let rec aux acc s =
796 let until =
797 try Some (String.index_from s (List.hd acc) '\n')
798 with Not_found -> None
799 in
800 match until with
801 | Some until -> aux (until+1 :: acc) s
802 | None -> Array.of_list (List.rev acc)
803 in
804 aux [0] str
805 in
806 let pos_index (li,col) = lines_index.(li - 1) + col in
807 let extract start stop =
808 if stop < start then raise Not_found;
809 String.sub str start (stop - start)
810 in
811 let value_list_str lastpos vlst vlst_raw =
812 let extract_pos start stop = extract (pos_index start) (pos_index stop) in
813 let def_blank blank = OpamStd.Option.default "\n " blank in
814 let find_split f =
815 let rec aux p = function
816 | x::r when f x -> Some (p, x, r)
817 | p::r -> aux (Some p) r
818 | [] -> None
819 in
820 aux None
821 in
822 let full_vlst_raw = vlst_raw in
823 let rec aux lastpos blank acc vlst vlst_raw =
824 match vlst, vlst_raw with
825 | v::r, vraw :: rraw when OpamPrinter.value_equals v vraw ->
826 let blank = extract lastpos (pos_index vraw.pos.start) in
827 let str = extract_pos vraw.pos.start vraw.pos.stop in
828 let new_v = blank ^ str in
829 let blank = Some blank in
830 let lastpos = pos_index vraw.pos.stop in
831 aux lastpos blank (new_v :: acc) r rraw
832 | v::r , _ ->
833 (match find_split (OpamPrinter.value_equals v) full_vlst_raw with
834 | Some (pvraw, vraw, rraw) ->
835 let str = extract_pos vraw.pos.start vraw.pos.stop in
836 let blank, lastpos =
837 if pos_index vraw.pos.start - lastpos <= 0 then
838 def_blank blank, lastpos
839 else
840 (let start = match pvraw with
841 | Some pvraw -> pos_index pvraw.pos.stop
842 | None -> lastpos
843 in
844 let stop = pos_index vraw.pos.start in
845 extract start stop),
846 pos_index vraw.pos.stop
847 in
848 let new_v = blank ^ str in
849 let blank = Some blank in
850 aux lastpos blank (new_v :: acc) r rraw
851 | None ->
852 let blank, rraw, lastpos =
853 match vlst_raw with
854 | vraw :: rraw ->
855 let blank = extract lastpos (pos_index vraw.pos.start) in
856 let rraw, lastpos =
857 if OpamStd.List.find_opt
858 (OpamPrinter.value_equals vraw) vlst <> None then
859 vlst_raw, lastpos
860 else
861 rraw, pos_index vraw.pos.stop
862 in
863 blank, rraw, lastpos
864 | [] -> def_blank blank, vlst_raw, lastpos
865 in
866 let new_v = blank ^ (OpamPrinter.value v) in
867 let blank = Some blank in
868 aux lastpos blank (new_v :: acc) r rraw)
869 | [], _ -> acc
870 in
871 aux lastpos None [] vlst vlst_raw
872 in
873 let item_var_str name field =
874 let field_raw =
875 List.find (fun i -> it_ident i = `Var name) syn_file.file_contents
876 in
877 match field.pelem with
878 | Variable (n, { pelem = List { pelem = full_vlst;_}; _})
879 when n.pelem = name ->
880 let full_vlst_raw, full_vlst_raw_pos =
881 match field_raw.pelem with
882 | Variable (_, {pelem = List vlst_raw; pos}) -> vlst_raw.pelem, pos
883 | _ -> raise Not_found
884 in
885 (* if empty, rewrite full field *)
886 if full_vlst_raw = [] then OpamPrinter.items [field] else
887 (* aux *)
888 let item_var_str =
889 let lastpos = pos_index full_vlst_raw_pos.start +1 in
890 let final_list = value_list_str lastpos full_vlst full_vlst_raw in
891 String.concat "" (List.rev final_list)
892 in
893 let beginning =
894 let start = pos_index field_raw.pos.start in
895 let stop = pos_index full_vlst_raw_pos.start +1 in
896 extract start stop
897 in
898 let ending =
899 let start = pos_index (List.hd (List.rev full_vlst_raw)).pos.stop in
900 let stop = pos_index full_vlst_raw_pos.stop in
901 extract start stop
902 in
903 beginning ^ item_var_str ^ ending
904 | _ -> OpamPrinter.items [field]
905 in
906 (* Fields *)
907 let get_padding item lastpos =
908 let start = pos_index item.pos.start in
909 let stop = pos_index item.pos.stop in
910 let padding = extract lastpos start in
911 padding, stop
912 in
913 let field_str item lastpos strs =
914 let start = pos_index item.pos.start in
915 let padding, stop = get_padding item lastpos in
916 let field = extract start stop in
917 field :: padding :: strs, stop
918 in
919 let rem, (strs, lastpos) =
920 List.fold_left (fun (rem0, (strs, lastpos)) item ->
921 let rem =
922 List.filter (fun i -> it_ident i <> it_ident item) rem0
923 in
924 let pos = item.pos in
925 match item.pelem with
926 | Variable (name, v) ->
927 let name = name.pelem in
928 (try
929 let ppa = OpamStd.List.assoc String.equal name fields in
930 match snd (Pp.print ppa t) with
931 | None
932 | Some { pelem = List { pelem = []; _}; _}
933 | Some { pelem = List
934 { pelem = [ { pelem = List
935 { pelem = []; _}; _}]; _}; _} ->
936 rem, (strs, pos_index item.pos.stop)
937 | field_syn_t when
938 field_syn_t =
939 snd (Pp.print ppa (Pp.parse ppa ~pos (empty, Some v)))
940 ->
941 (* unchanged *)
942 rem, (field_str item lastpos strs)
943 | _ ->
944 try
945 let field =
946 List.find (fun i -> it_ident i = `Var name) syn_t.file_contents
947 in
948 let f = item_var_str name field in
949 let padding, stop = get_padding item lastpos in
950 rem, (f :: padding :: strs, stop)
951 with Not_found -> rem0, (strs, pos_index item.pos.stop)
952 with Not_found | OpamPp.Bad_format _ ->
953 if OpamStd.String.starts_with ~prefix:"x-" name &&
954 OpamStd.List.find_opt (fun i -> it_ident i = `Var name)
955 syn_t.file_contents <> None then
956 rem, (field_str item lastpos strs)
957 else rem0, (strs, pos_index item.pos.stop))
958 | Section {section_kind; section_name; section_items} ->
959 let section_kind = section_kind.pelem in
960 let section_items = section_items.pelem in
961 let section_name = OpamStd.Option.map (fun x -> x.pelem) section_name in
962 (try
963 rem,
964 let ppa =
965 OpamStd.List.assoc String.equal section_kind sections
966 in
967 let print_sec ppa t =
968 match snd (Pp.print ppa t) with
969 | None -> None
970 | Some v ->
971 OpamStd.List.assoc_opt (OpamStd.Option.equal String.equal)
972 section_name v
973 in
974 let sec_field_t = print_sec ppa t in
975 if sec_field_t <> None &&
976 sec_field_t =
977 print_sec ppa
978 (Pp.parse ppa ~pos
979 (empty, Some [section_name, section_items]))
980 then
981 (* unchanged *)
982 field_str item lastpos strs
983 else
984 let f =
985 List.filter
986 (fun i -> it_ident i = `Sec (section_kind, section_name))
987 syn_t.file_contents
988 in
989 let padding, stop = get_padding item lastpos in
990 (OpamPrinter.items f :: padding :: strs), stop
991 with Not_found | OpamPp.Bad_format _ ->
992 rem0, (strs, pos_index item.pos.stop)))
993 (syn_t.file_contents, ([], 0)) syn_file.file_contents
994 in
995 let str = String.concat "" (List.rev strs) in
996 let str =
997 if rem = [] then str else
998 str ^ "\n" ^ (OpamPrinter.items rem)
999 in
1000 let str =
1001 let last = lines_index.(Array.length lines_index -1) in
1002 if last <= lastpos then str else str ^ extract lastpos last
1003 in
1004 str
1005
1006 let contents pp ?(filename=dummy_file) t =
1007 Pp.print pp (filename, t)
1008
1009 let to_list pp ?(filename=dummy_file) t =
1010 let rec aux acc pfx = function
1011 | {pelem=Section ({section_kind; section_name=None; section_items});_} :: r ->
1012 aux (aux acc (section_kind.pelem :: pfx) section_items.pelem) pfx r
1013 | {pelem=Section ({section_kind; section_name=Some n; section_items});_} :: r ->
1014 aux
1015 (aux acc (Printf.sprintf "%s(%s)" section_kind.pelem n.pelem :: pfx)
1016 section_items.pelem)
1017 pfx r
1018 | {pelem=Variable (name, value);_} :: r ->
1019 aux (((name.pelem :: pfx), value) :: acc) pfx r
1020 | [] -> acc
1021 in
1022 List.rev_map
1023 (fun (pfx, value) -> String.concat "." (List.rev pfx), value)
1024 (aux [] [] (contents pp ~filename t).file_contents)
1025
1026 end
1027
1028 module type SyntaxFileArg = sig
1029 val internal: string
1030 val atomic: bool
1031 val format_version: OpamVersion.t
1032 type t
1033 val empty: t
1034 val pp: (opamfile, filename * t) Pp.t
1035 end
1036
1037 module SyntaxFile(X: SyntaxFileArg) : IO_FILE with type t := X.t = struct
1038
1039 module IO = struct
1040 let to_opamfile filename t = Pp.print X.pp (filename, t)
1041
1042 let catch_future_syntax_error = function
1043 | {file_contents = [{pelem = Variable({pelem = "opam-version"; _}, {pelem = String ver; _}); _ };
1044 {pelem = Section {section_kind = {pelem = "#"; _}; _}; pos}]; _}
1045 when OpamVersion.(compare (nopatch (of_string ver)) (nopatch X.format_version)) <= 0 ->
1046 raise (OpamPp.Bad_version (Some pos, "Parse error"))
1047 | opamfile -> opamfile
1048
1049 let of_channel filename (ic:in_channel) =
1050 let opamfile = Syntax.of_channel filename ic |> catch_future_syntax_error in
1051 Pp.parse X.pp ~pos:(pos_file filename) opamfile
1052 |> snd
1053
1054 let to_channel filename oc t =
1055 Syntax.to_channel filename oc (to_opamfile filename t)
1056
1057 let of_string (filename:filename) str =
1058 let opamfile = Syntax.of_string filename str |> catch_future_syntax_error in
1059 Pp.parse X.pp ~pos:(pos_file filename) opamfile
1060 |> snd
1061
1062 let to_string filename t =
1063 Syntax.to_string filename (to_opamfile filename t)
1064 end
1065
1066 include IO
1067 include X
1068 include MakeIO(struct
1069 include X
1070 include IO
1071 let atomic = false
1072 end)
1073
1074 end
1075
1076 (* Error less reading for forward compatibility of opam roots *)
1077 module type BestEffortArg = sig
1078 include SyntaxFileArg
1079
1080 (* Version of file format, as understood by [opam-file-format] *)
1081 val file_format_version: OpamVersion.t [@@ocaml.warning "-32"]
1082
1083 (* Construct the syntax pp, under some conditions. If [condition] is given,
1084 it is passed to [OpamFormat.show_erros] call, for error display
1085 conditions, default is to display it as a warning. If [f] is given, it is
1086 passed to [OpamFormat.check_opam_version] call, it is the check function,
1087 default is to check regarding [file_format_version]. *)
1088 val pp_cond:
1089 ?f:(OpamVersion.t -> bool) -> ?condition:(t -> bool) -> unit ->
1090 (opamfile, filename * t) Pp.t
1091 end
1092
1093 module type BestEffortRead = sig
1094 type t
1095 val read: t typed_file -> t
1096 val read_opt: t typed_file -> t option
1097 val safe_read: t typed_file -> t
1098 val read_from_channel: ?filename:t typed_file -> in_channel -> t
1099 val read_from_string: ?filename:t typed_file -> string -> t
1100 end
1101
1102 module MakeBestEffort (S: BestEffortArg) : BestEffortRead
1103 with type t := S.t = struct
1104 module ES = struct
1105 include S
1106 let pp =
1107 pp_cond
1108 (* to read newer oapm root with newer `opam-version` field, we need to
1109 be less strict on the check_opam_version
1110 ~f:(fun _ -> true)
1111 *)
1112 ~condition:(fun _ -> false) ()
1113 end
1114 include ES
1115 include SyntaxFile(ES)
1116 end
1117
1118 (** (1) Internal files *)
1119
1120 (** Structure shared by a few file formats *)
1121 module Wrappers = struct
1122
1123 type t = {
1124 pre_build : command list;
1125 wrap_build : command list;
1126 post_build : command list;
1127 pre_install : command list;
1128 wrap_install : command list;
1129 post_install : command list;
1130 pre_remove : command list;
1131 wrap_remove : command list;
1132 post_remove : command list;
1133 pre_session : command list;
1134 post_session : command list;
1135 }
1136
1137 let empty = {
1138 pre_build = [];
1139 wrap_build = [];
1140 post_build = [];
1141 pre_install = [];
1142 wrap_install = [];
1143 post_install = [];
1144 pre_remove = [];
1145 wrap_remove = [];
1146 post_remove = [];
1147 pre_session = [];
1148 post_session = []
1149 }
1150
1151 let pre_build t = t.pre_build
1152 let wrap_build t = t.wrap_build
1153 let post_build t = t.post_build
1154 let pre_install t = t.pre_install
1155 let wrap_install t = t.wrap_install
1156 let post_install t = t.post_install
1157 let pre_remove t = t.pre_remove
1158 let wrap_remove t = t.wrap_remove
1159 let post_remove t = t.post_remove
1160 let pre_session t = t.pre_session
1161 let post_session t = t.post_session
1162
1163 let with_pre_build pre_build t = { t with pre_build }
1164 let with_wrap_build wrap_build t = { t with wrap_build }
1165 let with_post_build post_build t = { t with post_build }
1166 let with_pre_install pre_install t = { t with pre_install }
1167 let with_wrap_install wrap_install t = { t with wrap_install }
1168 let with_post_install post_install t = { t with post_install }
1169 let with_pre_remove pre_remove t = { t with pre_remove }
1170 let with_wrap_remove wrap_remove t = { t with wrap_remove }
1171 let with_post_remove post_remove t = { t with post_remove }
1172 let with_pre_session pre_session t = { t with pre_session }
1173 let with_post_session post_session t = { t with post_session }
1174
1175 let fields = [
1176 "pre-build-commands", Pp.ppacc
1177 with_pre_build pre_build
1178 (Pp.V.map_list ~depth:2 Pp.V.command);
1179 "pre-install-commands", Pp.ppacc
1180 with_pre_install pre_install
1181 (Pp.V.map_list ~depth:2 Pp.V.command);
1182 "pre-remove-commands", Pp.ppacc
1183 with_pre_remove pre_remove
1184 (Pp.V.map_list ~depth:2 Pp.V.command);
1185 "pre-session-commands", Pp.ppacc
1186 with_pre_session pre_session
1187 (Pp.V.map_list ~depth:2 Pp.V.command);
1188 "wrap-build-commands", Pp.ppacc
1189 with_wrap_build wrap_build
1190 (Pp.V.map_list ~depth:2 Pp.V.command);
1191 "wrap-install-commands", Pp.ppacc
1192 with_wrap_install wrap_install
1193 (Pp.V.map_list ~depth:2 Pp.V.command);
1194 "wrap-remove-commands", Pp.ppacc
1195 with_wrap_remove wrap_remove
1196 (Pp.V.map_list ~depth:2 Pp.V.command);
1197 "post-build-commands", Pp.ppacc
1198 with_post_build post_build
1199 (Pp.V.map_list ~depth:2 Pp.V.command);
1200 "post-install-commands", Pp.ppacc
1201 with_post_install post_install
1202 (Pp.V.map_list ~depth:2 Pp.V.command);
1203 "post-remove-commands", Pp.ppacc
1204 with_post_remove post_remove
1205 (Pp.V.map_list ~depth:2 Pp.V.command);
1206 "post-session-commands", Pp.ppacc
1207 with_post_session post_session
1208 (Pp.V.map_list ~depth:2 Pp.V.command);
1209 ]
1210
1211 let with_default ~default t =
1212 let f = function [] -> fun l -> l | l -> fun _ -> l in
1213 {
1214 pre_build = f t.pre_build default.pre_build;
1215 wrap_build = f t.wrap_build default.wrap_build;
1216 post_build = f t.post_build default.post_build;
1217 pre_install = f t.pre_install default.pre_install;
1218 wrap_install = f t.wrap_install default.wrap_install;
1219 post_install = f t.post_install default.post_install;
1220 pre_remove = f t.pre_remove default.pre_remove;
1221 wrap_remove = f t.wrap_remove default.wrap_remove;
1222 post_remove = f t.post_remove default.post_remove;
1223 pre_session = f t.pre_session default.pre_session;
1224 post_session = f t.post_session default.post_session;
1225 }
1226
1227 let add ~outer ~inner = {
1228 pre_build = outer.pre_build @ inner.pre_build;
1229 wrap_build = outer.wrap_build @ inner.wrap_build;
1230 post_build = inner.post_build @ outer.post_build;
1231 pre_install = outer.pre_install @ inner.pre_install;
1232 wrap_install = outer.wrap_install @ inner.wrap_install;
1233 post_install = inner.post_install @ outer.post_install;
1234 pre_remove = outer.pre_remove @ inner.pre_remove;
1235 wrap_remove = outer.wrap_remove @ inner.wrap_remove;
1236 post_remove = inner.post_remove @ outer.post_remove;
1237 pre_session = outer.pre_session @ inner.pre_session;
1238 post_session = inner.post_session @ outer.post_session;
1239 }
1240
1241 end
1242
1243 (** General opam configuration (config) *)
1244
1245 module ConfigSyntax = struct
1246
1247 let internal = "config"
1248 let atomic = false
1249 let format_version = OpamVersion.of_string "2.1"
1250 let file_format_version = OpamVersion.of_string "2.0"
1251 let root_version = OpamVersion.of_string "2.2~alpha"
1252
1253 let default_old_root_version = OpamVersion.of_string "2.1~~previous"
1254
1255 type t = {
1256 opam_version : opam_version;
1257 opam_root_version: opam_version;
1258 repositories : repository_name list;
1259 installed_switches : switch list;
1260 switch : switch option;
1261 jobs : int option;
1262 dl_tool : arg list option;
1263 dl_jobs : int;
1264 dl_cache : url list option;
1265 wrappers : Wrappers.t;
1266 solver_criteria : (solver_criteria * string) list;
1267 best_effort_prefix : string option;
1268 solver : arg list option;
1269 global_variables : (variable * variable_contents * string) list;
1270 eval_variables : (variable * string list * string) list;
1271 validation_hook : arg list option;
1272 default_compiler : formula;
1273 default_invariant : formula;
1274 depext: bool;
1275 depext_run_installs : bool;
1276 depext_cannot_install : bool;
1277 depext_bypass: OpamSysPkg.Set.t;
1278 sys_pkg_manager_cmd: filename OpamStd.String.Map.t;
1279 swh_fallback: bool;
1280 }
1281
1282 let opam_version t = t.opam_version
1283 let opam_root_version t = t.opam_root_version
1284 let opam_root_version_opt t =
1285 if OpamVersion.compare t.opam_root_version default_old_root_version = 0 then
1286 None else Some t.opam_root_version
1287 let repositories t = t.repositories
1288 let installed_switches t = t.installed_switches
1289 let switch t = t.switch
1290 let previous_switch t =
1291 let not_current = match t.switch with
1292 | Some switch -> fun x -> not (OpamSwitch.equal switch x)
1293 | None -> fun _ -> true
1294 in
1295 OpamStd.List.find_opt (fun switch ->
1296 not_current switch && not (OpamSwitch.is_external switch))
1297 t.installed_switches
1298 let jobs t = t.jobs
1299 let dl_tool t = t.dl_tool
1300 let dl_jobs t = t.dl_jobs
1301 let dl_cache t = OpamStd.Option.default [] t.dl_cache
1302 let criteria t = t.solver_criteria
1303 let best_effort_prefix t = t.best_effort_prefix
1304 let criterion kind t =
1305 try Some OpamStd.(List.assoc Compare.equal kind t.solver_criteria)
1306 with Not_found -> None
1307 let solver t = t.solver
1308 let wrappers t = t.wrappers
1309
1310 let global_variables t = t.global_variables
1311 let eval_variables t = t.eval_variables
1312
1313 let validation_hook t = t.validation_hook
1314 let default_compiler t = t.default_compiler
1315 let default_invariant t = t.default_invariant
1316
1317 let depext t = t.depext
1318 let depext_run_installs t = t.depext_run_installs
1319 let depext_cannot_install t = t.depext_cannot_install
1320 let depext_bypass t = t.depext_bypass
1321
1322 let sys_pkg_manager_cmd t = t.sys_pkg_manager_cmd
1323
1324 let swh_fallback t = t.swh_fallback
1325
1326 let with_opam_version opam_version t = { t with opam_version }
1327 let with_opam_root_version opam_root_version t = { t with opam_root_version }
1328 let with_repositories repositories t = { t with repositories }
1329 let with_installed_switches installed_switches t =
1330 { t with installed_switches }
1331 let with_switch switch t =
1332 let installed_switches =
1333 switch :: List.filter (fun x -> not (OpamSwitch.equal switch x))
1334 t.installed_switches
1335 in
1336 { t with switch = Some switch; installed_switches }
1337 let with_switch_opt switch_opt t =
1338 match switch_opt with
1339 | Some switch -> with_switch switch t
1340 | None -> { t with switch = None }
1341 let with_jobs jobs t = { t with jobs = Some jobs}
1342 let with_jobs_opt jobs t = { t with jobs }
1343 let with_dl_tool dl_tool t = { t with dl_tool = Some dl_tool }
1344 let with_dl_tool_opt dl_tool t = { t with dl_tool }
1345 let with_dl_jobs dl_jobs t = { t with dl_jobs }
1346 let with_dl_cache dl_cache t = { t with dl_cache = Some dl_cache }
1347 let with_criteria solver_criteria t = { t with solver_criteria }
1348 let with_criterion kind criterion t =
1349 let solver_criteria =
1350 (kind, criterion)::OpamStd.(List.remove_assoc Compare.equal
1351 kind t.solver_criteria)
1352 in
1353 { t with solver_criteria }
1354 let with_best_effort_prefix s t = { t with best_effort_prefix = Some s }
1355 let with_best_effort_prefix_opt s t = { t with best_effort_prefix = s }
1356 let with_solver solver t = { t with solver = Some solver }
1357 let with_solver_opt solver t = { t with solver }
1358 let with_wrappers wrappers t = { t with wrappers }
1359 let with_global_variables global_variables t = { t with global_variables }
1360 let with_eval_variables eval_variables t = { t with eval_variables }
1361 let with_validation_hook validation_hook t =
1362 { t with validation_hook = Some validation_hook}
1363 let with_validation_hook_opt validation_hook t = { t with validation_hook }
1364 let with_default_compiler default_compiler t = { t with default_compiler }
1365 let with_default_invariant default_invariant t = { t with default_invariant }
1366 let with_depext depext t = { t with depext }
1367 let with_depext_run_installs depext_run_installs t =
1368 { t with depext_run_installs }
1369 let with_depext_cannot_install depext_cannot_install t =
1370 { t with depext_cannot_install }
1371 let with_depext_bypass depext_bypass t = { t with depext_bypass }
1372 let with_sys_pkg_manager_cmd sys_pkg_manager_cmd t =
1373 { t with sys_pkg_manager_cmd }
1374 let with_swh_fallback swh_fallback t = { t with swh_fallback }
1375
1376 let empty = {
1377 opam_version = file_format_version;
1378 opam_root_version = default_old_root_version;
1379 repositories = [];
1380 installed_switches = [];
1381 switch = None;
1382 jobs = None;
1383 dl_tool = None;
1384 dl_jobs = 1;
1385 dl_cache = None;
1386 solver_criteria = [];
1387 best_effort_prefix = None;
1388 solver = None;
1389 wrappers = Wrappers.empty;
1390 global_variables = [];
1391 eval_variables = [];
1392 validation_hook = None;
1393 default_compiler = OpamFormula.Empty;
1394 default_invariant = OpamFormula.Empty;
1395 depext = true;
1396 depext_run_installs = true;
1397 depext_cannot_install = false;
1398 depext_bypass = OpamSysPkg.Set.empty;
1399 sys_pkg_manager_cmd = OpamStd.String.Map.empty;
1400 swh_fallback = true;
1401 }
1402
1403 (* When adding a field, make sure to add it in
1404 [OpamConfigCommand.global_allowed_fields] if it is a user modifiable field.
1405 When creating sections, make sure to update
1406 [OpamConfigCommand.global_allowed_sections] and
1407 [OpamConfigCommand.get_scope]. *)
1408 let fields =
1409 let with_switch sw t =
1410 if t.switch = None then with_switch sw t
1411 else Pp.bad_format "Multiple switch specifications"
1412 in
1413 [
1414 "opam-version", Pp.ppacc
1415 with_opam_version opam_version
1416 (Pp.V.string -| Pp.of_module "opam-version" (module OpamVersion));
1417 "opam-root-version", Pp.ppacc
1418 with_opam_root_version opam_root_version
1419 (Pp.V.string -| Pp.of_module "opam-version" (module OpamVersion));
1420 "repositories", Pp.ppacc
1421 with_repositories repositories
1422 (Pp.V.map_list ~depth:1
1423 (Pp.V.string -|
1424 Pp.of_module "repository" (module OpamRepositoryName)));
1425 "installed-switches", Pp.ppacc
1426 with_installed_switches installed_switches
1427 (Pp.V.map_list ~depth:1
1428 (Pp.V.string -|
1429 Pp.of_module "switch" (module OpamSwitch)));
1430 "switch", Pp.ppacc_opt
1431 with_switch switch
1432 (Pp.V.string -| Pp.of_module "switch" (module OpamSwitch));
1433 "jobs", Pp.ppacc_opt
1434 with_jobs jobs
1435 Pp.V.pos_int;
1436 "download-command", Pp.ppacc_opt
1437 with_dl_tool dl_tool
1438 (Pp.V.map_list ~depth:1 Pp.V.arg);
1439 "download-jobs", Pp.ppacc
1440 with_dl_jobs dl_jobs
1441 Pp.V.pos_int;
1442 "archive-mirrors", Pp.ppacc_opt
1443 with_dl_cache (fun t -> t.dl_cache)
1444 (Pp.V.map_list ~depth:1 Pp.V.url);
1445 "solver-criteria", Pp.ppacc_opt
1446 (with_criterion `Default) (criterion `Default)
1447 Pp.V.string;
1448 "solver-upgrade-criteria", Pp.ppacc_opt
1449 (with_criterion `Upgrade) (criterion `Upgrade)
1450 Pp.V.string;
1451 "solver-fixup-criteria", Pp.ppacc_opt
1452 (with_criterion `Fixup) (criterion `Fixup)
1453 Pp.V.string;
1454 "best-effort-prefix-criteria", Pp.ppacc_opt
1455 with_best_effort_prefix best_effort_prefix
1456 Pp.V.string;
1457 "solver", Pp.ppacc_opt
1458 with_solver solver
1459 (Pp.V.map_list ~depth:1 Pp.V.arg);
1460 "global-variables", Pp.ppacc
1461 with_global_variables global_variables
1462 (Pp.V.map_list ~depth:2
1463 (Pp.V.map_triple
1464 (Pp.V.ident -| Pp.of_module "variable" (module OpamVariable))
1465 Pp.V.variable_contents
1466 Pp.V.string));
1467 "eval-variables", Pp.ppacc
1468 with_eval_variables eval_variables
1469 (Pp.V.map_list ~depth:2
1470 (Pp.V.map_triple
1471 (Pp.V.ident -| Pp.of_module "variable" (module OpamVariable))
1472 (Pp.V.map_list Pp.V.string)
1473 Pp.V.string));
1474 "repository-validation-command", Pp.ppacc_opt
1475 with_validation_hook validation_hook
1476 (Pp.V.map_list ~depth:1 Pp.V.arg);
1477 "default-compiler", Pp.ppacc
1478 with_default_compiler default_compiler
1479 (Pp.V.package_formula `Disj Pp.V.(constraints Pp.V.version));
1480 "default-invariant", Pp.ppacc
1481 with_default_invariant default_invariant
1482 (Pp.V.package_formula `Conj Pp.V.(constraints Pp.V.version));
1483 "depext", Pp.ppacc
1484 with_depext depext
1485 Pp.V.bool;
1486 "depext-run-installs", Pp.ppacc
1487 with_depext_run_installs depext_run_installs
1488 Pp.V.bool;
1489 "depext-cannot-install", Pp.ppacc
1490 with_depext_cannot_install depext_cannot_install
1491 Pp.V.bool;
1492 "depext-bypass", Pp.ppacc
1493 with_depext_bypass depext_bypass
1494 (Pp.V.map_list ~depth:1
1495 (Pp.V.string -| Pp.of_module "sys-package" (module OpamSysPkg)) -|
1496 Pp.of_pair "System package set" OpamSysPkg.Set.(of_list, elements));
1497 "sys-pkg-manager-cmd", Pp.ppacc
1498 with_sys_pkg_manager_cmd sys_pkg_manager_cmd
1499 ((Pp.V.map_list ~depth:2
1500 (Pp.V.map_pair
1501 Pp.V.string
1502 (Pp.V.string -| Pp.of_module "filename" (module OpamFilename))))
1503 -| Pp.of_pair "Distribution Map" OpamStd.String.Map.(of_list, bindings));
1504 "swh-fallback", Pp.ppacc
1505 with_swh_fallback swh_fallback
1506 Pp.V.bool;
1507
1508 (* deprecated fields *)
1509 "alias", Pp.ppacc_opt
1510 with_switch OpamStd.Option.none
1511 (Pp.V.string -| Pp.of_module "switch-name" (module OpamSwitch));
1512 "ocaml-version", Pp.ppacc_opt
1513 with_switch OpamStd.Option.none
1514 (Pp.V.string -| Pp.of_module "switch-name" (module OpamSwitch));
1515 "cores", Pp.ppacc_opt
1516 with_jobs OpamStd.Option.none
1517 Pp.V.pos_int;
1518 "system-ocaml-version", Pp.ppacc_ignore;
1519 ] @
1520 List.map
1521 (fun (fld, ppacc) -> fld, Pp.embed with_wrappers wrappers ppacc)
1522 Wrappers.fields
1523
1524 let pp_cond ?f ?condition () =
1525 let name = internal in
1526 let format_version = file_format_version in
1527 Pp.I.map_file @@
1528 Pp.I.check_opam_version ?f ~format_version () -|
1529 Pp.I.opam_version ~format_version () -|
1530 Pp.I.fields ~name ~empty fields -|
1531 Pp.I.show_errors ~name ?condition ()
1532
1533 let pp = pp_cond ()
1534
1535 let to_list = Syntax.to_list pp
1536 end
1537
1538 module Config = struct
1539 include ConfigSyntax
1540 include SyntaxFile(ConfigSyntax)
1541
1542 module BestEffort = MakeBestEffort(ConfigSyntax)
1543
1544 let raw_root_version f =
1545 try
1546 let opamfile = OpamParser.file (OpamFilename.to_string (filename f)) in
1547 Some (OpamStd.List.find_map (function
1548 | { pelem = Variable ({ pelem = "opam-root-version"; _},
1549 {pelem = String version; _}); _} ->
1550 Some (OpamVersion.of_string version)
1551 | _ -> None)
1552 opamfile.file_contents)
1553 with
1554 | Sys_error _ | Not_found -> None
1555 end
1556
1557 module InitConfigSyntax = struct
1558 let internal = "init-config"
1559 let atomic = false
1560 let format_version = OpamVersion.of_string "2.0"
1561
1562 type t = {
1563 opam_version : opam_version;
1564 repositories : (repository_name * (url * trust_anchors option)) list;
1565 default_compiler : formula;
1566 default_invariant : formula;
1567 jobs : int option;
1568 dl_tool : arg list option;
1569 dl_jobs : int option;
1570 dl_cache : url list option;
1571 solver_criteria : (solver_criteria * string) list;
1572 solver : arg list option;
1573 wrappers : Wrappers.t;
1574 global_variables : (variable * variable_contents * string) list;
1575 eval_variables : (variable * string list * string) list;
1576 recommended_tools : (string list * string option * filter option) list;
1577 required_tools : (string list * string option * filter option) list;
1578 init_scripts : ((string * string) * filter option) list;
1579 }
1580
1581 let opam_version t = t.opam_version
1582 let repositories t = t.repositories
1583 let default_compiler t = t.default_compiler
1584 let default_invariant t = t.default_invariant
1585 let jobs t = t.jobs
1586 let dl_tool t = t.dl_tool
1587 let dl_jobs t = t.dl_jobs
1588 let dl_cache t = OpamStd.Option.default [] t.dl_cache
1589 let solver_criteria t = t.solver_criteria
1590 let solver t = t.solver
1591 let wrappers t = t.wrappers
1592 let global_variables t = t.global_variables
1593 let eval_variables t = t.eval_variables
1594 let recommended_tools t = t.recommended_tools
1595 let required_tools t = t.required_tools
1596 let init_scripts t = t.init_scripts
1597 let criterion kind t =
1598 OpamStd.(List.assoc_opt Compare.equal kind t.solver_criteria)
1599
1600 let with_opam_version opam_version t = {t with opam_version}
1601 let with_repositories repositories t = {t with repositories}
1602 let with_default_compiler default_compiler t = {t with default_compiler}
1603 let with_default_invariant default_invariant t = {t with default_invariant}
1604 let with_jobs jobs t = {t with jobs}
1605 let with_dl_tool dl_tool t = {t with dl_tool}
1606 let with_dl_jobs dl_jobs t = {t with dl_jobs}
1607 let with_dl_cache dl_cache t = {t with dl_cache = Some dl_cache}
1608 let with_solver_criteria solver_criteria t = {t with solver_criteria}
1609 let with_solver solver t = {t with solver}
1610 let with_wrappers wrappers t = {t with wrappers}
1611 let with_global_variables global_variables t = {t with global_variables}
1612 let with_eval_variables eval_variables t = {t with eval_variables}
1613 let with_recommended_tools recommended_tools t = {t with recommended_tools}
1614 let with_required_tools required_tools t = {t with required_tools}
1615 let with_init_scripts init_scripts t = {t with init_scripts}
1616 let with_criterion kind criterion t =
1617 let solver_criteria =
1618 (kind, criterion)::OpamStd.(List.remove_assoc Compare.equal
1619 kind t.solver_criteria)
1620 in
1621 { t with solver_criteria }
1622
1623 let empty = {
1624 opam_version = format_version;
1625 repositories = [];
1626 default_compiler = OpamFormula.Empty;
1627 default_invariant = OpamFormula.Empty;
1628 jobs = None;
1629 dl_tool = None;
1630 dl_jobs = None;
1631 dl_cache = None;
1632 solver_criteria = [];
1633 solver = None;
1634 wrappers = Wrappers.empty;
1635 global_variables = [];
1636 eval_variables = [];
1637 recommended_tools = [];
1638 required_tools = [];
1639 init_scripts = [];
1640 }
1641
1642 let pp_repository_def =
1643 Pp.V.map_options_3
1644 (Pp.V.string -|
1645 Pp.of_module "repository" (module OpamRepositoryName))
1646 (Pp.opt @@ Pp.singleton -| Pp.V.url)
1647 (Pp.map_list Pp.V.string)
1648 (Pp.opt @@
1649 Pp.singleton -| Pp.V.int -|
1650 OpamPp.check ~name:"quorum" ~errmsg:"quorum must be >= 0" ((<=) 0)) -|
1651 Pp.pp
1652 (fun ~pos:_ (name, url, fingerprints, quorum) ->
1653 name, url,
1654 match fingerprints with [] -> None | fingerprints ->
1655 Some {fingerprints; quorum = OpamStd.Option.default 1 quorum})
1656 (fun (name, url, ta) -> match ta with
1657 | Some ta -> name, url, ta.fingerprints, Some ta.quorum
1658 | None -> name, url, [], None)
1659
1660 let fields =
1661 [
1662 "opam-version", Pp.ppacc
1663 with_opam_version opam_version
1664 (Pp.V.string -| Pp.of_module "opam-version" (module OpamVersion));
1665 "repositories", Pp.ppacc
1666 with_repositories repositories
1667 (Pp.V.map_list ~depth:1 @@
1668 pp_repository_def -|
1669 Pp.pp (fun ~pos -> function
1670 | (name, Some url, ta) -> (name, (url, ta))
1671 | (_, None, _) -> Pp.bad_format ~pos "Missing repository URL")
1672 (fun (name, (url, ta)) -> (name, Some url, ta)));
1673 "default-compiler", Pp.ppacc
1674 with_default_compiler default_compiler
1675 (Pp.V.package_formula `Disj Pp.V.(constraints Pp.V.version));
1676 "default-invariant", Pp.ppacc
1677 with_default_invariant default_invariant
1678 (Pp.V.package_formula `Disj Pp.V.(constraints Pp.V.version));
1679 "jobs", Pp.ppacc_opt
1680 (with_jobs @* OpamStd.Option.some) jobs
1681 Pp.V.pos_int;
1682 "download-command", Pp.ppacc_opt
1683 (with_dl_tool @* OpamStd.Option.some) dl_tool
1684 (Pp.V.map_list ~depth:1 Pp.V.arg);
1685 "download-jobs", Pp.ppacc_opt
1686 (with_dl_jobs @* OpamStd.Option.some) dl_jobs
1687 Pp.V.pos_int;
1688 "archive-mirrors", Pp.ppacc
1689 with_dl_cache dl_cache
1690 (Pp.V.map_list ~depth:1 Pp.V.url);
1691 "solver-criteria", Pp.ppacc_opt
1692 (with_criterion `Default) (criterion `Default)
1693 Pp.V.string;
1694 "solver-upgrade-criteria", Pp.ppacc_opt
1695 (with_criterion `Upgrade) (criterion `Upgrade)
1696 Pp.V.string;
1697 "solver-fixup-criteria", Pp.ppacc_opt
1698 (with_criterion `Fixup) (criterion `Fixup)
1699 Pp.V.string;
1700 "solver", Pp.ppacc_opt
1701 (with_solver @* OpamStd.Option.some) solver
1702 (Pp.V.map_list ~depth:1 Pp.V.arg);
1703 "global-variables", Pp.ppacc
1704 with_global_variables global_variables
1705 (Pp.V.map_list ~depth:2
1706 (Pp.V.map_triple
1707 (Pp.V.ident -| Pp.of_module "variable" (module OpamVariable))
1708 Pp.V.variable_contents
1709 Pp.V.string));
1710 "eval-variables", Pp.ppacc
1711 with_eval_variables eval_variables
1712 (Pp.V.map_list ~depth:2
1713 (Pp.V.map_triple
1714 (Pp.V.ident -| Pp.of_module "variable" (module OpamVariable))
1715 (Pp.V.map_list Pp.V.string)
1716 Pp.V.string));
1717 "recommended-tools", Pp.ppacc
1718 with_recommended_tools recommended_tools
1719 (Pp.V.map_list
1720 (Pp.V.map_options_2
1721 (Pp.V.map_list ~depth:1 Pp.V.string)
1722 (Pp.opt @@ Pp.singleton -| Pp.V.string)
1723 (Pp.opt Pp.V.filter)));
1724 "required-tools", Pp.ppacc
1725 with_required_tools required_tools
1726 (Pp.V.map_list
1727 (Pp.V.map_options_2
1728 (Pp.V.map_list ~depth:1 Pp.V.string)
1729 (Pp.opt @@ Pp.singleton -| Pp.V.string)
1730 (Pp.opt Pp.V.filter)));
1731 "init-scripts", Pp.ppacc
1732 with_init_scripts init_scripts
1733 (Pp.V.map_list ~depth:2
1734 (Pp.V.map_option
1735 (Pp.V.map_pair
1736 (Pp.V.string)
1737 (Pp.V.string_tr))
1738 (Pp.opt Pp.V.filter)));
1739 ] @
1740 List.map
1741 (fun (fld, ppacc) -> fld, Pp.embed with_wrappers wrappers ppacc)
1742 Wrappers.fields
1743
1744
1745 let pp =
1746 let name = internal in
1747 Pp.I.map_file @@
1748 Pp.I.check_opam_version ~optional:true ~format_version () -|
1749 Pp.I.fields ~name ~empty fields -|
1750 Pp.I.show_errors ~name ~strict:true ()
1751
1752 let add t1 t2 =
1753 let opt = function None -> fun o -> o | some -> fun _ -> some in
1754 let list = function [] -> fun l -> l | l -> fun _ -> l in
1755 {
1756 opam_version = t2.opam_version;
1757 repositories = list t2.repositories t1.repositories;
1758 default_compiler =
1759 if t2.default_compiler <> Empty
1760 then t2.default_compiler else t1.default_compiler;
1761 default_invariant =
1762 if t2.default_invariant <> Empty
1763 then t2.default_invariant else t1.default_invariant;
1764 jobs = opt t2.jobs t1.jobs;
1765 dl_tool = opt t2.dl_tool t1.dl_tool;
1766 dl_jobs = opt t2.dl_jobs t1.dl_jobs;
1767 dl_cache = opt t2.dl_cache t1.dl_cache;
1768 solver_criteria =
1769 List.fold_left (fun acc c ->
1770 try
1771 (c, OpamStd.(List.assoc Compare.equal c t2.solver_criteria))::acc
1772 with Not_found ->
1773 try
1774 (c, OpamStd.(List.assoc Compare.equal c t1.solver_criteria))::acc
1775 with Not_found -> acc)
1776 [] [`Fixup; `Upgrade; `Default];
1777 solver = opt t2.solver t1.solver;
1778 wrappers = Wrappers.with_default ~default:t1.wrappers t2.wrappers;
1779 global_variables = list t2.global_variables t1.global_variables;
1780 eval_variables = list t2.eval_variables t1.eval_variables;
1781 recommended_tools = list t2.recommended_tools t1.recommended_tools;
1782 required_tools = list t2.required_tools t1.required_tools;
1783 init_scripts = list t2.init_scripts t1.init_scripts;
1784 }
1785
1786 end
1787 module InitConfig = struct
1788 include InitConfigSyntax
1789 include SyntaxFile(InitConfigSyntax)
1790 end
1791
1792 module Repos_configSyntax = struct
1793
1794 let internal = "repos-config"
1795 let atomic = false
1796 let format_version = OpamVersion.of_string "2.0"
1797 let file_format_version = OpamVersion.of_string "2.0"
1798
1799 type t = ((url * trust_anchors option) option) OpamRepositoryName.Map.t
1800
1801 let empty = OpamRepositoryName.Map.empty
1802
1803 let fields = [
1804 "repositories",
1805 Pp.ppacc (fun x _ -> x) (fun x -> x)
1806 ((Pp.V.map_list ~depth:1 @@
1807 InitConfigSyntax.pp_repository_def -|
1808 Pp.pp
1809 (fun ~pos:_ -> function
1810 | (name, Some url, ta) -> name, Some (url, ta)
1811 | (name, None, _) -> name, None)
1812 (fun (name, def) -> match def with
1813 | Some (url, ta) -> name, Some url, ta
1814 | None -> name, None, None)) -|
1815 Pp.of_pair "repository-url-list"
1816 OpamRepositoryName.Map.(of_list, bindings));
1817 ]
1818
1819 let pp_cond ?f ?condition () =
1820 let name = internal in
1821 let format_version = file_format_version in
1822 Pp.I.map_file @@
1823 Pp.I.check_opam_version ~optional:true ?f ~format_version () -|
1824 Pp.I.opam_version ~format_version ~undefined:true () -|
1825 Pp.I.fields ~name ~empty fields -|
1826 Pp.I.show_errors ~name ?condition ()
1827
1828 let pp = pp_cond ()
1829
1830 end
1831 module Repos_config = struct
1832 include Repos_configSyntax
1833 include SyntaxFile(Repos_configSyntax)
1834 module BestEffort = MakeBestEffort(Repos_configSyntax)
1835 end
1836
1837 module Switch_configSyntax = struct
1838
1839 let internal = "switch-config"
1840 let atomic = false
1841 let format_version = OpamVersion.of_string "2.1"
1842 let file_format_version = OpamVersion.of_string "2.0"
1843 let oldest_compatible_format_version = OpamVersion.of_string "2.0"
1844
1845 type t = {
1846 opam_version: OpamVersion.t;
1847 synopsis: string;
1848 repos: repository_name list option;
1849 paths: (std_path * string) list;
1850 variables: (variable * variable_contents) list;
1851 opam_root: dirname option;
1852 wrappers: Wrappers.t;
1853 env: env_update list;
1854 invariant: OpamFormula.t option;
1855 depext_bypass: OpamSysPkg.Set.t;
1856 }
1857
1858 let empty = {
1859 opam_version = file_format_version;
1860 synopsis = "";
1861 repos = None;
1862 paths = [];
1863 variables = [];
1864 opam_root = None;
1865 wrappers = Wrappers.empty;
1866 env = [];
1867 invariant = None;
1868 depext_bypass = OpamSysPkg.Set.empty;
1869 }
1870
1871 (* When adding a field or section, make sure to add it in
1872 [OpamConfigCommand.switch_allowed_fields] and
1873 [OpamConfigCommand.switch_allowed_sections] if it is a user modifiable
1874 field *)
1875 let sections = [
1876 "paths", Pp.ppacc
1877 (fun paths t -> {t with paths}) (fun t -> t.paths)
1878 (Pp.I.anonymous_section Pp.I.items -|
1879 Pp.map_list
1880 (Pp.map_pair
1881 (Pp.of_pair "std-path" (std_path_of_string, string_of_std_path))
1882 Pp.V.string));
1883 "variables", Pp.ppacc
1884 (fun variables t -> {t with variables}) (fun t -> t.variables)
1885 (Pp.I.anonymous_section Pp.I.items -|
1886 Pp.map_list
1887 (Pp.map_pair
1888 (Pp.of_module "variable" (module OpamVariable))
1889 Pp.V.variable_contents));
1890 ]
1891
1892 let fields = [
1893 "opam-version", Pp.ppacc
1894 (fun opam_version t -> {t with opam_version}) (fun t -> t.opam_version)
1895 (Pp.V.string -| Pp.of_module "opam-version" (module OpamVersion));
1896 "synopsis", Pp.ppacc
1897 (fun synopsis t -> {t with synopsis}) (fun t -> t.synopsis)
1898 Pp.V.string;
1899 "repositories",
1900 Pp.ppacc_opt (fun r t -> {t with repos = Some r}) (fun t -> t.repos)
1901 (Pp.V.map_list ~depth:1 @@
1902 Pp.V.string -| Pp.of_module "repo" (module OpamRepositoryName));
1903 "opam-root", Pp.ppacc_opt
1904 (fun r t -> {t with opam_root = Some r}) (fun t -> t.opam_root)
1905 (Pp.V.string -| Pp.of_module "dirname" (module OpamFilename.Dir));
1906 "setenv", Pp.ppacc
1907 (fun env t -> {t with env}) (fun t -> t.env)
1908 (Pp.V.map_list ~depth:2 Pp.V.env_binding);
1909 "invariant", Pp.ppacc_opt
1910 (fun inv t -> {t with invariant = Some inv }) (fun t -> t.invariant)
1911 (Pp.V.package_formula `Conj Pp.V.(constraints version));
1912 "depext-bypass", Pp.ppacc
1913 (fun depext_bypass t -> { t with depext_bypass})
1914 (fun t -> t.depext_bypass)
1915 (Pp.V.map_list ~depth:1
1916 (Pp.V.string -| Pp.of_module "sys-package" (module OpamSysPkg)) -|
1917 Pp.of_pair "System package set" OpamSysPkg.Set.(of_list, elements));
1918 ] @
1919 List.map
1920 (fun (fld, ppacc) ->
1921 fld, Pp.embed (fun wrappers t -> {t with wrappers}) (fun t -> t.wrappers) ppacc)
1922 Wrappers.fields
1923
1924 let pp_cond ?f ?condition () =
1925 let name = internal in
1926 let format_version = file_format_version in
1927 Pp.I.map_file @@
1928 Pp.I.check_opam_version ?f ~format_version () -|
1929 Pp.I.opam_version ~format_version () -|
1930 Pp.I.fields ~name ~empty ~sections fields -|
1931 Pp.I.show_errors ~name ?condition ()
1932
1933 let pp = pp_cond ()
1934
1935 let variable t s =
1936 OpamStd.List.assoc_opt OpamVariable.equal s t.variables
1937
1938 let path t p =
1939 OpamStd.(List.assoc_opt Compare.equal p t.paths)
1940
1941 let wrappers t = t.wrappers
1942
1943 let to_list = Syntax.to_list pp
1944 end
1945
1946 module Switch_config = struct
1947 include Switch_configSyntax
1948 include SyntaxFile(Switch_configSyntax)
1949 module BestEffort = MakeBestEffort(Switch_configSyntax)
1950 end
1951
1952 module SwitchSelectionsSyntax = struct
1953
1954 let internal = "switch-state"
1955 let atomic = false
1956 let format_version = OpamVersion.of_string "2.0"
1957 let file_format_version = OpamVersion.of_string "2.0"
1958
1959 type t = switch_selections
1960
1961 let empty = {
1962 sel_installed = OpamPackage.Set.empty;
1963 sel_roots = OpamPackage.Set.empty;
1964 sel_compiler = OpamPackage.Set.empty;
1965 sel_pinned = OpamPackage.Set.empty;
1966 }
1967
1968 let pp_package =
1969 Pp.of_module "package" (module OpamPackage)
1970
1971 let pp_pkglist =
1972 Pp.V.map_list (Pp.V.string -| pp_package) -|
1973 Pp.pp (fun ~pos:_ -> OpamPackage.Set.of_list) OpamPackage.Set.elements
1974
1975 let fields = [
1976 "opam-version", Pp.ppacc
1977 (fun _ t -> t) (fun _ -> file_format_version)
1978 (Pp.V.string -| Pp.of_module "opam-version" (module OpamVersion));
1979 "compiler", Pp.ppacc
1980 (fun sel_compiler t -> {t with sel_compiler}) (fun t -> t.sel_compiler)
1981 pp_pkglist;
1982 "roots", Pp.ppacc
1983 (fun sel_roots t -> {t with sel_roots})
1984 (fun t -> t.sel_roots)
1985 pp_pkglist;
1986 "installed", Pp.ppacc
1987 (fun installed t ->
1988 {t with sel_installed = installed})
1989 (fun t -> t.sel_installed)
1990 pp_pkglist;
1991 "pinned", Pp.ppacc
1992 (fun sel_pinned t -> {t with sel_pinned}) (fun t -> t.sel_pinned)
1993 (Pp.V.map_list ~depth:1
1994 (Pp.V.option -|
1995 (* The contents of the option is obsolete, the information is now
1996 contained in the overlay only *)
1997 Pp.pp (fun ~pos:_ (nv,_) -> nv) (fun nv -> nv, []) -|
1998 Pp.V.string -| pp_package) -|
1999 Pp.of_pair "Package set" OpamPackage.Set.(of_list, elements))
2000 ]
2001
2002 let pp_cond ?f ?condition () =
2003 let name = "switch-state" in
2004 let format_version = file_format_version in
2005 Pp.I.map_file @@
2006 Pp.I.check_opam_version ~optional:true ?f ~format_version () -|
2007 Pp.I.opam_version ~format_version () -|
2008 Pp.I.fields ~name ~empty fields -|
2009 Pp.I.show_errors ~name ?condition ()
2010
2011 let pp = pp_cond ()
2012
2013 end
2014
2015 module SwitchSelections = struct
2016 type t = switch_selections
2017 include SyntaxFile(SwitchSelectionsSyntax)
2018 module BestEffort = MakeBestEffort(SwitchSelectionsSyntax)
2019 end
2020
2021 (** Local repository config file (repo/<repo>/config) *)
2022
2023 module Repo_config_legacySyntax = struct
2024
2025 let internal = "repo-file"
2026 let atomic = false
2027 let format_version = OpamVersion.of_string "1.2"
2028
2029 type t = {
2030 repo_name : repository_name;
2031 repo_root : dirname;
2032 repo_url : url;
2033 repo_priority : int;
2034 }
2035
2036 let empty = {
2037 repo_name = OpamRepositoryName.of_string "<none>";
2038 repo_url = OpamUrl.empty;
2039 repo_root = OpamFilename.raw_dir "<none>";
2040 repo_priority = 0;
2041 }
2042
2043 let fields = [
2044 "name", Pp.ppacc
2045 (fun repo_name (r:t) -> {r with repo_name})
2046 (fun r -> r.repo_name)
2047 (Pp.V.string -|
2048 Pp.of_module "repository-name" (module OpamRepositoryName));
2049 "address", Pp.ppacc
2050 (fun repo_url (r:t) -> {r with repo_url})
2051 (fun r -> r.repo_url)
2052 Pp.V.url;
2053 "kind", Pp.ppacc_opt (* deprecated *)
2054 (fun backend (r:t) ->
2055 {r with repo_url = {r.repo_url with OpamUrl.backend}})
2056 OpamStd.Option.none
2057 (Pp.V.string -|
2058 Pp.of_pair "repository-kind"
2059 OpamUrl.(backend_of_string, string_of_backend));
2060 "priority", Pp.ppacc
2061 (fun repo_priority (r:t) -> {r with repo_priority})
2062 (fun r -> r.repo_priority)
2063 Pp.V.int;
2064 "root", Pp.ppacc
2065 (fun repo_root (r:t) -> {r with repo_root})
2066 (fun r -> r.repo_root)
2067 (Pp.V.string -|
2068 Pp.of_module "directory" (module OpamFilename.Dir));
2069 ]
2070
2071 let pp =
2072 let name = internal in
2073 Pp.I.map_file @@
2074 Pp.I.fields ~name ~empty ~mandatory_fields:["root";"address";"name"]
2075 fields -|
2076 Pp.I.show_errors ~name ~strict:true ()
2077
2078 end
2079 module Repo_config_legacy = struct
2080 include Repo_config_legacySyntax
2081 include SyntaxFile(Repo_config_legacySyntax)
2082 end
2083
2084 (** Global or package switch-local configuration variables.
2085 (<switch>/config/global-config.config,
2086 <switch>/lib/<pkgname>/opam.config) *)
2087
2088 module Dot_configSyntax = struct
2089
2090 let internal = ".config"
2091 let atomic = false
2092 let format_version = OpamVersion.of_string "2.0"
2093
2094 type t = {
2095 vars: (variable * variable_contents) list;
2096 file_depends: (filename * OpamHash.t) list;
2097 }
2098
2099 let empty = {
2100 vars = [];
2101 file_depends = [];
2102 }
2103
2104 let create vars = { empty with vars }
2105
2106 let vars t = t.vars
2107 let with_vars vars t = { t with vars }
2108
2109 let file_depends t = t.file_depends
2110 let with_file_depends file_depends t = { t with file_depends }
2111
2112 let pp_variables =
2113 Pp.I.items -|
2114 Pp.map_list
2115 (Pp.map_pair
2116 (Pp.of_module "variable" (module OpamVariable))
2117 Pp.V.variable_contents)
2118
2119 let pp_contents =
2120 Pp.I.fields ~name:"config-file" ~empty
2121 ~sections:[
2122 "variables", Pp.ppacc with_vars vars
2123 (Pp.I.anonymous_section pp_variables)
2124 ]
2125 [
2126 "opam-version", Pp.ppacc (fun _ t -> t) (fun _ -> format_version)
2127 (Pp.V.string -| Pp.of_module "opam-version" (module OpamVersion));
2128 "file-depends", Pp.ppacc with_file_depends file_depends
2129 (Pp.V.map_list ~depth:2 @@ Pp.V.map_pair
2130 (Pp.V.string -| Pp.of_module "path" (module OpamFilename))
2131 (Pp.V.string -| Pp.of_module "checksum" (module OpamHash)))
2132 ]
2133 -| Pp.I.show_errors ~name:internal ()
2134
2135 (* Files with the variables at toplevel and no other fields are allowed for
2136 backwards-compat, when opam-version is unset or too old *)
2137 let pp =
2138 Pp.I.map_file @@
2139 Pp.I.check_opam_version ~format_version ~optional:true () -|
2140 Pp.I.opam_version ~format_version () -|
2141 Pp.I.field "opam-version"
2142 (Pp.parse
2143 (Pp.V.string -| Pp.of_module "opam-version" (module OpamVersion)))
2144 -| Pp.pp
2145 (fun ~pos (opam_version_opt, s) ->
2146 match opam_version_opt with
2147 | Some v when
2148 OpamVersion.compare v (OpamVersion.of_string "1.3~dev3") > 0 ->
2149 Pp.parse ~pos pp_contents s
2150 | _ -> {empty with vars = Pp.parse ~pos pp_variables s})
2151 (fun t -> None, Pp.print pp_contents t)
2152
2153
2154 let variables t = List.rev_map fst t.vars
2155
2156 let bindings t = t.vars
2157
2158 let variable t s =
2159 try Some (OpamStd.List.assoc OpamVariable.equal s t.vars)
2160 with Not_found -> None
2161
2162 let set k v t =
2163 let vars = OpamStd.List.remove_assoc OpamVariable.equal k t.vars in
2164 let vars =
2165 match v with
2166 | Some v -> (k,v) :: vars
2167 | None -> vars
2168 in
2169 { t with vars }
2170
2171 end
2172 module Dot_config = struct
2173 include Dot_configSyntax
2174 include SyntaxFile(Dot_configSyntax)
2175 end
2176
2177 (** (2) General, public repository format *)
2178
2179 (** Public repository definition file (<repo>/repo) *)
2180
2181 module RepoSyntax = struct
2182
2183 let internal = "repo"
2184 let atomic = false
2185 let format_version = OpamVersion.of_string "2.0"
2186
2187 type t = {
2188 opam_version : OpamVersion.t option;
2189 browse : string option;
2190 upstream : string option;
2191 redirect : (string * filter option) list;
2192 root_url : url option;
2193 dl_cache : string list option;
2194 announce : (string * filter option) list;
2195 stamp : string option;
2196 }
2197
2198 let create
2199 ?browse ?upstream ?opam_version
2200 ?(redirect=[]) ?root_url ?dl_cache ?(announce=[]) ?stamp () =
2201 { opam_version; browse; upstream; redirect; root_url; dl_cache; announce;
2202 stamp; }
2203
2204 let empty = create ()
2205
2206 let opam_version t = t.opam_version
2207 let browse t = t.browse
2208 let upstream t = t.upstream
2209 let redirect t = t.redirect
2210 let root_url t = t.root_url
2211 let dl_cache t = OpamStd.Option.default [] t.dl_cache
2212 let announce t = t.announce
2213 let stamp t = t.stamp
2214
2215 let with_opam_version opam_version t =
2216 { t with opam_version = Some opam_version }
2217 let with_browse browse t = { t with browse = Some browse }
2218 let with_upstream upstream t = { t with upstream = Some upstream }
2219 let with_redirect redirect t = { t with redirect }
2220 let with_root_url root_url t = { t with root_url = Some root_url }
2221 let with_dl_cache dl_cache t = { t with dl_cache = Some dl_cache }
2222 let with_announce announce t = { t with announce }
2223 let with_stamp id t = { t with stamp = Some id }
2224 let with_stamp_opt stamp t = { t with stamp }
2225
2226 let fields = [
2227 "opam-version", Pp.ppacc_opt
2228 with_opam_version opam_version
2229 (Pp.V.string -| Pp.of_module "opam-version" (module OpamVersion));
2230 "browse", Pp.ppacc_opt with_browse browse Pp.V.string;
2231 "upstream", Pp.ppacc_opt with_upstream upstream Pp.V.string;
2232 "redirect", Pp.ppacc
2233 with_redirect redirect
2234 (Pp.V.map_list ~depth:1
2235 (Pp.V.map_option Pp.V.string (Pp.opt Pp.V.filter)));
2236 "archive-mirrors", Pp.ppacc
2237 with_dl_cache dl_cache
2238 (Pp.V.map_list ~depth:1 Pp.V.string);
2239 "announce", Pp.ppacc
2240 with_announce announce
2241 (Pp.V.map_list ~depth:1
2242 (Pp.V.map_option Pp.V.string (Pp.opt Pp.V.filter)));
2243 "stamp", Pp.ppacc_opt
2244 with_stamp stamp
2245 Pp.V.string
2246 ]
2247
2248 let pp =
2249 let name = internal in
2250 Pp.I.map_file @@
2251 Pp.I.check_opam_version ~format_version ~optional:true () -|
2252 Pp.I.opam_version ~format_version () -|
2253 Pp.I.fields ~name ~empty fields -|
2254 Pp.I.show_errors ~name
2255 ~condition:(function
2256 | {opam_version = Some v; _} ->
2257 OpamVersion.(compare format_version v) >= 0
2258 | _ -> true)
2259 ()
2260
2261 end
2262 module Repo = struct
2263 include RepoSyntax
2264 include SyntaxFile(RepoSyntax)
2265 end
2266
2267
2268 (** Package url field in opam file. Formerly, file
2269 (<repo>/packages/.../url) *)
2270
2271 module URLSyntax = struct
2272
2273 let internal = "url-file"
2274 let atomic = false
2275 let format_version = OpamVersion.of_string "1.2"
2276
2277 type t = {
2278 url : url;
2279 mirrors : url list;
2280 checksum: OpamHash.t list;
2281 swhid: OpamSWHID.t option;
2282 errors : (string * Pp.bad_format) list;
2283 subpath : subpath option;
2284 }
2285
2286 let create ?(mirrors=[]) ?(checksum=[]) ?swhid ?subpath url =
2287 {
2288 url; mirrors; checksum; swhid; errors = []; subpath;
2289 }
2290
2291 let empty = {
2292 url = OpamUrl.empty;
2293 mirrors = [];
2294 checksum= [];
2295 swhid = None;
2296 errors = [];
2297 subpath = None;
2298 }
2299
2300 let url t = t.url
2301 let mirrors t = t.mirrors
2302 let checksum t = t.checksum
2303 let swhid t = t.swhid
2304 let subpath t = t.subpath
2305
2306 let with_url url t = { t with url }
2307 let with_mirrors mirrors t = { t with mirrors }
2308 let with_checksum checksum t = { t with checksum = checksum }
2309 let with_swhid swhid t = { t with swhid = Some swhid }
2310 let with_swhid_opt swhid t = { t with swhid = swhid }
2311 let with_subpath subpath t = { t with subpath = Some subpath }
2312 let with_subpath_opt subpath t = { t with subpath = subpath }
2313
2314 let fields =
2315 let with_url url t =
2316 if t.url <> OpamUrl.empty then Pp.bad_format "Too many URLS"
2317 else with_url url t
2318 in
2319 [
2320 "src", Pp.ppacc with_url url
2321 Pp.V.url;
2322 "archive", Pp.ppacc_opt with_url OpamStd.Option.none
2323 (Pp.V.url_with_backend `http);
2324 "http", Pp.ppacc_opt with_url OpamStd.Option.none
2325 (Pp.V.url_with_backend `http);
2326 "git", Pp.ppacc_opt with_url OpamStd.Option.none
2327 (Pp.V.url_with_backend `git);
2328 "darcs", Pp.ppacc_opt with_url OpamStd.Option.none
2329 (Pp.V.url_with_backend `darcs);
2330 "hg", Pp.ppacc_opt with_url OpamStd.Option.none
2331 (Pp.V.url_with_backend `hg);
2332 "local", Pp.ppacc_opt with_url OpamStd.Option.none
2333 (Pp.V.url_with_backend `rsync);
2334 "checksum", Pp.ppacc with_checksum checksum
2335 (Pp.V.map_list ~depth:1
2336 (Pp.V.string -| Pp.of_module "checksum" (module OpamHash)));
2337 "mirrors", Pp.ppacc with_mirrors mirrors
2338 (Pp.V.map_list ~depth:1 Pp.V.url);
2339 "subpath", Pp.ppacc_opt
2340 with_subpath subpath
2341 (Pp.V.string -| Pp.of_module "subpath" (module OpamFilename.SubPath));
2342 ]
2343
2344 let pp_contents =
2345 let name = internal in
2346 Pp.I.fields ~name ~empty fields -|
2347 Pp.I.on_errors ~name (fun t e -> {t with errors = e::t.errors}) -|
2348 Pp.pp ~name
2349 (fun ~pos t ->
2350 if t.url = OpamUrl.empty then OpamPp.bad_format ~pos "missing URL"
2351 else t)
2352 (fun x -> x) -|
2353 Pp.pp ~name
2354 (fun ~pos:_ t ->
2355 let swhid, mirrors = OpamStd.List.pick OpamSWHID.is_valid t.mirrors in
2356 match swhid with
2357 | None -> t
2358 | Some swhid_url ->
2359 (match OpamSWHID.of_url swhid_url with
2360 | None ->
2361 Pp.warn "Bad format of SWHID url: %s" (OpamUrl.to_string swhid_url);
2362 t
2363 | swhid ->
2364 { t with swhid ; mirrors }))
2365 (fun t ->
2366 match t.swhid with
2367 | None -> t
2368 | Some swhid ->
2369 { t with
2370 swhid = None;
2371 mirrors = OpamSWHID.to_url swhid :: t.mirrors })
2372
2373 let pp = Pp.I.map_file pp_contents
2374
2375 end
2376 module URL = struct
2377 include URLSyntax
2378 include SyntaxFile(URLSyntax)
2379 end
2380
2381
2382
2383 (** (3) Opam package format *)
2384
2385 module OPAMSyntax = struct
2386
2387 let internal = "opam"
2388 let atomic = false
2389 let format_version = OpamVersion.of_string "2.0"
2390
2391 type t = {
2392 opam_version: opam_version;
2393
2394 (* Package ident *)
2395 name : OpamPackage.Name.t option;
2396 version : OpamPackage.Version.t option;
2397
2398 (* Relationships; solver and availability info *)
2399 depends : filtered_formula;
2400 depopts : filtered_formula;
2401 conflicts : filtered_formula;
2402 conflict_class : name list;
2403 available : filter;
2404 flags : package_flag list;
2405 env : env_update list;
2406
2407 (* Build instructions *)
2408 build : command list;
2409 run_test : command list;
2410 install : command list;
2411 remove : command list;
2412
2413 (* Auxiliary data affecting the build *)
2414 substs : basename list;
2415 patches : (basename * filter option) list;
2416 build_env : env_update list;
2417 features : (OpamVariable.t * filtered_formula * string) list;
2418 extra_sources: (basename * URL.t) list;
2419
2420 (* User-facing data used by opam *)
2421 messages : (string * filter option) list;
2422 post_messages: (string * filter option) list;
2423 depexts : (OpamSysPkg.Set.t * filter) list;
2424 libraries : (string * filter option) list;
2425 syntax : (string * filter option) list;
2426 dev_repo : url option;
2427 pin_depends: (package * url) list;
2428
2429 (* Package database details *)
2430 maintainer : string list;
2431 author : string list;
2432 license : string list;
2433 tags : string list;
2434 homepage : string list;
2435 doc : string list;
2436 bug_reports: string list;
2437
2438 (* Extension fields (x-foo: "bar") *)
2439 extensions : value OpamStd.String.Map.t;
2440
2441 (* Extra sections *)
2442 url : URL.t option;
2443 descr : Descr.t option;
2444
2445 (* Extra data, not actually file fields *)
2446
2447 (* Related metadata directory (not an actual field of the file)
2448 This can be used to locate e.g. the files/ overlays *)
2449 metadata_dir: (repository_name option * string) option;
2450
2451 (* Names and hashes of the files below files/ *)
2452 extra_files: (OpamFilename.Base.t * OpamHash.t) list option;
2453
2454 (* Origin *)
2455 locked: string option;
2456
2457 (* Stores any file errors for printing them later *)
2458 format_errors: (string * Pp.bad_format) list;
2459
2460 (* Deprecated, for compat and proper linting *)
2461 ocaml_version: (OpamFormula.relop * string) OpamFormula.formula option;
2462 os : (bool * string) generic_formula;
2463
2464 deprecated_build_test : command list;
2465 deprecated_build_doc : command list;
2466 }
2467
2468 let empty = {
2469 opam_version = format_version;
2470
2471 name = None;
2472 version = None;
2473
2474 depends = OpamFormula.Empty;
2475 depopts = OpamFormula.Empty;
2476 conflicts = OpamFormula.Empty;
2477 available = FBool true;
2478 flags = [];
2479 conflict_class = [];
2480 env = [];
2481
2482 build = [];
2483 run_test = [];
2484 install = [];
2485 remove = [];
2486
2487 substs = [];
2488 patches = [];
2489 build_env = [];
2490 features = [];
2491 extra_sources = [];
2492
2493 messages = [];
2494 post_messages = [];
2495 depexts = [];
2496 libraries = [];
2497 syntax = [];
2498 dev_repo = None;
2499 pin_depends= [];
2500
2501 maintainer = [];
2502 author = [];
2503 license = [];
2504 tags = [];
2505 homepage = [];
2506 doc = [];
2507 bug_reports = [];
2508
2509 extensions = OpamStd.String.Map.empty;
2510 url = None;
2511 descr = None;
2512
2513 metadata_dir = None;
2514 extra_files = None;
2515
2516 locked = None;
2517
2518 format_errors = [];
2519
2520 ocaml_version = None;
2521 os = Empty;
2522 deprecated_build_test = [];
2523 deprecated_build_doc = [];
2524 }
2525
2526 let create nv =
2527 let name = Some (nv.OpamPackage.name) in
2528 let version = Some (nv.OpamPackage.version) in
2529 { empty with name; version }
2530
2531 let check t name = function
2532 | None ->
2533 let pos =
2534 OpamStd.Option.Op.(>>|) t.metadata_dir @@ function
2535 | Some r, rel ->
2536 { pos_null with
2537 filename =
2538 Printf.sprintf "<%s>/%s/opam" (OpamRepositoryName.to_string r) rel }
2539 | None, d ->
2540 pos_file OpamFilename.Op.(OpamFilename.Dir.of_string d // "opam")
2541 in
2542 Pp.bad_format ?pos "Field '%s:' is required" name
2543 | Some n -> n
2544
2545 let ext_field_prefix = "x-"
2546 let is_ext_field = OpamStd.String.starts_with ~prefix:ext_field_prefix
2547
2548 (* Getters *)
2549
2550 let opam_version t = t.opam_version
2551 let name (t:t) = check t "name" t.name
2552 let name_opt (t:t) = t.name
2553 let version (t:t) = check t "version" t.version
2554 let version_opt (t:t) = t.version
2555 let package t = OpamPackage.create (name t) (version t)
2556
2557 let depends t = t.depends
2558 let depopts t = t.depopts
2559 let conflicts t = t.conflicts
2560 let conflict_class t = t.conflict_class
2561 let available t = t.available
2562 let flags t = t.flags
2563 let has_flag f t = List.mem f t.flags
2564 let env (t:t) =
2565 List.map
2566 (fun env -> match t.name, env with
2567 | Some name, (var,op,value,None) ->
2568 var, op, value,
2569 Some ("Updated by package " ^ OpamPackage.Name.to_string name)
2570 | _, b -> b)
2571 t.env
2572
2573 let build t = t.build
2574 let run_test t = t.deprecated_build_test @ t.run_test
2575 let deprecated_build_test t = t.deprecated_build_test
2576 let deprecated_build_doc t = t.deprecated_build_doc
2577 let install t = t.install
2578 let remove t = t.remove
2579
2580 let substs t = t.substs
2581 let patches t = t.patches
2582 let build_env t = t.build_env
2583 let features t = t.features
2584 let extra_sources t = t.extra_sources
2585
2586 let messages t = t.messages
2587 let post_messages t = t.post_messages
2588 let depexts t = t.depexts
2589 let libraries t = t.libraries
2590 let syntax t = t.syntax
2591 let dev_repo t = t.dev_repo
2592 let pin_depends t = t.pin_depends
2593
2594 let maintainer t = t.maintainer
2595 let author t = t.author
2596 let license t = t.license
2597 let tags t = t.tags
2598 let homepage t = t.homepage
2599 let doc t = t.doc
2600 let bug_reports t = t.bug_reports
2601
2602 let extensions t = t.extensions
2603 let extended t fld parse =
2604 if not (is_ext_field fld) then invalid_arg "OpamFile.OPAM.extended";
2605 try
2606 let s = OpamStd.String.Map.find fld t.extensions in
2607 (try Some (parse s) with
2608 | Pp.Bad_format _ as e -> raise (Pp.add_pos s.pos e))
2609 with Not_found -> None
2610
2611 let url t = t.url
2612 let descr t = t.descr
2613 let synopsis t = OpamStd.Option.map Descr.synopsis t.descr
2614 let descr_body t = match t.descr with
2615 | None | Some (_, "") -> None
2616 | Some (_, text) -> Some text
2617 let get_url t = match url t with Some u -> Some (URL.url u) | None -> None
2618
2619 let format_errors t = t.format_errors
2620
2621 let metadata_dir t = t.metadata_dir
2622 let extra_files t = t.extra_files
2623
2624 let locked t = t.locked
2625
2626 (* Setters *)
2627
2628 let with_opam_version opam_version t = { t with opam_version }
2629
2630 let with_name name (t:t) = { t with name = Some name }
2631 let with_name_opt name (t:t) = { t with name }
2632 let with_version version (t:t) = { t with version = Some version }
2633 let with_version_opt version (t:t) = { t with version }
2634 let with_nv nv (t:t) =
2635 { t with name = Some (nv.OpamPackage.name);
2636 version = Some (nv.OpamPackage.version) }
2637
2638 let with_depends depends t = { t with depends }
2639 let with_depopts depopts t = { t with depopts }
2640 let with_conflicts conflicts t = {t with conflicts }
2641 let with_conflict_class conflict_class t = { t with conflict_class }
2642 let with_available available t = { t with available }
2643 let with_flags flags t = { t with flags }
2644 let add_flags flags t =
2645 { t with flags = OpamStd.List.sort_nodup compare (flags @ t.flags) }
2646 let with_env env t = { t with env }
2647
2648 let with_build build t = { t with build }
2649 let with_run_test run_test t = { t with run_test }
2650 let with_deprecated_build_test deprecated_build_test t =
2651 { t with deprecated_build_test }
2652 let with_deprecated_build_doc deprecated_build_doc t =
2653 { t with deprecated_build_doc }
2654 let with_install install t = { t with install }
2655 let with_remove remove t = { t with remove }
2656
2657 let with_substs substs t = { t with substs }
2658 let with_patches patches t = { t with patches }
2659 let with_build_env build_env t = { t with build_env }
2660 let with_features features t = {t with features }
2661 let with_extra_sources extra_sources t = { t with extra_sources }
2662
2663 let with_messages messages t = { t with messages }
2664 let with_post_messages post_messages t = { t with post_messages }
2665 let with_depexts depexts t = { t with depexts = depexts }
2666 let with_libraries libraries t = { t with libraries }
2667 let with_syntax syntax t = { t with syntax }
2668 let with_dev_repo dev_repo t = { t with dev_repo = Some dev_repo }
2669 let with_dev_repo_opt dev_repo t = { t with dev_repo }
2670 let with_pin_depends pin_depends t = { t with pin_depends }
2671
2672 let with_maintainer maintainer t = { t with maintainer }
2673 let with_author author t = { t with author }
2674 let with_license license t = { t with license }
2675 let with_tags tags t = { t with tags }
2676 let with_homepage homepage t = { t with homepage }
2677 let with_doc doc t = { t with doc }
2678 let with_bug_reports bug_reports t = { t with bug_reports }
2679
2680 let with_extensions extensions t =
2681 if not (OpamStd.String.Map.for_all (fun k _ -> is_ext_field k) extensions)
2682 then invalid_arg "OpamFile.OPAM.with_extensions";
2683 {t with
2684 extensions = extensions }
2685 let add_extension t fld syn =
2686 if not (is_ext_field fld) then invalid_arg "OpamFile.OPAM.add_extension";
2687 {t with
2688 extensions = OpamStd.String.Map.add fld syn t.extensions }
2689 let remove_extension t fld =
2690 if not (is_ext_field fld) then invalid_arg "OpamFile.OPAM.remove_extension";
2691 {t with extensions = OpamStd.String.Map.remove fld t.extensions }
2692
2693 let with_url url t =
2694 let format_errors =
2695 List.map (fun (name,bf) -> "url."^name, bf) url.URL.errors
2696 in
2697 { t with url = Some url;
2698 format_errors = format_errors @ t.format_errors }
2699 let with_url_opt url t =
2700 let format_errors = match url with
2701 | None -> []
2702 | Some u -> List.map (fun (name,bf) -> "url."^name, bf) u.URL.errors
2703 in
2704 { t with url;
2705 format_errors = format_errors @ t.format_errors }
2706
2707 let with_descr descr t = { t with descr = Some descr }
2708 let with_descr_opt descr t = { t with descr }
2709 let with_synopsis synopsis t =
2710 { t with descr =
2711 Some (synopsis, OpamStd.Option.default "" (descr_body t)) }
2712 let with_descr_body text t =
2713 { t with descr =
2714 Some (OpamStd.Option.default "" (synopsis t), text) }
2715
2716 let with_metadata_dir metadata_dir t = { t with metadata_dir }
2717 let with_extra_files extra_files t = { t with extra_files = Some extra_files }
2718 let with_extra_files_opt extra_files t = { t with extra_files }
2719
2720 let with_locked_opt locked t = { t with locked }
2721
2722 let with_format_errors format_errors t = { t with format_errors }
2723
2724 let with_ocaml_version ocaml_version t =
2725 { t with ocaml_version = Some ocaml_version }
2726 let with_os os t = { t with os }
2727
2728 (* Adds an opam constraint as an 'available' constraint, without restricting
2729 the file format compatibility *)
2730 let pp_minimal_opam_version min_version =
2731 let opam_version_var = OpamVariable.of_string "opam-version" in
2732 let add_avail_constr t =
2733 if OpamVersion.compare t.opam_version min_version >= 0 then t else
2734 let available =
2735 let opam_restricted =
2736 OpamFilter.fold_down_left (fun acc filter ->
2737 acc ||
2738 match filter with
2739 | FOp (FIdent ([], var, None), (`Eq|`Geq), FString version)
2740 | FOp (FString version, (`Eq|`Leq), FIdent ([], var, None)) ->
2741 var = opam_version_var &&
2742 OpamVersion.(compare (of_string version) min_version) >= 0
2743 | _ -> false)
2744 false t.available
2745 in
2746 if opam_restricted then t.available else
2747 let opam_restriction =
2748 FOp (FIdent ([], opam_version_var, None), `Geq,
2749 FString (OpamVersion.to_string min_version))
2750 in
2751 match t.available with
2752 | FBool true -> opam_restriction
2753 | available -> FAnd (available, opam_restriction)
2754 in
2755 { t with available }
2756 in
2757 let parse ~pos:_ t =
2758 add_avail_constr t
2759 (* This is not strictly needed since we know the constraint will be
2760 verified for the running opam version, but avoids a discrepency if
2761 re-parsing a printed file. *)
2762 in
2763 let print t =
2764 (* remove constraints that are already implied by the file format
2765 version *)
2766 let available =
2767 OpamFilter.map_up (function
2768 | FOp (FIdent ([], var, None), (`Eq|`Geq), FString version)
2769 | FOp (FString version, (`Eq|`Leq), FIdent ([], var, None))
2770 when var = opam_version_var &&
2771 OpamVersion.compare (OpamVersion.of_string version)
2772 t.opam_version <= 0
2773 -> FBool true
2774 | FAnd (FBool true, f) | FAnd (f, FBool true) -> f
2775 | FOr (FBool true, _) | FOr (_, FBool true) -> FBool true
2776 | f -> f
2777 )
2778 t.available
2779 in
2780 add_avail_constr { t with available }
2781 (* The constraint needs to be added here as well, in case the file was
2782 just generated and has a subpath without the constraint already *)
2783 in
2784 Pp.pp parse print
2785
2786 (* Post-processing functions used for some fields (optional, because we
2787 don't want them when linting). It's better to do them in the same pass
2788 as parsing, because it allows one to get file positions, which we lose
2789 afterwards *)
2790
2791 (* Allow 'flag:xxx' tags as flags, for compat *)
2792 let flag_of_tag tag =
2793 let prefix = "flags:" in
2794 if OpamStd.String.starts_with ~prefix tag then
2795 Some (pkg_flag_of_string (OpamStd.String.remove_prefix ~prefix tag))
2796 else None
2797
2798 let cleanup_depopts opam_version ~pos depopts =
2799 if OpamFormatConfig.(!r.skip_version_checks) ||
2800 OpamVersion.compare opam_version (OpamVersion.of_string "1.2") < 0
2801 then depopts
2802 else
2803 (* Make sure depopts are a pure disjunction *)
2804 let rec aux acc disjunction =
2805 List.fold_left (fun acc -> function
2806 | OpamFormula.Atom _ as atom -> atom :: acc
2807 | f ->
2808 Pp.warn ~pos
2809 "Optional dependencies must be a disjunction. \
2810 Treated as such.";
2811 aux acc
2812 (OpamFormula.fold_left (fun acc a -> OpamFormula.Atom a::acc)
2813 [] f)
2814 )
2815 acc disjunction
2816 in
2817 OpamFormula.ors_to_list depopts
2818 |> aux []
2819 |> List.rev
2820 |> OpamFormula.ors
2821
2822 let cleanup_conflicts opam_version ~pos conflicts =
2823 (* Conflicts were encoded as a conjunction before 1.3, which didn't match
2824 the semantics. The rewrite is done for all versions, but on 1.3+ it
2825 should be an error. *)
2826 let is_disjunction f =
2827 List.for_all (function Atom _ -> true | _ -> false)
2828 OpamFormula.(ors_to_list f)
2829 in
2830 if is_disjunction conflicts then conflicts else
2831 let force_disjunction f =
2832 OpamFormula.map_formula (function
2833 | And (a, b) -> Or (a, b)
2834 | f -> f)
2835 f
2836 in
2837 if OpamVersion.(compare opam_version (of_string "1.3") >= 0) then
2838 Pp.warn ~pos "Conflicts must be a disjunction, '&' is not \
2839 supported (treated as '|').";
2840 force_disjunction conflicts
2841
2842 let cleanup_flags _opam_version ~pos flags =
2843 let known_flags =
2844 List.filter (function Pkgflag_Unknown _ -> false | _ -> true)
2845 flags in
2846 if known_flags <> flags then
2847 Pp.warn ~pos
2848 "Unknown package flags %s ignored"
2849 (OpamStd.Format.pretty_list (OpamStd.List.filter_map (function
2850 | Pkgflag_Unknown s -> Some s
2851 | _ -> None)
2852 flags));
2853 known_flags
2854
2855 let cleanup_tags opam_version ~pos tags =
2856 let flags = OpamStd.List.filter_map flag_of_tag tags in
2857 ignore (cleanup_flags opam_version ~pos flags);
2858 tags
2859
2860 let cleanup_dev_repo opam_version ~pos:_ dev_repo =
2861 if OpamVersion.(compare opam_version (of_string "1.3") >= 0) then
2862 dev_repo
2863 else
2864 OpamUrl.parse ~handle_suffix:true (OpamUrl.to_string dev_repo)
2865
2866 let pp_basename =
2867 Pp.V.string -|
2868 Pp.of_module "file" (module OpamFilename.Base)
2869
2870 (* Field parser-printers *)
2871
2872 (* [field name, (pure pp, pp including cleanup/check function)] *)
2873 let fields_gen =
2874 let no_cleanup (ppacc: ?cleanup:(pos:_ -> _) -> _) set get pp =
2875 let p = ppacc set get pp in p, p
2876 in
2877 let with_cleanup cleanup (ppacc: ?cleanup:(pos:_ -> _) -> _) set get pp =
2878 let cleanup ~pos acc x = cleanup acc.opam_version ~pos x in
2879 ppacc set get pp,
2880 ppacc set get ~cleanup pp
2881 in
2882 [
2883 "opam-version", no_cleanup Pp.ppacc with_opam_version opam_version
2884 (Pp.V.string -| Pp.of_module "opam-version" (module OpamVersion));
2885 "name", no_cleanup Pp.ppacc_opt with_name name_opt
2886 Pp.V.pkgname;
2887 "version", no_cleanup Pp.ppacc_opt with_version version_opt
2888 (Pp.V.string_tr -| Pp.of_module "version" (module OpamPackage.Version));
2889
2890 "synopsis", no_cleanup Pp.ppacc_opt with_synopsis synopsis
2891 Pp.V.string_tr;
2892 "description", no_cleanup Pp.ppacc_opt with_descr_body descr_body
2893 Pp.V.string_tr;
2894
2895 "maintainer", no_cleanup Pp.ppacc with_maintainer maintainer
2896 (Pp.V.map_list ~depth:1 Pp.V.string);
2897 "authors", no_cleanup Pp.ppacc
2898 with_author author
2899 (Pp.V.map_list ~depth:1 Pp.V.string);
2900 "author", no_cleanup Pp.ppacc
2901 (fun a t -> if t.author = [] then with_author a t else
2902 Pp.bad_format "multiple \"authors:\" fields" author)
2903 (fun _ -> [])
2904 (Pp.V.map_list ~depth:1 Pp.V.string);
2905 "license", no_cleanup Pp.ppacc with_license license
2906 (Pp.V.map_list ~depth:1 Pp.V.string);
2907 "tags", with_cleanup cleanup_tags Pp.ppacc with_tags tags
2908 (Pp.V.map_list ~depth:1 Pp.V.string);
2909 "homepage", no_cleanup Pp.ppacc with_homepage homepage
2910 (Pp.V.map_list ~depth:1 Pp.V.string);
2911 "doc", no_cleanup Pp.ppacc with_doc doc
2912 (Pp.V.map_list ~depth:1 Pp.V.string);
2913 "bug-reports", no_cleanup Pp.ppacc with_bug_reports bug_reports
2914 (Pp.V.map_list ~depth:1 Pp.V.string);
2915
2916 "depends", no_cleanup Pp.ppacc with_depends depends
2917 (Pp.V.package_formula `Conj Pp.V.(filtered_constraints ext_version));
2918 "depopts", with_cleanup cleanup_depopts Pp.ppacc with_depopts depopts
2919 (Pp.V.package_formula `Disj Pp.V.(filtered_constraints ext_version));
2920 "conflicts", with_cleanup cleanup_conflicts
2921 Pp.ppacc with_conflicts conflicts
2922 (Pp.V.package_formula `Disj Pp.V.(filtered_constraints ext_version));
2923 "conflict-class", no_cleanup Pp.ppacc with_conflict_class conflict_class
2924 (Pp.V.map_list ~depth:1 Pp.V.pkgname);
2925 "available", no_cleanup Pp.ppacc with_available available
2926 (Pp.V.list_depth 1 -| Pp.V.list -| Pp.V.filter);
2927 "flags", with_cleanup cleanup_flags Pp.ppacc add_flags flags
2928 (Pp.V.map_list ~depth:1 @@
2929 Pp.V.ident -|
2930 Pp.of_pair "package-flag" (pkg_flag_of_string, string_of_pkg_flag));
2931 "setenv", no_cleanup Pp.ppacc with_env env
2932 (Pp.V.map_list ~depth:2 Pp.V.env_binding);
2933
2934 "build", no_cleanup Pp.ppacc with_build build
2935 (Pp.V.map_list ~depth:2 Pp.V.command);
2936 "run-test", no_cleanup Pp.ppacc with_run_test run_test
2937 (Pp.V.map_list ~depth:2 Pp.V.command);
2938 "install", no_cleanup Pp.ppacc with_install install
2939 (Pp.V.map_list ~depth:2 Pp.V.command);
2940 "remove", no_cleanup Pp.ppacc with_remove remove
2941 (Pp.V.map_list ~depth:2 Pp.V.command);
2942
2943 "substs", no_cleanup Pp.ppacc with_substs substs
2944 (Pp.V.map_list ~depth:1 pp_basename);
2945 "patches", no_cleanup Pp.ppacc with_patches patches
2946 (Pp.V.map_list ~depth:1 @@
2947 Pp.V.map_option pp_basename (Pp.opt Pp.V.filter));
2948 "build-env", no_cleanup Pp.ppacc with_build_env build_env
2949 (Pp.V.map_list ~depth:2 Pp.V.env_binding);
2950 "features", no_cleanup Pp.ppacc with_features features
2951 (Pp.V.map_list ~depth:1 @@
2952 Pp.V.map_options_2
2953 (Pp.V.ident -| Pp.of_module "variable" (module OpamVariable))
2954 (Pp.V.package_formula_items `Conj Pp.V.(filtered_constraints ext_version))
2955 (Pp.singleton -| Pp.V.string));
2956
2957 "messages", no_cleanup Pp.ppacc with_messages messages
2958 (Pp.V.map_list ~depth:1 @@
2959 Pp.V.map_option Pp.V.string_tr (Pp.opt Pp.V.filter));
2960 "post-messages", no_cleanup Pp.ppacc with_post_messages post_messages
2961 (Pp.V.map_list ~depth:1 @@
2962 Pp.V.map_option Pp.V.string_tr (Pp.opt Pp.V.filter));
2963 "depexts", no_cleanup Pp.ppacc with_depexts depexts
2964 (let map_syspkg =
2965 (Pp.V.map_list
2966 (Pp.V.string -| Pp.of_module "sys-package" (module OpamSysPkg))
2967 -| Pp.pp (fun ~pos:_ -> OpamSysPkg.Set.of_list) OpamSysPkg.Set.elements)
2968 in
2969 Pp.fallback
2970 (Pp.V.map_list ~depth:2 @@
2971 Pp.V.map_option map_syspkg (Pp.V.filter))
2972 (Pp.V.map_list ~depth:3
2973 (let rec filter_of_taglist = function
2974 | [] -> FBool true
2975 | [v] -> FString v
2976 | v :: r -> FAnd (FString v, filter_of_taglist r)
2977 in
2978 Pp.V.map_pair
2979 (Pp.V.map_list Pp.V.string -|
2980 Pp.of_pair "tag-list"
2981 (filter_of_taglist, fun _ -> assert false))
2982 map_syspkg -|
2983 Pp.pp (fun ~pos:_ (a,b) -> b,a) (fun (b,a) -> a,b))));
2984 "libraries", no_cleanup Pp.ppacc with_libraries libraries
2985 (Pp.V.map_list ~depth:1 @@
2986 Pp.V.map_option Pp.V.string (Pp.opt Pp.V.filter));
2987 "syntax", no_cleanup Pp.ppacc with_syntax syntax
2988 (Pp.V.map_list ~depth:1 @@
2989 Pp.V.map_option Pp.V.string (Pp.opt Pp.V.filter));
2990 "dev-repo", with_cleanup cleanup_dev_repo Pp.ppacc_opt with_dev_repo dev_repo
2991 (Pp.V.string -|
2992 Pp.of_pair "vc-url"
2993 OpamUrl.(parse ?backend:None ~handle_suffix:false ~from_file:true,
2994 to_string));
2995 "pin-depends", no_cleanup Pp.ppacc with_pin_depends pin_depends
2996 (OpamFormat.V.map_list ~depth:2
2997 (OpamFormat.V.map_pair
2998 (OpamFormat.V.string -|
2999 OpamPp.of_module "versioned package" (module OpamPackage))
3000 (OpamFormat.V.string -|
3001 OpamPp.of_module "URL" (module OpamUrl))));
3002
3003 "extra-files", no_cleanup Pp.ppacc_opt with_extra_files extra_files
3004 (Pp.V.map_list ~depth:2 @@
3005 Pp.V.map_pair
3006 pp_basename
3007 (Pp.V.string -| Pp.of_module "checksum" (module OpamHash)));
3008
3009 (* deprecated fields, here for compat *)
3010 "configure-style", (Pp.ppacc_ignore, Pp.ppacc_ignore);
3011
3012 "ocaml-version", no_cleanup
3013 Pp.ppacc_opt with_ocaml_version OpamStd.Option.none
3014 (Pp.V.list_depth 1 -| Pp.V.list -|
3015 Pp.V.constraints Pp.V.compiler_version);
3016 "os", no_cleanup Pp.ppacc_opt with_os OpamStd.Option.none
3017 Pp.V.os_constraint;
3018 "descr", no_cleanup Pp.ppacc_opt with_descr OpamStd.Option.none
3019 (Pp.V.string_tr -|
3020 Pp.of_pair "descr" Descr.(of_string (), to_string ()));
3021 "extra-sources", no_cleanup Pp.ppacc_opt
3022 with_extra_sources OpamStd.Option.none
3023 (Pp.V.map_list ~depth:2 @@
3024 Pp.V.map_pair
3025 (Pp.V.map_option
3026 Pp.V.url
3027 (Pp.opt @@ Pp.singleton -| pp_basename))
3028 (Pp.V.string -| Pp.of_module "checksum" (module OpamHash))
3029 -| Pp.pp
3030 (fun ~pos:_ ((u,b),md5) ->
3031 OpamStd.Option.default
3032 (OpamFilename.Base.of_string (OpamUrl.basename u)) b,
3033 URL.create ~checksum:[md5] u)
3034 (fun (f, urlf) ->
3035 URL.((url urlf, Some f), List.hd (checksum urlf))));
3036 "build-test", no_cleanup Pp.ppacc_opt
3037 with_deprecated_build_test OpamStd.Option.none
3038 (Pp.V.map_list ~depth:2 Pp.V.command);
3039 "build-doc", no_cleanup Pp.ppacc_opt
3040 with_deprecated_build_doc (fun x -> Some (deprecated_build_doc x))
3041 (Pp.V.map_list ~depth:2 Pp.V.command);
3042 ]
3043
3044 (* These don't have a printer and their info is stored in new fields *)
3045 let alias_fields = [
3046 "author", "authors";
3047 "descr", "description";
3048 ]
3049
3050 (* These don't have a printer and their info can't be retrieved in the same
3051 format anymore *)
3052 let deprecated_fields = [
3053 "ocaml-version";
3054 "os";
3055 "configure-style";
3056 "extra-sources";
3057 "build-test";
3058 "build-doc";
3059 ]
3060
3061 let fields =
3062 List.map (fun (name, (_, cleaned_up_pp)) -> name, cleaned_up_pp)
3063 fields_gen
3064
3065 let sections = [
3066 "url", Pp.ppacc_opt with_url url (Pp.I.anonymous_section URL.pp_contents);
3067 "extra-source", Pp.ppacc with_extra_sources extra_sources
3068 (Pp.map_list
3069 (Pp.map_pair
3070 (Pp.pp
3071 (fun ~pos -> function
3072 | Some o -> OpamFilename.Base.of_string o
3073 | None -> Pp.bad_format ~pos "missing extra-source name")
3074 (fun b -> Some (OpamFilename.Base.to_string b)))
3075 URL.pp_contents))
3076 ]
3077
3078 let raw_fields =
3079 List.map (fun (name, (raw_pp, _)) -> name, raw_pp)
3080 fields_gen
3081
3082 let handle_flags_in_tags =
3083 let parse ~pos:_ t =
3084 let flags =
3085 List.fold_left (fun flags tag ->
3086 match flag_of_tag tag with
3087 | Some flag -> flag :: flags
3088 | None -> flags)
3089 t.flags t.tags
3090 in
3091 {t with flags}
3092 in
3093 let print t =
3094 let flags, tags =
3095 List.fold_left (fun (flags, tags) tag ->
3096 match flag_of_tag tag with
3097 | Some flag ->
3098 if List.mem flag flags then
3099 List.filter ((<>) flag) flags, tag::tags
3100 else flags, tags
3101 | None -> flags, tag::tags)
3102 (t.flags,[]) (List.rev t.tags)
3103 in
3104 {t with flags; tags}
3105 in
3106 Pp.pp parse print
3107
3108 let handle_deprecated_available =
3109 let add_available available filter =
3110 match available with
3111 | FBool true -> filter
3112 | f -> FAnd (filter, f)
3113 in
3114 let parse ~pos:_ t =
3115 let available = t.available in
3116 let available =
3117 match t.ocaml_version with
3118 | None -> available
3119 | Some ocaml_version ->
3120 let var = OpamVariable.of_string "ocaml-version" in
3121 let mk_atom (op,v) =
3122 FOp (FIdent ([], var, None), op, FString v)
3123 in
3124 let filter = OpamFilter.of_formula mk_atom ocaml_version in
3125 add_available available filter
3126 in
3127 let available =
3128 match t.os with
3129 | Empty -> available
3130 | os ->
3131 let var = OpamVariable.of_string "os" in
3132 let mk_atom (eq,name) =
3133 FOp (FIdent ([], var, None), (if eq then `Eq else `Neq), FString name)
3134 in
3135 let filter = OpamFilter.of_formula mk_atom os in
3136 add_available available filter
3137 in
3138 { t with available }
3139 in
3140 Pp.pp parse (fun x -> x)
3141
3142 let handle_subpath_2_0 =
3143 let subpath_xfield = "x-subpath" in
3144 let pp_constraint =
3145 pp_minimal_opam_version (OpamVersion.of_string "2.1")
3146 in
3147 let parse ~pos t =
3148 if OpamVersion.(compare t.opam_version (of_string "2.0") > 0) then t
3149 else
3150 match OpamStd.String.Map.find_opt subpath_xfield t.extensions with
3151 | Some {pelem = String subpath;_} ->
3152 let url = match t.url with
3153 | Some u ->
3154 Some (URL.with_subpath (OpamFilename.SubPath.of_string subpath) u)
3155 | None -> None
3156 in
3157 { t with url }
3158 |> Pp.parse ~pos pp_constraint
3159 | Some {pos;_} ->
3160 Pp.bad_format ~pos "Field %s must be a string"
3161 (OpamConsole.colorise `underline subpath_xfield)
3162 | None -> t
3163 in
3164 let print t =
3165 match t.url with
3166 | Some ({ URL.subpath = Some sb ; _ } as url) ->
3167 if OpamVersion.(compare t.opam_version (of_string "2.0") > 0) then t
3168 else
3169 add_extension t subpath_xfield
3170 (nullify_pos @@ String (OpamFilename.SubPath.to_string sb))
3171 |> with_url (URL.with_subpath_opt None url)
3172 |> Pp.print pp_constraint
3173 | _ -> t
3174 in
3175 Pp.pp parse print
3176
3177 let handle_locked =
3178 let locked_xfield = "x-locked" in
3179 let pp_constraint =
3180 pp_minimal_opam_version (OpamVersion.of_string "2.1")
3181 in
3182 let parse ~pos t =
3183 if OpamVersion.(compare t.opam_version (of_string "2.0") > 0) then t
3184 else
3185 match OpamStd.String.Map.find_opt locked_xfield t.extensions with
3186 | Some {pelem = String locked;_} ->
3187 { t with locked = Some locked }
3188 |> Pp.parse ~pos pp_constraint
3189 | Some {pos; _} ->
3190 Pp.bad_format ~pos "Field %s must be a bool"
3191 (OpamConsole.colorise `underline locked_xfield)
3192 | None -> { t with locked = None }
3193 in
3194 let print t =
3195 if OpamVersion.(compare t.opam_version (of_string "2.0") > 0) then t
3196 else
3197 match t.locked with
3198 | None | Some "" ->
3199 remove_extension t locked_xfield
3200 | Some locked ->
3201 add_extension t locked_xfield (nullify_pos @@ String locked)
3202 |> Pp.print pp_constraint
3203 in
3204 Pp.pp parse print
3205
3206 (* Doesn't handle package name encoded in directory name *)
3207 let pp_raw_fields =
3208 Pp.I.check_opam_version ~format_version () -|
3209 Pp.I.opam_version ~format_version () -|
3210 Pp.I.partition_fields ~section:true (is_ext_field @> not) -| Pp.map_pair
3211 (Pp.I.fields ~name:"opam-file" ~empty ~sections fields -|
3212 Pp.I.on_errors (fun t e -> {t with format_errors=e::t.format_errors}) -|
3213 handle_flags_in_tags -|
3214 handle_deprecated_available)
3215 (Pp.I.items -|
3216 OpamStd.String.Map.(Pp.pp (fun ~pos:_ -> of_list) bindings)) -|
3217 Pp.pp
3218 (fun ~pos:_ (t, extensions) -> with_extensions extensions t)
3219 (fun t -> t, extensions t) -|
3220 Pp.check (fun t ->
3221 OpamVersion.(compare t.opam_version (of_string "2.0") > 0) ||
3222 OpamStd.Option.Op.(t.url >>= URL.subpath) = None)
3223 ~errmsg:"The url.subpath field is not allowed in files with \
3224 `opam-version` <= 2.0" -|
3225 handle_subpath_2_0 -|
3226 handle_locked
3227
3228 let pp_raw = Pp.I.map_file @@ pp_raw_fields
3229
3230 let pp =
3231 pp_raw -|
3232 Pp.pp
3233 (fun ~pos:_ (filename, t) ->
3234 filename,
3235 let metadata_dir =
3236 if filename <> dummy_file
3237 then Some (None, OpamFilename.(Dir.to_string (dirname filename)))
3238 else None
3239 in
3240 let t = { t with metadata_dir } in
3241 match OpamPackage.of_filename filename with
3242 | Some nv ->
3243 if t.name <> None && t.name <> Some nv.name ||
3244 t.version <> None && t.version <> Some nv.version
3245 then
3246 Pp.warn
3247 "This file is for package '%s' but has mismatching fields%s%s."
3248 (OpamPackage.to_string nv)
3249 (OpamStd.Option.to_string
3250 (fun n -> " 'name:"^OpamPackage.Name.to_string n)
3251 t.name)
3252 (OpamStd.Option.to_string
3253 (fun v -> " 'version:"^OpamPackage.Version.to_string v)
3254 t.version);
3255 with_nv nv t
3256 | None -> t)
3257 (fun (filename, t) ->
3258 filename,
3259 match OpamPackage.of_filename filename, t.name, t.version with
3260 | Some _, None, None -> t
3261 | None, Some _, Some _ -> t
3262 | None, _, _ ->
3263 OpamConsole.log "FILE(opam)"
3264 "Outputting opam file %s with unspecified name or version"
3265 (OpamFilename.to_string filename);
3266 t
3267 | Some nv, _, _ ->
3268 if t.name <> None && t.name <> Some (nv.OpamPackage.name) ||
3269 t.version <> None && t.version <> Some (nv.OpamPackage.version)
3270 then
3271 OpamConsole.warning
3272 "Skipping inconsistent 'name:' or 'version:' fields (%s) \
3273 while saving %s"
3274 (OpamPackage.to_string @@
3275 OpamPackage.create
3276 (OpamStd.Option.default (nv.OpamPackage.name) t.name)
3277 (OpamStd.Option.default (nv.OpamPackage.version) t.version))
3278 (OpamFilename.prettify filename);
3279 {t with name = None; version = None}
3280 )
3281
3282 let to_string_with_preserved_format
3283 ?format_from ?format_from_string filename t =
3284 Syntax.to_string_with_preserved_format
3285 ?format_from ?format_from_string filename ~empty
3286 ~sections ~fields:raw_fields pp t
3287
3288 let write_with_preserved_format
3289 ?format_from ?format_from_string filename t =
3290 let s =
3291 to_string_with_preserved_format ?format_from ?format_from_string
3292 filename t
3293 in
3294 OpamFilename.write filename s
3295
3296 let contents = Syntax.contents pp
3297
3298 let to_list = Syntax.to_list pp
3299
3300 let print_field_as_syntax field t =
3301 if List.mem field deprecated_fields then raise Not_found;
3302 let field =
3303 try OpamStd.List.assoc String.equal field alias_fields
3304 with Not_found -> field
3305 in
3306 match OpamStd.String.cut_at field '.' with
3307 | None ->
3308 if is_ext_field field
3309 then OpamStd.String.Map.find_opt field t.extensions
3310 else snd (Pp.print (OpamStd.List.assoc String.equal field fields) t)
3311 | Some (sec, field) ->
3312 match snd (Pp.print (OpamStd.List.assoc String.equal sec sections) t) with
3313 | None -> None
3314 | Some items ->
3315 (* /!\ returns only the first result for multiple named sections *)
3316 Some (OpamStd.List.find_map (fun i -> match i.pelem with
3317 | Variable (f, contents) when f.pelem = field -> Some contents
3318 | _ -> None)
3319 (List.flatten (List.map snd items)))
3320
3321 end
3322 module OPAM = struct
3323 include OPAMSyntax
3324 include SyntaxFile(OPAMSyntax)
3325
3326 (** Extra stuff for opam files *)
3327
3328 let effective_part ?(modulo_state=false) (t:t) =
3329 let t_modulo_state = if modulo_state then empty else t in
3330 let effective_url u =
3331 match URL.checksum u with
3332 | [] -> URL.create (URL.url u) (* ignore mirrors *)
3333 | cksum::_ ->
3334 URL.with_checksum [cksum] URL.empty
3335 (* ignore actual url and extra checksums *)
3336 in
3337 {
3338 opam_version = empty.opam_version;
3339
3340 name = t.name;
3341 version = t.version;
3342
3343 depends = t_modulo_state.depends;
3344 depopts = t_modulo_state.depopts;
3345 conflicts = t_modulo_state.conflicts;
3346 conflict_class = t_modulo_state.conflict_class;
3347 available = t_modulo_state.available;
3348 flags =
3349 (List.filter (function
3350 | Pkgflag_Plugin -> true
3351 | Pkgflag_LightUninstall
3352 | Pkgflag_Verbose
3353 | Pkgflag_Compiler
3354 | Pkgflag_Conf
3355 | Pkgflag_AvoidVersion
3356 | Pkgflag_Deprecated
3357 | Pkgflag_Unknown _
3358 -> false)
3359 t.flags);
3360 env = t.env;
3361
3362 build = t.build;
3363 run_test = t.deprecated_build_test @ t.run_test;
3364 install = t.install;
3365 remove = t.remove;
3366
3367 substs = t.substs;
3368 patches = t.patches;
3369 build_env = t.build_env;
3370 features = t.features;
3371 extra_sources = List.map (fun (name, u) -> (name, effective_url u)) t.extra_sources;
3372
3373 messages = empty.messages;
3374 post_messages = empty.post_messages;
3375 depexts = empty.depexts;
3376 libraries = empty.libraries;
3377 syntax = empty.syntax;
3378 dev_repo = empty.dev_repo;
3379 pin_depends = empty.pin_depends;
3380
3381 maintainer = empty.maintainer;
3382 author = empty.author;
3383 license = empty.license;
3384 tags = empty.tags;
3385 homepage = empty.homepage;
3386 doc = empty.doc;
3387 bug_reports = empty.bug_reports;
3388
3389 extensions = empty.extensions;
3390 url = OpamStd.Option.map effective_url t.url;
3391 descr = empty.descr;
3392
3393 metadata_dir = empty.metadata_dir;
3394 extra_files = OpamStd.Option.Op.(t.extra_files ++ Some []);
3395
3396 locked = None;
3397
3398 format_errors = empty.format_errors;
3399
3400 ocaml_version = empty.ocaml_version;
3401 os = empty.os;
3402
3403 deprecated_build_test = []; (* merged into run_test *)
3404 deprecated_build_doc = t.deprecated_build_doc;
3405 }
3406
3407 let effectively_equal ?(modulo_state=false) o1 o2 =
3408 effective_part ~modulo_state o1 = effective_part ~modulo_state o2
3409
3410 let equal o1 o2 =
3411 with_metadata_dir None o1 = with_metadata_dir None o2
3412
3413 let get_metadata_dir ~repos_roots o =
3414 match metadata_dir o with
3415 | None -> None
3416 | Some (None, abs) ->
3417 Some (OpamFilename.Dir.of_string abs)
3418 | Some (Some r, rel) ->
3419 Some OpamFilename.Op.(repos_roots r / rel)
3420
3421 let get_extra_files ~repos_roots o =
3422 OpamStd.Option.Op.(
3423 (get_metadata_dir ~repos_roots o >>= fun mdir ->
3424 let files_dir = OpamFilename.Op.(mdir / "files") in
3425 extra_files o >>| List.map @@ fun (basename, hash) ->
3426 OpamFilename.create files_dir basename,
3427 basename, hash)
3428 +! []
3429 )
3430
3431 let print_errors ?file o =
3432 if o.format_errors <> [] then
3433 OpamConsole.error "In the opam file%s:\n%s\
3434 %s %s been %s."
3435 (match o.name, o.version, file, o.metadata_dir with
3436 | Some n, Some v, _, _ ->
3437 Printf.sprintf " for %s"
3438 (OpamPackage.to_string (OpamPackage.create n v))
3439 | _, _, Some f, _ ->
3440 Printf.sprintf " at %s" (to_string f)
3441 | _, _, _, Some (None, dir) ->
3442 Printf.sprintf " in %s" dir
3443 | _, _, _, Some (Some repo, dir) ->
3444 Printf.sprintf " %s from repository %s"
3445 (Filename.concat dir "opam")
3446 (OpamRepositoryName.to_string repo)
3447 | _ -> "")
3448 (OpamStd.Format.itemize
3449 (fun (_, bf) -> Pp.string_of_bad_format (OpamPp.Bad_format bf))
3450 o.format_errors)
3451 (OpamStd.List.concat_map ", " (fun (f,_) -> Printf.sprintf "'%s'" f)
3452 o.format_errors)
3453 (match o.format_errors with [_] -> "has" | _ -> "have")
3454 (OpamConsole.colorise `bold "ignored")
3455 end
3456
3457
3458 (** Optional package.install files (<source>/<pkgname>.install,
3459 <repo>/packages/.../files/<pkgname>.install) *)
3460
3461 module Dot_installSyntax = struct
3462
3463 let internal = ".install"
3464 let atomic = false
3465 let format_version = OpamVersion.of_string "2.0"
3466
3467 type t = {
3468 bin : (basename optional * basename option) list;
3469 sbin : (basename optional * basename option) list;
3470 lib : (basename optional * basename option) list;
3471 toplevel: (basename optional * basename option) list;
3472 stublibs: (basename optional * basename option) list;
3473 share : (basename optional * basename option) list;
3474 share_root: (basename optional * basename option) list;
3475 etc : (basename optional * basename option) list;
3476 doc : (basename optional * basename option) list;
3477 man : (basename optional * basename option) list;
3478 libexec : (basename optional * basename option) list;
3479 lib_root: (basename optional * basename option) list;
3480 libexec_root: (basename optional * basename option) list;
3481 misc : (basename optional * filename) list;
3482 }
3483
3484 let empty = {
3485 lib = [];
3486 bin = [];
3487 sbin = [];
3488 toplevel = [];
3489 stublibs = [];
3490 misc = [];
3491 share = [];
3492 share_root = [];
3493 etc = [];
3494 man = [];
3495 libexec = [];
3496 lib_root = [];
3497 libexec_root = [];
3498 doc = [];
3499 }
3500
3501 let bin t = t.bin
3502 let sbin t = t.sbin
3503 let lib t = t.lib
3504 let toplevel t = t.toplevel
3505 let stublibs t = t.stublibs
3506 let misc t = t.misc
3507 let share t = t.share
3508 let share_root t = t.share_root
3509 let etc t = t.etc
3510 let raw_man t = t.man
3511 let doc t = t.doc
3512 let libexec t = t.libexec
3513 let lib_root t = t.lib_root
3514 let libexec_root t = t.libexec_root
3515
3516 let with_bin bin t = { t with bin }
3517 let with_sbin sbin t = { t with sbin }
3518 let with_lib lib t = { t with lib }
3519 let with_toplevel toplevel t = { t with toplevel }
3520 let with_stublibs stublibs t = { t with stublibs }
3521 let with_misc misc t = { t with misc }
3522 let with_share share t = { t with share }
3523 let with_share_root share_root t = { t with share_root }
3524 let with_etc etc t = { t with etc }
3525 let with_man man t = { t with man }
3526 let with_doc doc t = { t with doc }
3527 let with_libexec libexec t = { t with libexec }
3528 let with_lib_root lib_root t = { t with lib_root }
3529 let with_libexec_root libexec_root t = { t with libexec_root }
3530
3531 let add_man_section_dir src =
3532 let file = Filename.basename (OpamFilename.Base.to_string src.c) in
3533 let section =
3534 let base =
3535 if Filename.check_suffix file ".gz"
3536 then Filename.chop_suffix file ".gz" else file
3537 in
3538 let dot = String.rindex base '.' in
3539 if dot < String.length base - 1 then
3540 match base.[dot+1] with
3541 | '1'..'8' as c -> Some (Printf.sprintf "man%c" c)
3542 | _ -> None
3543 else None
3544 in
3545 OpamStd.Option.Op.(
3546 section >>|
3547 (fun s -> Filename.concat s file) >>|
3548 OpamFilename.Base.of_string
3549 )
3550
3551 let man t =
3552 List.map (fun (src, dst) ->
3553 src,
3554 match dst with
3555 | Some _ -> dst
3556 | None -> add_man_section_dir src
3557 ) t.man
3558
3559 (* Filenames starting by ? are not always present. *)
3560 let pp_optional =
3561 Pp.pp ~name:"file-name"
3562 (fun ~pos:_ str ->
3563 let mk = OpamFilename.Base.of_string in
3564 if String.length str > 0 && str.[0] = '?' then
3565 { optional = true;
3566 c = mk (String.sub str 1 (String.length str - 1)) }
3567 else
3568 { optional = false;
3569 c = mk str })
3570 (fun op ->
3571 if op.optional then "?" ^ OpamFilename.Base.to_string op.c
3572 else OpamFilename.Base.to_string op.c)
3573
3574 let fields =
3575 let pp_field =
3576 Pp.V.map_list ~depth:1 @@ Pp.V.map_option
3577 (Pp.V.string -| pp_optional)
3578 (Pp.opt @@
3579 Pp.singleton -| Pp.V.string -|
3580 Pp.of_module "rel-filename" (module OpamFilename.Base))
3581 in
3582 let pp_misc =
3583 Pp.V.map_list ~depth:1 @@ Pp.V.map_option
3584 (Pp.V.string -| pp_optional)
3585 (Pp.singleton -| Pp.V.string -| Pp.pp ~name:"abs-filename"
3586 (fun ~pos s ->
3587 if not (Filename.is_relative s) then OpamFilename.of_string s
3588 else Pp.bad_format ~pos
3589 "%s is not an absolute filename." s)
3590 OpamFilename.to_string)
3591 in
3592 [
3593 "lib", Pp.ppacc with_lib lib pp_field;
3594 "bin", Pp.ppacc with_bin bin pp_field;
3595 "sbin", Pp.ppacc with_sbin sbin pp_field;
3596 "misc", Pp.ppacc with_misc misc pp_misc;
3597 "toplevel", Pp.ppacc with_toplevel toplevel pp_field;
3598 "stublibs", Pp.ppacc with_stublibs stublibs pp_field;
3599 "share", Pp.ppacc with_share share pp_field;
3600 "share_root", Pp.ppacc with_share_root share_root pp_field;
3601 "etc", Pp.ppacc with_etc etc pp_field;
3602 "doc", Pp.ppacc with_doc doc pp_field;
3603 "man", Pp.ppacc with_man raw_man pp_field;
3604 "libexec", Pp.ppacc with_libexec libexec pp_field;
3605 "lib_root", Pp.ppacc with_lib_root lib_root pp_field;
3606 "libexec_root", Pp.ppacc with_libexec_root libexec_root pp_field;
3607 ]
3608
3609 let pp =
3610 let name = internal in
3611 Pp.I.map_file @@
3612 Pp.I.check_opam_version ~optional:true ~format_version () -|
3613 Pp.I.opam_version ~format_version ~undefined:true () -|
3614 Pp.I.fields ~name ~empty fields -|
3615 Pp.I.show_errors ~name () -|
3616 Pp.check ~errmsg:"man file without destination or recognised suffix"
3617 (fun t ->
3618 List.for_all (function
3619 | m, None -> add_man_section_dir m <> None
3620 | _, Some _ -> true)
3621 t.man)
3622
3623 end
3624 module Dot_install = struct
3625 include Dot_installSyntax
3626 include SyntaxFile(Dot_installSyntax)
3627 end
3628
3629
3630 module ChangesSyntax = struct
3631 let internal = "changes"
3632 let atomic = false
3633 let format_version = OpamVersion.of_string "2.0"
3634
3635 open OpamDirTrack
3636
3637 type t = OpamDirTrack.t
3638
3639 module SM = OpamStd.String.Map
3640
3641 let empty = SM.empty
3642
3643 let field kind get_kind =
3644 Pp.ppacc
3645 (fun files t ->
3646 List.fold_left (fun t (f,digest) -> SM.add f (kind digest) t) t files)
3647 (fun t ->
3648 SM.fold (fun f op acc ->
3649 match get_kind op with Some dg -> (f, dg) :: acc | None -> acc)
3650 t []
3651 |> List.rev)
3652 (Pp.V.map_list ~depth:1 @@
3653 Pp.V.map_option
3654 Pp.V.string
3655 (Pp.opt (Pp.singleton -| Pp.V.string -|
3656 Pp.of_pair "digest" (digest_of_string, string_of_digest))))
3657
3658 let fields = [
3659 "added", field
3660 (function Some dg -> Added dg
3661 | None -> Pp.bad_format "Missing digest")
3662 (function Added dg -> Some (Some dg) | _ -> None);
3663 "removed", field
3664 (function Some _ -> Pp.bad_format "Extra digest"
3665 | None -> Removed)
3666 (function Removed -> Some None | _ -> None);
3667 "contents-changed", field
3668 (function Some dg -> Contents_changed dg
3669 | None -> Pp.bad_format "Missing digest")
3670 (function Contents_changed dg -> Some (Some dg) | _ -> None);
3671 "perm-changed", field
3672 (function Some dg -> Perm_changed dg
3673 | None -> Pp.bad_format "Missing digest")
3674 (function Perm_changed dg -> Some (Some dg) | _ -> None);
3675 "kind-changed", field
3676 (function Some dg -> Kind_changed dg
3677 | None -> Pp.bad_format "Missing digest")
3678 (function Kind_changed dg -> Some (Some dg) | _ -> None);
3679 ]
3680
3681 let pp_contents =
3682 Pp.I.check_opam_version ~format_version ~optional:true () -|
3683 Pp.I.opam_version ~format_version ~undefined:true () -|
3684 Pp.I.fields ~name:internal ~empty fields -|
3685 Pp.I.show_errors ~name:internal ()
3686
3687 let pp = Pp.I.map_file pp_contents
3688 end
3689
3690 module Changes = struct
3691 type t = OpamDirTrack.t
3692 include SyntaxFile(ChangesSyntax)
3693 end
3694
3695 module SwitchExportSyntax = struct
3696
3697 let internal = "switch-export"
3698 let atomic = false
3699 let format_version = OpamVersion.of_string "2.1"
3700
3701 type t = {
3702 selections: switch_selections;
3703 extra_files: string OpamHash.Map.t;
3704 overlays: OPAM.t OpamPackage.Name.Map.t;
3705 }
3706
3707 let empty = {
3708 selections = SwitchSelectionsSyntax.empty;
3709 extra_files = OpamHash.Map.empty;
3710 overlays = OpamPackage.Name.Map.empty;
3711 }
3712
3713
3714 let fields =
3715 [ "extra-files", Pp.ppacc (fun extra_files t -> { t with extra_files })
3716 (fun t -> t.extra_files)
3717 ((Pp.V.map_list ~depth:2 @@
3718 (Pp.V.map_pair
3719 (Pp.V.string -| Pp.of_module "checksum" (module OpamHash))
3720 Pp.V.string)) -|
3721 Pp.of_pair "HashMap" OpamHash.Map.(of_list, bindings))
3722 ] @
3723 List.map
3724 (fun (fld, ppacc) ->
3725 fld, Pp.embed (fun selections t -> { t with selections })
3726 (fun t -> t.selections) ppacc)
3727 SwitchSelectionsSyntax.fields
3728
3729 let pp =
3730 let name = "export-file" in
3731 Pp.I.map_file @@
3732 Pp.I.check_opam_version ~format_version () -|
3733 Pp.I.opam_version ~format_version ~undefined:true () -|
3734 Pp.I.partition (fun i -> match i.pelem with
3735 | Section ({ section_kind={pelem="package";_}; section_name=Some _; _ }) ->
3736 false
3737 | _ -> true) -|
3738 Pp.map_pair
3739 (Pp.I.fields ~name ~empty fields -|
3740 Pp.I.show_errors ~name ())
3741 (Pp.map_list
3742 (Pp.I.section "package" -|
3743 Pp.map_pair
3744 (Pp.map_option
3745 (Pp.of_module "package-name" (module OpamPackage.Name)))
3746 OPAMSyntax.pp_raw_fields -|
3747 Pp.pp
3748 (fun ~pos:_ (name, opam) ->
3749 match name with
3750 | Some name -> name, OPAM.with_name name opam
3751 | None -> OPAM.name opam, opam)
3752 (fun (name, opam) ->
3753 Some name, OPAM.with_name_opt None opam)) -|
3754 Pp.of_pair "package-metadata-map"
3755 OpamPackage.Name.Map.(of_list,bindings)) -|
3756 Pp.pp
3757 (fun ~pos:_ (t, overlays) -> {t with overlays})
3758 (fun t -> t, t.overlays)
3759
3760 end
3761
3762 module SwitchExport = struct
3763 type t = SwitchExportSyntax.t = {
3764 selections: switch_selections;
3765 extra_files: string OpamHash.Map.t;
3766 overlays: OPAM.t OpamPackage.Name.Map.t;
3767 }
3768
3769 include SyntaxFile(SwitchExportSyntax)
3770 end
3771
3772
3773 module CompSyntax = struct
3774
3775 let internal = "comp"
3776 let atomic = false
3777 let format_version = OpamVersion.of_string "1.2"
3778
3779 type compiler = string
3780 type compiler_version = string
3781
3782 type t = {
3783 opam_version : opam_version ;
3784 name : compiler ;
3785 version : compiler_version ;
3786 preinstalled : bool;
3787 src : url option ;
3788 patches : url list ;
3789 configure : string list ;
3790 make : string list ;
3791 build : command list ;
3792 packages : formula ;
3793 env : env_update list;
3794 tags : string list;
3795 }
3796
3797 let empty = {
3798 opam_version = format_version;
3799 name = "<none>";
3800 version = "<none>";
3801 src = None;
3802 preinstalled = false;
3803 patches = [];
3804 configure = [];
3805 make = [];
3806 build = [];
3807 packages = OpamFormula.Empty;
3808 env = [];
3809 tags = [];
3810 }
3811
3812 let create_preinstalled name version packages env =
3813 let mk n = Atom (n, Empty) in
3814 let packages = OpamFormula.ands (List.map mk packages) in
3815 { empty with name; version; preinstalled = true; packages; env }
3816
3817 let name (t:t) = t.name
3818 let version (t:t) = t.version
3819 let patches t = t.patches
3820 let configure t = t.configure
3821 let make t = t.make
3822 let build t = t.build
3823 let src t = t.src
3824 let opam_version t = t.opam_version
3825
3826 let packages t = t.packages
3827 let preinstalled t = t.preinstalled
3828 let env (t:t) =
3829 List.map (function
3830 | var,op,value,None ->
3831 var, op, value,
3832 Some ("Updated by compiler " ^ t.name)
3833 | b -> b)
3834 t.env
3835
3836 let tags t = t.tags
3837
3838 let with_opam_version opam_version t = {t with opam_version}
3839 let with_name name (t:t) = {t with name}
3840 let with_version version (t:t) = {t with version}
3841 let with_src src t = { t with src }
3842 let with_patches patches t = {t with patches}
3843 let with_configure configure t = {t with configure}
3844 let with_make make t = {t with make}
3845 let with_build build t = {t with build}
3846 let with_packages packages t = {t with packages}
3847 let with_preinstalled preinstalled t = {t with preinstalled}
3848 let with_env env t = {t with env}
3849 let with_tags tags t = {t with tags}
3850
3851 let fields =
3852 let with_src url t =
3853 if t.src <> None then Pp.bad_format "Too many URLS"
3854 else with_src (Some url) t
3855 in
3856 [
3857 "opam-version", Pp.ppacc with_opam_version opam_version
3858 (Pp.V.string -| Pp.of_module "opam-version" (module OpamVersion));
3859 "name", Pp.ppacc_opt with_name
3860 (fun t -> if t.name = empty.name then None else Some t.name)
3861 Pp.V.string;
3862 "version", Pp.ppacc_opt with_version
3863 (fun t -> if t.version = empty.version then None else Some t.version)
3864 Pp.V.string;
3865
3866 "src", Pp.ppacc_opt with_src src
3867 Pp.V.url;
3868 "http", Pp.ppacc_opt with_src OpamStd.Option.none
3869 (Pp.V.url_with_backend `http);
3870 "archive", Pp.ppacc_opt with_src OpamStd.Option.none
3871 (Pp.V.url_with_backend `http);
3872 "git", Pp.ppacc_opt with_src OpamStd.Option.none
3873 (Pp.V.url_with_backend `git);
3874 "darcs", Pp.ppacc_opt with_src OpamStd.Option.none
3875 (Pp.V.url_with_backend `darcs);
3876 "hg", Pp.ppacc_opt with_src OpamStd.Option.none
3877 (Pp.V.url_with_backend `hg);
3878 "local", Pp.ppacc_opt with_src OpamStd.Option.none
3879 (Pp.V.url_with_backend `rsync);
3880
3881
3882 "patches", Pp.ppacc with_patches patches
3883 (Pp.V.map_list ~depth:1 @@ Pp.V.url);
3884
3885 "configure", Pp.ppacc with_configure configure
3886 (Pp.V.map_list ~depth:1 Pp.V.string);
3887 "make", Pp.ppacc with_make make
3888 (Pp.V.map_list ~depth:1 Pp.V.string);
3889 "build", Pp.ppacc with_build build
3890 (Pp.V.map_list ~depth:1 Pp.V.command);
3891
3892 "packages", Pp.ppacc with_packages packages
3893 (Pp.V.package_formula `Conj (Pp.V.constraints Pp.V.version));
3894 "env", Pp.ppacc with_env env
3895 (Pp.V.map_list ~depth:2 Pp.V.env_binding);
3896 "preinstalled", Pp.ppacc_opt with_preinstalled
3897 (fun t -> if t.preinstalled then Some true else None)
3898 Pp.V.bool;
3899 "tags", Pp.ppacc with_tags tags
3900 (Pp.V.map_list ~depth:1 Pp.V.string);
3901 ]
3902
3903 let system_compiler = "system"
3904
3905 let version_of_name name =
3906 match OpamStd.String.cut_at name '+' with
3907 | Some (v,_) -> v
3908 | None -> name
3909
3910 let pp_raw =
3911 let name = internal in
3912 Pp.I.map_file @@
3913 Pp.I.check_opam_version ~format_version () -|
3914 Pp.I.opam_version ~format_version () -|
3915 Pp.I.fields ~name ~empty fields -|
3916 Pp.I.show_errors ~name () -|
3917 Pp.check ~errmsg:"fields 'build:' and 'configure:'+'make:' are mutually \
3918 exclusive "
3919 (fun t -> t.build = [] || t.configure = [] && t.make = [])
3920
3921 let of_filename f =
3922 if OpamFilename.check_suffix f ".comp" then
3923 f
3924 |> OpamFilename.chop_extension
3925 |> OpamFilename.basename
3926 |> OpamFilename.Base.to_string
3927 |> fun x -> Some x
3928 else
3929 None
3930
3931 let pp =
3932 pp_raw -|
3933 Pp.pp
3934 (fun ~pos (filename, (t:t)) ->
3935 filename, match of_filename filename with
3936 | None ->
3937 if t.name = empty.name ||
3938 t.name <> "system" && t.version = empty.version
3939 then
3940 Pp.bad_format ~pos
3941 "File name not in the form <name>.<version>, and missing 'name:' \
3942 or 'version:' fields"
3943 else
3944 Pp.warn ~pos
3945 ".comp file name not in the form <name>.<version>";
3946 t
3947 | Some name ->
3948 let version =
3949 if name = "system" then t.version
3950 else version_of_name name
3951 in
3952 if t.name <> empty.name && t.name <> name then
3953 Pp.warn ~pos "Mismatching file name and 'name:' field";
3954 if name <> system_compiler &&
3955 t.version <> empty.version && t.version <> version then
3956 Pp.warn ~pos "Mismatching file name and 'version:' field";
3957 {t with name; version})
3958 (fun (filename, t) ->
3959 filename, match of_filename filename with
3960 | None ->
3961 if t.name = empty.name ||
3962 t.name <> system_compiler && t.version = empty.version
3963 then
3964 OpamConsole.warning
3965 "Outputting .comp file %s with unspecified name or version"
3966 (OpamFilename.to_string filename);
3967 t
3968 | Some name ->
3969 let version =
3970 if name = system_compiler then t.version
3971 else version_of_name name
3972 in
3973 if t.name <> empty.name && t.name <> name ||
3974 name <> system_compiler &&
3975 t.version <> empty.version && t.version <> version
3976 then
3977 OpamConsole.warning
3978 "Skipping inconsistent 'name:' or 'version:' fields (%s.%s) \
3979 while saving %s"
3980 t.name version (OpamFilename.to_string filename);
3981 { t with name = empty.name })
3982
3983 let to_package ?package comp descr_opt =
3984 let package = match package with
3985 | Some p -> p
3986 | None ->
3987 OpamPackage.create
3988 (OpamPackage.Name.of_string "ocaml")
3989 (OpamPackage.Version.of_string (name comp))
3990 in
3991 let nofilter x = x, (None: filter option) in
3992 let depends =
3993 OpamFormula.map (fun (n, formula) ->
3994 let cstr (op, v) =
3995 OpamFormula.ands [
3996 Atom (Constraint (op, FString (OpamPackage.Version.to_string v)));
3997 ]
3998 in
3999 let post_flag =
4000 Filter (FIdent ([], OpamVariable.of_string "post", None))
4001 in
4002 Atom (n, OpamFormula.ands
4003 [OpamFormula.map cstr formula; Atom post_flag]))
4004 (OpamFormula.ands [
4005 Atom (OpamPackage.Name.of_string "ocaml",
4006 Atom (`Eq, OpamPackage.Version.of_string comp.version));
4007 comp.packages
4008 ])
4009 in
4010 let url =
4011 OpamStd.Option.map
4012 (fun url -> URL.with_url url URL.empty)
4013 comp.src
4014 in
4015 let build, install =
4016 match comp.build with
4017 | [] ->
4018 List.map (fun l -> nofilter (List.map nofilter l)) [
4019 (List.map (fun s -> CString s) ("./configure" :: configure comp ))
4020 @ [ CString "-prefix"; CIdent "prefix" ];
4021 CIdent "make" :: List.map (fun s -> CString s) (make comp);
4022 ],
4023 List.map (fun l -> nofilter (List.map nofilter l)) [
4024 [ CIdent "make"; CString "install" ];
4025 ]
4026 | cl ->
4027 match List.rev cl with
4028 | install::cl -> List.rev cl, [install]
4029 | [] -> assert false
4030 in
4031 let extra_sources =
4032 List.map (fun url ->
4033 OpamFilename.Base.of_string (OpamUrl.basename url),
4034 URL.create url)
4035 comp.patches
4036 in
4037 let patches =
4038 List.map
4039 (fun u -> nofilter (OpamFilename.Base.of_string (OpamUrl.basename u)))
4040 comp.patches
4041 in
4042 let pkg = OPAM.create package in
4043 { pkg with
4044 OPAM.
4045 depends;
4046 build;
4047 install;
4048 maintainer = [ "[email protected]" ];
4049 extra_sources;
4050 patches;
4051 env = comp.env;
4052 flags = [Pkgflag_Compiler];
4053 url;
4054 descr = descr_opt;
4055 }
4056
4057 end
4058 module Comp = struct
4059 include CompSyntax
4060 include SyntaxFile(CompSyntax)
4061 end