myocamlbuild.ml 5.06 KB
Newer Older
Stephane Glondu's avatar
Stephane Glondu committed
1 2
open Ocamlbuild_plugin

3 4 5 6
let debug =
  try Sys.getenv "BELENIOS_DEBUG" <> ""
  with Not_found -> false

7 8 9 10 11 12 13 14 15 16 17
let try_exec cmd =
  Sys.command (cmd ^ " >/dev/null 2>&1") = 0

let has_ocamlopt = try_exec "which ocamlopt"

let native_compilation =
  try Sys.getenv "OCAMLBEST" = "native"
  with Not_found -> has_ocamlopt

let exe_suffix = if native_compilation then ".native" else ".byte"

Stephane Glondu's avatar
Stephane Glondu committed
18 19 20 21 22
let atdgen_action opts env build =
  let x = env "%.atd" in
  let d = Pathname.dirname x and f = Pathname.basename x in
  Cmd (S [A"cd"; P d; Sh"&&"; A"atdgen"; S opts; P f])

23 24
let js_of_ocaml env build =
  Cmd (S [A"js_of_ocaml"; P (env "%.byte")])
Stephane Glondu's avatar
Stephane Glondu committed
25

26 27 28 29
let ( / ) = Filename.concat

let platform_rules kind =
  let lib = "src" / "lib" in
30 31 32 33 34 35 36 37 38
  let platform_dir = "src" / "platform" in
  let platform_mod = platform_dir / kind / "platform" in
  let platform_lib = platform_dir / "platform-" ^ kind in
  let ml = platform_mod ^ ".ml" in
  let mli = platform_mod ^ ".mli" in
  let mllib = platform_lib ^ ".mllib" in
  rule mllib ~deps:[ml] ~prods:[mllib] (fun _ _ ->
    (* technically, there is no dependency, but we need the directory to
       exist for the following *)
39 40 41
    Echo ([platform_dir / kind / "Belenios_version"; "\n";
           platform_dir / kind / "Platform"; "\n"],
          mllib)
42
  );
43
  dep ["file:" ^ ml] [mli];
44 45
  copy_rule mli (lib / "platform.mli") mli;
  ocaml_lib platform_lib
46

Stephane Glondu's avatar
Stephane Glondu committed
47 48 49 50 51 52 53 54 55
let build_rule () =
  let genversion = "genversion.sh" in
  let deps = ["VERSION"; genversion] in
  let prod = "BUILD" in
  let builder _ _ =
    Cmd (S [A "sh"; P genversion; Sh ">"; P prod])
  in
  rule "BUILD" ~deps ~prod builder

56
let version_rules kind =
Stephane Glondu's avatar
Stephane Glondu committed
57
  let file = "BUILD" in
58 59
  let deps = [file; "src/platform/" ^ kind ^ "/belenios_version.mli"] in
  let prod = "src/platform/" ^ kind ^ "/belenios_version.ml" in
Stephane Glondu's avatar
Stephane Glondu committed
60 61 62 63 64 65 66 67 68 69 70
  let builder _ _ =
    let version, build =
      let ic = open_in file in
      let version = input_line ic in
      let build = input_line ic in
      close_in ic;
      version, build
    in
    let lines = Printf.([
      sprintf "let version = \"%s\"" version;
      sprintf "let build = \"%s\"" build;
71
      sprintf "let debug = %b" debug;
Stephane Glondu's avatar
Stephane Glondu committed
72 73 74
    ]) in
    Echo (lines, prod)
  in
75 76 77 78 79
  copy_rule
    (kind / "belenios_tool.mli")
    "src/lib/belenios_version.mli"
    ("src/platform/" ^ kind ^ "/belenios_version.mli");
  rule ("BUILD -> " ^ kind ^ "/belenios_version.ml") ~deps ~prod builder
Stephane Glondu's avatar
Stephane Glondu committed
80

Stephane Glondu's avatar
Stephane Glondu committed
81 82 83 84
let copy_static f =
  let base = Filename.basename f in
  copy_rule base f ("src/static" / base)

Stephane Glondu's avatar
Stephane Glondu committed
85 86 87 88 89 90 91 92 93
let () = dispatch & function

  | Before_options ->

    Options.use_ocamlfind := true;
    Options.make_links := false;

  | After_rules ->

Stephane Glondu's avatar
Stephane Glondu committed
94
    Pathname.define_context "src/web" ["src/lib"];
