preprocessorflagsocamlbuildcamlp4

Compiling with ocamlbuild and camlp5


I am trying to make a project I am working on compile with ocamlbuild, in order to avoid the use of a regular Makefile, which I find to be slightly more complicated.

Specifically, I have a syntax extension file (extend.ml), which I need to be compiled first. In a regular Makefile there would be a rule:

extend.cmo: extend.ml
    $(OCAMLC) -pp "camlp5o pa_extend.cmo q_MLast.cmo" -I +camlp5 -c $<

and then, for calculating the dependencies there would be a rule like this:

depend: $(MLFILES) extend.cmo
    $(OCAMLDEP) -pp "camlp5o ./extend.cmo"

Of course, the creation of any object file would require a similar rule to the one above.


My question is, how can I integrate these rules/requirements to one ocamlbuild command (if possible)?

I have tried to compile the extend.ml file first, and then use the following command:

ocamlbuild -pp "camlp5o ./extend.cmo" -I +camlp5 -use-menhir -no-hygiene Main.byte

but I don't think it's optimal in any way.

Unfortunately, I am not familiar with the use of ocamlbuild as a compilation tool, so any help will be much appreciated.


Solution

  • You could define two new tags, compile_extend and use_extend, that specify the expected options. In your myocamlbuild.ml file:

    open Ocamlbuild_plugin
    
    let my_flags () =
      flag ["ocaml"; "pp"; "compile_extend"]
        (S [A"camlp5o"; A "pa_extend.cmo"; A "q_MLast.cmo"]);
      flag ["ocaml"; "pp"; "use_extend"]
        (S [A"camlp5o"; A "extend.cmo"]);
      (* files with the use_extend flag must depend on extend.cmo *)      
      dep  ["ocaml"; "use_extend"] ["extend.cmo"];
       ()
    
    let () =
      dispatch (function
        | After_rules ->
          my_flags (); 
        | _ -> ())
    

    Then you would have a tags file with:

    "extend.cmo": compile_extend
    <Main.*>: use_extend
    

    That said, this is all bling guessing, I have not tested this setup. Could you provide a tarball with an example extend.ml file and Main.ml allowing to reproduce your situation?