Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

C. Build File API

A build file reaches buildl through one table, bound to two names: the global buildl, and airsstack.buildl beside the other airsstack modules (json, path, regex, hash and glob). The book's examples start with local b = buildl. The table holds twelve functions:

FunctionReturnsDeclares
target(name, options)nothinga target that builds files
test(name, options)nothinga test target
discover(name, options)nothinga discovery target
rule(name, options)nothinga command several targets share
alias(name, target)nothinga second name for a target
option(name, options)nothinga build setting
subdir(path)nothinganother directory with a build file
sources(pattern)a list of file names
folder()a string
discovered(label)a string, or nil
decode_all(text)a list of values
use(name)the plugin's module

The names come from crates/buildl-lua/src/module.rs, where the table is filled. This build file asks the table for its own keys and shows them in an error, since a build file cannot print:

local names = {}
for k in pairs(buildl) do names[#names + 1] = k end
table.sort(names)
error("buildl holds: " .. table.concat(names, " "), 0)
$ buildl check
error: build.lua: runtime error: lua error in build.lua: runtime error: buildl holds: alias decode_all discover discovered folder option rule sources subdir target test use
stack traceback:
	[C]: in ?
	[C]: in function 'error'
	build.lua:4: in main chunk

How arguments are read

The rules every function shares:

  • An options table accepts only the keys listed for its function. Any other key is an error, so a misspelt key never passes silently.
  • A list option takes strings, and may hold lists of strings one level deep, which are spliced in place: { "go.mod", b.sources("*.go") } is one flat list.
  • A name, a path and a label are plain strings.
  • A function that refuses its arguments fails the build file with the line that called it. A pcall around the call does not undo that: the build file has still failed.
  • File names in inputs and patterns in sources are relative to the folder of the build file.

Labels

deps, rule, an alias's target and discovered name a target in one of three ways:

FormMeans
"//dir:name"the target name declared in the build file of dir; "//:name" is the root's
":name"the target name in this build file's folder
"name"the same as ":name"

Placeholders in commands

A command never holds a real path. Inside each string of run, these words are replaced just before the action runs:

PlaceholderBecomes
$inthe target's inputs, in the order written
$outthe target's outputs, in the order written
$depsevery output of every dependency
$opt:namethe value of the build setting name

A string that is exactly $in, $out or $deps becomes one argument per path, and none when the list is empty. Inside a longer string, such as "$out/app" or "--src=$in", the paths are joined by single spaces. $input is not a placeholder: the word after $ must be exactly in, out or deps. The paths are relative to the workspace root.

target(name, options)

Declares a target that builds files. Returns nothing.

OptionTypeDefaultMeaning
runlist of stringsthe command: program first, then its arguments
rulelabela rule whose run is the command instead; exactly one of run and rule
inputslist of strings{}the files the command reads; a file a .buildlignore ignores is refused (exit code 5), a plugin's inputs included
depslist of labels{}the targets whose outputs the command reads
outputslist of strings{ name }the files the command writes
envtable from name to "key" or "unkeyed"{}the environment variables the command is given, read from yours, each with its mark, below
networkbooleanfalsetrue says the command reaches the network; plan and the approver show it
stdoutbooleanfalsetrue makes what the command prints the output; needs exactly one output
alwaysbooleanfalsetrue runs the command on every build instead of caching it
b.target("hello.txt", { run = { "sh", "-c", "echo hello > $out" } })

The marks of env

env names each variable and marks it. The mark says whether the variable's value is part of what makes the action's key:

MarkGiven to the commandIn the action keySent to a remote server
"key"its valuename and valuename and value
"unkeyed"its valuename onlyno
b.target("app", {
  run = { "cargo", "build" },
  env = { RUSTFLAGS = "key", HOME = "unkeyed", PATH = "unkeyed" },
})
  • Every variable is marked: there is no default mark. env = {}, or no env, declares none.
  • A list such as env = { "HOME" }, and a table that mixes the two, such as env = { "HOME", PATH = "unkeyed" }, are refused.
  • A mark that is not the string "key" or "unkeyed" is refused.
  • TZ, LC_ALL and SOURCE_DATE_EPOCH are refused with either mark: the sandbox sets them.
  • An "unkeyed" variable that is not set on the machine is not given to the command, and the key is the same as when it is set.
  • A "key" variable whose value holds one of this machine's folders is refused, and so is a command that does. Exit Codes lists the messages.
  • "unkeyed" needs the name in env.unkeyed in buildl.toml, beside env.read ([ceiling]).

test(name, options)

Declares a test, run by buildl test and passed when its command exits 0. Returns nothing. It takes the options of target except stdout, and outputs defaults to none: a test is a record of its exit status and what it printed.

OptionTypeDefault
runlist of strings
rulelabel
inputslist of strings{}
depslist of labels{}
outputslist of strings{}
envtable from name to mark{}
networkbooleanfalse
alwaysbooleanfalse
b.test("hello_test", { run = { "grep", "-q", "hello", "$deps" }, deps = { ":hello.txt" } })

discover(name, options)

Declares a discovery target: an action whose one output, <name>.json, a later pass of the build files reads with discovered. Returns nothing. It takes the options of target except outputs and always, since its output is fixed and it is always cached.

OptionTypeDefault
runlist of strings
rulelabel
inputslist of strings{}
depslist of labels{}
envtable from name to mark{}
networkbooleanfalse
stdoutbooleanfalse
b.discover("pkgs", { run = { "go", "list", "-json=ImportPath", "./..." }, stdout = true })

rule(name, options)

Declares a command that targets name with rule instead of writing their own run. Its placeholders take the values of the target that uses it. Returns nothing.

OptionTypeDefaultMeaning
runlist of stringsrequiredthe command
descstringnonethe line shown for each action that uses the rule; placeholders are replaced in it too
b.rule("join", { run = { "cat", "$in" }, desc = "joining $in" })

alias(name, target)

Declares name as a second name for target, a label. Returns nothing.

b.alias("default", ":hello.txt")

option(name, options)

Declares a build setting, which a command reads as $opt:name and a run changes with --set name=value. Returns nothing.

OptionTypeDefaultMeaning
defaultstringrequiredthe value when no --set gives another
b.option("greeting", { default = "hello" })

subdir(path)

Adds the build file of the folder path, relative to this one, to the workspace. It is read too, and its targets are named by its folder from the workspace root, such as //lib:name. A path that leaves the workspace is refused, and so is a folder a .buildlignore ignores (exit code 5). Returns nothing.

b.subdir("lib")

sources(pattern)

Returns the files below this build file's folder whose path matches pattern, as a list of strings relative to the folder, sorted. * matches within one folder name and stops at /; **/ matches any number of folders, none included. Only regular files are listed, links are not followed, and the output directory and .buildl are never searched. A path a .buildlignore ignores is not listed, and an ignored folder is not entered. Every other entry visited counts against [declaration] walk_entries; an ignored one does not. A .buildlignore the walk meets that cannot be used stops the build file with exit code 5.

local go_files = b.sources("**/*.go")

folder()

Returns the folder of the build file that is being read, as a string relative to the workspace root: "" in the root build file, "lib" in lib/build.lua, "lib/net" in lib/net/build.lua. It takes no arguments. Inside a plugin it is the folder of the build file that called the plugin, not the plugin's own.

local dir = b.folder()

discovered(label)

Returns the output of the discovery target label as a string, or nil when it has not run yet. build and test run discovery targets first, then read the build files again, so in that second pass the string is there.

local listed = b.discovered(":pkgs")

decode_all(text)

Returns a list of the JSON values in text, one after another, as airsstack.json.decode would give each. Values are separated by white space, which may be left out beside an object, an array or a string. Empty text gives an empty list. Unlike the other functions, its errors are plain Lua errors that pcall can catch.

for _, pkg in ipairs(b.decode_all(listed or "")) do --[[ one target per package ]] end

use(name)

Loads the plugin buildl.toml names under [plugins.<name>] and returns what its entry file returns, usually a table of functions. A second use of the same name in the same build file returns the same value without loading the plugin again.

local zip = b.use("zip")

What is taken away

A build file runs with no way to reach the world or to differ from one run to the next. Three standard Lua functions are removed, set to nil:

RemovedWhy
printa build file declares, it does not talk; an error is the way to show something
math.randoma random number would declare something different on every run
math.randomseedgoes with math.random

tostring and string.format are also changed: for a table or function without its own __tostring they raise an error instead of giving text with a memory address in it, which would differ on every run.