Refactor xpath parser and ast in a submodule.
[tatoo.git] / myocamlbuild.ml
1 (***********************************************************************)
2 (*                                                                     *)
3 (*                               TAToo                                 *)
4 (*                                                                     *)
5 (*                     Kim Nguyen, LRI UMR8623                         *)
6 (*                   Université Paris-Sud & CNRS                       *)
7 (*                                                                     *)
8 (*  Copyright 2010-2013 Université Paris-Sud and Centre National de la *)
9 (*  Recherche Scientifique. All rights reserved.  This file is         *)
10 (*  distributed under the terms of the GNU Lesser General Public       *)
11 (*  License, with the special exception on linking described in file   *)
12 (*  ../LICENSE.                                                        *)
13 (*                                                                     *)
14 (***********************************************************************)
15
16 (*
17   Time-stamp: <Last modified on 2013-01-30 19:09:57 CET by Kim Nguyen>
18 *)
19
20 open Ocamlbuild_plugin
21 open Command
22 open Myocamlbuild_config
23 open Format
24
25 let print_list l =
26   eprintf "%![%s]%!\n" (String.concat ", " l)
27
28 let _A x = A x
29 let _S ?(extra=N) l = S (List.map (fun e -> (S [extra; _A e] )) l)
30 ;;
31 let cxx_flags_for_ml = [ _S ~extra:(_A "-ccopt") cxx_flags ]
32 let cxx_flags = ref [ _S cxx_flags ]
33 let project_dirs = [ src_path; include_path ]
34 let cxx_include_flags = _S cxx_includes
35 let cxx_link_flags = ref  [ _S cxx_lpaths; _S cxx_libs]
36 let native_link_flags = ref (List.map (fun s -> s ^ ".cmxa") ocaml_link)
37 let byte_link_flags =  ref ("-custom" :: (List.map (fun s -> s ^ ".cma") ocaml_link))
38 let link_flags = [ A"-linkpkg" ]
39 let libs_files = List.map (fun s -> "file:" ^ s) cxx_libs_objects
40
41
42 let native_compile_flags = ref [A"-fno-PIC"]
43 let compile_flags = ref []
44
45 let dwsize = sprintf "-DWORDSIZE%i" Sys.word_size
46
47 (* Utils *)
48 let ( @= ) r l = r := !r @ l
49 let ( =:: ) r e = r := e :: !r
50
51 (* Pre-processed files *)
52 let pp_macro_options = ref
53   [ A dwsize; A "-I"; P include_path ]
54
55 let include_full_path =  Pathname.pwd / include_path
56 module Depends =
57 struct
58 open Scanf
59 let scan_include ml =
60   let ic = open_in ml and includes = ref [] in
61   begin
62     try
63       while true do
64         let s = input_line ic in
65         if String.length s > 0 then
66           try
67             sscanf s " INCLUDE \"%s@\"" (fun s -> includes =:: include_path /s)
68           with Scan_failure _ -> ()
69       done
70     with End_of_file -> close_in ic
71   end;
72   !includes
73
74 let ocaml ml =
75   let rec loop file =
76     let includes = scan_include file in
77     List.fold_left (fun a i -> (loop i) @ a) includes includes
78   in
79   let includes = loop ml in
80     dep [ "file:" ^ ml ] includes
81
82 let parse_depends depfile =
83   let ichan = open_in depfile in
84   let iscan = Scanning.from_channel ichan in
85   let includes = ref [] in
86   bscanf iscan " %_s@: %s " ignore;
87   try
88     while true do
89       bscanf iscan " %s " (
90         function "" -> raise End_of_file
91           | "\\" -> ()
92           | s ->  includes =::s)
93     done; []
94   with
95     _ -> close_in ichan;!includes
96
97 let cxx cpp =
98   let depfile = !Options.build_dir /" __cxx_depends.tmp" 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 depfile in
104   let includes' = (List.filter (Pathname.is_relative) includes) in
105   dep [ "compile"; "file:" ^ cpp ] includes'
106 end
107
108 let cxx_compile env _build =
109   let cpp = env "%.cpp" and obj = env "%.o" in
110   let tags = (tags_of_pathname cpp) ++ "compile" ++ "c++" in
111   Cmd(S[T tags; A cxx_cmd; A "-o" ; P obj; A "-c";  S !cxx_flags; cxx_include_flags; P cpp])
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; A "-syntax"; A "camlp4o" ]
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 not (List.mem "log" !Options.tags) then begin
130         pp_macro_options @= [ A "-DNLOG" ];
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 "c++: cpp & depends -> o" ~prod:"%.o" ~deps:[ "%.cpp" ] cxx_compile;
165       let syntax_flags = S[  S (ppopt !pp_macro_options) ]
166       in
167       flag [ "ocaml"; "ocamldep"] syntax_flags;
168       flag [ "ocaml"; "compile" ] (S[ A "-cc"; A cxx_cmd; S cxx_flags_for_ml ;  syntax_flags; S !compile_flags ]);
169       flag [ "ocaml"; "native"; "compile" ] (S !native_compile_flags);
170       flag [ "ocaml"; "link" ]
171         (S [ S link_flags ; A "-cc"; A cxx_cmd; S cxx_flags_for_ml; A "-cclib" ;
172              Quote (S [ _S cstub_lib;  S !cxx_link_flags]) ]);
173       flag [ "ocaml"; "byte"; "link" ] (_S !byte_link_flags);
174       flag [ "ocaml"; "native"; "link" ] (_S !native_link_flags);
175       flag [ "c"; "ocamlmklib"]  (A "-custom")
176     | _ -> ()
177 end