Mojo rules
Mojo is the fifth family. mopm, the Mojo package manager, decides the units of a build; buildrules runs what mopm printed, as cached actions on a farm.
The split mirrors Cargo. Cargo decides the units of a build and buildrules runs rustc once per unit. For Mojo, mopm prints the unit graph and buildrules runs mopm unit build once per unit.
- mopm owns what "build Mojo" means. buildrules has no second Mojo driver.
buildrules mojo syncstores mopm's unit graph, byte for byte, as a committed table (buildrules.mojo.lock.bzl) besidemopm.toml, like the Cargo table.- Mojo compiles a whole package directory at once, so one unit is one package (and one test file or binary per leaf unit).
- A registry dependency is not compiled: it is a hash-pinned download of the package's prebuilt archive.
What you write #
load(":buildrules.mojo.bzl", "mojo_package")
mojo_package(path = ".")mojo_package(path = ".", table, natives = {}, test_data = {}, test_env = {}, test_defines = {}, test_layout = "default", test_hang_limit_s = {}, conda = False, visibility = ["PUBLIC"]). The generated buildrules.mojo.bzl binds table. natives maps a native library file name to the target that builds it, only when the unit graph names one, for example natives = {"libcnative.so.1": "//registry/cnative:libcnative"}. test_data gives a test the files it reads, {"<test file or *>": {"<staged path>": "<file or target>"}}: they are copied to a directory that is the working directory of the test and of its coverage run (named by BUILDRULES_TEST_DATA), a target is staged with its executable bit so a test can start a program another target built, and changing a staged file runs the test again. test_env = {"NAME": "value"} gives every test a variable. test_defines = {"NAME": value} (a string, a bool or an int) builds every test with -D NAME=value, in its run, its coverage run and its branch coverage run, and builds nothing else with it: the library, which every dependent imports, is built without them. Every test and every coverage run also has a TMPDIR, TEST_TMPDIR and HOME of its own, absolute and empty at the start, which test_env may not set. test_layout = "installed" runs each test from a fresh root instead, the way a program installed on a machine runs: the binary at <root>/bin/<exe>, the staged data under <root>/share, and <root> as the working directory (BUILDRULES_TEST_DATA then names <root>/share); the coverage run uses the same layout, and a value other than "default" and "installed" is refused. Every test run is also stopped as hung when it has not finished within 1800 seconds: the whole process group is killed, the status is 125, one line starting hang limit: says so, and the run is red like any failing test (no marker, so :lib is unbuildable). test_hang_limit_s = {"<test file or *>": seconds} raises the limit for the tests it names, a positive whole number of seconds: raise it for a test that is genuinely long, never lower it to make a test pass. A test never waits for a time to pass, so the limit is not an assertion about speed; a test that reaches it did not finish at all. conda = True adds :conda, the package as a registry would serve it (mopm pack of the gated :lib): a package that builds native libraries of its own has one without it. Its dependencies that are workspace members, children or siblings ({ path = "../b", version = "0.1" }; a path dependency without a version cannot be packed), are staged at their repository paths so that mopm pack reads their manifests, and the .conda holds the package alone and records each member as an ordinary versioned dependency. A path that leaves the directory, is staged twice, or collides with the coverage sources, and a variable the runner sets, are refused when the package loads. A workspace member (a path dependency) has a BUCK file of its own. buildrules mojo sync writes buildrules.mojo.bzl and the table; buildrules mojo check is the offline check. See the CLI reference.
| Target | What it is |
|---|---|
:lib | the package's .mojoc |
:test | every test file of the package, compiled and run |
:build | the library and the binaries named in mopm.toml |
:coverage | line coverage from the tests run under a pinned coverage tool; red if a source of the library is in no report. A package with no tests says so. |
:ci | :build and :test |
:check | the validation that every target above carries: the files the table was made from still hash to what it recorded, and toolchains/mojo_pins.bzl still holds the table's toolchain |
A test is two targets, :<stem>__bin (the compile, on any worker) and :<stem> (the run, on the worker of the target's CPU level).
Mojo versions #
A .mojoc built by one Mojo version does not load in another, so the Mojo version is part of the identity of a unit and of its cache key. You choose it in a mopm-toolchain.toml, found by walking up from the package, the way rust-toolchain.toml works. The file is required, there is no default, and exact versions only: a build without it fails at buildrules mojo sync, at buildrules mojo check and at analysis, names the file and prints this example. A Mojo version the pinned mopm cannot drive is refused by buildrules mojo sync; nothing falls back to another Mojo.
[toolchain]
mopm = "0.0.48"
mojo = "1.0.0"Pinning mopm today #
buildrules pins mopm like it pins Cargo: a version, URL, sha256 and size in rules/mopm/pins.bzl, and the mopm binary is an input of every unit action, so a mopm bump re-keys every unit on purpose. mopm = "N.N.N" in mopm-toolchain.toml chooses a version.
mopm 0.0.48 is released and pinned: rules/mopm/pins.bzl holds the URL, sha256 and size of its linux-x86_64 release tarball.
- To serve the download from a mirror, point it at a copy of the tarball with
url_mapin the.buckconfig.localof the cell that declares the download. The sha256 is still checked, so a mirror cannot change the bytes. - To pin a mopm of your own, add
[mopm."0.0.2"]withurl,sha256andsizetobuildrules-pins.tomland setmopm = "0.0.2"inmopm-toolchain.toml.buildrules mojo synccopies the table intotoolchains/mopm_pins.bzl, andtoolchains//:mopm_<version>is the target that builds it. - A version in neither place is refused by
buildrules mojo sync, and the message names the file and the keys to add. buildrules mojo sync --mopm PATHruns an executable you name instead.
What is checked #
- A change to
mopm.toml,mopm.lock,mopm-toolchain.tomlor a source without a sync makesbuildrules mojo checkred. - A change to
mopm.toml,mopm.lock,mopm-toolchain.tomlortoolchains/mojo_pins.bzlwithout a sync fails the build too: every step ofmojo_packagecarries a:checkvalidation.
See the rules reference for the other families.