Merge branch 'handle-stdout'
[SXSI/xpathcomp.git] / myocamlbuild.ml
index 23901cd..6121130 100644 (file)
@@ -4,26 +4,23 @@ open Myocamlbuild_config
 open Format
 
 let print_list l =
-  Printf.eprintf "%![";
-  List.iter (fun s -> Printf.eprintf " '%s' " s) l;
-  Printf.eprintf "]\n%!"
+  eprintf "%![%s]%!\n" (String.concat ", " l)
 
-
-(*let cxx_flags = S (List.map ( fun x -> A x) cxx_flags)*)
 let _A x = A x
 let _S ?(extra=N) l = S (List.map (fun e -> (S [extra; _A e] )) l)
 ;;
-
+let cxx_flags_for_ml = [ _S ~extra:(_A "-ccopt") cxx_flags ]
 let cxx_flags = ref [ _S cxx_flags ]
-
 let project_dirs = [ src_path; include_path ]
 let cxx_include_flags = _S cxx_includes
 let cxx_link_flags = ref  [ _S cxx_lpaths; _S cxx_libs]
 let native_link_flags = ref (List.map (fun s -> s ^ ".cmxa") ocaml_link)
 let byte_link_flags =  ref ("-custom" :: (List.map (fun s -> s ^ ".cma") ocaml_link))
 let link_flags = [ A"-linkpkg" ]
+let libs_files = List.map (fun s -> "file:" ^ s) cxx_libs_objects
+
 
-let native_compile_flags = ref [A"-fno-PIC"]
+let native_compile_flags = if Sys.word_size = 64 then ref [A"-fno-PIC"] else ref []
 let compile_flags = ref []
 
 let dwsize = sprintf "-DWORDSIZE%i" Sys.word_size
@@ -41,19 +38,19 @@ module Depends =
 struct
 open Scanf
 let scan_include ml =
-  let ic = try open_in ml with
-      _ -> open_in (include_full_path / ml)
-  in
-  let includes = ref [] in
-  try
-    while true do
-      let s = input_line ic in
-      try
-       if String.length s > 0 then sscanf s " INCLUDE \"%s@\"" ((=::) includes)
-      with
-         Scan_failure _ -> ()
-    done; []
-  with End_of_file -> close_in ic; !includes
+  let ic = open_in ml and includes = ref [] in
+  begin
+    try
+      while true do
+       let s = input_line ic in
+       if String.length s > 0 then
+         try
+           sscanf s " INCLUDE \"%s@\"" (fun s -> includes =:: include_path /s)
+         with Scan_failure _ -> ()
+      done
+    with End_of_file -> close_in ic
+  end;
+  !includes
 
 let ocaml ml =
   let rec loop file =
@@ -61,57 +58,42 @@ let ocaml ml =
     List.fold_left (fun a i -> (loop i) @ a) includes includes
   in
   let includes = loop ml in
-    dep [ "file:" ^ ml ]
-      (List.map (fun s ->  include_path / s) includes)
+    dep [ "file:" ^ ml ] includes
 
-let parse_depends file depfile =
+let parse_depends depfile =
   let ichan = open_in depfile in
   let iscan = Scanning.from_channel ichan in
-  let () = bscanf iscan " %s@: " ignore in
-  let () = bscanf iscan " %s " ignore in
   let includes = ref [] in
+  bscanf iscan " %_s@: %s " ignore;
   try
     while true do
-      try
-       let s = bscanf iscan " %s " (fun s -> s) in
-       if s = "" then raise End_of_file;
-       if s <> "\\" then includes =::s
-      with
-         Scan_failure _ -> ()
+      bscanf iscan " %s " (
+       function "" -> raise End_of_file
+         | "\\" -> ()
+         | s ->  includes =::s)
     done; []
   with
-      End_of_file -> close_in ichan; !includes
-
-let uniq l =
-  let rec loop l acc =
-    match l with
-      | []  -> acc
-      | [ e ] -> e::acc
-      | e1 :: ((e2 :: _) as ll) ->
-       loop ll (if e1 = e2 then acc else e1 :: acc)
-  in
-  loop (List.sort compare l) []
+    _ -> close_in ichan;!includes
 
 let cxx cpp =
-  let depfile = ( cpp ^ ".depends") in
+  let depfile = !Options.build_dir /" __cxx_depends.tmp" in
   let cmd = Cmd (S[ A cxx_cmd ; S !cxx_flags; cxx_include_flags ; A"-MM";
-                   A "-MG"; A "-MF";  P depfile; P cpp])
+                   A "-MF";  P depfile; P cpp])
   in
   let () = Command.execute ~quiet:true ~pretend:false cmd in
