ocamlbuild, a tool for automatic compilation of OCaml projects

Warning: this presentation has a degraded style compared to the Beamer/PDF version


Why such a tool?

What does ocamlbuild handle?

Regular OCaml projects of arbitrary size Trivially handled using the command line options.
Mostly regular OCaml projects with common exceptions Requires writing one tag file (_tags) that declares those exceptions.
Almost any project Accomplished by writing an ocamlbuild plugin.

What does ocamlbuild provide?

  • Automated whole-project compilation
  • Minimal recompilation
  • Lots of useful targets (doc, debugging, profiling...)
  • Supports multiple build directories
  • Automatic and safe cleaning
  • A source directory uncluttered by object files
  • A portable tool shipped with OCaml
  • Regular OCaml projects

    What's a regular OCaml project?

    How difficult is it to build regular projects by hand?

    How does ocamlbuild manage all that?

    It has a lot of hand-crafted Ocaml-specific compilation logic!


    To be fair... Some of these projects require that a version.ml or stdlib.ml file be generated beforehand.

    Dealing with exceptions to standard rules

    What's an exception?

    Make and exceptions

    The tags, our way to specify exceptions

    "funny.ml":           rectypes
    <**/*.ml*>:           warn_A, warn_error_A, debug, dtypes
    <**/*.cmx>:           inline(9)
    "foo.ml" or "bar.ml": warn_v, warn_error_v
    "vendor.ml":          -warn_A, -warn_error_A
    <main.{byte,native}>: use_unix
    "main.byte":          use_dynlink, linkall
    "test":               not_hygienic
    <satsolver.cm[io]>:   precious

    How tags and rules give commands

    Files are tagged using tagging rules
    "foo/bar.ml": rectypes
    Rules then produce commands with tagged holes
    let tagged_hole =
      tags_for(ml)++"ocaml"++"compile"++"byte" in
    Cmd(S[A"ocamlc";A"-c";T tagged_hole;P ml;A"-o";P cmo])
    These holes are filled by command fragments (such as flags)
    flag ["ocaml"; "compile"; "byte"; "rectypes"]

    Tags and dependencies

    One can define dependencies triggered by combinations of tags
    dep ["ocaml"; "link"; "byte"; "program"; "plugin:foo"]
        ["plugin/pluginlib.cma"; "plugin/plugin_foo.cmo"]
    By tagging files we make things happen
    "test.byte": plugin:foo

    Writing an ocamlbuild plugin

    Not a specific language, but plain OCaml code

    A plugin example

    Let's read it in live...

    General features

    Parallel execution where applicable

    A status bar for your visual comfort

    Hygiene and sterilization

    ocamlbuild has a Hygiene Squad (HS) that checks your source tree for cleanliness
    HS can be told of exceptions Files or directories tagged as not_hygienic or precious.

    Some supported tools