Usable version:
[tatoo.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 let libs_files = List.map (fun s -> "file:" ^ s) cxx_libs_objects
21
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 = open_in ml and includes = ref [] in
42   begin
43     try
44       while true do
45         let s = input_line ic in
46         if String.length s > 0 then
47           try
48             sscanf s " INCLUDE \"%s@\"" (fun s -> includes =:: include_path /s)
49           with Scan_failure _ -> ()
50       done
51     with End_of_file -> close_in ic
52   end;
53   !includes
54
55 let ocaml ml =
56   let rec loop file =
57     let includes = scan_include file in
58     List.fold_left (fun a i -> (loop i) @ a) includes includes
59   in
60   let includes = loop ml in
61     dep [ "file:" ^ ml ] includes
62
63 let parse_depends depfile =
64   let ichan = open_in depfile in
65   let iscan = Scanning.from_channel ichan in
66   let includes = ref [] in
67   bscanf iscan " %_s@: %s " ignore;
68   try
69     while true do
70       bscanf iscan " %s " (
71         function "" -> raise End_of_file
72           | "\\" -> ()
73           | s ->  includes =::s)
74     done; []
75   with
76     _ -> close_in ichan;!includes
77
78 let cxx cpp =
79   let depfile = !Options.build_dir /" __cxx_depends.tmp" in
80   let cmd = Cmd (S[ A cxx_cmd ; S !cxx_flags; cxx_include_flags ; A"-MM";
81                     A "-MF";  P depfile; P cpp])
82   in
83   let () = Command.execute ~quiet:true ~pretend:false cmd in
84   let includes = parse_depends depfile in
85   let includes' = (List.filter (Pathname.is_relative) includes) in
86   dep [ "compile"; "file:" ^ cpp ] includes'
87 end
88
89 let cxx_compile env _build =
90   let cpp = env "%.cpp" and obj = env "%.o" in
91   let tags = (tags_of_pathname cpp) ++ "compile" ++ "c++" in
92   Cmd(S[T tags; A cxx_cmd; A "-o" ; P obj; A "-c";  S !cxx_flags; cxx_include_flags; P cpp])
93
94 (* Native compile and link action *)
95
96 let ocamlfind x = S[ T (Tags.singleton "ocamlfind"); A"ocamlfind"; x ; A "-package"; A ocamlfind_packages ]
97
98 let ppopt l = List.map (fun e -> S[ A"-ppopt"; e ]) l
99
100 let () = dispatch begin
101   function
102     | Before_rules ->
103
104       Options.ocamlc := ocamlfind  (A"ocamlc");
105       Options.ocamlopt := ocamlfind (A"ocamlopt");
106       Options.ocamldep := ocamlfind (A"ocamldep");
107       Options.ocamldoc := ocamlfind (A"ocamldoc");
108       Options.ocamlmktop := ocamlfind (A"ocamlmktop");
109
110       if not (List.mem "log" !Options.tags) then begin
111         pp_macro_options @= [ A "-DNLOG" ];
112       end;
113       if (List.mem "profile" !Options.tags) then begin
114         pp_macro_options @= [ A "-DPROFILE" ];
115         native_compile_flags @= [A "-p" ];
116         native_link_flags @= [ "-p" ];
117         cxx_flags @= [ A "-pg" ];
118         cxx_link_flags @= [ A "-pg" ];
119       end;
120
121       if (List.mem "debug" !Options.tags) then begin
122         pp_macro_options @= [ A "-DDEBUG" ];
123         cxx_flags @= [ A "-O0"; A "-g" ];
124         cxx_link_flags @= [ A "-g" ];
125       end
126       else begin
127         compile_flags @= [A "-noassert"];
128         pp_macro_options @= [ A "-unsafe" ];
129         native_compile_flags @= [ A "-inline"; A ocaml_inline ];
130         cxx_flags @= [ A "-O3" ]
131       end;
132
133       let dir_path = Pathname.pwd / src_path in
134       let dir = Pathname.readdir dir_path in
135
136       Array.iter (fun entry ->
137         if Pathname.check_extension entry "ml" then
138           Depends.ocaml (src_path / entry)
139         else if Pathname.check_extension entry "cpp" then
140           Depends.cxx (src_path / entry)
141       ) dir;
142
143     | After_rules ->
144       dep [ "link" ] cstub_lib;
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; S cxx_flags_for_ml ;  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; S cxx_flags_for_ml; 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