How-To: Buck 2 and OCaml - Build system

I’ve just made a repo containing examples on how to use Buck 2 with OCaml using the ocaml-scripts:

OCaml-Buck-2-Examples - GitHub
The example projects do also contain Dune files, so you can compare them.

All questions, suggestions and corrections are welcome, also for Meta’s OCaml scrips.

Why Buck 2 and not Bazel?

First: I have never tried Bazel (or the OCaml support), so I cannot say anything about it.
I’ve chosen Buck 2 because OCaml support is included by Facebook itself, the Prelude contains all language rules of Buck 2, there are no “build in” and “external” languages as with Bazel. Buck 2 is written in Rust (which I use, so no extra tooling needed to build it), Bazel in Java. And Bazel is by Google.

But I would just look at the support of each for the languages you want to/need to/must use.

3 Likes

Very cool.

Evidently, a couple of the creators of Buck2 gave a talk about this subject at ICFP this year.

The slide deck for the talk is available there here.

Hopefully we’ll see a video of the presentation on YouTube soon as well. :crossed_fingers:

Update: I see you’ve linked to the talk from your README, but having the link here is probably good nonetheless.

Update: The slides were actually posted elsewhere, linked above.

1 Like

Yes, this is a good idea.
The official OCaml examples (which do not use Opam packages/the ocaml-scripts) which this talk is about are located in the Buck 2 repository, in the examples subdirectory OCaml examples.

Btw. Neil Mitchell is the person behind
Shake and the Build Systems a la Carte paper.

I’ve just added examples on how to use Alcotest (PPX) inline tests with Buck 2, see the README and the new examples in Inline Test Runners

The ppx_inline_test inline test runner (for ppx_expect too) should work the same, the only problem will be passing the library to the runner.

Btw. since the last post I’ve also added examples on how to use Buck with the vcpkg package manger and the Conan 1 package manager for C++.

Hi @Release-Candidate I haven’t used Buck, but what do you gain from using it vs dune ?

If you are just building OCaml locally: not much, you mainly lose the hidden magic Dune does (for example with modules, PPX, inline test runners, …) - if this is an advantage or disadvantage of Buck 2, you must decide for yourself ;). And if you do not use Dune, you need a .merlin file for your project’s sources, for Merlin and/or the OCaml LSP: Project configuration · ocaml/merlin Wiki · GitHub

The main advantages of Buck 2: support for many (not as much out of the box yet as Bazel) languages and distributed builds. I’m (officially :wink: mainly a C and C++ developer, and have used Makefiles (POSIX, BSD, GNU, NMake), Autoconf/Automake, IMake, CMake, QMake and some others I’ve forgotten about. I also have a bit of Typescript for some Websites and Rust and Python to build.

All supported languages (everybody can add language support there) are in the Buck 2 Prelude

Buck 2 uses the Remote Execution Protocol (like Bazel), so you can use for example use
BuildBuddy - Buck 2 BuildBuddy Examples
or
Buildbarn - Buck 2 Buildbarn Exmples
for distributed builds.

Information about Bazel: OBazl Toolsuite - tools for building OCaml with Bazel

2 Likes

OBazl developer here. FYI I’m in the middle of prepping a new release with updated docs.

Added a chapter on how to generate the Merlin files: Using Buck 2 with OCaml-LSP or Merlin