2007-02-07 00:59:16 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* ocamlbuild *)
|
|
|
|
(* *)
|
|
|
|
(* Nicolas Pouillard, Berke Durak, projet Gallium, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 2007 Institut National de Recherche en Informatique et *)
|
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
2009-03-03 08:54:58 -08:00
|
|
|
|
2007-02-07 00:59:16 -08:00
|
|
|
(* Original author: Nicolas Pouillard *)
|
|
|
|
open My_std
|
|
|
|
open Format
|
|
|
|
open Log
|
|
|
|
open Pathname.Operators
|
|
|
|
open Tags.Operators
|
|
|
|
open Tools
|
2007-02-26 08:27:45 -08:00
|
|
|
open Flags
|
2007-02-07 00:59:16 -08:00
|
|
|
open Command;;
|
|
|
|
|
|
|
|
|
|
|
|
module S = Set.Make(String)
|
|
|
|
|
2008-07-25 07:26:13 -07:00
|
|
|
let flag_and_dep tags cmd_spec =
|
|
|
|
flag tags cmd_spec;
|
|
|
|
let ps = Command.fold_pathnames (fun p ps -> p :: ps) (Cmd cmd_spec) [] in
|
|
|
|
dep tags ps
|
|
|
|
|
2007-02-07 00:59:16 -08:00
|
|
|
let stdlib_dir = lazy begin
|
|
|
|
(* FIXME *)
|
2012-02-15 01:10:52 -08:00
|
|
|
let ocamlc_where = sprintf "%s/ocamlc.where" !Options.build_dir in
|
2007-02-07 00:59:16 -08:00
|
|
|
let () = Command.execute ~quiet:true (Cmd(S[!Options.ocamlc; A"-where"; Sh">"; P ocamlc_where])) in
|
|
|
|
String.chomp (read_file ocamlc_where)
|
|
|
|
end
|
|
|
|
|
2010-01-22 06:36:57 -08:00
|
|
|
let pflag_and_dep tags ptag cmd_spec =
|
|
|
|
Param_tags.declare ptag
|
|
|
|
(fun param ->
|
|
|
|
flag_and_dep (Param_tags.make ptag param :: tags) (cmd_spec param))
|
|
|
|
|
2007-02-07 00:59:16 -08:00
|
|
|
let module_name_of_filename f = String.capitalize (Pathname.remove_extensions f)
|
|
|
|
let module_name_of_pathname x =
|
|
|
|
module_name_of_filename (Pathname.to_string (Pathname.basename x))
|
|
|
|
|
|
|
|
let ignore_stdlib x =
|
|
|
|
if !Options.nostdlib then false
|
|
|
|
else
|
|
|
|
let x' = !*stdlib_dir/((String.uncapitalize x)-.-"cmi") in
|
|
|
|
Pathname.exists x'
|
|
|
|
|
|
|
|
let non_dependencies = ref []
|
2007-11-21 10:32:32 -08:00
|
|
|
let non_dependency m1 m2 =
|
|
|
|
(* non_dependency was not supposed to accept pathnames without extension. *)
|
|
|
|
if String.length (Pathname.get_extensions m1) = 0 then
|
|
|
|
invalid_arg "non_dependency: no extension";
|
|
|
|
non_dependencies := (m1, m2) :: !non_dependencies
|
|
|
|
|
|
|
|
let path_importance path x =
|
|
|
|
if List.mem (path, x) !non_dependencies
|
2007-02-08 08:53:39 -08:00
|
|
|
|| (List.mem x !Options.ignore_list) then begin
|
2007-11-21 10:32:32 -08:00
|
|
|
let () = dprintf 3 "This module (%s) is ignored by %s" x path in
|
2007-02-08 08:53:39 -08:00
|
|
|
`ignored
|
|
|
|
end
|
|
|
|
else if ignore_stdlib x then `just_try else `mandatory
|
2007-02-07 00:59:16 -08:00
|
|
|
|
|
|
|
let expand_module include_dirs module_name exts =
|
2007-10-08 07:19:34 -07:00
|
|
|
let dirname = Pathname.dirname module_name in
|
|
|
|
let basename = Pathname.basename module_name in
|
|
|
|
let module_name_cap = dirname/(String.capitalize basename) in
|
|
|
|
let module_name_uncap = dirname/(String.uncapitalize basename) in
|
2007-02-07 00:59:16 -08:00
|
|
|
List.fold_right begin fun include_dir ->
|
|
|
|
List.fold_right begin fun ext acc ->
|
2007-10-08 07:19:34 -07:00
|
|
|
include_dir/(module_name_uncap-.-ext) ::
|
|
|
|
include_dir/(module_name_cap-.-ext) :: acc
|
2007-02-07 00:59:16 -08:00
|
|
|
end exts
|
|
|
|
end include_dirs []
|
|
|
|
|
|
|
|
let string_list_of_file file =
|
|
|
|
with_input_file file begin fun ic ->
|
|
|
|
Lexers.blank_sep_strings (Lexing.from_channel ic)
|
|
|
|
end
|
|
|
|
let print_path_list = Pathname.print_path_list
|
|
|
|
|
|
|
|
let ocaml_ppflags tags =
|
|
|
|
let flags = Flags.of_tags (tags++"ocaml"++"pp") in
|
|
|
|
let reduced = Command.reduce flags in
|
|
|
|
if reduced = N then N else S[A"-pp"; Quote reduced]
|
|
|
|
|
|
|
|
let ocaml_add_include_flag x acc =
|
|
|
|
if x = Pathname.current_dir_name then acc else A"-I" :: A x :: acc
|
|
|
|
|
|
|
|
let ocaml_include_flags path =
|
|
|
|
S (List.fold_right ocaml_add_include_flag (Pathname.include_dirs_of (Pathname.dirname path)) [])
|
|
|
|
|
|
|
|
let info_libraries = Hashtbl.create 103
|
|
|
|
|
|
|
|
let libraries = Hashtbl.create 103
|
|
|
|
let libraries_of m =
|
|
|
|
try Hashtbl.find libraries m with Not_found -> []
|
|
|
|
let use_lib m lib = Hashtbl.replace libraries m (lib :: libraries_of m)
|
|
|
|
|
2007-02-26 08:27:45 -08:00
|
|
|
let ocaml_lib ?(extern=false) ?(byte=true) ?(native=true) ?dir ?tag_name libpath =
|
|
|
|
let add_dir x =
|
|
|
|
match dir with
|
|
|
|
| Some dir -> S[A"-I"; P dir; x]
|
|
|
|
| None -> x
|
|
|
|
in
|
|
|
|
let tag_name =
|
|
|
|
match tag_name with
|
|
|
|
| Some x -> x
|
|
|
|
| None -> "use_" ^ Pathname.basename libpath
|
|
|
|
in
|
2008-07-25 07:26:13 -07:00
|
|
|
let flag_and_dep tags lib =
|
|
|
|
flag tags (add_dir (A lib));
|
2008-07-25 07:49:03 -07:00
|
|
|
if not extern then dep tags [lib] (* cannot happen? *)
|
2008-07-25 07:26:13 -07:00
|
|
|
in
|
2007-02-26 08:27:45 -08:00
|
|
|
Hashtbl.replace info_libraries tag_name (libpath, extern);
|
2007-10-08 07:19:34 -07:00
|
|
|
if extern then begin
|
|
|
|
if byte then
|
2008-07-25 07:26:13 -07:00
|
|
|
flag_and_dep ["ocaml"; tag_name; "link"; "byte"] (libpath^".cma");
|
2007-10-08 07:19:34 -07:00
|
|
|
if native then
|
2008-07-25 07:26:13 -07:00
|
|
|
flag_and_dep ["ocaml"; tag_name; "link"; "native"] (libpath^".cmxa");
|
2007-10-08 07:19:34 -07:00
|
|
|
end else begin
|
|
|
|
if not byte && not native then
|
|
|
|
invalid_arg "ocaml_lib: ~byte:false or ~native:false only works with ~extern:true";
|
|
|
|
end;
|
2007-02-26 08:27:45 -08:00
|
|
|
match dir with
|
|
|
|
| None -> ()
|
2010-01-20 08:26:46 -08:00
|
|
|
| Some dir ->
|
|
|
|
List.iter
|
|
|
|
(fun x -> flag ["ocaml"; tag_name; x] (S[A"-I"; P dir]))
|
|
|
|
["compile"; "doc"; "infer_interface"]
|
2007-02-26 08:27:45 -08:00
|
|
|
|
2007-02-07 00:59:16 -08:00
|
|
|
let cmi_of = Pathname.update_extensions "cmi"
|
2007-11-21 10:32:32 -08:00
|
|
|
|
|
|
|
exception Ocamldep_error of string
|
|
|
|
|
|
|
|
let read_path_dependencies =
|
|
|
|
let path_dependencies = Hashtbl.create 103 in
|
|
|
|
let read path =
|
|
|
|
let module_name = module_name_of_pathname path in
|
|
|
|
let depends = path-.-"depends" in
|
|
|
|
with_input_file depends begin fun ic ->
|
|
|
|
let ocamldep_output =
|
|
|
|
try Lexers.ocamldep_output (Lexing.from_channel ic)
|
|
|
|
with Lexers.Error msg -> raise (Ocamldep_error(Printf.sprintf "Ocamldep.ocamldep: bad output (%s)" msg)) in
|
|
|
|
let deps =
|
|
|
|
List.fold_right begin fun (path, deps) acc ->
|
|
|
|
let module_name' = module_name_of_pathname path in
|
2010-01-22 04:48:24 -08:00
|
|
|
if module_name' = module_name
|
2007-11-21 10:32:32 -08:00
|
|
|
then List.union deps acc
|
|
|
|
else raise (Ocamldep_error(Printf.sprintf "Ocamldep.ocamldep: multiple files in ocamldep output (%s not expected)" path))
|
|
|
|
end ocamldep_output [] in
|
|
|
|
let deps =
|
|
|
|
if !Options.nostdlib && not (Tags.mem "nopervasives" (tags_of_pathname path)) then
|
|
|
|
"Pervasives" :: deps
|
|
|
|
else deps in
|
|
|
|
let deps' = List.fold_right begin fun dep acc ->
|
|
|
|
match path_importance path dep with
|
|
|
|
| `ignored -> acc
|
|
|
|
| (`just_try | `mandatory) as importance -> (importance, dep) :: acc
|
|
|
|
end deps [] in
|
|
|
|
Hashtbl.replace path_dependencies path
|
|
|
|
(List.union (try Hashtbl.find path_dependencies path with Not_found -> []) deps');
|
|
|
|
deps'
|
|
|
|
end
|
|
|
|
in read
|
|
|
|
|
|
|
|
let path_dependencies_of = memo read_path_dependencies
|