ocaml / dune

A composable build system for OCaml.
https://dune.build/
MIT License
1.61k stars 400 forks source link

[RFC] Anonymous actions with targets #4417

Open ghost opened 3 years ago

ghost commented 3 years ago

This RFC proposes to add a variant of Action_builder.action where the action produces some files.

Why would we want that?

After all, if an action produces files, then it is not really anonymous.

The goal is to avoid the ppx driver hash table in preprocessing.ml. We generate a ppx driver on-demand for a given set of ppx rewriters. We generate it at: _build/<context>/.ppx/<digest-of-ppx-rewriter-set>/ppx.exe.

At the point where we need this driver, it's simple: we just compute this path and depend on it. At the point where we need to produce the rule for it however, things are a bit more tedious as we need to reverse <digest-of-ppx-rewriter-set> into the actual set of ppx rewriter set. And reverting a MD5 digest is kinda hard.

To cope with that, we maintain a reverse table mapping digests to ppx rewriter sets, and whenever we compute the digest of a ppx rewriter set we fill this reverse table. In practice, the code filling the table is always executed before the code accessing it, so we are good.

However, now that we have anonymous actions, we could use that rather than using this trick.

How would that work?

Whenever we need a ppx driver, we would call a memoized function "give me the driver for this ppx set", and this function would produce the driver via an anonymous action with the driver as target. Eliminating the need for the reverse table entirely.

Implementation

Just like anonymous actions, except that we would use the actual targets of the rule to uniquely identify it, just as we do for normal rules.

ghost commented 3 years ago

BTW, this would be useful for other systems that generate binaries on-demand, such as cinaps or mdx.