• en

Ocamlbuild macros

OCamlbuild with macro preprocessing

To enable the use of the Camlp4 INCLUDE macro facility, one has to create a plugin that records the dependence of the sources w.r.t. the included file.

Here is an example where the source file (source.ml) and the included file (other.ml) are in the src/ directory.

myocamlbuild.ml

 open Ocamlbuild_plugin
 
 let () =
   dispatch begin function
   | After_rules ->
     let includes = ["src/other.ml"] in
     dep ["ocaml"; "ocamldep"] includes;
     dep ["ocaml"; "compile"] includes;
 
     let pp_src = S[A"-pp"; A"camlp4of -Isrc"] in
     flag ["ocaml"; "ocamldep"] & pp_src;
     flag ["ocaml"; "compile"] & pp_src;
 
   | _ -> ()
   end

src/source.ml

 INCLUDE "other.ml"

src/other.ml

 let f x = x + 1
 
 let () = print_endline "here."