fix
[camlunity.git] / myocamlbuild_config.ml
blobb5f8628c49add909b5c704e1427ef9985aba9187
1 (**
2 r1 (2009-08-11)
4 This ocamlbuild plugin will try to find libraries by name using (in order)
5 - local myocamlbuild.config file
6 - ocamlfind
8 Sample myocamlbuild.config :
10 extlib=C:/my/contrib/extlib-1.5.1
11 deriving=C:/my/contrib/deriving-0.1.1/lib
12 oUnit=C:/my/contrib/ounit-1.0.3
16 (** querying ocamlfind *)
18 let chomp s =
19 let is_nl ch = match ch with | '\n' | '\r' -> true | _ -> false in
20 let rec cut n =
21 if n = 0 then 0 else if is_nl s.[n-1] then cut (n-1) else n
23 let ls = String.length s in
24 let n = cut ls in
25 if n = ls then s else String.sub s 0 n
27 let ocamlfind lib =
28 let cin = Unix.open_process_in (Printf.sprintf "ocamlfind -query %s" lib) in
29 let s = chomp (input_line cin) in
30 (* let s = Filename.quote s in*)
31 ignore (Unix.close_process_in cin);
34 (** querying config *)
36 let file_lines name =
37 let l = ref [] in
38 begin try
39 let ch = open_in name in
40 begin try while true do l := input_line ch :: !l done with End_of_file -> () end;
41 close_in_noerr ch
42 with
43 exn -> ()
44 end;
47 let read_config name =
48 let l = file_lines name in
49 let split s =
50 let index = String.index s '=' in
51 (String.sub s 0 index, String.sub s (index+1) ((String.length s) - index - 1))
53 let split s = try split s with _ -> "","" in
54 List.map split l
56 (** usage *)
58 let config = read_config "myocamlbuild.config"
59 let () =
60 match config with
61 | [] -> prerr_endline "No config, will use ocamlfind"
62 | _ -> prerr_endline "Using config : ";
63 List.iter (fun (x,y) -> Printf.eprintf "%s=%s\n%!" x y) config
65 let lib name =
66 try
67 List.assoc name config
68 with exn ->
69 try
70 ocamlfind name
71 with exn ->
72 "+" ^ name
74 let extern ?cma pkg_name = Ocamlbuild_plugin.ocaml_lib ~extern:true ~dir:(lib pkg_name) (match cma with Some s -> s | None -> pkg_name)