Examples by language
One section per language, each with the commands and the files. Part of how to use buildrules. Replace the origin and commit as in initialize your repository.
beta/ in the repository, with the commands in each STEPS file. The CPU-level and hand-written Rust examples are built from examples/ by //ci:matrix.Rust #
buildrules init . --git <origin> --commit <commit>
buildrules cargo new crates/hello
buildrules sync
buildrules check
buck2 build //crates/hello:bin //crates/hello:test //crates/hello:lintPer member the targets are :lib, :bin (if it has a binary), :debug, :release, :test (one .passed marker per Cargo test target), :lint, :typecheck (the pinned rustc to metadata only, as cargo check; no clippy), :fmt (the pinned rustfmt --check), :flycheck (the lint as diagnostics, for an editor), :sources, and one per action named in the policy. A member's BUCK file is two lines:
load("//:buildrules.bzl", "cargo_package")
cargo_package(path = ".")For an existing package that already builds with Cargo, buildrules cargo adopt crates/existing adopts it (no path means every member).
cargo test still runs them; :test and :coverage do not), alternate-registry dependencies, git dependencies not pinned by commit or not on github.com, a root [package].After editing Cargo.toml or Cargo.lock #
buck2 build //:sync --out .The one command, run where the farm builds; nothing runs cargo on your machine. It writes into the repository every file the sync changed (Cargo.lock too, if Cargo had to change it): commit them. buck2 build //:sync_check is red until the committed files are what a sync writes. The two targets are one cargo_sync call in the root BUCK (the macro is in @buildrules//rules/rust:sync.bzl), with the workspace files as srcs, every member's :sources and //third_party/buildrules:files as deps, and the channel of rust-toolchain.toml as rustc.
The action has no network. Every crates.io package of Cargo.lock is downloaded by the sha256 Cargo.lock records and every git commit as GitHub's archive by the sha256 and size third_party/buildrules/git_archives.tsv records; a file whose bytes are not the pinned ones is refused, naming the package.
Cargo.lock already names: a crate or a git commit the lock does not name yet is refused by name, and buildrules sync on a machine with a network adds it once. A file the sync removed is named in the build's output, to be deleted by hand.A stale table in a pull request #
buck2 build //:sync_patch --out DIREvery workspace root also has :sync_patch: the same sync on the farm, whose output says what it changed. DIR/verdict.txt is none, tables (only the generated files are stale, so the compile errors of a build that reads the table follow from it) or lock (cargo changed Cargo.lock too), then one line per file; DIR/offer/sync.patch is the change as a patch git apply takes. The table check of a build says STALE TABLE ONLY after the files it names. A repository whose farm lane runs on GitHub Actions can add three steps that build it when the farm build of a pull request fails, upload offer/ as an artifact and write the two commands that take it (gh run download, git apply) into the job summary; the patch is offered, never pushed, and the job stays red. The steps are in docs/how.md.
rust-analyzer #
buildrules rust-project developThis builds what rust-analyzer needs that only a build makes (registry sources, build-script output, proc-macros, the pinned standard library and rust-analyzer itself) where actions run, downloads it under buck-out/buildrules/ra, and writes rust-project.json at the root. Nothing runs a rustc or cargo of your machine. Point the editor's rust-analyzer at buck-out/buildrules/ra/sysroot/bin/rust-analyzer, the pinned one whose proc-macro server matches the compiler that built the proc-macros; it finds rust-project.json at the root. Its check on save is buildrules rust-project check, which builds the member's :flycheck. The file holds this machine's absolute paths: add it to .gitignore, and run develop again after Cargo.lock changes or buck2 clean. See the command.
Node.js #
buildrules init . --git <origin> --commit <commit>
buildrules npm sync web
buildrules npm check web
buck2 build //web:ci //web:dist{
"name": "hello-node",
"version": "1.0.0",
"private": true,
"scripts": { "test": "node test.js", "build": "node build.js" },
"buildrules": { "actions": { "ci": ["test", "build"] }, "dist": "dist" }
}web/package-lock.json is npm's own (lockfile version 3). npm sync writes buildrules.lock.bzl and buildrules.bzl beside it and, if absent, a BUCK file:
load(":buildrules.bzl", "npm_package")
npm_package(path = ".")The targets are :node_modules, :typecheck, :lint, :test, :build, :dist, :check, and one per action. npm ci && npm test && npm run build keeps working outside Buck2. Install scripts do not run, and scripts that use pipes, redirection, npm or npx are refused with the fix. The first npm sync downloads each locked tarball once to learn its sha256; --seed FILE reuses a table's hashes to skip that. Other npm_package arguments: node (a node_dist of your own), step_data, step_tools, step_env, srcs, exclude, data, visibility.
Zig #
There is no sync step. Write the rules by hand:
load("@buildrules//rules/zig:zig.bzl", "zig_exe", "zig_test")
zig_test(
name = "hello_unit",
src = "hello_test.zig",
imports = ["hello.zig"],
)
zig_exe(
name = "hello",
src = "main.zig",
imports = ["hello.zig"],
unit_tests = [":hello_unit"],
)buck2 build //:helloThe executable is not built unless its unit tests pass. The rules use Zig 0.12.0 by default; for 0.15.2 add zig = "buildrules//rules/pins:zig_next" to both rules. zig_exe takes optimize (default ReleaseSafe), strip (default true) and target (default x86_64-linux-musl). zig_module shares code between targets.
C and C++ #
Add the C/C++ toolchain to toolchains/BUCK; init writes only Rust:
load("@buildrules//rules/cxx:toolchain.bzl", "cxx_toolchains")
load("@buildrules//rules/rust:toolchain.bzl", "rust_toolchains")
load(":rust_pins.bzl", "EXTRA_RUST_VERSIONS")
rust_toolchains(extra = EXTRA_RUST_VERSIONS)
cxx_toolchains()Then use the prelude's rules with no load:
cxx_binary(
name = "hello_c",
srcs = ["hello.c"],
compiler_flags = ["-O2", "-Wall", "-Werror"],
)buck2 build //:hello_cThere is no host compiler: the compiler is zig cc of the pinned Zig, and nm, objcopy, strip and the prelude's Python tools are refusals that name the tool. To prove a binary independent of its build directory, add hermetic_check(name = "hermetic_c", exe = ":hello_c") from @buildrules//rules/cxx:defs.bzl; to prove its glibc needs, glibc_floor_check. To follow the platform's CPU level pass cxx_toolchains(target_cpu = zig_cpu()) with zig_cpu from @buildrules//rules:isa.bzl.
Mojo #
A Mojo package is a mopm project. The layout, from the beta/mojo repository:
mopm-toolchain.toml
toolchains/BUCK rust, cxx, mopm and mojo toolchains (see below)
hello/mopm.toml [package] name, version, mojo; [targets] lib, tests
hello/mopm.lock written by `mopm lock`
hello/BUCK the two lines below
hello/src/hello/__init__.mojo
hello/tests/test_hello.mojoload(":buildrules.mojo.bzl", "mojo_package")
mojo_package(path = ".")load("@buildrules//rules:isa.bzl", "zig_cpu")
load("@buildrules//rules/cxx:toolchain.bzl", "cxx_toolchains")
load("@buildrules//rules/mojo:toolchain.bzl", "mojo_toolchains")
load("@buildrules//rules/mopm:toolchain.bzl", "mopm_toolchains")
load("@buildrules//rules/rust:toolchain.bzl", "rust_toolchains")
load(":mojo_pins.bzl", "MOJO_TOOLCHAINS")
load(":mopm_pins.bzl", "EXTRA_MOPM_VERSIONS")
load(":rust_pins.bzl", "EXTRA_RUST_VERSIONS")
rust_toolchains(extra = EXTRA_RUST_VERSIONS)
cxx_toolchains(target_cpu = zig_cpu())
mopm_toolchains(extra = EXTRA_MOPM_VERSIONS)
mojo_toolchains(MOJO_TOOLCHAINS)mopm lock # mopm's own: mopm.toml -> mopm.lock (needs the package registry)
buildrules mojo sync # mopm's unit graph -> buildrules.mojo.lock.bzl
buck2 build //hello:ci //hello:coverage
buildrules mojo check # offline gate; `buildrules check` also runs itThe targets are :lib (the .mojoc), :test and one :<stem> per test file, :build (library and binaries), :coverage (line coverage under kcov and branch coverage from the compiler's own IR, with a coverage_floor.txt that holds both; red if a library source is in no report), :ci and :check. A workspace member is a package with its own BUCK file. Native libraries are declared with natives = {"libx.so.1": "//path:target"}. mopm is separate software: see mopm.dev for its install and the registry.
rules/mopm/pins.bzl. A row marked release_pending is a stand-in: its sha256 and size are those of a development build, not of the published mopm release at the same URL, so the download fails the hash check, a fresh clone cannot run this flow end to end, and the beta/mojo leg is skipped. A row without that mark is the published release, and the leg runs. The file shows which it is. Against a stand-in, point the download at a copy of its tarball with buildrules.url_map; with either, you can pin a different mopm version you supply yourself in buildrules-pins.toml.[mopm."0.0.2"] # a version buildrules does not pin; one it pins (0.0.48 today) is refused here
url = "<release url>"
sha256 = "<64 hex>"
size = <bytes>Mojo builds with the one Mojo version that mopm pins: one Mojo version per repository.
Several languages in one repository #
beta/mixed is a Rust package, a C program, a Zig program and a Node package in one repository:
buildrules init . --git <origin> --commit <commit>
buildrules cargo new crates/hello
buildrules sync
buildrules npm sync web
buildrules check
buildrules npm check web
buck2 build //crates/hello:bin //crates/hello:test //native/c:hello_c //native/zig:hello //web:ci //web:distbuildrules check runs the Cargo checks (and the Mojo checks if there are Mojo packages); npm check covers each npm package.
CPU levels #
examples/isa builds Rust and C at an instruction-set level and reads the machine code:
buck2 build --target-platforms buildrules//platforms:linux_x86_64_v3 \
//examples/isa:audit //examples/isa:audit_c //examples/isa:isa_lib_testisa_audit fails unless the binary holds ymm (256-bit AVX2) instructions at v3 or v4 and none below. The test runs on a worker that reports ISA=x86-64-v3.
Hand-written Rust rules #
For a crate described without Cargo, use rust_library, rust_binary, rust_test and run_check:
load("@buildrules//rules/rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
load("@buildrules//rules/core:run_check.bzl", "run_check")
rust_test(name = "hello_lib_test", srcs = ["src/lib.rs"], crate_root = "src/lib.rs", crate_name = "hello_lib")
rust_library(name = "hello_lib", srcs = ["src/lib.rs"], crate_root = "src/lib.rs", gate_tests = [":hello_lib_test"])
rust_binary(name = "hello", srcs = ["src/main.rs"], crate_root = "src/main.rs", deps = [":hello_lib"])
run_check(name = "hello_runs", exe = ":hello", expected_stdout = "hello, world")gate_tests welds the test: the library cannot be built unless its test passes.