1 (***********************************************************************)
4 (* Nicolas Pouillard, Berke Durak, projet Gallium, INRIA Rocquencourt *)
6 (* Copyright 2007 Institut National de Recherche en Informatique et *)
7 (* en Automatique. All rights reserved. This file is distributed *)
8 (* under the terms of the Q Public License version 1.0. *)
10 (***********************************************************************)
12 (* $Id: ppcache.ml,v 1.1.4.1 2007-11-21 20:55:26 ertai Exp $ *)
13 (* Original author: Nicolas Pouillard *)
16 open Pathname.Operators
17 let () = Log.level
:= -1000
20 Format.eprintf
"Usage: %s <preprocess-command> <preprocess-args>@." Sys.argv
.(0);
23 let () = if Array.length
Sys.argv
< 2 then usage ()
25 let args = List.tl
(Array.to_list
Sys.argv
)
27 let buf = Buffer.create
2048
29 let digest_file file
=
30 Buffer.add_string
buf (Digest.file file
)
31 let digest_string string =
32 Buffer.add_string
buf (Digest.string string)
34 let search_in_path x
=
35 if Sys.file_exists x
then x
else
37 with Not_found
-> (Format.eprintf
"Command not found %s@." x
; exit
3)
41 | ocamlrun
:: x
:: _
when String.contains_string ocamlrun
0 "ocamlrun" <> None
->
42 digest_file (search_in_path ocamlrun
); x
48 let () = digest_file (search_in_path cmd)
52 | [] -> Digest.string (Buffer.contents
buf)
53 | ("-impl"|"-intf") :: x
:: xs
->
54 digest_string x
; digest_file x
; loop xs
58 let ext = Pathname.get_extension x
in
61 | "cmo" | "cma" | "ml" | "mli" -> digest_file x
65 let digest = loop args;;
67 let cache_dir = "/tmp/ppcache";; (* FIXME *)
69 let () = Shell.mkdir_p
cache_dir;;
71 let path = cache_dir/(Digest.to_hex
digest);;
73 let cat path = with_input_file ~bin
:true path (fun ic
-> copy_chan ic stdout
);;
75 if sys_file_exists
path then
81 let cmd = atomize
args in
82 if !output = "" then begin
83 let tmp = path^
".tmp" in
84 Command.execute
(Cmd
(S
[cmd; Sh
">"; A
tmp]));
88 Command.execute
(Cmd
cmd);