Scrap your Camlp4

ppx has been out for a while but it seems like the community has been taking its time transitioning away from camlp4. There’s probably a couple of reasons for that:

  1. Only the very latest version of the compiler (4.02.3) works with the most common ppx’s.

  2. People aren’t aware how easy it is to convert your code with JSC’s excellent camlp4-to-ppx

The purpose of this post is to help out with the 2nd point a with a lightning fast walk through of how to do use it:

First, let’s get the code and install it:

$ git clone https://github.com/janestreet/camlp4-to-ppx && cd camlp4-to-ppx && make

This should yield us a small executable that will convert a camlp4 source file to ppx:

$ ./main.native
Usage: main.native FILE

All that remains is gluing together a small driver to run this on your project:

#!/bin/bash

# Make sure to set this correctly
p4toppx="/path/to/camlp4-to-ppx/main.native"

# If you're on OSX, install gnu sed with $ brew install gnu-sed
# s/gsed/sed/ on the next line if you're on linux
ppxify() { gsed -ni r<($p4toppx $1) $1; }

# you can tweak how you select your source files
files=$(git ls-files '*.ml*' | grep '\.mli\?$')
for f in $files; do ppxify $f; done

Copy and tweak the script above and you can test it out:

$ https://github.com/mirage/ocaml-cohttp
$ cd ocaml-cohttp
$ ./ppxify.sh

Now go on and upgrade your package to use ppx. Here’s to not dealing with camlp4 in 2016.

Comments

comments powered by Disqus