Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/ocamldep.ml b/ocamldep.ml
- index 3146027..507f09b 100644
- --- a/ocamldep.ml
- +++ b/ocamldep.ml
- @@ -27,6 +27,8 @@ let native_only = ref false
- let force_slash = ref false
- let error_occurred = ref false
- let raw_dependencies = ref false
- +let full_dependencies = ref false
- +let one_line = ref false
- (* Fix path to use '/' as directory separator instead of '\'.
- Only under Windows. *)
- @@ -57,6 +59,7 @@ let add_to_synonym_list synonyms suffix =
- error_occurred := true
- end
- +(* Find file 'name' (capitalized) in search path *)
- let find_file name =
- let uname = String.uncapitalize name in
- let rec find_in_array a pos =
- @@ -77,24 +80,61 @@ let rec find_file_in_list = function
- [] -> raise Not_found
- | x :: rem -> try find_file x with Not_found -> find_file_in_list rem
- -let find_dependency modname (byt_deps, opt_deps) =
- +(* Convert dependency on module 'modname' into dependency on compilation artefacts.
- + First, we try looking for modname.mli.
- + If it is found, we depend on compiled interface.
- + If not, we look for .ml and depend on compiled interface + compiled object file(s)
- +*)
- +type file_kind = ML | MLI;;
- +
- +let find_dependency target_kind modname (byt_deps, opt_deps) =
- try
- let candidates = List.map ((^) modname) !mli_synonyms in
- let filename = find_file_in_list candidates in
- let basename = Filename.chop_extension filename in
- - let optname =
- - if List.exists (fun ext -> Sys.file_exists (basename ^ ext)) !ml_synonyms
- - then basename ^ ".cmx"
- - else basename ^ ".cmi" in
- - ((basename ^ ".cmi") :: byt_deps, optname :: opt_deps)
- + let new_byt_dep = basename ^ ".cmi" (* no cross-module inlining, so just .cmi
- + regardless of compilation mode *)
- + in
- + let ml_exists = List.exists (fun ext -> Sys.file_exists (basename ^ ext)) !ml_synonyms in
- + let new_opt_dep =
- + if !full_dependencies then
- + match target_kind with
- + | MLI -> [basename ^ ".cmi"]
- + (* in native mode, depend interface + implementation for cross-module inlining *)
- + | ML -> if ml_exists
- + then [basename ^ ".cmi"; basename ^ ".cmx"; basename ^ ".o"]
- + else [basename ^ ".cmi"]
- + else
- + (* this is a make-specific hack that makes .cmx to be a 'proxy' target that would force
- + the dependency on .cmi via transitivity *)
- + if ml_exists
- + then [ basename ^ ".cmx" ]
- + else [ basename ^ ".cmi" ]
- + in
- + ( new_byt_dep :: byt_deps, new_opt_dep @ opt_deps)
- with Not_found ->
- try
- + (* "just .ml" case *)
- let candidates = List.map ((^) modname) !ml_synonyms in
- let filename = find_file_in_list candidates in
- let basename = Filename.chop_extension filename in
- let bytename =
- - basename ^ (if !native_only then ".cmx" else ".cmo") in
- - (bytename :: byt_deps, (basename ^ ".cmx") :: opt_deps)
- + if !full_dependencies then
- + match target_kind with
- + | MLI -> basename ^ ".cmi"
- + | ML -> basename ^ ".cmo"
- + else
- + (* again, make-specific hack *)
- + basename ^ (if !native_only then ".cmx" else ".cmo") in
- + let optnames =
- + if !full_dependencies
- + then match target_kind with
- + | MLI -> [basename ^ ".cmi"]
- + (* in native mode, depend interface + implementation for cross-module inlining *)
- + | ML -> [basename ^ ".cmi"; basename ^ ".cmx"; basename ^ ".o"]
- + else [ basename ^ ".cmx" ]
- + in
- + (bytename :: byt_deps, optnames @ opt_deps)
- with Not_found ->
- (byt_deps, opt_deps)
- @@ -128,22 +168,21 @@ let print_filename s =
- end
- ;;
- -let print_dependencies target_file deps =
- - print_filename target_file; print_string depends_on;
- +let print_dependencies target_files deps =
- let rec print_items pos = function
- [] -> print_string "\n"
- | dep :: rem ->
- - if pos + 1 + String.length dep <= 77 then begin
- - print_string " "; print_filename dep;
- + if !one_line || (pos + 1 + String.length dep <= 77) then begin
- + if pos <> 0 then print_string " "; print_filename dep;
- print_items (pos + String.length dep + 1) rem
- end else begin
- print_string escaped_eol; print_filename dep;
- print_items (String.length dep + 4) rem
- end in
- - print_items (String.length target_file + 1) deps
- + print_items 0 (target_files @ [depends_on] @ deps)
- let print_raw_dependencies source_file deps =
- - print_filename source_file; print_string ":";
- + print_filename source_file; print_string depends_on;
- Depend.StringSet.iter
- (fun dep ->
- if (String.length dep > 0)
- @@ -223,15 +262,29 @@ let ml_file_dependencies source_file =
- print_raw_dependencies source_file !Depend.free_structure_names
- end else begin
- let basename = Filename.chop_extension source_file in
- - let init_deps =
- + let byte_targets =
- + if !native_only then [] else [ basename ^ ".cmo" ] in
- + let native_targets =
- + if !full_dependencies
- + then [ basename ^ ".cmx"; basename ^ ".o" ]
- + else [ basename ^ ".cmx" ] in
- + (*
- + If we have an .mli, then .ml is used to compile just the module object file:
- + foo.cmi + foo.ml -> foo.cmo or foo.cmx+foo.o
- +
- + Otherwise, foo.ml produces foo.cmi + object file(s)
- + *)
- + let init_deps = if !full_dependencies then [source_file] else [] in
- + let cmi_name = basename ^ ".cmi" in
- + let init_deps, extra_targets =
- if List.exists (fun ext -> Sys.file_exists (basename ^ ext)) !mli_synonyms
- - then let cmi_name = basename ^ ".cmi" in ([cmi_name], [cmi_name])
- - else ([], []) in
- - let (byt_deps, opt_deps) =
- - Depend.StringSet.fold find_dependency
- + then (cmi_name :: init_deps, cmi_name :: init_deps), []
- + else (init_deps, init_deps), ( if !full_dependencies then [cmi_name] else [] ) in
- + let (byt_deps, native_deps) =
- + Depend.StringSet.fold (find_dependency ML)
- !Depend.free_structure_names init_deps in
- - print_dependencies (basename ^ ".cmo") byt_deps;
- - print_dependencies (basename ^ ".cmx") opt_deps
- + if not !native_only then print_dependencies (byte_targets @ extra_targets) byt_deps;
- + print_dependencies (native_targets @ extra_targets) native_deps;
- end;
- close_in ic; remove_preprocessed input_file
- with x ->
- @@ -248,16 +301,17 @@ let mli_file_dependencies source_file =
- print_raw_dependencies source_file !Depend.free_structure_names
- end else begin
- let basename = Filename.chop_extension source_file in
- + let init_deps = if !full_dependencies then ([source_file],[source_file]) else ([],[]) in
- let (byt_deps, opt_deps) =
- - Depend.StringSet.fold find_dependency
- - !Depend.free_structure_names ([], []) in
- - print_dependencies (basename ^ ".cmi") byt_deps
- + Depend.StringSet.fold (find_dependency MLI)
- + !Depend.free_structure_names init_deps in
- + print_dependencies [basename ^ ".cmi"] byt_deps
- end;
- close_in ic; remove_preprocessed input_file
- with x ->
- close_in ic; remove_preprocessed input_file; raise x
- -type file_kind = ML | MLI;;
- +(* Dispatch on file type, print dependencies, handle and report errors *)
- let file_dependencies_as kind source_file =
- Location.input_name := source_file;
- @@ -327,6 +381,10 @@ let _ =
- " Print module dependencies in raw form (not suitable for make)";
- "-native", Arg.Set native_only,
- " Generate dependencies for a pure native-code project (no .cmo files)";
- + "-full", Arg.Set full_dependencies,
- + " Generate full set of dependency rules (not accommodating for make shortcomings)";
- + "-one-line", Arg.Set one_line,
- + " Output one line per file, regardless of the length";
- "-pp", Arg.String(fun s -> preprocessor := Some s),
- "<cmd> Pipe sources through preprocessor <cmd>";
- "-slash", Arg.Set force_slash,
Add Comment
Please, Sign In to add comment