/rules_ocaml

OCaml build rules for Bazel

Primary LanguageStarlarkApache License 2.0Apache-2.0

OCaml rules for Bazel

Rules

Overview

Build OCaml with Bazel. Very experimental.

Setup

Add the following to your WORKSPACE file.

git_repository(
    name = "io_bazel_rules_ocaml",
    remote = "https://github.com/jin/rules_ocaml.git",
    commit = "7a0a6e5226af5f09eb6e3379b901d8f2ffdb8bbf",
)

load("@io_bazel_rules_ocaml//ocaml:repo.bzl", "ocaml_repositories")
ocaml_repositories(
    opam_packages = {
        # Put your OPAM dependencies here
        "lwt": "3.1.0",
        "yojson": "1.4.0",
    },
)

and this to your BUILD files.

load("@io_bazel_rules_ocaml//ocaml:ocaml.bzl", "ocaml_native_binary", "ocaml_bytecode_binary", "ocaml_interface")

ocaml_repositories() will download and compile the OCaml toolchain, including ocamlbuild, ocamlfind and OPAM itself, as the official source doesn't release binaries directly and requires the use of OPAM for installations.

The initial load time of toolchain compilation will take a few minutes and will not happen again for the same project.

Rules

ocaml_native_binary/ocaml_bytecode_binary

Generates a native binary using ocamlopt or bytecode binary using ocamlc.

ocaml_native_library(name, srcs, src_root, opam_packages)
ocaml_bytecode_library(name, srcs, src_root, opam_packages)

Example

ocaml_native_binary(
    name = "hello_world",
    srcs = glob(["examples/*.ml"]),
    src_root = "examples/hello_world.ml",
    opam_packages = ["yojson", "lwt"],
)

ocaml_bytecode_binary(
    name = "other_binary",
    srcs = [
      "examples/foo.ml",
      "examples/bar.ml",
      "examples/entry.ml",
    ],
    src_root = "examples/entry.ml",
    opam_packages = ["pkg_foo", "pkg_bar"],
)
Attributes
name

Name, required

A unique name for this target

srcs

List of labels, required

List of OCaml .ml source files used to build the library

src_root

Label, optional

The OCaml .ml source file used for the binary's entry point.

Defaults to main.ml if not specified.

opam_packages

List of strings, optional

The name of the OPAM package dependencies required by this binary.

The packages (and their versions) must already be defined in your WORKSPACE file's ocaml_repositories().

ocaml_interface

Generates a .mli file of the source file.

ocaml_interface(name, src)
Attributes
name

Name, required

A unique name for this target

src

Label

The OCaml .ml source file used for generating the interface file

Example

ocaml_interface(
    name = "hello_world_interface",
    src = "examples/hello_world.ml",
)

Projects using rules_ocaml