Directly compiling and using ppx_deriving

I’ve grown used to tools like ppx_inline_test and ppx_show in my OCaml code built by Dune; and I’m currently headed down the road of integrating them into my BuckleScript-to-JavaScript pipeline. (Lord, how I miss Dune!)

However, at the moment, I can’t even get a simple hand-built ppx driver working! Can anybody walk this newbie thru what he’s doing wrong?

$ cat
type foo = A of int | B of float
[@@deriving show]

$ ocamlfind opt -predicates ppx_driver -linkpkg -linkall -o ppx_driver \
    -package -package ppx_deriving
findlib: [WARNING] Interface topdirs.cmi occurs in several directories: /Users/ec/Documents/Code/Source/ppx_deriving/_opam/lib/ocaml/compiler-libs, /Users/ec/Documents/Code/Source/ppx_deriving/_opam/lib/ocaml

$ ocamlc -c -ppx "./ppx_driver -as-ppx"
File "", line 1:
Error: External preprocessor does not produce a valid file
Command line: ./ppx_driver -as-ppx '/var/folders/xt/wm01x2h50nv993t_csb1t5qm0000gn/T/camlppx7ee45a' '/var/folders/xt/wm01x2h50nv993t_csb1t5qm0000gn/T/camlppx3eb5cb'

Trying ./ppx_driver -help produces no output, nor any errors … in fact, no input, files, or stdin seems to cause it to do anything whatsoever.

Please help me learn how ppx-drivers work! D:

Check this out. That lets you do ./ppx to view the preprocessed version of a source file.

To actually integrate it into a build, if you’re using BuckleScript and bsb, this is one way:

  1. Compile ppx_deriving with BS. You can do this by forking it, adding a bsconfig.json, then doing bsb -make-world.
  2. In your project, npm install your repository and add its name (check its package.json) to bsconfig.json under bs-dependencies. This makes ppx_deriving’s runtime library available.
  3. opam install ppx_deriving on a BS-compatible switch (4.02.3 last time I tried this).
  4. Tell BS to call the opam-installed driver. Alternatively, you can try to get the driver into node_modules somehow.

This worked for me a few months ago. I don’t know if the Reason community has since come up with a better way to use ppx preprocessors.