1 (***********************************************************************)
5 (* Kim Nguyen, LRI UMR8623 *)
6 (* Université Paris-Sud & CNRS *)
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 *)
14 (***********************************************************************)
17 Time-stamp: <Last modified on 2013-01-30 19:09:57 CET by Kim Nguyen>
20 open Ocamlbuild_plugin
22 open Myocamlbuild_config
26 eprintf "%![%s]%!\n" (String.concat ", " l)
29 let _S ?(extra=N) l = S (List.map (fun e -> (S [extra; _A e] )) l)
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
42 let native_compile_flags = ref [A"-fno-PIC"]
43 let compile_flags = ref []
45 let dwsize = sprintf "-DWORDSIZE%i" Sys.word_size
48 let ( @= ) r l = r := !r @ l
49 let ( =:: ) r e = r := e :: !r
51 (* Pre-processed files *)
52 let pp_macro_options = ref
53 [ A dwsize; A "-I"; P include_path ]
55 let include_full_path = Pathname.pwd / include_path
60 let ic = open_in ml and includes = ref [] in
64 let s = input_line ic in
65 if String.length s > 0 then
67 sscanf s " INCLUDE \"%s@\"" (fun s -> includes =:: include_path /s)
68 with Scan_failure _ -> ()
70 with End_of_file -> close_in ic
76 let includes = scan_include file in
77 List.fold_left (fun a i -> (loop i) @ a) includes includes
79 let includes = loop ml in
80 dep [ "file:" ^ ml ] includes
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;
90 function "" -> raise End_of_file
95 _ -> close_in ichan;!includes
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])
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'
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])
113 (* Native compile and link action *)
115 let ocamlfind x = S[ T (Tags.singleton "ocamlfind"); A"ocamlfind"; x ; A "-package"; A ocamlfind_packages; A "-syntax"; A "camlp4o" ]
117 let ppopt l = List.map (fun e -> S[ A"-ppopt"; e ]) l
119 let () = dispatch begin
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");
129 if not (List.mem "log" !Options.tags) then begin
130 pp_macro_options @= [ A "-DNLOG" ];
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" ];
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" ];
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" ]
152 let dir_path = Pathname.pwd / src_path in
153 let dir = Pathname.readdir dir_path in
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)
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) ]
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")