Rule without target? command to run after every successful build

Problem:

  1. I have dune build -w running

  2. I have a directory proj/my_data with:

proj/my_data/dune
proj/my_data/foo.ml
proj/my_data/bar.ml

  1. On every successful compile of proj/my_data from dune build -w, I want a program to run, generate bindings at proj/run_out/*.rs

  2. The first thing I want to try is dune rules: Stanza Reference — Dune documentation .

  3. However, dune rules wants to specify an in directory target. I want to specify an out of source directory.

===

What is the simplest way to specify: on every successful build, run this external program.

I think that’s what the universe dependency is for (cf the specification of dependencies ).

If the program outputs a file run_out/…, you can create a rule which have this file as a target (with the experimental dir stanza), the program.exe as a dependency, and an action which launch the program.exe.

Have I missed something ?

If you would have no real target, invent one, inside the action, put (progn (run ./program.exe) (run touch dummy_target)) where dummy_target is a target you have declared. The touch command create the file and make dune happy.

1 Like

Link describing Frederic_Loyer’s solution: Stanza Reference — Dune documentation.


Here’s another approach: If it is acceptable to have a dune file in your target directory, you could do that and stick your rule there. Some setup like this:

proj_root
  - dune-project
  - my_data
    - dune <= has your gen_rs.exe spec
    - gen_rs.ml <= generates your rust files
    - ... other data/ml files needed ...
  - run_out
    - dune <= has the rule to generate rs files which will be placed here

I could imagine a proj_root/run_out/dune file looking like this:

(rule
 (targets foo.rs bar.rs)
 (mode
  (promote (until-clean)))
 (deps ../my_data/gen_rs.exe)
 (action
  (run ../my_data/gen_rs.exe)))

You will need to specify individual rs outfiles in this dune file, however, which could be a pain if you have many generated.

On dune build then rs files will be generated, and on dune clean they will be eliminated.