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)
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" ]
21 let native_compile_flags = ref [A"-fno-PIC"]
22 let compile_flags = ref []
24 let dwsize = sprintf "-DWORDSIZE%i" Sys.word_size
27 let ( @= ) r l = r := !r @ l
28 let ( =:: ) r e = r := e :: !r
30 (* Pre-processed files *)
31 let pp_macro_options = ref
32 [ A "-parser"; A "macro"; A dwsize; A "-I"; P include_path ]
34 let include_full_path = Pathname.pwd / include_path
39 let ic = open_in ml and includes = ref [] in
43 let s = input_line ic in
44 if String.length s > 0 then
46 sscanf s " INCLUDE \"%s@\"" (fun s -> includes =:: include_path /s)
47 with Scan_failure _ -> ()
49 with End_of_file -> close_in ic
55 let includes = scan_include file in
56 List.fold_left (fun a i -> (loop i) @ a) includes includes
58 let includes = loop ml in
59 dep [ "file:" ^ ml ] includes
61 let parse_depends depfile =
62 let ichan = open_in depfile in
63 let iscan = Scanning.from_channel ichan in
64 let includes = ref [] in
65 bscanf iscan " %_s@: %s " ignore;
69 function "" -> raise End_of_file
74 _ -> close_in ichan;!includes
77 let depfile = !Options.build_dir /" __cxx_depends.tmp" in
78 let cmd = Cmd (S[ A cxx_cmd ; S !cxx_flags; cxx_include_flags ; A"-MM";
79 A "-MF"; P depfile; P cpp])
81 let () = Command.execute ~quiet:true ~pretend:false cmd in
82 let includes = parse_depends depfile in
83 let includes' = (List.filter (Pathname.is_relative) includes) in
84 dep [ "compile"; "file:" ^ cpp ] includes'
87 let cxx_compile env _build =
88 let cpp = env "%.cpp" and obj = env "%.o" in
89 let tags = (tags_of_pathname cpp) ++ "compile" ++ "c++" in
90 Cmd(S[T tags; A cxx_cmd; A "-o" ; P obj; A "-c"; S !cxx_flags; cxx_include_flags; P cpp])
92 (* Native compile and link action *)
94 let ocamlfind x = S[ T (Tags.singleton "ocamlfind"); A"ocamlfind"; x ; A "-package"; A ocamlfind_packages ]
96 let ppopt l = List.map (fun e -> S[ A"-ppopt"; e ]) l
98 let () = dispatch begin
102 Options.ocamlc := ocamlfind (A"ocamlc");
103 Options.ocamlopt := ocamlfind (A"ocamlopt");
104 Options.ocamldep := ocamlfind (A"ocamldep");
105 Options.ocamldoc := ocamlfind (A"ocamldoc");
106 Options.ocamlmktop := ocamlfind (A"ocamlmktop");
108 if not (List.mem "trace" !Options.tags) then begin
109 pp_macro_options @= [ A "-DNTRACE" ];
111 if (List.mem "profile" !Options.tags) then begin
112 pp_macro_options @= [ A "-DPROFILE" ];
113 native_compile_flags @= [A "-p" ];
114 native_link_flags @= [ "-p" ];
115 cxx_flags @= [ A "-pg" ];
116 cxx_link_flags @= [ A "-pg" ];
119 if (List.mem "debug" !Options.tags) then begin
120 pp_macro_options @= [ A "-DDEBUG" ];
121 cxx_flags @= [ A "-O0"; A "-g" ];
122 cxx_link_flags @= [ A "-g" ];
125 compile_flags @= [A "-noassert"];
126 pp_macro_options @= [ A "-unsafe" ];
127 native_compile_flags @= [ A "-inline"; A ocaml_inline ];
128 cxx_flags @= [ A "-O3" ]
131 let dir_path = Pathname.pwd / src_path in
132 let dir = Pathname.readdir dir_path in
134 Array.iter (fun entry ->
135 if Pathname.check_extension entry "ml" then
136 Depends.ocaml (src_path / entry)
137 else if Pathname.check_extension entry "cpp" then
138 Depends.cxx (src_path / entry)
142 dep [ "link" ] cstub_lib;
144 rule "c++: cpp & depends -> o" ~prod:"%.o" ~deps:[ "%.cpp" ] cxx_compile;
145 let syntax_flags = S ([ A "-syntax"; A "camlp4o";
146 S (ppopt [A "-printer" ; A"Camlp4OCamlAstDumper"]);
147 S (ppopt !pp_macro_options) ])
149 flag [ "ocaml"; "ocamldep"] syntax_flags;
150 flag [ "ocaml"; "compile" ] (S[ A "-cc"; A cxx_cmd; S cxx_flags_for_ml ; syntax_flags; S !compile_flags ]);
151 flag [ "ocaml"; "native"; "compile" ] (S !native_compile_flags);
152 flag [ "ocaml"; "link" ]
153 (S [ S link_flags ; A "-cc"; A cxx_cmd; S cxx_flags_for_ml; A "-cclib" ;
154 Quote (S [ _S cstub_lib; S !cxx_link_flags]) ]);
155 flag [ "ocaml"; "byte"; "link" ] (_S !byte_link_flags);
156 flag [ "ocaml"; "native"; "link" ] (_S !native_link_flags);
157 flag [ "c"; "ocamlmklib"] (S[ A "-custom"; ])