1999-01-27 02:53:54 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Objective Caml *)
|
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 1999 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1999-01-27 02:53:54 -08:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
open Format
|
1999-01-27 02:53:54 -08:00
|
|
|
open Location
|
|
|
|
open Longident
|
|
|
|
open Parsetree
|
|
|
|
|
|
|
|
|
|
|
|
(* Print the dependencies *)
|
|
|
|
|
2003-03-03 09:20:39 -08:00
|
|
|
let load_path = ref ([] : (string * string array) list)
|
2000-02-07 05:08:12 -08:00
|
|
|
let native_only = ref false
|
2002-06-07 06:30:00 -07:00
|
|
|
let force_slash = ref false
|
2003-03-03 09:20:39 -08:00
|
|
|
let error_occurred = ref false
|
|
|
|
|
|
|
|
let add_to_load_path dir =
|
|
|
|
try
|
2003-04-06 05:41:54 -07:00
|
|
|
let dir = Misc.expand_directory Config.standard_library dir in
|
2003-03-03 09:20:39 -08:00
|
|
|
let contents = Sys.readdir dir in
|
|
|
|
load_path := !load_path @ [dir, contents]
|
|
|
|
with Sys_error msg ->
|
|
|
|
fprintf Format.err_formatter "@[Bad -I option: %s@]@." msg;
|
|
|
|
error_occurred := true
|
|
|
|
|
|
|
|
let concat_filename dirname filename =
|
|
|
|
if dirname = Filename.current_dir_name then filename
|
|
|
|
else if !force_slash then dirname ^ "/" ^ filename
|
|
|
|
else Filename.concat dirname filename
|
2002-06-07 06:30:00 -07:00
|
|
|
|
|
|
|
let find_file name =
|
2003-03-03 09:20:39 -08:00
|
|
|
let uname = String.uncapitalize name in
|
|
|
|
let rec find_in_array a pos =
|
|
|
|
if pos >= Array.length a then None else begin
|
|
|
|
let s = a.(pos) in
|
|
|
|
if s = name || s = uname then Some s else find_in_array a (pos + 1)
|
|
|
|
end in
|
|
|
|
let rec find_in_path = function
|
|
|
|
[] -> raise Not_found
|
|
|
|
| (dir, contents) :: rem ->
|
|
|
|
match find_in_array contents 0 with
|
|
|
|
Some truename -> concat_filename dir truename
|
|
|
|
| None -> find_in_path rem in
|
|
|
|
find_in_path !load_path
|
2000-02-07 05:08:12 -08:00
|
|
|
|
1999-01-27 02:53:54 -08:00
|
|
|
let find_dependency modname (byt_deps, opt_deps) =
|
|
|
|
try
|
2002-06-07 06:30:00 -07:00
|
|
|
let filename = find_file (modname ^ ".mli") in
|
1999-01-27 02:53:54 -08:00
|
|
|
let basename = Filename.chop_suffix filename ".mli" in
|
2003-03-03 09:20:39 -08:00
|
|
|
let optname =
|
|
|
|
if Sys.file_exists (basename ^ ".ml")
|
1999-01-27 02:53:54 -08:00
|
|
|
then basename ^ ".cmx"
|
2003-03-03 09:20:39 -08:00
|
|
|
else basename ^ ".cmi" in
|
|
|
|
((basename ^ ".cmi") :: byt_deps, optname :: opt_deps)
|
1999-01-27 02:53:54 -08:00
|
|
|
with Not_found ->
|
|
|
|
try
|
2002-06-07 06:30:00 -07:00
|
|
|
let filename = find_file (modname ^ ".ml") in
|
1999-01-27 02:53:54 -08:00
|
|
|
let basename = Filename.chop_suffix filename ".ml" in
|
2003-03-03 09:20:39 -08:00
|
|
|
let bytename =
|
|
|
|
basename ^ (if !native_only then ".cmx" else ".cmo") in
|
|
|
|
(bytename :: byt_deps, (basename ^ ".cmx") :: opt_deps)
|
1999-01-27 02:53:54 -08:00
|
|
|
with Not_found ->
|
|
|
|
(byt_deps, opt_deps)
|
|
|
|
|
2004-01-16 07:24:03 -08:00
|
|
|
let (depends_on, escaped_eol) = (": ", "\\\n ")
|
1999-01-27 02:53:54 -08:00
|
|
|
|
2005-03-24 09:20:54 -08:00
|
|
|
let print_filename s =
|
|
|
|
if not (String.contains s ' ') then begin
|
|
|
|
print_string s;
|
|
|
|
end else begin
|
|
|
|
let rec count n i =
|
|
|
|
if i >= String.length s then n
|
|
|
|
else if s.[i] = ' ' then count (n+1) (i+1)
|
|
|
|
else count n (i+1)
|
|
|
|
in
|
|
|
|
let spaces = count 0 0 in
|
|
|
|
let result = String.create (String.length s + spaces) in
|
|
|
|
let rec loop i j =
|
|
|
|
if i >= String.length s then ()
|
|
|
|
else if s.[i] = ' ' then begin
|
|
|
|
result.[j] <- '\\';
|
|
|
|
result.[j+1] <- ' ';
|
|
|
|
loop (i+1) (j+2);
|
|
|
|
end else begin
|
|
|
|
result.[j] <- s.[i];
|
|
|
|
loop (i+1) (j+1);
|
|
|
|
end
|
|
|
|
in
|
|
|
|
loop 0 0;
|
|
|
|
print_string result;
|
|
|
|
end
|
|
|
|
;;
|
|
|
|
|
1999-01-27 02:53:54 -08:00
|
|
|
let print_dependencies target_file deps =
|
|
|
|
match deps with
|
|
|
|
[] -> ()
|
|
|
|
| _ ->
|
2005-03-24 09:20:54 -08:00
|
|
|
print_filename target_file; print_string depends_on;
|
1999-01-27 02:53:54 -08:00
|
|
|
let rec print_items pos = function
|
|
|
|
[] -> print_string "\n"
|
|
|
|
| dep :: rem ->
|
|
|
|
if pos + String.length dep <= 77 then begin
|
2005-03-24 09:20:54 -08:00
|
|
|
print_filename dep; print_string " ";
|
1999-01-27 02:53:54 -08:00
|
|
|
print_items (pos + String.length dep + 1) rem
|
|
|
|
end else begin
|
2005-03-24 09:20:54 -08:00
|
|
|
print_string escaped_eol; print_filename dep; print_string " ";
|
1999-01-27 02:53:54 -08:00
|
|
|
print_items (String.length dep + 5) rem
|
|
|
|
end in
|
|
|
|
print_items (String.length target_file + 2) deps
|
|
|
|
|
2001-08-04 08:37:39 -07:00
|
|
|
(* Optionally preprocess a source file *)
|
|
|
|
|
|
|
|
let preprocessor = ref None
|
|
|
|
|
|
|
|
let preprocess sourcefile =
|
|
|
|
match !preprocessor with
|
|
|
|
None -> sourcefile
|
|
|
|
| Some pp ->
|
|
|
|
flush stdout;
|
|
|
|
let tmpfile = Filename.temp_file "camlpp" "" in
|
|
|
|
let comm = Printf.sprintf "%s %s > %s" pp sourcefile tmpfile in
|
|
|
|
if Sys.command comm <> 0 then begin
|
|
|
|
Misc.remove_file tmpfile;
|
|
|
|
Printf.eprintf "Preprocessing error\n";
|
|
|
|
exit 2
|
|
|
|
end;
|
|
|
|
tmpfile
|
|
|
|
|
|
|
|
let remove_preprocessed inputfile =
|
|
|
|
match !preprocessor with
|
|
|
|
None -> ()
|
|
|
|
| Some _ -> Misc.remove_file inputfile
|
|
|
|
|
|
|
|
(* Parse a file or get a dumped syntax tree in it *)
|
|
|
|
|
|
|
|
exception Outdated_version
|
|
|
|
|
|
|
|
let is_ast_file ic ast_magic =
|
|
|
|
try
|
|
|
|
let buffer = String.create (String.length ast_magic) in
|
|
|
|
really_input ic buffer 0 (String.length ast_magic);
|
|
|
|
if buffer = ast_magic then true
|
|
|
|
else if String.sub buffer 0 9 = String.sub ast_magic 0 9 then
|
|
|
|
raise Outdated_version
|
|
|
|
else false
|
|
|
|
with
|
|
|
|
Outdated_version ->
|
|
|
|
failwith "Ocaml and preprocessor have incompatible versions"
|
|
|
|
| _ -> false
|
|
|
|
|
|
|
|
let parse_use_file ic =
|
|
|
|
if is_ast_file ic Config.ast_impl_magic_number then
|
2004-11-30 10:57:04 -08:00
|
|
|
let _source_file = input_value ic in
|
2001-08-04 08:37:39 -07:00
|
|
|
[Ptop_def (input_value ic : Parsetree.structure)]
|
|
|
|
else begin
|
|
|
|
seek_in ic 0;
|
|
|
|
let lb = Lexing.from_channel ic in
|
|
|
|
Parse.use_file lb
|
|
|
|
end
|
|
|
|
|
|
|
|
let parse_interface ic =
|
|
|
|
if is_ast_file ic Config.ast_intf_magic_number then
|
2004-11-30 10:57:04 -08:00
|
|
|
let _source_file = input_value ic in
|
2001-08-04 08:37:39 -07:00
|
|
|
(input_value ic : Parsetree.signature)
|
|
|
|
else begin
|
|
|
|
seek_in ic 0;
|
|
|
|
let lb = Lexing.from_channel ic in
|
|
|
|
Parse.interface lb
|
|
|
|
end
|
|
|
|
|
1999-03-11 01:56:15 -08:00
|
|
|
(* Process one file *)
|
|
|
|
|
1999-01-27 02:53:54 -08:00
|
|
|
let file_dependencies source_file =
|
|
|
|
Location.input_name := source_file;
|
1999-03-11 01:56:15 -08:00
|
|
|
if Sys.file_exists source_file then begin
|
|
|
|
try
|
2002-03-18 07:08:53 -08:00
|
|
|
Depend.free_structure_names := Depend.StringSet.empty;
|
2001-08-04 08:37:39 -07:00
|
|
|
let input_file = preprocess source_file in
|
|
|
|
let ic = open_in_bin input_file in
|
1999-03-11 01:56:15 -08:00
|
|
|
try
|
|
|
|
if Filename.check_suffix source_file ".ml" then begin
|
2001-08-04 08:37:39 -07:00
|
|
|
let ast = parse_use_file ic in
|
2002-03-18 07:08:53 -08:00
|
|
|
Depend.add_use_file Depend.StringSet.empty ast;
|
1999-03-11 01:56:15 -08:00
|
|
|
let basename = Filename.chop_suffix source_file ".ml" in
|
|
|
|
let init_deps =
|
|
|
|
if Sys.file_exists (basename ^ ".mli")
|
|
|
|
then let cmi_name = basename ^ ".cmi" in ([cmi_name], [cmi_name])
|
|
|
|
else ([], []) in
|
|
|
|
let (byt_deps, opt_deps) =
|
2002-03-18 07:08:53 -08:00
|
|
|
Depend.StringSet.fold find_dependency !Depend.free_structure_names init_deps in
|
1999-03-11 01:56:15 -08:00
|
|
|
print_dependencies (basename ^ ".cmo") byt_deps;
|
|
|
|
print_dependencies (basename ^ ".cmx") opt_deps
|
|
|
|
end else
|
|
|
|
if Filename.check_suffix source_file ".mli" then begin
|
2001-08-04 08:37:39 -07:00
|
|
|
let ast = parse_interface ic in
|
2002-03-18 07:08:53 -08:00
|
|
|
Depend.add_signature Depend.StringSet.empty ast;
|
1999-03-11 01:56:15 -08:00
|
|
|
let basename = Filename.chop_suffix source_file ".mli" in
|
|
|
|
let (byt_deps, opt_deps) =
|
2002-03-18 07:08:53 -08:00
|
|
|
Depend.StringSet.fold find_dependency !Depend.free_structure_names ([], []) in
|
1999-03-11 01:56:15 -08:00
|
|
|
print_dependencies (basename ^ ".cmi") byt_deps
|
|
|
|
end else
|
|
|
|
();
|
2001-08-04 08:37:39 -07:00
|
|
|
close_in ic; remove_preprocessed input_file
|
1999-03-11 01:56:15 -08:00
|
|
|
with x ->
|
2001-08-04 08:37:39 -07:00
|
|
|
close_in ic; remove_preprocessed input_file;
|
|
|
|
raise x
|
1999-03-11 01:56:15 -08:00
|
|
|
with x ->
|
2000-03-06 14:12:09 -08:00
|
|
|
let report_err = function
|
2002-11-01 09:06:47 -08:00
|
|
|
| Lexer.Error(err, range) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf Format.err_formatter "@[%a%a@]@."
|
2002-11-01 09:06:47 -08:00
|
|
|
Location.print range Lexer.report_error err
|
1999-03-11 01:56:15 -08:00
|
|
|
| Syntaxerr.Error err ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf Format.err_formatter "@[%a@]@."
|
1999-03-11 01:56:15 -08:00
|
|
|
Syntaxerr.report_error err
|
|
|
|
| Sys_error msg ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf Format.err_formatter "@[I/O error:@ %s@]@." msg
|
|
|
|
| x -> raise x in
|
|
|
|
error_occurred := true;
|
|
|
|
report_err x
|
1999-03-11 01:56:15 -08:00
|
|
|
end
|
1999-01-27 02:53:54 -08:00
|
|
|
|
|
|
|
(* Entry point *)
|
|
|
|
|
2000-02-07 05:08:12 -08:00
|
|
|
let usage = "Usage: ocamldep [-I <dir>] [-native] <files>"
|
1999-01-27 02:53:54 -08:00
|
|
|
|
2004-11-26 17:04:19 -08:00
|
|
|
let print_version () =
|
|
|
|
printf "ocamldep, version %s@." Sys.ocaml_version;
|
|
|
|
exit 0;
|
|
|
|
;;
|
|
|
|
|
1999-01-27 02:53:54 -08:00
|
|
|
let _ =
|
1999-12-13 01:48:38 -08:00
|
|
|
Clflags.classic := false;
|
2003-03-03 09:20:39 -08:00
|
|
|
add_to_load_path Filename.current_dir_name;
|
1999-03-11 01:56:15 -08:00
|
|
|
Arg.parse [
|
2003-03-03 09:20:39 -08:00
|
|
|
"-I", Arg.String add_to_load_path,
|
2000-02-07 05:08:12 -08:00
|
|
|
"<dir> Add <dir> to the list of include directories";
|
|
|
|
"-native", Arg.Set native_only,
|
2001-08-04 08:37:39 -07:00
|
|
|
" Generate dependencies for a pure native-code project \
|
|
|
|
(no .cmo files)";
|
2004-11-26 17:04:19 -08:00
|
|
|
"-pp", Arg.String(fun s -> preprocessor := Some s),
|
|
|
|
"<command> Pipe sources through preprocessor <command>";
|
2002-06-07 06:30:00 -07:00
|
|
|
"-slash", Arg.Set force_slash,
|
|
|
|
" (for Windows) Use forward slash / instead of backslash \\ in file paths";
|
2004-11-26 17:04:19 -08:00
|
|
|
"-version", Arg.Unit print_version,
|
|
|
|
" Print version and exit";
|
1999-03-11 01:56:15 -08:00
|
|
|
] file_dependencies usage;
|
|
|
|
exit (if !error_occurred then 2 else 0)
|