with apologies

`just` OCaml

Richard Mortier · February 07, 2025 · #tech #code #ocaml

In similar vein to a recent post, I have also started using just when I periodically need to rebuild my OCaml tool1 ocal. So I ended up replacing the old Makefile with a shiny new Justfile.

As it also proved useful in another (more esoteric) tool I wrote for parsing out exam results for my students so I can paste into email easily, I thought I’d put it here for the record. So here it is…

1

Largely due to NixOS upgrades moving tools into different locations.

Usual preamble of course:

_default:
    @just --list

Then set some common variables:

PWD := env("PWD")
DOCDIR := "_build/default/_doc/_html"
BUILDDIR := "_build/install/default/bin"

Then set the target — the tool name, in this case ocal (so named as this is an OCaml re-implementation of a tool approximating the trad Unix cal tool):

TARGET := "ocal"

Now for the actually useful stuff: some targets. Mostly these just call out to dune but in a way I find more intuitive.

# build targets
build:
    dune build @all

# cleanup
clean:
    dune clean

# uninstall targets
uninstall:
    dune uninstall

# run any tests
test:
    dune runtest

# format sources
format:
    dune fmt

Some compound calls next.

First, before building we might need to install dependencies, so do so in the time-honoured fashion:

# install dependencies
depends:
    opam install --yes dune-release odoc
    opam install --yes . --deps-only

Next, to install I first build ready to install, then symlink the resulting binary into the right place in my home directory:

# install targets
install: build
    dune build @install
    ln -sf {{PWD}}/{{BUILDDIR}}/{{TARGET}} ~/.local/bin/

To lint all the things, invoke dune twice:

# lint everything
lint:
    dune build @lint
    dune-release lint

Similarly, to build the docs, build all the docs:

# build docs
doc:
    dune build @doc
    dune build @doc-private

Try to open the docs on Linux and if that fails, on MacOS:

# open the docs for reading
read: doc
    handlr open {{DOCDIR}}/index.html || open {{DOCDIR}}

Finally, tag and create a release; not actually done this in ages so no idea if dune-release invocations are still a thing, let alone correct!

# tag and create a release
release:
    dune-release tag
    dune-release -vv