-  let includes = parse_depends cpp depfile in
-  List.filter (Pathname.is_relative) (uniq includes)
+  let includes = parse_depends depfile in
+  let includes' = (List.filter (Pathname.is_relative) includes) in
+  dep [ "compile"; "file:" ^ cpp ] includes'
 end
 
-let cxx_compile env build =
-  let src = env "%.cpp" and obj = env "%.o" in
-  let local_include = Depends.cxx src in
-  let local_dispatch = List.map (fun p -> List.map (fun p' -> p'/p) project_dirs) local_include in
-  let  () = ignore (build local_dispatch) in
-  Cmd(S[A cxx_cmd; A "-o" ; P obj; A "-c";  S !cxx_flags; cxx_include_flags; P src])
+let cxx_compile env _build =
+  let cpp = env "%.cpp" and obj = env "%.o" in
+  let tags = (tags_of_pathname cpp) ++ "compile" ++ "c++" in
+  Cmd(S[T tags; A cxx_cmd; A "-o" ; P obj; A "-c";  S !cxx_flags; cxx_include_flags; P cpp])
 
 (* Native compile and link action *)
 
-let ocamlfind x = S[ A"ocamlfind"; x ; A "-package"; A ocamlfind_packages ]
+let ocamlfind x = S[ T (Tags.singleton "ocamlfind"); A"ocamlfind"; x ; A "-package"; A ocamlfind_packages ]
 
 let ppopt l = List.map (fun e -> S[ A"-ppopt"; e ]) l
 
@@ -125,7 +107,9 @@ let () = dispatch begin
       Options.ocamldoc := ocamlfind (A"ocamldoc");
       Options.ocamlmktop := ocamlfind (A"ocamlmktop");
 
-
+      if not (List.mem "log" !Options.tags) then begin
+       pp_macro_options @= [ A "-DNLOG" ];
+      end;
       if (List.mem "profile" !Options.tags) then begin
        pp_macro_options @= [ A "-DPROFILE" ];
        native_compile_flags @= [A "-p" ];
@@ -143,7 +127,7 @@ let () = dispatch begin
        compile_flags @= [A "-noassert"];
        pp_macro_options @= [ A "-unsafe" ];
        native_compile_flags @= [ A "-inline"; A ocaml_inline ];
-       cxx_flags @= [ A "-O3" ]
+       cxx_flags @= [ A "-O3"; A "-DNDEBUG" ]
       end;
 
       let dir_path = Pathname.pwd / src_path in
@@ -152,23 +136,22 @@ let () = dispatch begin
       Array.iter (fun entry ->
        if Pathname.check_extension entry "ml" then
          Depends.ocaml (src_path / entry)
-       (* else if Pathname.check_extension entry "cpp" then
-         Depends.cxx (src_path / entry) *)
+       else if Pathname.check_extension entry "cpp" then
+         Depends.cxx (src_path / entry)
       ) dir;
 
     | After_rules ->
       dep [ "link" ] cstub_lib;
-      rule "compile cpp -> o" ~prod:"%.o" ~deps:[ "%.cpp"] cxx_compile;
-
+      rule "c++: cpp & depends -> o" ~prod:"%.o" ~deps:[ "%.cpp" ] cxx_compile;
       let syntax_flags = S ([ A "-syntax"; A "camlp4o";
                            S (ppopt [A "-printer" ; A"Camlp4OCamlAstDumper"]);
                            S (ppopt !pp_macro_options) ])
       in
       flag [ "ocaml"; "ocamldep"] syntax_flags;
-      flag [ "ocaml"; "compile" ] (S[ A "-cc"; A cxx_cmd; syntax_flags; S !compile_flags ]);
+      flag [ "ocaml"; "compile" ] (S[ A "-cc"; A cxx_cmd; S cxx_flags_for_ml ;  syntax_flags; S !compile_flags ]);
       flag [ "ocaml"; "native"; "compile" ] (S !native_compile_flags);
       flag [ "ocaml"; "link" ]
-       (S [ S link_flags ; A "-cc"; A cxx_cmd; A "-cclib" ;
+       (S [ S link_flags ; A "-cc"; A cxx_cmd; S cxx_flags_for_ml; A "-cclib" ;
             Quote (S [ _S cstub_lib;  S !cxx_link_flags]) ]);
       flag [ "ocaml"; "byte"; "link" ] (_S !byte_link_flags);
       flag [ "ocaml"; "native"; "link" ] (_S !native_link_flags);