myocamlbuild.ml 2.37 KB
Newer Older
fpottier's avatar
fpottier committed
1 2 3 4 5 6 7 8 9
open Ocamlbuild_plugin
open Command

(* ---------------------------------------------------------------------------- *)

(* Compilation flags. *)

let flags () =
  (* -inline 1000 *)
10
  flag ["ocaml"; "compile"; "native"] (S [A "-inline"; A "1000"]);
POTTIER Francois's avatar
POTTIER Francois committed
11 12
  (* -noassert *)
  flag ["ocaml"; "compile"; "noassert"] (S [A "-noassert"]);
13
  (* nazi warnings *)
14
  flag ["ocaml"; "compile"; "my_warnings"] (S[A "-w"; A "@1..49-4-9-41-44"])
fpottier's avatar
fpottier committed
15 16 17 18 19 20 21 22 23

(* ---------------------------------------------------------------------------- *)

(* Dealing with the two parsers. *)

(* Just for fun, Menhir comes with two parsers for its own input files. One is
   called [yacc-parser.mly] and is built using [ocamlyacc]. The other is called
   [fancy-parser.mly] and is built using Menhir. It depends on [standard.mly].
   The choice between the two parsers is determined by the presence of the tag
24
   [fancy_parser]. *)
fpottier's avatar
fpottier committed
25

POTTIER Francois's avatar
POTTIER Francois committed
26 27 28 29
let fancy () : bool =
  mark_tag_used "fancy_parser";
  Tags.mem "fancy_parser" (tags_of_pathname "")

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
let compile_messages grammar messages target =
  rule
    "compile a custom table of messages"
    ~prod:target
    ~deps:[ grammar; messages ]
    (fun env _ ->
      Cmd(S[
        !Options.ocamlyacc; (* menhir *)
        (* no additional flags; may allow them in the future *)
        P (env grammar);
        A "--compile-errors"; P (env messages);
        Sh ">"; Px (env target);
      ]))

let parser_configuration () =
  (* Create [parser.mly] by copying the appropriate source file. *)
POTTIER Francois's avatar
POTTIER Francois committed
46 47 48 49 50
  copy_rule "create parser.mly"
    (* source: *)
    (if fancy() then "fancy-parser.mly" else "yacc-parser.mly")
    (* target: *)
    "parser.mly"
51 52
  ;
  (* Create [Driver.ml] by copying the appropriate source file. *)
POTTIER Francois's avatar
POTTIER Francois committed
53 54 55 56
  copy_rule "create Driver.ml" 
    (* source: *)
    (if fancy() then "fancyDriver.ml" else "yaccDriver.ml")
    (* target: *)
57
    "Driver.ml"
58 59 60 61 62 63 64 65 66
  ;
  (* In the fancy case, use Menhir to generate [FancyParserMessages.ml] based
     on [fancy-parser.messages], which is maintained by hand. *)
  if fancy() then
    compile_messages
      (* sources: *)
      "parser.mly" "fancy-parser.messages"
      (* target: *)
      "FancyParserMessages.ml"
67

fpottier's avatar
fpottier committed
68 69 70 71 72 73 74
(* ---------------------------------------------------------------------------- *)

(* Define custom compilation rules. *)

let () =
  dispatch (function After_rules ->
    (* Add our rules after the standard ones. *)
75
    parser_configuration();
fpottier's avatar
fpottier committed
76 77 78
    flags();
  | _ -> ()
  )