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