rgrinberg
rgrinberg

Reputation: 9878

Ocamlbuild copy over dependencies to _build

I'm trying to use ocamlbuild to automatically invoke piqi to generate ocaml modules from protobuf definitions. This is a 2 step process where I have to go from a %.protobuf file to a %.proto.piqi and finally to %_piqi.ml.

My rule for the first step looks as follows:

rule "piqi: .proto -> .piqi"
  ~dep:"%.proto"
  ~prod:"%.proto.piqi"
  begin fun env build ->
    Cmd (S [ A piqi ; A "of-proto"
           ; A "-I"; P protobuf_include
           ; P (env "%.proto") ]
        )
  end;

But this doesn't work because the %.proto.piqi target is actually dependent on all the "*.proto" files in my source directory because the individual .proto files import each other in the source. However, I'm not sure how to express this dependency between them in ocamlbuild. It would be enough if all of the proto files where copied over to _build rather than just the one in ~dep

Upvotes: 2

Views: 115

Answers (1)

gasche
gasche

Reputation: 31459

Calling build on a file from inside a rule action will register it as a dynamic dependency of the target. You can just loop over all *.proto files that you know may be accessed by the current one (either globbing the source directory or, more cleverly if that gives any benefit, parsing the include statements) and build them.

(Another way to think of this solution is to remark that, if you wanted to have some foo.proto file generated by some preprocessing step from a foo.proto.pp, then you would need any compilation needing foo.proto to actually call build on it.)

See the dynamic dependencies section of the new manual draft.

P.S.: I don't know anything about protobuf but from protoc --help it looks like protoc --include-imports --descriptor_set_out FILE may give you the list of a .proto dependencies in some format. Parse that and call build on all of them, and you've got a nice and robust rule.

Upvotes: 1

Related Questions