| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
type arg_source = CmdLine | ProjectFile |
| 16 |
|
| 17 |
|
| 18 |
type 'a sourced = { thing : 'a; source : arg_source } |
| 19 |
|
| 20 |
type meta_file = Absent | Present of string | Generate of string |
| 21 |
|
| 22 |
type 'a project = { |
| 23 |
project_file : string option; |
| 24 |
makefile : string option; |
| 25 |
native_compiler : native_compiler option; |
| 26 |
docroot : string option; |
| 27 |
|
| 28 |
files : string sourced list; |
| 29 |
cmd_line_files : string sourced list; |
| 30 |
meta_file : meta_file; |
| 31 |
|
| 32 |
ml_includes : path sourced list; |
| 33 |
r_includes : (path * logic_path) sourced list; |
| 34 |
q_includes : (path * logic_path) sourced list; |
| 35 |
extra_args : string sourced list; |
| 36 |
defs : (string * string) sourced list; |
| 37 |
|
| 38 |
extra_data : 'a; |
| 39 |
} |
| 40 |
and logic_path = string |
| 41 |
and path = { path : string; canonical_path : string } |
| 42 |
and native_compiler = |
| 43 |
| NativeYes |
| 44 |
| NativeNo |
| 45 |
| NativeOndemand |
| 46 |
|
| 47 |
|
| 48 |
let mk_project project_file makefile native_compiler extra_data = { |
| 49 |
project_file; |
| 50 |
makefile; |
| 51 |
native_compiler; |
| 52 |
docroot = None; |
| 53 |
|
| 54 |
files = []; |
| 55 |
cmd_line_files = []; |
| 56 |
ml_includes = []; |
| 57 |
meta_file = Absent; |
| 58 |
r_includes = []; |
| 59 |
q_includes = []; |
| 60 |
extra_args = []; |
| 61 |
defs = []; |
| 62 |
|
| 63 |
extra_data; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
let rec post_canonize f = |
| 69 |
if Filename.basename f = Filename.current_dir_name |
| 70 |
then let dir = Filename.dirname f in |
| 71 |
if dir = Filename.current_dir_name then f else post_canonize dir |
| 72 |
else f |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
exception Parsing_error of string |
| 77 |
|
| 78 |
let buffer buf = |
| 79 |
let ans = Buffer.contents buf in |
| 80 |
let () = Buffer.clear buf in |
| 81 |
ans |
| 82 |
|
| 83 |
let rec parse_string buf s = match input_char s with |
| 84 |
| ' ' | '\n' | '\r' | '\t' -> buffer buf |
| 85 |
| '#' -> |
| 86 |
parse_skip_comment s; |
| 87 |
buffer buf |
| 88 |
| c -> |
| 89 |
let () = Buffer.add_char buf c in |
| 90 |
parse_string buf s |
| 91 |
| exception End_of_file -> buffer buf |
| 92 |
|
| 93 |
and parse_string2 buf s = match input_char s with |
| 94 |
| '"' -> buffer buf |
| 95 |
| c -> |
| 96 |
let () = Buffer.add_char buf c in |
| 97 |
parse_string2 buf s |
| 98 |
| exception End_of_file -> raise (Parsing_error "unterminated string") |
| 99 |
|
| 100 |
and parse_skip_comment s = match input_char s with |
| 101 |
| '\n' -> () |
| 102 |
| _ -> parse_skip_comment s |
| 103 |
| exception End_of_file -> () |
| 104 |
|
| 105 |
and parse_args buf accu s = match input_char s with |
| 106 |
| ' ' | '\n' | '\r' | '\t' -> parse_args buf accu s |
| 107 |
| '#' -> |
| 108 |
let () = parse_skip_comment s in |
| 109 |
parse_args buf accu s |
| 110 |
| '"' -> |
| 111 |
let str = parse_string2 buf s in |
| 112 |
parse_args buf (str :: accu) s |
| 113 |
| c -> |
| 114 |
let () = Buffer.add_char buf c in |
| 115 |
let str = parse_string buf s in |
| 116 |
parse_args buf (str :: accu) s |
| 117 |
| exception End_of_file -> accu |
| 118 |
|
| 119 |
exception UnableToOpenProjectFile of string |
| 120 |
|
| 121 |
let parse f = |
| 122 |
let c = try open_in f with Sys_error msg -> raise (UnableToOpenProjectFile msg) in |
| 123 |
let buf = Buffer.create 64 in |
| 124 |
let res = parse_args buf [] c in |
| 125 |
close_in c; |
| 126 |
List.rev res |
| 127 |
|
| 128 |
let parse_native ~warning_fn ~error proj flag = |
| 129 |
if proj.native_compiler <> None then |
| 130 |
(warning_fn "-native-compiler set more than once."); |
| 131 |
let native = match flag with |
| 132 |
| "yes" -> NativeYes |
| 133 |
| "no" -> NativeNo |
| 134 |
| "ondemand" -> NativeOndemand |
| 135 |
| _ -> error ("invalid option \""^flag^"\" passed to -native-compiler") |
| 136 |
in |
| 137 |
{ proj with native_compiler = Some native } |
| 138 |
|
| 139 |
let escape_char c = |
| 140 |
match c with |
| 141 |
| '\'' -> "\'" |
| 142 |
| '\n' -> "\\n" |
| 143 |
| '\r' -> "\\r" |
| 144 |
| '\t' -> "\\t" |
| 145 |
| c -> String.make 1 c |
| 146 |
|
| 147 |
let check_filename f = |
| 148 |
let a = ref None in |
| 149 |
let check_char c = |
| 150 |
match c with |
| 151 |
| '\n' | '\r' | '\t' | '\\' | '\'' | '"' | ' ' | '#' | '$' | '%' -> a := Some c |
| 152 |
| _ -> () |
| 153 |
in |
| 154 |
String.iter check_char f; |
| 155 |
match !a with |
| 156 |
| Some c -> raise (Parsing_error ("Unsupported filename, contains '" |
| 157 |
^ (escape_char c) ^ "' : \"" ^ String.escaped f ^ "\"")) |
| 158 |
| None -> () |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
let process_extra_args arg = |
| 164 |
let buf = Buffer.create 64 in |
| 165 |
let out_list = ref [] in |
| 166 |
let inside_quotes = ref false in |
| 167 |
let has_leftovers = ref false in |
| 168 |
let process_char c = |
| 169 |
match c with |
| 170 |
| '\'' -> |
| 171 |
inside_quotes := not !inside_quotes; |
| 172 |
has_leftovers := true; |
| 173 |
| ' ' -> |
| 174 |
if !inside_quotes then |
| 175 |
(has_leftovers := true; Buffer.add_char buf ' ') |
| 176 |
else |
| 177 |
if !has_leftovers then |
| 178 |
(has_leftovers := false; |
| 179 |
out_list := buffer buf :: !out_list) |
| 180 |
| c -> has_leftovers := true; Buffer.add_char buf c |
| 181 |
in |
| 182 |
String.iter process_char arg; |
| 183 |
if !has_leftovers then |
| 184 |
out_list := buffer buf :: !out_list; |
| 185 |
List.rev !out_list |
| 186 |
|
| 187 |
let expand_paths project = |
| 188 |
let orig_dir = Filename.current_dir_name in |
| 189 |
let orig_dir = if orig_dir = "." then "" else orig_dir in |
| 190 |
let abs_f f = CUnix.correct_path f orig_dir in |
| 191 |
let rec expand_dir path rv = |
| 192 |
let add_file f = |
| 193 |
if List.mem (Filename.extension f) |
| 194 |
[".v"; ".mli"; ".ml"; ".mlg"; ".mlpack"; ".mllib"] then |
| 195 |
rv := {thing=abs_f f; source=ProjectFile} :: !rv |
| 196 |
in |
| 197 |
if Sys.file_exists path && Sys.is_directory path then |
| 198 |
System.process_directory (fun fname -> |
| 199 |
match fname with |
| 200 |
| FileRegular f -> add_file (if path <> "." then Filename.concat path f else f) |
| 201 |
| FileDir (p,_) -> expand_dir p rv |
| 202 |
) path |
| 203 |
else |
| 204 |
add_file path |
| 205 |
in |
| 206 |
List.fold_left (fun rv path -> |
| 207 |
let exp = ref [] in |
| 208 |
expand_dir path.thing exp; |
| 209 |
(List.sort (fun a b -> String.compare a.thing b.thing) !exp) @ rv) |
| 210 |
[] project.files |
| 211 |
|
| 212 |
let process_cmd_line ~warning_fn orig_dir parse_extra proj args = |
| 213 |
let parsing_project_file = ref (proj.project_file <> None) in |
| 214 |
let sourced x = { thing = x; source = if !parsing_project_file then ProjectFile else CmdLine } in |
| 215 |
let orig_dir = |
| 216 |
if orig_dir = "." then "" else orig_dir in |
| 217 |
let error s = (Format.eprintf "Error: @[%s@].@\n%!" s; exit 1) in |
| 218 |
let mk_path d = |
| 219 |
let p = CUnix.correct_path d orig_dir in |
| 220 |
{ path = CUnix.remove_path_dot (post_canonize p); |
| 221 |
canonical_path = CUnix.canonical_path_name p } in |
| 222 |
let rec aux proj = function |
| 223 |
| [] -> proj |
| 224 |
| "-impredicative-set" :: _ -> |
| 225 |
error "Use \"-arg -impredicative-set\" instead of \"-impredicative-set\"" |
| 226 |
|
| 227 |
| "-Q" :: d :: lp :: r -> |
| 228 |
let lp = if String.equal lp "Coq" then "Corelib" else lp in |
| 229 |
aux { proj with q_includes = proj.q_includes @ [sourced (mk_path d,lp)] } r |
| 230 |
| "-I" :: d :: r -> |
| 231 |
aux { proj with ml_includes = proj.ml_includes @ [sourced (mk_path d)] } r |
| 232 |
| "-R" :: d :: lp :: r -> |
| 233 |
let lp = if String.equal lp "Coq" then "Corelib" else lp in |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
aux { proj with r_includes = proj.r_includes @ [sourced (mk_path d,lp)] } r |
| 238 |
|
| 239 |
| "-native-compiler" :: flag :: r -> |
| 240 |
let proj = parse_native ~warning_fn ~error proj flag in |
| 241 |
aux proj r |
| 242 |
|
| 243 |
| "-f" :: file :: r -> |
| 244 |
if !parsing_project_file then |
| 245 |
raise (Parsing_error ("Invalid option -f in project file " ^ Option.get proj.project_file)); |
| 246 |
let file = CUnix.remove_path_dot (CUnix.correct_path file orig_dir) in |
| 247 |
let () = match proj.project_file with |
| 248 |
| None -> () |
| 249 |
| Some _ -> warning_fn "Multiple project files are deprecated." |
| 250 |
in |
| 251 |
parsing_project_file := true; |
| 252 |
let r' = try parse file with UnableToOpenProjectFile msg -> error msg in |
| 253 |
let proj = aux { proj with project_file = Some file } r' in |
| 254 |
parsing_project_file := false; |
| 255 |
aux proj r |
| 256 |
|
| 257 |
| "-o" :: file :: r -> |
| 258 |
if !parsing_project_file then |
| 259 |
raise (Parsing_error ("Invalid option -o in project file " ^ Option.get proj.project_file)); |
| 260 |
if String.contains file '/' then |
| 261 |
error "Output file must be in the current directory"; |
| 262 |
if proj.makefile <> None then |
| 263 |
error "Option -o given more than once"; |
| 264 |
aux { proj with makefile = Some file } r |
| 265 |
|
| 266 |
| "-docroot" :: p :: r -> |
| 267 |
if proj.docroot <> None then |
| 268 |
error "Option -docroot given more than once"; |
| 269 |
aux { proj with docroot = Some p } r |
| 270 |
|
| 271 |
| "-generate-meta-for-package" :: m :: r -> |
| 272 |
if proj.meta_file <> Absent then |
| 273 |
error "Option -generate-meta-for-package cannot be repeated"; |
| 274 |
aux { proj with meta_file = Generate m } r |
| 275 |
|
| 276 |
|
| 277 |
| v :: "=" :: def :: r -> |
| 278 |
aux { proj with defs = proj.defs @ [sourced (v,def)] } r |
| 279 |
| "-arg" :: a :: r -> |
| 280 |
aux { proj with extra_args = proj.extra_args @ List.map sourced (process_extra_args a) } r |
| 281 |
| f :: r when CString.is_prefix "-" f -> begin match parse_extra f r proj.extra_data with |
| 282 |
| None -> raise (Parsing_error ("Unknown option " ^ f)) |
| 283 |
| Some (r,extra_data) -> aux { proj with extra_data } r |
| 284 |
end |
| 285 |
| f :: r -> |
| 286 |
let abs_f = CUnix.correct_path f orig_dir in |
| 287 |
let ext = Filename.extension abs_f in |
| 288 |
let proj = |
| 289 |
match ext with |
| 290 |
| ".v" |
| 291 |
| ".ml" |
| 292 |
| ".mli" |
| 293 |
| ".mlg" |
| 294 |
| ".mllib" |
| 295 |
| ".mlpack" -> |
| 296 |
check_filename f; |
| 297 |
{ proj with files = (sourced abs_f) :: proj.files } |
| 298 |
| ".ml4" -> |
| 299 |
let msg = Printf.sprintf "camlp5 macro files not supported anymore, please port %s to coqpp" abs_f in |
| 300 |
raise (Parsing_error msg) |
| 301 |
| _ -> |
| 302 |
if CString.is_prefix "META." (Filename.basename abs_f) then |
| 303 |
if proj.meta_file = Absent then |
| 304 |
{ proj with meta_file = Present abs_f } |
| 305 |
else |
| 306 |
raise (Parsing_error "only one META.package file can be specified") |
| 307 |
else if ext = "" && not (CString.is_prefix "-" f) then begin |
| 308 |
check_filename f; |
| 309 |
{ proj with files = (sourced abs_f) :: proj.files } |
| 310 |
end else |
| 311 |
raise (Parsing_error ("Unknown option " ^ f)) in |
| 312 |
aux proj r |
| 313 |
in |
| 314 |
let proj = aux proj args in |
| 315 |
|
| 316 |
let rec filter_extra proj = function |
| 317 |
| [] -> { proj with extra_args = [] } |
| 318 |
| { thing = "-native-compiler" } :: { thing = flag } :: r -> |
| 319 |
let proj = parse_native ~warning_fn ~error proj flag in |
| 320 |
filter_extra proj r |
| 321 |
| extra :: r -> |
| 322 |
let proj = filter_extra proj r in |
| 323 |
{ proj with extra_args = extra :: proj.extra_args } |
| 324 |
in |
| 325 |
let project = filter_extra proj proj.extra_args in |
| 326 |
let cmd_line_files = CList.rev (CList.map_filter |
| 327 |
(function {thing; source=CmdLine} -> Some {thing; source=CmdLine} |
| 328 |
| {source=ProjectFile} -> None) |
| 329 |
project.files) in |
| 330 |
{ project with cmd_line_files; files = expand_paths project } |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
let cmdline_args_to_project ~warning_fn ~curdir ~parse_extra extra args = |
| 335 |
process_cmd_line ~warning_fn curdir parse_extra (mk_project None None None extra) args |
| 336 |
|
| 337 |
let read_project_file ~warning_fn f = |
| 338 |
process_cmd_line ~warning_fn (Filename.dirname f) (fun _ _ () -> None) |
| 339 |
(mk_project (Some f) None None ()) (parse f) |
| 340 |
|
| 341 |
let rec find_project_file ~from ~projfile_name = |
| 342 |
let fname = Filename.concat from projfile_name in |
| 343 |
if Sys.file_exists fname then Some fname |
| 344 |
else |
| 345 |
let newdir = Filename.dirname from in |
| 346 |
if newdir = from then None |
| 347 |
else find_project_file ~from:newdir ~projfile_name |
| 348 |
|
| 349 |
let all_files { files } = files |
| 350 |
|
| 351 |
let files_by_suffix files suffixes = |
| 352 |
List.filter (fun file -> List.mem (Filename.extension file.thing) suffixes) files |
| 353 |
|
| 354 |
let map_sourced_list f l = List.map (fun x -> f x.thing) l |
| 355 |
|
| 356 |
let map_cmdline f l = CList.map_filter (function |
| 357 |
| {thing=x; source=CmdLine} -> Some (f x) |
| 358 |
| {source=ProjectFile} -> None) l |
| 359 |
|
| 360 |
let coqtop_args_from_project |
| 361 |
{ ml_includes; r_includes; q_includes; extra_args } |
| 362 |
= |
| 363 |
let map = map_sourced_list in |
| 364 |
let args = |
| 365 |
map (fun { canonical_path = i } -> ["-I"; i]) ml_includes @ |
| 366 |
map (fun ({ canonical_path = i }, l) -> ["-Q"; i; l]) q_includes @ |
| 367 |
map (fun ({ canonical_path = p }, l) -> ["-R"; p; l]) r_includes @ |
| 368 |
[map (fun x -> x) extra_args] in |
| 369 |
List.flatten args |
| 370 |
|
| 371 |
let forget_source {thing} = thing |
| 372 |
|
| 373 |
|