I’m inordinately pleased to announce that a Proof-of-Concept Bazel build of the OCaml compilers and tools (latest trunk version) is available for testing and exploration. Tested on MacOS 12.6 and Ubuntu 20.04.5 LTS. It is available at
This uses a stripped-down version of rules_ocaml, embedded in the repo in subdirectory bzl
.
Currently the following targets build and run: ocamlc, ocamlopt.byte, ocamllex, all of the tools in the tools/
subdirectory.
Supports Zig as an alternate C toolchain and as a cross-compiler. I was also able to use the LLVM toolchain at GitHub - grailbio/bazel-toolchain: LLVM toolchain for bazel, but that was months ago and I have not tested it with this new version. But the code is in the WORKSPACE.bazel file, so getting it to work would be a good starter project.
Cross-compilation (using Zig) is supported for the C code. Full support is going to take some more work. For example, to compile a linux runtime on a mac:
$ bazel build boot/bin:ocamlrun --config=mac_linuxamd64
It uses some interesting Bazel features. For example, it uses platforms and toolchains to support the various compilers - instead of building ocamlc.byte or ocamlopt.byte, you build one target (boot/compiler) and pass CLI args telling Bazel which platform (vm or sys) should host the build compiler and which should be the target. Similarly, debug and instrumented variants are controlled by parameters rather than separate targets. It also supports fine-grained control of compile/link flags, so developers should be able to optimize for diffferent scenarios. You can tell it whether or not to compile .mli files separately. You can pass a custom primitives
file for use with -use-prims
and it will be used everywhere. Etc.
It’s far from polished but I think it works well enough for people to do a little testing and exploration.
Even if you have no interest in Bazel you may find the notes in the bzl/docs subdir worth looking at. I spent a lot of time studying the Makefiles and trying to understand the build, and took pretty extensive notes. They’re not very well organized but they have a lot of info, I think.
Feedback welcome. I’m not sure how far I’ll go with this, but I at least want to get complete cross-compilation working, and I’d also really like to get persistent workers going.
Enjoy!
Gregg