3 open Myocamlbuild_config
7 eprintf "%![%s]%!\n" (String.concat ", " l)
9 (*let cxx_flags = S (List.map ( fun x -> A x) cxx_flags)*)
11 let _S ?(extra=N) l = S (List.map (fun e -> (S [extra; _A e] )) l)
14 let cxx_flags = ref [ _S cxx_flags ]
16 let project_dirs = [ src_path; include_path ]
17 let cxx_include_flags = _S cxx_includes
18 let cxx_link_flags = ref [ _S cxx_lpaths; _S cxx_libs]
19 let native_link_flags = ref (List.map (fun s -> s ^ ".cmxa") ocaml_link)
20 let byte_link_flags = ref ("-custom" :: (List.map (fun s -> s ^ ".cma") ocaml_link))
21 let link_flags = [ A"-linkpkg" ]
23 let native_compile_flags = ref [A"-fno-PIC"]
24 let compile_flags = ref []
26 let dwsize = sprintf "-DWORDSIZE%i" Sys.word_size
29 let ( @= ) r l = r := !r @ l
30 let ( =:: ) r e = r := e :: !r
32 (* Pre-processed files *)
33 let pp_macro_options = ref
34 [ A "-parser"; A "macro"; A dwsize; A "-I"; P include_path ]
36 let include_full_path = Pathname.pwd / include_path
41 let ic = try open_in ml with
42 _ -> open_in (include_full_path / ml)
44 let includes = ref [] in
48 let s = input_line ic in
49 if String.length s > 0 then
50 try sscanf s " INCLUDE \"%s@\"" ((=::) includes)
51 with Scan_failure _ -> ()
53 with End_of_file -> close_in ic
59 let includes = scan_include file in
60 List.fold_left (fun a i -> (loop i) @ a) includes includes
62 let includes = loop ml in
64 (List.map (fun s -> include_path / s) includes)
66 let parse_depends file depfile =
67 let ichan = open_in depfile in
68 let iscan = Scanning.from_channel ichan in
69 let includes = ref [] in
71 bscanf iscan " %s@: " ignore;
72 bscanf iscan " %s " ignore;
76 let s = bscanf iscan " %s " (fun s -> s) in
77 if s = "" then raise End_of_file;
78 if s <> "\\" then includes =::s
83 End_of_file -> close_in ichan
92 | e1 :: ((e2 :: _) as ll) ->
93 loop ll (if e1 = e2 then acc else e1 :: acc)
95 loop (List.sort compare l) []
98 let depfile = ( cpp ^ ".depends") 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 cpp depfile in
104 let includes' = uniq (List.filter (Pathname.is_relative) includes) in
105 dep [ "compile"; "file:" ^ cpp ] includes'
108 let cxx_compile env build =
109 let src = env "%.cpp" and obj = env "%.o" in
110 let tags = (tags_of_pathname src) ++ "compile" ++ "c++" in
111 Cmd(S[T tags; A cxx_cmd; A "-o" ; P obj; A "-c"; S !cxx_flags; cxx_include_flags; P src])
113 (* Native compile and link action *)
115 let ocamlfind x = S[ T (Tags.singleton "ocamlfind"); A"ocamlfind"; x ; A "-package"; A ocamlfind_packages ]
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 (List.mem "trace" !Options.tags) then begin
130 pp_macro_options @= [ A "-DTRACE" ];
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 "compile cpp -> o" ~prod:"%.o" ~deps:[ "%.cpp" ] cxx_compile;
166 let syntax_flags = S ([ A "-syntax"; A "camlp4o";
167 S (ppopt [A "-printer" ; A"Camlp4OCamlAstDumper"]);
168 S (ppopt !pp_macro_options) ])
170 flag [ "ocaml"; "ocamldep"] syntax_flags;
171 flag [ "ocaml"; "compile" ] (S[ A "-cc"; A cxx_cmd; syntax_flags; S !compile_flags ]);
172 flag [ "ocaml"; "native"; "compile" ] (S !native_compile_flags);
173 flag [ "ocaml"; "link" ]
174 (S [ S link_flags ; A "-cc"; A cxx_cmd; A "-cclib" ;
175 Quote (S [ _S cstub_lib; S !cxx_link_flags]) ]);
176 flag [ "ocaml"; "byte"; "link" ] (_S !byte_link_flags);
177 flag [ "ocaml"; "native"; "link" ] (_S !native_link_flags);
178 flag [ "c"; "ocamlmklib"] (S[ A "-custom"; ])