#!/usr/bin/env ocaml
-let target = ref "main.otarget"
+let file_content =
+ let buffer = Buffer.create 128 in
+ begin fun f ->
+ Buffer.clear buffer;
+ try
+ let in_f = open_in f in
+ while true do
+ let line = input_line in_f in
+ Buffer.add_string buffer line;
+ Buffer.add_char buffer ' ';
+ done;
+ failwith "finished"
+ with
+ e -> let s = Buffer.contents buffer in s
+ end
+
+let target = ref [ ]
let debug = ref false
let profile = ref false
let verbose = ref false
+let dir = Sys.getcwd ()
+let project_root = Filename.dirname Sys.argv.(0)
+
let () =
for i = 1 to Array.length Sys.argv - 1 do
match Sys.argv.(i) with
| "-d" -> debug := true
| "-p" -> profile := true
| "-v" -> verbose := true
- | x -> target := x
+ | x -> target := x :: !target
done
let oprofile = if !profile then " -tag profile " else ""
let odebug = if !profile then " -tag debug " else ""
-let otarget = if !target = "clean" then " -clean " else !target
-let overbose = if !verbose then " -classic-display " else ""
-
-let cmd = "ocamlbuild -use-ocamlfind " ^ overbose ^ oprofile ^ odebug ^ otarget
-let _ = Sys.command cmd
+let clean_first = ref false
+let () =
+ Sys.chdir project_root;
+ Printf.printf "Entering directory `%s'\n%!" project_root
+let otarget = List.fold_left (fun acc t ->
+ if t = "clean" || t = "-clean" then (clean_first := true; acc)
+ else
+ let t =
+ if Filename.check_suffix t ".otarget" then
+ file_content ((Filename.chop_suffix t ".otarget") ^ ".itarget")
+ else t
+ in
+ t ^ " " ^ acc) "" (if (!target == []) then [ "main.otarget" ] else !target)
+let overbose = if !verbose then " -classic-display " else ""
+let clean_cmd = if !clean_first then "ocamlbuild -clean;" else ""
+let build_cmd = if otarget = "" then "" else
+ Printf.sprintf "ocamlbuild -use-ocamlfind %s %s %s %s"
+ overbose oprofile odebug otarget
+let i = Sys.command (clean_cmd ^ build_cmd)
+let () = Sys.chdir dir;
+ Printf.printf "Leaving directory `%s'\n%!" project_root
+let _ = exit i