Debian Codegraph @

tools/ocamllibdep.mll

raw
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 {
12 exception Syntax_error of int*int
13
14 let syntax_error lexbuf =
15 raise (Syntax_error (Lexing.lexeme_start lexbuf, Lexing.lexeme_end lexbuf))
16
17 }
18
19 let space = [' ' '\t' '\n' '\r']
20 let lowercase = ['a'-'z']
21 let uppercase = ['A'-'Z']
22 let identchar = ['A'-'Z' 'a'-'z' '_' '\'' '0'-'9']
23 let caml_up_ident = uppercase identchar*
24 let caml_low_ident = lowercase identchar*
25
26 rule mllib_list = parse
27 | uppercase+ { let s = Lexing.lexeme lexbuf in
28 s :: mllib_list lexbuf }
29 | caml_up_ident { let s = String.uncapitalize_ascii (Lexing.lexeme lexbuf)
30 in s :: mllib_list lexbuf }
31 | "*predef*" { mllib_list lexbuf }
32 | space+ { mllib_list lexbuf }
33 | eof { [] }
34 | _ { syntax_error lexbuf }
35
36 {
37 open Printf
38
39 (* Makefile's escaping rules are awful: $ is escaped by doubling and
40 other special characters are escaped by backslash prefixing while
41 backslashes themselves must be escaped only if part of a sequence
42 followed by a special character (i.e. in case of ambiguity with a
43 use of it as escaping character). Moreover (even if not crucial)
44 it is apparently not possible to directly escape ';' and leading '\t'. *)
45
46 let escape =
47 let s' = Buffer.create 10 in
48 fun s ->
49 Buffer.clear s';
50 for i = 0 to String.length s - 1 do
51 let c = s.[i] in
52 if c = ' ' || c = '#' || c = ':' (* separators and comments *)
53 || c = '%' (* pattern *)
54 || c = '?' || c = '[' || c = ']' || c = '*' (* expansion in filenames *)
55 || i=0 && c = '~' && (String.length s = 1 || s.[1] = '/' ||
56 'A' <= s.[1] && s.[1] <= 'Z' ||
57 'a' <= s.[1] && s.[1] <= 'z') (* homedir expansion *)
58 then begin
59 let j = ref (i-1) in
60 while !j >= 0 && s.[!j] = '\\' do
61 Buffer.add_char s' '\\'; decr j (* escape all preceding '\' *)
62 done;
63 Buffer.add_char s' '\\';
64 end;
65 if c = '$' then Buffer.add_char s' '$';
66 Buffer.add_char s' c
67 done;
68 Buffer.contents s'
69
70 (* Filename.concat but always with a '/' *)
71 let is_dir_sep s i =
72 match Sys.os_type with
73 | "Unix" -> s.[i] = '/'
74 | "Cygwin" | "Win32" ->
75 let c = s.[i] in c = '/' || c = '\\' || c = ':'
76 | _ -> assert false
77
78 let (//) dirname filename =
79 let l = String.length dirname in
80 if l = 0 || is_dir_sep dirname (l-1)
81 then dirname ^ filename
82 else dirname ^ "/" ^ filename
83
84 (** [get_extension f l] checks whether [f] has one of the extensions
85 listed in [l]. It returns [f] without its extension, alongside with
86 the extension. When no extension match, [(f,"")] is returned *)
87
88 let rec get_extension f = function
89 | [] -> (f, "")
90 | s :: _ when Filename.check_suffix f s -> (Filename.chop_suffix f s, s)
91 | _ :: l -> get_extension f l
92
93 let file_name s = function
94 | None -> s
95 | Some "." -> s
96 | Some d -> d // s
97
98 type dir = string option
99
100 let add_directory add_file phys_dir =
101 let open Unix in
102 let files = Sys.readdir phys_dir in
103 Array.iter (fun f ->
104 (* we avoid all files starting by '.' *)
105 if f.[0] <> '.' then
106 let phys_f = if phys_dir = "." then f else phys_dir//f in
107 match try (stat phys_f).st_kind with _ -> S_BLK with
108 | S_REG -> add_file phys_dir f
109 | _ -> ()) files
110
111 let error_cannot_parse s (i,j) =
112 Printf.eprintf "File \"%s\", characters %i-%i: Syntax error\n" s i j;
113 exit 1
114
115 let error_unknown_extension name =
116 Printf.eprintf "Don't know what to do with \"%s\"\n" name;
117 Printf.eprintf "Usage: ocamllibdep [-I dir] [-c] [file.mllib] [file.mlpack]\n";
118 exit 1
119
120 let error_cannot_open msg =
121 Format.eprintf "Error: @[%s@].@\n%!" msg;
122 exit 1
123
124 let error_cannot_stat err name =
125 Format.eprintf "Error: @[cannot stat %s (%s)@].@\n%!" name (Unix.error_message err);
126 exit 1
127
128 let same_path_opt s s' =
129 let nf s = (* ./foo/a.ml and foo/a.ml are the same file *)
130 if Filename.is_implicit s
131 then "." // s
132 else s
133 in
134 let s = match s with None -> "." | Some s -> nf s in
135 let s' = match s' with None -> "." | Some s' -> nf s' in
136 s = s'
137
138 let warning_ml_clash x s suff s' suff' =
139 if suff = suff' && not (same_path_opt s s') then
140 eprintf
141 "*** Warning: %s%s already found in %s (discarding %s%s)\n" x suff
142 (match s with None -> "." | Some d -> d)
143 ((match s' with None -> "." | Some d -> d) // x) suff
144
145 let mkknown () =
146 let h = (Hashtbl.create 19 : (string, dir * string) Hashtbl.t) in
147 let add x s suff =
148 try let s',suff' = Hashtbl.find h x in warning_ml_clash x s' suff' s suff
149 with Not_found -> Hashtbl.add h x (s,suff)
150 and search x =
151 try Some (fst (Hashtbl.find h x))
152 with Not_found -> None
153 in add, search
154
155 let add_ml_known, search_ml_known = mkknown ()
156 let add_mlpack_known, search_mlpack_known = mkknown ()
157
158 let mllibAccu = ref ([] : (string * dir) list)
159 let mlpackAccu = ref ([] : (string * dir) list)
160
161 let add_caml_known phys_dir f =
162 let basename,suff = get_extension f [".ml";".mlg";".mlpack"] in
163 match suff with
164 | ".ml"|".mlg" -> add_ml_known basename (Some phys_dir) suff
165 | ".mlpack" -> add_mlpack_known basename (Some phys_dir) suff
166 | _ -> ()
167
168 let add_caml_dir phys_dir =
169 try add_directory add_caml_known phys_dir
170 with
171 | Sys_error msg -> error_cannot_open msg
172 | Unix.Unix_error (e, "stat", a) -> error_cannot_stat e a
173
174 let treat_file_modules md ext =
175 try
176 let chan = open_in (md ^ ext) in
177 let list = mllib_list (Lexing.from_channel chan) in
178 List.fold_left
179 (fun acc str ->
180 match search_mlpack_known str with
181 | Some mldir -> (file_name str mldir) :: acc
182 | None ->
183 match search_ml_known str with
184 | Some mldir -> (file_name str mldir) :: acc
185 | None -> acc) [] (List.rev list)
186 with
187 | Sys_error _ -> []
188 | Syntax_error (i,j) -> error_cannot_parse (md^ext) (i,j)
189
190 let addQueue q v = q := v :: !q
191
192 let treat_file old_name =
193 let name = Filename.basename old_name in
194 let dirname = Some (Filename.dirname old_name) in
195 match get_extension name [".mllib";".mlpack"] with
196 | (base,".mllib") -> addQueue mllibAccu (base,dirname)
197 | (base,".mlpack") -> addQueue mlpackAccu (base,dirname)
198 | _ -> error_unknown_extension old_name
199
200 let mllib_dependencies () =
201 List.iter
202 (fun (name,dirname) ->
203 let fullname = file_name name dirname in
204 let deps = treat_file_modules fullname ".mllib" in
205 let sdeps = String.concat " " deps in
206 let efullname = escape fullname in
207 printf "%s_MLLIB_DEPENDENCIES:=%s\n" efullname sdeps;
208 printf "%s.cma:$(addsuffix .cmo,$(%s_MLLIB_DEPENDENCIES))\n"
209 efullname efullname;
210 printf "%s.cmxa:$(addsuffix .cmx,$(%s_MLLIB_DEPENDENCIES))\n"
211 efullname efullname;
212 flush stdout)
213 (List.rev !mllibAccu)
214
215 let coq_makefile_mode = ref false
216
217 let print_for_pack modname d =
218 if !coq_makefile_mode then
219 printf "%s.cmx : FOR_PACK=-for-pack %s\n" d modname
220 else
221 printf "%s_FORPACK:= -for-pack %s\n" d modname
222
223 let mlpack_dependencies () =
224 List.iter
225 (fun (name,dirname) ->
226 let fullname = file_name name dirname in
227 let modname = String.capitalize_ascii name in
228 let deps = treat_file_modules fullname ".mlpack" in
229 let sdeps = String.concat " " deps in
230 let efullname = escape fullname in
231 printf "%s_MLPACK_DEPENDENCIES:=%s\n" efullname sdeps;
232 List.iter (print_for_pack modname) deps;
233 printf "%s.cmo:$(addsuffix .cmo,$(%s_MLPACK_DEPENDENCIES))\n"
234 efullname efullname;
235 printf "%s.cmx:$(addsuffix .cmx,$(%s_MLPACK_DEPENDENCIES))\n"
236 efullname efullname;
237 flush stdout)
238 (List.rev !mlpackAccu)
239
240 let rec parse = function
241 | "-c" :: r -> coq_makefile_mode := true; parse r
242 | "-I" :: r :: ll ->
243 (* To solve conflict (e.g. same filename in kernel and checker)
244 we allow to state an explicit order *)
245 add_caml_dir r;
246 parse ll
247 | f :: ll -> treat_file f; parse ll
248 | [] -> ()
249
250 let main () =
251 if Array.length Sys.argv < 2 then exit 1;
252 parse (List.tl (Array.to_list Sys.argv));
253 mllib_dependencies ();
254 mlpack_dependencies ()
255
256 let _ = main ()
257 }