Debian Codegraph @

tools/coqdep/lib/makefile.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* Makefile's escaping rules are awful: $ is escaped by doubling and
12 other special characters are escaped by backslash prefixing while
13 backslashes themselves must be escaped only if part of a sequence
14 followed by a special character (i.e. in case of ambiguity with a
15 use of it as escaping character). Moreover (even if not crucial)
16 it is apparently not possible to directly escape ';' and leading '\t'. *)
17
18 let escape =
19 let s' = Buffer.create 10 in
20 fun s ->
21 Buffer.clear s';
22 for i = 0 to String.length s - 1 do
23 let c = s.[i] in
24 if c = ' ' || c = '#' || c = ':' (* separators and comments *)
25 || c = '%' (* pattern *)
26 || c = '?' || c = '[' || c = ']' || c = '*' (* expansion in filenames *)
27 || i=0 && c = '~' && (String.length s = 1 || s.[1] = '/' ||
28 'A' <= s.[1] && s.[1] <= 'Z' ||
29 'a' <= s.[1] && s.[1] <= 'z') (* homedir expansion *)
30 then begin
31 let j = ref (i-1) in
32 while !j >= 0 && s.[!j] = '\\' do
33 Buffer.add_char s' '\\'; decr j (* escape all preceding '\' *)
34 done;
35 Buffer.add_char s' '\\';
36 end;
37 if c = '$' then Buffer.add_char s' '$';
38 Buffer.add_char s' c
39 done;
40 Buffer.contents s'
41
42 open Format
43
44 type dynlink = Opt | Byte | Both | No | Variable
45 let option_dynlink = ref Both
46
47 let set_dyndep = function
48 | "no" -> option_dynlink := No
49 | "opt" -> option_dynlink := Opt
50 | "byte" -> option_dynlink := Byte
51 | "both" -> option_dynlink := Both
52 | "var" -> option_dynlink := Variable
53 | o -> CErrors.user_err Pp.(str "Incorrect -dyndep option: " ++ str o)
54
55 let mldep_to_make base =
56 match !option_dynlink with
57 | No -> []
58 | Byte -> [sprintf "%s.cma" base]
59 | Opt -> [sprintf "%s.cmxs" base]
60 | Both ->
61 [sprintf "%s.cma" base; sprintf "%s.cmxs" base]
62 | Variable ->
63 [sprintf "%s%s" base "$(DYNLIB)"]
64
65 let string_of_dep ~suffix = let open Dep_info.Dep in
66 function
67 | Require basename -> [escape basename ^ suffix]
68 | Ml base -> mldep_to_make (escape base)
69 | Other s -> [escape s]
70
71 let string_of_dependency_list ~suffix deps =
72 List.map (string_of_dep ~suffix) deps
73 |> List.concat |> String.concat " "
74
75 let option_noglob = ref false
76 let option_write_vos = ref false
77 let set_noglob glob = option_noglob := glob
78 let set_write_vos vos = option_write_vos := vos
79
80 let print_dep fmt { Dep_info.name; deps } =
81 let ename = escape name in
82 let glob = if !option_noglob then "" else ename^".glob " in
83 fprintf fmt "%s.vo %s%s.v.beautified %s.required_vo: %s.v %s\n" ename glob ename ename ename
84 (string_of_dependency_list ~suffix:".vo" deps);
85 if !option_write_vos then
86 fprintf fmt "%s.vos %s.vok %s.required_vos: %s.v %s\n" ename ename ename ename
87 (string_of_dependency_list ~suffix:".vos" deps);
88 fprintf fmt "%!"