Using jbuilder to run protoc
I’m starting on a new Ocaml project, and I wanted to use protobuffers
as my serialization format. ocaml-protoc
seems like a fairly
well-adopted implementation of a protobuf compiler for ocaml, but
figuring out how to get it to run automatically as part of a
jbuilder
build was not particularly well documented.
Jbuilder behaves somewhat like make (and ironically uses a lisp-ish
configuration language). Given this, I made a rule whose output would
be the generated .ml
and .mli
files, and required those resulting
files to be installed (causing them to get generated at build time).
I added these two stanzas to my jbuild
file:
(rule
((targets (messages_types.mli messages_types.ml
messages_pb.mli messages_pb.ml))
(deps (messages.proto))
(action
(run ocaml-protoc -binary -ml_out ./ ${<}))))
(install
((section etc)
(files (messages_*.mli messages_*.ml))))
I’m still getting used to jbuilder, so I’m sure there’s less repetitive ways of getting this accomplished. If you have any suggestions, let me know!