95
    Pathname.define_context "src/tool" ["src/lib"];
Stephane Glondu's avatar
Stephane Glondu committed
96
    Pathname.define_context "src/booth" ["src/lib"];
97
    Pathname.define_context "demo" ["src/lib"];
98
    Pathname.define_context "stuff" ["src/lib"];
Stephane Glondu's avatar
Stephane Glondu committed
99
    Pathname.define_context "." ["src/lib"];
Stephane Glondu's avatar
Stephane Glondu committed
100 101 102

    (* the following avoids an ocamlfind warning, it should be built-in *)
    flag ["doc"; "thread"] (A"-thread");
Stephane Glondu's avatar
Stephane Glondu committed
103

Stephane Glondu's avatar
Stephane Glondu committed
104 105 106
    (* there seems to be no built-in tag for this... *)
    flag ["compile"; "interf"] (A"-no-keep-locs");

Stephane Glondu's avatar
Stephane Glondu committed
107 108 109 110
    rule "%.atd -> %_t.ml & %_t.mli" ~deps:["%.atd"] ~prods:["%_t.ml"; "%_t.mli"]
      (atdgen_action [A"-t"]);
    rule "%.atd -> %_j.ml & %_j.mli" ~deps:["%.atd"] ~prods:["%_j.ml"; "%_j.mli"]
      (atdgen_action [A"-j"; A"-j-std"]);
111 112 113

    rule "%.byte -> %.js" ~deps:["%.byte"] ~prods:["%.js"] js_of_ocaml;

114 115 116 117
    rule "%.md -> %.html" ~deps:["%.md"] ~prods:["%.html"]
      (fun env build ->
        Cmd (S [A"markdown"; P (env "%.md"); Sh">"; P (env "%.html")])
      );
Stephane Glondu's avatar
Stephane Glondu committed
118

Stephane Glondu's avatar
Stephane Glondu committed
119
    build_rule ();
120 121
    version_rules "native";
    version_rules "js";
122 123 124
    platform_rules "native";
    platform_rules "js";

125 126 127
    copy_rule "jsbn.js" "ext/booth/js/jsbn/jsbn.js" "src/static/jsbn.js";
    copy_rule "jsbn2.js" "ext/booth/js/jsbn/jsbn2.js" "src/static/jsbn2.js";
    copy_rule "sjcl.js" "ext/booth/js/sjcl/sjcl.js" "src/static/sjcl.js";
128 129
    copy_rule "random.js" "src/platform/js/random.js" "src/static/random.js";

130
    copy_rule "belenios-tool" ("src/tool/tool_cmdline" ^ exe_suffix) "belenios-tool";
131 132 133
    copy_rule "belenios-tool.js" "src/tool/tool_js.js" "src/static/tool_js.js";
    copy_rule "belenios-tool.html" "src/tool/belenios-tool.html" "src/static/belenios-tool.html";

134
    copy_rule "encrypting.gif" "ext/booth/encrypting.gif" "src/static/encrypting.gif";
135

136
    copy_rule "tool_js_booth.js" "src/tool/tool_js_booth.js" "src/static/tool_js_booth.js";
Stephane Glondu's avatar
Stephane Glondu committed
137
    copy_rule "tool_js_tkeygen.js" "src/tool/tool_js_tkeygen.js" "src/static/tool_js_tkeygen.js";
138
    copy_rule "tool_js_ttkeygen.js" "src/tool/tool_js_ttkeygen.js" "src/static/tool_js_ttkeygen.js";
Stephane Glondu's avatar
Stephane Glondu committed
139
    copy_rule "tool_js_credgen.js" "src/tool/tool_js_credgen.js" "src/static/tool_js_credgen.js";
140
    copy_rule "tool_js_questions.js" "src/tool/tool_js_questions.js" "src/static/tool_js_questions.js";
141
    copy_rule "tool_js_pd.js" "src/tool/tool_js_pd.js" "src/static/tool_js_pd.js";
Stephane Glondu's avatar
Stephane Glondu committed
142

Stephane Glondu's avatar
Stephane Glondu committed
143 144 145 146 147 148 149 150 151
    List.iter
      copy_static
      [
        "ext/css/reset.css";
        "ext/css/styled-elements.css";
        "ext/css/style.css";
        "ext/css/superfish.css";
      ]

Stephane Glondu's avatar
Stephane Glondu committed
152
  | _ -> ()