Using camlp5 from dune?

Besides copying/pasting from https://github.com/Kakadu/OCanren/blob/dune/camlp5/dune , is there another resouce on how to use camlp5 from dune ? I’m looking for a from first principle line by line explaination of how to setup camlp5 in dune.

Thanks!

Maybe @Chet_Murthy could help you.

I think your question can be broken down into three parts:

  1. If I know which Camlp5 packages I want to use during preprocessing, how do I invoke them?

The method OCanRen uses, is to build a preprocessor executable (using “mkcamlp5”) and then use that to preprocess files. My preference is to just use “not-ocamlfind preprocess” to do the same.

You can find the only example (b/c I’m gonna use dune, thank very much but Makefiles are much better) here: https://github.com/chetmurthy/ppx_jsobject_conv/blob/experimental-parsetree-quotation-hacking/src2/dune

This project has two copies of the code: one that uses the standard PPX infra (src) and one that uses Camlp5-based pa_ppx infra (src2).

Now, the thing is, dune doesn’t know how to apply preprocessors except for the standard PPX infra. So you have to pick a filename prefix (say, .ML) and then you write a rule to transform (e.g.) code.orig.mlm into code.ml. Also, that preprocessor line

not-ocamlfind preprocess -package fmt,unix,camlp5,ounit2,pa_ppx.utils,camlp5.pr_dump -package pa_ppx.deriving_plugins.std,pa_ppx_parsetree.quotations_413,pa_ppx_quotation2extension -syntax camlp5o  %{src}

uses a bunch of Camlp5 packages, and its output is an OCaml standard-format serialized AST. So when dune gets ahold of it, it will have the right line-numbers from the original source. But sometimes the macro-preprocessing messes up the line-numbers, so you want code.ml to be actual OCaml source. In that case, you can remove “camlp5.pr_dump” add the package “camlp5.pr_o” to the list of packages. [“pr_dump” dumps the final AST in OCaml form; “pr_o” prints it in official syntax]

OK, so that pretty much covers how to use existing camlp5 packages.

  1. How do I create new Camlp5-based preprocessor packages?

This … well, this is what this project does, and it does it using dune for builds, not Makefiles.

  1. How do I create new Camlp5-based preprocessor packages in the same Dune project where I use them ?

This is different from #2, b/c in #2, the idea is in one project I create a new preprocessor, install it as findlib packages and then in another project I use that preprocessor. But internally, Dune doesn’t use findlib packages as their “library” structuring mechanism, so I have no idea how I would do this.

I think OCanRen does it somehow, but hey, I’m not gonna look, b/c with Makefiles, it’s easy-peasy to identify subsidiary libraries of a project with findlib packages.

This is very relevant b/c if you write a Camlp5-based preprocessor in a dune-based project, and want to write tests in the same project that invoke it, I don’t know how to do so.

Hope that helps.

P.S. I continue to believe it was a colossal mistake for dune to not map “dune library” to “findlib package”. Ah, well.