3 open Myocamlbuild_config
7 eprintf "%![%s]%!\n" (String.concat ", " l)
10 let _S ?(extra=N) l = S (List.map (fun e -> (S [extra; _A e] )) l)
13 let cxx_flags = ref [ _S cxx_flags ]
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" ]
22 let native_compile_flags = ref [A"-fno-PIC"]
23 let compile_flags = ref []
25 let dwsize = sprintf "-DWORDSIZE%i" Sys.word_size
28 let ( @= ) r l = r := !r @ l
29 let ( =:: ) r e = r := e :: !r
31 (* Pre-processed files *)
32 let pp_macro_options = ref
33 [ A "-parser"; A "macro"; A dwsize; A "-I"; P include_path ]
35 let include_full_path = Pathname.pwd / include_path
40 let ic = open_in ml and includes = ref [] in
44 let s = input_line ic in
45 if String.length s > 0 then
47 sscanf s " INCLUDE \"%s@\"" (fun s -> includes =:: include_path /s)
48 with Scan_failure _ -> ()
50 with End_of_file -> close_in ic
56 let includes = scan_include file in
57 List.fold_left (fun a i -> (loop i) @ a) includes includes
59 let includes = loop ml in
60 dep [ "file:" ^ ml ] includes
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;
70 function "" -> raise End_of_file
75 _ -> close_in ichan;!includes
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])
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'
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])
93 (* Native compile and link action *)
95 let ocamlfind x = S[ T (Tags.singleton "ocamlfind"); A"ocamlfind"; x ; A "-package"; A ocamlfind_packages ]
97 let ppopt l = List.map (fun e -> S[ A"-ppopt"; e ]) l
99 let () = dispatch begin
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");
109 if (List.mem "trace" !Options.tags) then begin
110 pp_macro_options @= [ A "-DTRACE" ];
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" ];
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" ];
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" ]
132 let dir_path = Pathname.pwd / src_path in
133 let dir = Pathname.readdir dir_path in
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)
143 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) ])
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"; ])