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" ]
20 let libs_files = List.map (fun s -> "file:" ^ s) cxx_libs_objects
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 = open_in ml and includes = ref [] in
45 let s = input_line ic in
46 if String.length s > 0 then
48 sscanf s " INCLUDE \"%s@\"" (fun s -> includes =:: include_path /s)
49 with Scan_failure _ -> ()
51 with End_of_file -> close_in ic
57 let includes = scan_include file in
58 List.fold_left (fun a i -> (loop i) @ a) includes includes
60 let includes = loop ml in
61 dep [ "file:" ^ ml ] includes
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;
71 function "" -> raise End_of_file
76 _ -> close_in ichan;!includes
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])
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'
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])
94 (* Native compile and link action *)
96 let ocamlfind x = S[ T (Tags.singleton "ocamlfind"); A"ocamlfind"; x ; A "-package"; A ocamlfind_packages ]
98 let ppopt l = List.map (fun e -> S[ A"-ppopt"; e ]) l
100 let () = dispatch begin
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");
110 if not (List.mem "log" !Options.tags) then begin
111 pp_macro_options @= [ A "-DNLOG" ];
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" ];
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" ];
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"; A "-DNDEBUG" ]
133 let dir_path = Pathname.pwd / src_path in
134 let dir = Pathname.readdir dir_path in
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)
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) ])
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"; ])