|
| 1 | +(* *********************************************************************) |
| 2 | +(* *) |
| 3 | +(* The Compcert verified compiler *) |
| 4 | +(* *) |
| 5 | +(* Bernhard Schommer, AbstInt Angewandte Informatik GmbH *) |
| 6 | +(* *) |
| 7 | +(* Copyright AbstInt Angewandte Informatik GmbH. All rights reserved. *) |
| 8 | +(* This file is distributed under the terms of the GNU General Public *) |
| 9 | +(* License as published by the Free Software Foundation, either *) |
| 10 | +(* version 2 of the License, or (at your option) any later version. *) |
| 11 | +(* *) |
| 12 | +(* *********************************************************************) |
| 13 | + |
| 14 | +(* Generate dependencies for directories by calling dependencie tool *) |
| 15 | + |
| 16 | +(* The tools *) |
| 17 | +let ocamlfind = ref "ocamlfind" |
| 18 | +let ocamldep = ref "ocamldep" |
| 19 | +let menhir = ref "menhir" |
| 20 | + |
| 21 | +(* Some controling options *) |
| 22 | +let use_ocamlfind = ref false |
| 23 | +let use_menhir = ref false |
| 24 | +let dirs_to_search = ref ([] : string list) |
| 25 | +let target_file = ref (None: string option) |
| 26 | +let error_occured = ref false |
| 27 | + |
| 28 | +(* Options for ocamldep *) |
| 29 | +let include_dirs = ref ([] : string list) |
| 30 | +let ml_synonyms = ref [".ml"] |
| 31 | +let mli_synonyms = ref [".mli"] |
| 32 | +let slash = ref false |
| 33 | +let native_only = ref false |
| 34 | +let raw_dependencies = ref false |
| 35 | +let pp_command = ref (None: string option) |
| 36 | +let ppx_command = ref ([] : string list) |
| 37 | +let all_dependencies = ref false |
| 38 | +let open_modules = ref ([] : string list) |
| 39 | +let one_line = ref false |
| 40 | +let ocamlfind_package = ref "" |
| 41 | +let ocamlfind_syntax = ref "" |
| 42 | +let ocamlfind_ppopt = ref ([] : string list) |
| 43 | + |
| 44 | +(* Helper functions for options *) |
| 45 | +let add_to_list li s = |
| 46 | + li := s :: !li |
| 47 | + |
| 48 | +let add_to_synonym_list synonyms suffix = |
| 49 | + if (String.length suffix) > 1 && (String.get suffix 0) = '.' then |
| 50 | + add_to_list synonyms suffix |
| 51 | + else |
| 52 | + Printf.eprintf "Bad file suffix '%s'.\n" suffix |
| 53 | + |
| 54 | +let usage = "Usage: recdepend [options] <directories>\nOptions are:" |
| 55 | + |
| 56 | +type file_type = |
| 57 | + | ML |
| 58 | + | MLI |
| 59 | + | MLL |
| 60 | + |
| 61 | +let get_files () = |
| 62 | + let rec files_of_dir acc dir = |
| 63 | + let files = Sys.readdir dir in |
| 64 | + let contains_files = ref false in |
| 65 | + let acc = Array.fold_left (fun acc f_name -> |
| 66 | + if Sys.is_directory (Filename.concat dir f_name) then |
| 67 | + files_of_dir acc (Filename.concat dir f_name) |
| 68 | + else |
| 69 | + if List.exists (Filename.check_suffix f_name) !ml_synonyms then |
| 70 | + begin |
| 71 | + contains_files := true; |
| 72 | + (ML,(Filename.concat dir f_name))::acc |
| 73 | + end |
| 74 | + else if List.exists (Filename.check_suffix f_name) !mli_synonyms then |
| 75 | + begin |
| 76 | + contains_files := true; |
| 77 | + (MLI,(Filename.concat dir f_name))::acc |
| 78 | + end |
| 79 | + else if !use_menhir && (Filename.check_suffix f_name ".mll") then |
| 80 | + begin |
| 81 | + contains_files := true; |
| 82 | + (MLL,(Filename.concat dir f_name))::acc |
| 83 | + end |
| 84 | + else |
| 85 | + acc) acc files in |
| 86 | + if !contains_files then |
| 87 | + add_to_list include_dirs dir; |
| 88 | + acc in |
| 89 | + try |
| 90 | + List.fold_left files_of_dir [] !dirs_to_search |
| 91 | + with _ -> |
| 92 | + error_occured := true; |
| 93 | + [] |
| 94 | + |
| 95 | +let compute_dependencies files = |
| 96 | + try let out_file = match !target_file with |
| 97 | + | None -> Unix.stdout |
| 98 | + | Some s -> Unix.openfile s [Unix.O_WRONLY; Unix.O_CREAT; Unix.O_TRUNC] 0o666 in |
| 99 | + let call_ocamldep file = |
| 100 | + let args = List.fold_left (fun args s -> "-I"::(s::args)) [file] !include_dirs in |
| 101 | + let args = List.fold_left (fun args syn -> |
| 102 | + if syn = ".ml" then |
| 103 | + args |
| 104 | + else |
| 105 | + "-ml-synonym"::(syn::args)) args !ml_synonyms in |
| 106 | + let args = List.fold_left (fun args syn -> |
| 107 | + if syn = ".mli" then |
| 108 | + args |
| 109 | + else |
| 110 | + "-mli-synonym"::(syn::args)) args !mli_synonyms in |
| 111 | + let args = if !slash then "-slash"::args else args in |
| 112 | + let args = if !native_only then "-native"::args else args in |
| 113 | + let args = if !raw_dependencies then "-modules"::args else args in |
| 114 | + let args = match !pp_command with |
| 115 | + | None -> args |
| 116 | + | Some s -> "-pp"::(s::args) in |
| 117 | + let args = List.fold_left (fun opts ppx -> "-ppx"::(ppx::args)) args !ppx_command in |
| 118 | + let args = if !all_dependencies then "-all"::args else args in |
| 119 | + let args = List.fold_left (fun opts o_mod -> "-open"::(o_mod::opts)) args !open_modules in |
| 120 | + let args = if !one_line then "-one-line"::args else args in |
| 121 | + let args = if !use_ocamlfind then |
| 122 | + let args = if !ocamlfind_package <> "" then |
| 123 | + "-package"::(!ocamlfind_package::args) |
| 124 | + else |
| 125 | + args in |
| 126 | + let args = if !ocamlfind_syntax <> "" then |
| 127 | + "-syntax"::(!ocamlfind_syntax::args) |
| 128 | + else |
| 129 | + args in |
| 130 | + let args = List.fold_left (fun args s -> "-ppopt"::(s::args)) args !ocamlfind_ppopt in |
| 131 | + !ocamlfind::("ocamldep"::args) |
| 132 | + else |
| 133 | + !ocamldep :: args in |
| 134 | + let argv = Array.of_list args in |
| 135 | + let pid = Unix.create_process argv.(0) argv Unix.stdin out_file Unix.stderr in |
| 136 | + let (_,status) = |
| 137 | + Unix.waitpid [] pid in |
| 138 | + let rc = (match status with |
| 139 | + | Unix.WEXITED rc -> rc |
| 140 | + | Unix.WSIGNALED _ |
| 141 | + | Unix.WSTOPPED _ -> -1) in |
| 142 | + error_occured := !error_occured || rc <> 0 in |
| 143 | + let call_menhir file = |
| 144 | + let args = [!menhir;"--depend";"--ocamldep";!ocamldep] in |
| 145 | + let args = if !raw_dependencies then args@["--raw-depend"] else args in |
| 146 | + let argv = Array.of_list args in |
| 147 | + let pid = Unix.create_process argv.(0) argv Unix.stdin out_file Unix.stderr in |
| 148 | + let (_,status) = |
| 149 | + Unix.waitpid [] pid in |
| 150 | + let rc = (match status with |
| 151 | + | Unix.WEXITED rc -> rc |
| 152 | + | Unix.WSIGNALED _ |
| 153 | + | Unix.WSTOPPED _ -> -1) in |
| 154 | + error_occured := !error_occured || rc <> 0 in |
| 155 | + List.iter (fun (f_type,f_name) -> |
| 156 | + match f_type with |
| 157 | + | ML |
| 158 | + | MLI -> call_ocamldep f_name |
| 159 | + | MLL -> if !use_menhir then call_menhir f_name) files; |
| 160 | + if !target_file <> None then Unix.close out_file |
| 161 | + with Unix.Unix_error _ -> |
| 162 | + error_occured := true |
| 163 | + |
| 164 | + |
| 165 | +let _ = |
| 166 | + Arg.parse [ |
| 167 | + "-all", Arg.Set all_dependencies, |
| 168 | + " Generate dependencies on all files"; |
| 169 | + "-dep", Arg.Set_string ocamldep, |
| 170 | + "<cmd> Use <cmd> instead of ocamldep"; |
| 171 | + "-I", Arg.String (add_to_list include_dirs), |
| 172 | + "<dir> Add <dir> to the list of include directories"; |
| 173 | + "-menhir", Arg.String (fun s -> use_menhir:= true; menhir := s), |
| 174 | + "<cmd> Use <cmd> instead of menhir"; |
| 175 | + "-ml-synonym", Arg.String (add_to_synonym_list ml_synonyms), |
| 176 | + "<e> Consider <e> as synonym of the .ml extension"; |
| 177 | + "-mli-synonym", Arg.String (add_to_synonym_list mli_synonyms), |
| 178 | + "<e> Consider <e> as synonym of the .mli extension"; |
| 179 | + "-modules", Arg.Set raw_dependencies, |
| 180 | + " Print module dependencies in raw form"; |
| 181 | + "-native", Arg.Set native_only, |
| 182 | + " Generate dependencies for native-code only"; |
| 183 | + "-o", Arg.String (fun s -> target_file := Some s), |
| 184 | + "<f> Write the dependencies in file <f>"; |
| 185 | + "-ocamlfind", Arg.String (fun s -> use_ocamlfind := true; ocamlfind := s), |
| 186 | + "<cmd> Use <cmd> instead of ocamlfind"; |
| 187 | + "-one-line", Arg.Set one_line, |
| 188 | + " Output only ine line per file, regardless of length"; |
| 189 | + "-open", Arg.String (add_to_list open_modules), |
| 190 | + "<module> Opens the module <module> before typing"; |
| 191 | + "-package", Arg.Set_string ocamlfind_package, |
| 192 | + " <p> Pass the package option <p> to ocamlfind"; |
| 193 | + "-ppopt", Arg.String (add_to_list ocamlfind_ppopt), |
| 194 | + " <p> Pass the camlp4 option <p> to ocamlfind"; |
| 195 | + "-pp", Arg.String (fun s -> pp_command := Some s), |
| 196 | + "<cmd> Pipe sources through preprocessor <cmd>"; |
| 197 | + "-ppx", Arg.String (add_to_list ppx_command), |
| 198 | + "<cmd> Pipe abstract syntax trees through preprocessor <cmd>"; |
| 199 | + "-slash", Arg.Set slash, |
| 200 | + " (Windows) Use foward slash / instead of backslash \\ in file paths"; |
| 201 | + "-syntax", Arg.Set_string ocamlfind_syntax, |
| 202 | + " <p> Pass the syntax option <p> to ocamlfind"; |
| 203 | + "-use-menhir", Arg.Set use_menhir, |
| 204 | + " Use menhir to callculate the dependencies of .mll files"; |
| 205 | + "-use-ocamlfind", Arg.Set use_ocamlfind, |
| 206 | + " Use ocamlfind as driver for ocamldepend";] |
| 207 | + (add_to_list dirs_to_search) usage; |
| 208 | + let files = get_files () in |
| 209 | + compute_dependencies files; |
| 210 | + exit (if !error_occured then 2 else 0) |
0 commit comments