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