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 (***********************************************************************)
13 (* Original author: Berke Durak *)
18 exception Exit_hygiene_violations
21 | Implies_not of pattern * pattern
26 type penalty = Warn | Fail
30 law_rules : rule list;
34 let list_collect f l =
35 let rec loop result = function
36 | [] -> List.rev result
39 | None -> loop result rest
40 | Some y -> loop (y :: result) rest
44 let list_none_for_all f l =
45 let rec loop = function
54 let sf = Printf.sprintf
56 module SS = Set.Make(String);;
58 let check ?sanitize laws entry =
59 let penalties = ref [] in
60 let microbes = ref SS.empty in
63 | Some fn -> if sys_file_exists fn then sys_remove fn
66 let remove path name =
67 if sanitize <> None then
68 microbes := SS.add (filename_concat path name) !microbes
70 let check_rule = fun entries -> function
74 | File(path, name, _, true) ->
75 if Filename.check_suffix name suffix then
78 Some(sf "File %s in %s has suffix %s" name path suffix)
82 | File _ | Dir _| Error _ | Nothing -> None
85 | Implies_not(suffix1, suffix2) ->
88 | File(path, name, _, true) ->
89 if Filename.check_suffix name suffix1 then
91 let base = Filename.chop_suffix name suffix1 in
92 let name' = base ^ suffix2 in
95 | File(_, name'', _, true) -> name' = name''
96 | File _ | Dir _ | Error _ | Nothing -> false
102 Some(sf "Files %s and %s should not be together in %s" name name' path)
109 | File _ | Dir _ | Error _ | Nothing -> None
113 let rec check_entry = function
114 | Dir(_,_,_,true,entries) ->
117 match List.concat (List.map (check_rule !*entries) law.law_rules) with
120 penalties := (law, explanations) :: !penalties
123 List.iter check_entry !*entries
124 | Dir _ | File _ | Error _ | Nothing -> ()
128 let microbes = !microbes in
129 if not (SS.is_empty microbes) then
133 Log.eprintf "sanitize: the following are files that should probably not be in your\n\
140 Log.eprintf "Remove them manually, don't use the -no-sanitize option, use -no-hygiene, or\n\
141 define hygiene exceptions using the tags or plugin mechanism.\n";
142 raise Exit_hygiene_violations
144 let m = SS.cardinal microbes in
146 "@[<hov 2>SANITIZE:@ a@ total@ of@ %d@ file%s@ that@ should@ probably\
147 @ not@ be@ in@ your@ source@ tree@ has@ been@ found.\
148 @ A@ script@ shell@ file@ %S@ is@ being@ created.\
149 @ Check@ this@ script@ and@ run@ it@ to@ remove@ unwanted@ files\
150 @ or@ use@ other@ options@ (such@ as@ defining@ hygiene@ exceptions\
151 @ or@ using@ the@ -no-hygiene@ option).@]"
152 m (if m = 1 then "" else "s") fn;
153 let oc = open_out_gen [Open_wronly; Open_creat; Open_trunc; Open_text] 0o755 fn in
154 let fp = Printf.fprintf in
156 # File generated by ocamlbuild\n\
159 \n" (Shell.quote_filename_if_needed Pathname.pwd);
162 fp oc "rm -f %s\n" (Shell.quote_filename_if_needed fn)
165 (* Also clean itself *)
166 fp oc "# Also clean the script itself\n";
167 fp oc "rm -f %s\n" (Shell.quote_filename_if_needed fn);