Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 207 lines (173 sloc) 5.912 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
open Ocamlbuild_pack
open Ocamlbuild_plugin
open Unix

let pdflatex = A"pdflatex"
let run_and_read = Ocamlbuild_pack.My_unix.run_and_read

(* ocamlfind command *)
let ocamlfind x = S[A"ocamlfind"; x]

let run_cmd cmd =
  try
    let ch = Unix.open_process_in cmd in
    let hg_version = input_line ch in
    let () = close_in ch in
    hg_version
  with | End_of_file -> "Not available"
    
let hg_revision = run_cmd "hg id -i"

let branch_version = run_cmd "hg branch"


let split s ch =
  let x = ref [] in
  let rec go s =
    let pos = String.index s ch in
    x := (String.before s pos)::!x;
    go (String.after s (pos + 1))
  in
  try
    go s
  with Not_found -> !x

let split_nl s = split s '\n'

let time =
  let tm = Unix.gmtime (Unix.time()) in
  Printf.sprintf "%02d/%02d/%04d %02d:%02d:%02d UTC"
    (tm.tm_mday) (tm.tm_mon + 1) (tm.tm_year + 1900)
    tm.tm_hour tm.tm_min tm.tm_sec

let make_version _ _ =
  let cmd =
    let template = "let hg_revision = %S\n" ^^
      "let compile_time = %S\n" ^^
      "let version = %S"
    in
    Printf.sprintf template hg_revision time branch_version
  in
  Cmd (S [A "echo"; Quote(Sh cmd); Sh ">"; P "version.ml"])

let before_space s =
  try
    String.before s (String.index s ' ')
  with Not_found -> s

(* this lists all supported packages *)
let find_packages () =
  List.map before_space (split_nl & run_and_read "ocamlfind list")

let path_to_bisect_instrument () =
  try
    let r = run_and_read "ocamlfind query bisect" in
    let r_stripped = List.hd(split_nl r ) in
    (* Printf.printf "\n\npath=%s\n\n\n" r_stripped ; *)
      r_stripped
  with _ -> "___could_not_find_bisect___"

let _ = dispatch & function
  | Before_options ->
    Options.ocamlc := ocamlfind & S[A"ocamlc"; A"-thread";];
    Options.ocamlopt := ocamlfind & S[A"ocamlopt";A"-thread";];
    Options.ocamldoc := ocamlfind & S[A"ocamldoc"];
    Options.ocamldep := ocamlfind & A"ocamldep"
  | After_rules ->
    rule "version.ml" ~prod: "version.ml" make_version;
    rule "LaTeX to PDF"
      ~prod:"%.pdf"
      ~dep:"%.tex"
      begin fun env _build ->
let tex = env "%.tex" in
(* let pdf = env "%.pdf" in *)
let tags = tags_of_pathname tex ++ "compile" ++ "LaTeX" ++ "pdf" in
let cmd = Cmd(S[pdflatex;A"-shell-escape";T tags;P tex;A"-halt-on-error"]) in
Seq[cmd;]
      end;
    dep ["compile";"LaTeX";"pdf";]
      ["doc/introduction.tex";
       "doc/client.tex";
       "doc/consistency.tex";
       "doc/protocol.tex";
       "doc/restarting.tex";
       "doc/part3.tex";
       "doc/state_machine.tex";
       "doc/states.eps";
      ];
    rule "Extract .tar.gz"
      ~dep:"3rd-party/%(f).tar.gz"
      ~stamp:"3rd-party/%(f).extracted"
      begin fun env _build ->
let archive = env "3rd-party/%(f).tar.gz" in
let () = Log.dprintf 0 "extracting %s" archive in
let cmd =
Cmd (S[(*A"echo";*)
A"tar";
A"zxvf";
A archive;
A"--directory";A"3rd-party";
]) in
Seq[cmd;]
      end;
    rule "configure 3rd-party"
      ~dep:"3rd-party/%(dir).extracted"
      ~stamp:"3rd-party/%(dir).configured"
      begin fun env _build ->
