Fix: make .cpp files *really* depend on the local .hpp file they include.
[SXSI/xpathcomp.git] / myocamlbuild.ml
1 open Ocamlbuild_plugin
2 open Command
3 open Myocamlbuild_config
4 open Format
5
6 let print_list l =
7   eprintf "%![%s]%!\n" (String.concat ", " l)
8
9 (*let cxx_flags = S (List.map ( fun x -> A x) cxx_flags)*)
10 let _A x = A x
11 let _S ?(extra=N) l = S (List.map (fun e -> (S [extra; _A e] )) l)
12 ;;
13
14 let cxx_flags = ref [ _S cxx_flags ]
15
16 let project_dirs = [ src_path; include_path ]
17 let cxx_include_flags = _S cxx_includes
18 let cxx_link_flags = ref  [ _S cxx_lpaths; _S cxx_libs]
19 let native_link_flags = ref (List.map (fun s -> s ^ ".cmxa") ocaml_link)
20 let byte_link_flags =  ref ("-custom" :: (List.map (fun s -> s ^ ".cma") ocaml_link))
21 let link_flags = [ A"-linkpkg" ]
22
23 let native_compile_flags = ref [A"-fno-PIC"]
24 let compile_flags = ref []
25
26 let dwsize = sprintf "-DWORDSIZE%i" Sys.word_size
27
28 (* Utils *)
29 let ( @= ) r l = r := !r @ l
30 let ( =:: ) r e = r := e :: !r
31
32 (* Pre-processed files *)
33 let pp_macro_options = ref
34   [ A "-parser"; A "macro"; A dwsize; A "-I"; P include_path ]
35
36 let include_full_path =  Pathname.pwd / include_path
37 module Depends =
38 struct
39 open Scanf
40 let scan_include ml =
41   let ic = try open_in ml with
42       _ -> open_in (include_full_path / ml)
43   in
44   let includes = ref [] in
45   let () =
46     try
47       while true do
48         let s = input_line ic in
49         if String.length s > 0 then
50           try sscanf s " INCLUDE \"%s@\"" ((=::) includes)
51           with Scan_failure _ -> ()
52       done
53     with End_of_file -> close_in ic
54   in
55   !includes
56
57 let ocaml ml =
58   let rec loop file =
59     let includes = scan_include file in
60     List.fold_left (fun a i -> (loop i) @ a) includes includes
61   in
62   let includes = loop ml in
63     dep [ "file:" ^ ml ]
64       (List.map (fun s ->  include_path / s) includes)
65
66 let parse_depends file depfile =
67   let ichan = open_in depfile in
68   let iscan = Scanning.from_channel ichan in
69   let includes = ref [] in
70   begin
71     bscanf iscan " %s@: " ignore;
72     bscanf iscan " %s " ignore;
73     try
74       while true do
75         try
76           let s = bscanf iscan " %s " (fun s -> s) in
77           if s = "" then raise End_of_file;
78           if s <> "\\" then includes =::s
79         with
80             Scan_failure _ -> ()
81       done
82     with
83         End_of_file -> close_in ichan
84   end;
85   !includes
86
87 let uniq l =
88   let rec loop l acc =
89     match l with
90       | []  -> acc
91       | [ e ] -> e::acc
92       | e1 :: ((e2 :: _) as ll) ->
93         loop ll (if e1 = e2 then acc else e1 :: acc)
94   in
95   loop (List.sort compare l) []
96
97 let cxx cpp =
98   let depfile = ( cpp ^ ".depends") in
99   let cmd = Cmd (S[ A cxx_cmd ; S !cxx_flags; cxx_include_flags ; A"-MM";
100                     A "-MF";  P depfile; P cpp])
101   in
102   let () = Command.execute ~quiet:true ~pretend:false cmd in
103   let includes = parse_depends cpp depfile in
104   let includes' = uniq (List.filter (Pathname.is_relative) includes) in
105   dep [ "compile"; "file:" ^ cpp ] includes'
106 end
107
108 let cxx_compile env build =
109   let src = env "%.cpp" and obj = env "%.o" in
110   let tags = (tags_of_pathname src) ++ "compile" ++ "c++" in
111   Cmd(S[T tags; A cxx_cmd; A "-o" ; P obj; A "-c";  S !cxx_flags; cxx_include_flags; P src])
112
113 (* Native compile and link action *)
114
115 let ocamlfind x = S[ T (Tags.singleton "ocamlfind"); A"ocamlfind"; x ; A "-package"; A ocamlfind_packages ]
116
117 let ppopt l = List.map (fun e -> S[ A"-ppopt"; e ]) l
118
119 let () = dispatch begin
120   function
121     | Before_rules ->
122
123       Options.ocamlc := ocamlfind  (A"ocamlc");
124       Options.ocamlopt := ocamlfind (A"ocamlopt");
125       Options.ocamldep := ocamlfind (A"ocamldep");
126       Options.ocamldoc := ocamlfind (A"ocamldoc");
127       Options.ocamlmktop := ocamlfind (A"ocamlmktop");
128
129       if (List.mem "trace" !Options.tags) then begin
130         pp_macro_options @= [ A "-DTRACE" ];
131       end;
132       if (List.mem "profile" !Options.tags) then begin
133         pp_macro_options @= [ A "-DPROFILE" ];
134         native_compile_flags @= [A "-p" ];
135         native_link_flags @= [ "-p" ];
136         cxx_flags @= [ A "-pg" ];
137         cxx_link_flags @= [ A "-pg" ];
138       end;
139
140       if (List.mem "debug" !Options.tags) then begin
141         pp_macro_options @= [ A "-DDEBUG" ];
142         cxx_flags @= [ A "-O0"; A "-g" ];
143         cxx_link_flags @= [ A "-g" ];
144       end
145       else begin
146         compile_flags @= [A "-noassert"];
147         pp_macro_options @= [ A "-unsafe" ];
148         native_compile_flags @= [ A "-inline"; A ocaml_inline ];
149         cxx_flags @= [ A "-O3" ]
150       end;
151
152       let dir_path = Pathname.pwd / src_path in
153       let dir = Pathname.readdir dir_path in
154
155       Array.iter (fun entry ->
156         if Pathname.check_extension entry "ml" then
157           Depends.ocaml (src_path / entry)
158         else if Pathname.check_extension entry "cpp" then
159           Depends.cxx (src_path / entry)
160       ) dir;
161
162     | After_rules ->
163       dep [ "link" ] cstub_lib;
164       rule "compile cpp -> o" ~prod:"%.o" ~deps:[ "%.cpp" ] cxx_compile;
165
166       let syntax_flags = S ([ A "-syntax"; A "camlp4o";
167                             S (ppopt [A "-printer" ; A"Camlp4OCamlAstDumper"]);
168                             S (ppopt !pp_macro_options) ])
169       in
170       flag [ "ocaml"; "ocamldep"] syntax_flags;
171       flag [ "ocaml"; "compile" ] (S[ A "-cc"; A cxx_cmd; syntax_flags; S !compile_flags ]);
172       flag [ "ocaml"; "native"; "compile" ] (S !native_compile_flags);
173       flag [ "ocaml"; "link" ]
174         (S [ S link_flags ; A "-cc"; A cxx_cmd; A "-cclib" ;
175              Quote (S [ _S cstub_lib;  S !cxx_link_flags]) ]);
176       flag [ "ocaml"; "byte"; "link" ] (_S !byte_link_flags);
177       flag [ "ocaml"; "native"; "link" ] (_S !native_link_flags);
178       flag [ "c"; "ocamlmklib"] (S[ A "-custom"; ])
179     | _ -> ()
180 end