Run `ocamlc -i` with `dune`

How can we run ocamlc -i on a module that is supposed to be compiled with dune (with local and non-local libraries and possibly other things that make manual compilation outside dune non-trivial)?

Ideally, I would like to run the same command that dune uses to compile the module, but with the option -i. (I could copy-paste the output of dune --verbose and add the option manually, but there is probably a more convenient way!)

2 Likes

Is using cmitomli on the cmi file produced by dune not an option?

1 Like

Yes, it can work, thanks for the suggestion! If no one proposes a more direct solution (for instance, a solution that does not require to go manually in _build/default/*/*.objs/byte/), I will accept yours!

You can write a custom rule to have Dune run cmitomli for you:

(rule
 (alias make_mli)
 (mode promote)
 (action
  (with-stdout-to mysig.sig 
   (run cmitomli %{cmi:path_to_module/wihout_extension}))))

And then run dune @make_mli.
There is a lot of improvement that can be made and it probably need some adaptation…

The rule documentation: Stanza Reference — dune documentation
Actions documentation: General Concepts — dune documentation
Specific variables for artifacts: Other Topics — dune documentation

1 Like

Relevant previous discussion: How can I generate .mli files from .ml files?

1 Like

It’s a bit of a shame we haven’t solved this in dune yet. There are problems in dune that make adding this feature not entirely trivial, but we should really fix the engine to accommodate features like this.

2 Likes

Thank you all for your suggestions and pointers! Since cmitomli is the state of the art, I accept @octachron’s solution, but, yes, that would be great if dune could do this more conveniently!