let dir = env "%(dir)" in
let () = Log.dprintf 0 "configuring %s" dir in
let evil_cmd =
Printf.sprintf "cd 3rd-party/%s && ./configure " dir in
let configure = Cmd (S[Sh evil_cmd;
A"--disable-bzip";
A"--disable-zlib";
A"--disable-shared";
]) in
Seq[configure;]
      end;
    rule "make 3rd-party"
      ~dep:"3rd-party/%(dir).configured"
      ~stamp:"3rd-party/%(dir).make"
      begin fun env _build ->
let dir = env "%(dir)" in
let () = Log.dprintf 0 "make 3rd-party %s" dir in
let make =
Printf.sprintf "cd 3rd-party/%s && make" dir in
let cmd = Cmd(Sh make) in
Seq[cmd]
      end;

      (* how to compile C stuff that needs tc *)
    let tc_home ="3rd-party/tokyocabinet-1.4.45" in
    flag ["compile"; "c";]
      (S[
A"-ccopt";A( "-I" ^ tc_home);
A"-ccopt";A"-I../src/tools";
      ]);
    flag ["compile";"c";]
      (S[
A"-ccopt";A"-msse4.2";
      ]);

    dep ["ocaml";"link";"is_main"]["src/otc/libotc.a"];

    dep ["ocaml";"link";"is_main"]["src/libcutil.a"];

    flag ["ocaml";"link";"is_main"](
      S[A"-linkpkg";
A"-ccopt"; A("-L" ^ tc_home);
A"-ccopt"; A"-ltokyocabinet";
       ]);

    flag ["ocaml";"byte";"link"] (S[A"-custom";]);

    flag ["ocaml";"compile";"warn_error"]
       (S[A"-warn-error";A"A"]);

    (* dependency of wrapper on tc *)
    dep ["compile";"c";"file:src/otc/otc_wrapper.c"]
      ["3rd-party/tokyocabinet-1.4.45.make"];

    flag ["pp"; "camlp4of"] & S[A"-loc"; A"loc"] ;

    flag ["compile";"ocaml";"use_bisect"]
      (S[A"-I";A"+bisect"]);


    flag ["ocaml";"byte";"link";"use_bisect"]
      (S[ A"-I"; A"+bisect"; A"bisect.cma"; ]);

    flag ["ocaml";"native";"link";"program";"use_bisect"]
      (S[A"-I";A"+bisect";A"bisect.cmxa"; ]);

    flag ["link";"use_bisect"] (S[A"-ccopt";A"--coverage";]);

    flag ["pp";"ocaml";"use_macro"] (S[A"camlp4of";A"pa_macro.cmo"]);
    flag ["pp";"ocaml";"use_bisect"]
      (S[A"-no_quot";A(path_to_bisect_instrument() ^ "/instrument.cmo")]);
    
    flag ["pp";"use_macro";"small_tlogs";
"file:src/tlog/tlogcommon.ml"] (S[A"-DSMALLTLOG"]);




    (* For each ocamlfind package one inject the -package option when
* compiling, computing dependencies, generating documentation and
* linking. *)
    List.iter begin fun pkg ->
      flag ["ocaml"; "compile"; "pkg_"^pkg] & S[A"-package"; A pkg];
      flag ["ocaml"; "ocamldep"; "pkg_"^pkg] & S[A"-package"; A pkg];
      flag ["ocaml"; "doc"; "pkg_"^pkg] & S[A"-package"; A pkg];
      (* only use -package for linking programs, not for linking .cma or .cmxa libraries *)
      flag ["ocaml"; "link"; "program"; "pkg_"^pkg] & S[A"-package"; A pkg];
      flag ["ocaml"; "infer_interface"; "pkg_"^pkg] & S[A"-package"; A pkg];
    end (find_packages ());

  | _ -> ()
Something went wrong with that request. Please try again.