1

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.

michalis-
  • 38
  • 1
  • 7
  • Could you provide a tarball with your code (or a simplified version thereof) to allow us to experiment to check that proposed solutions work? – gasche Oct 29 '15 at 15:33

1 Answers1

1

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?

gasche
  • 31,259
  • 3
  • 78
  • 100
  • Man, I wish this answer had came up sooner, it would have saved me a lot of trouble. Anyway, I came up with a similar solution myself about 1.5 month ago, but completely forgot to post a reply. Turned out, I needed an ocamlbuild plugin just like the one you are proposing. Thank you very much for your time. I am posting below links to my files, if you would like to see for yourself what did the trick. myocamlbuild.ml: https://www.dropbox.com/s/1dd66zl7zy52jsp/myocamlbuild.ml?dl=0 _tags file: https://www.dropbox.com/s/r0a5taua9rj9h5e/_tags?dl=0 – michalis- Oct 31 '15 at 14:08