CLI
The mach binary is the compiler driver. It dispatches on the first argument to a small set of commands, build, run, test and more, that compile, run and manage a project rooted at a mach.toml.
Invocation
mach <command> [options]
The compiler dispatches on argv[1]. With no command it prints usage and exits 1, and an unknown command is refused with the list of commands. mach help <command> and mach <command> --help print one command's usage and every flag it takes, and that page is the complete list.
The project commands take the project as a required positional: a directory, whose mach.toml is read, or the manifest file itself. The manifest is never guessed from the working directory:
$ mach build
error[cli.operand] missing project path. pass a project directory, such as '.'
A flag has a long form and often a short one (-a, --artifact). Its value is the next argument or follows an = (--report json, --report=json). Short flags without values bundle (-vv). A flag marked ... in the help repeats. A lone - is a positional operand, not a flag. A flag the previous major took is refused naming its replacement:
$ mach build . --jobs 2
error[cli.flag_removed] option '--jobs' is not allowed. use --threads
$ mach build . -O2
error[cli.flag_removed] option '-O2' is not allowed. use -O or --optimize
Commands
| Command | Summary |
|---|---|
build | compile a project or one entry file |
check | parse, resolve and type-check the project's source without building |
run | run an executable a build already wrote, passing the arguments after -- |
test | build the project's tests and run each in its own process |
fmt | format source in the canonical layout |
migrate | rewrite a project from the previous major's spelling into this one's |
clean | remove the project's build output |
dep | manage dependencies: list, add, remove, update, pull, outdated, verify |
init | create a project |
doc | write the project's reference documentation as Markdown |
info | show the host target, or every target |
help | show the usage of mach or of one command |
Shared flags
Every command takes --report. The commands that build, check or test a project share the selection and readout flags below. Each command's own flags follow in its section.
| Flag | Value | Effect |
|---|---|---|
--report <mode> | human, json, lines | how the command reports: human text (default), NDJSON records, or tab-separated record lines. See Reports. |
-a, --artifact <pattern> | name or glob | select [artifact.<name>] entries (default: the default selection) |
-t, --target <pattern> | name or glob | select [target.<name>] entries. In an entry build it names one <sys>-<arch>, as mach info targets prints it (default: the host's) |
-p, --profile <pattern> | name or glob | select [profile.<name>] entries (default: the sole profile, or the one marked default = true) |
--all | - | select every artifact, target and profile no other selector narrows |
-v, -vv | - | verbose readout: each phase as it closes, and with -vv each module too |
-q, --quiet | - | suppress non-error output. Combining it with -v is refused. |
--dep-warnings | - | also show the dependencies' warnings, each under its own allow and #[expect] |
A pattern is an exact name, which must be declared, or a glob where * matches any run of characters and ? one character. Quote a glob so the shell leaves it alone (-p '*'). -t native names the declared target matching the host. A target name that is not declared is refused naming the declared ones:
$ mach build . -t linux
error[selection.unknown] mach.toml: no target named 'linux' (the declared ones are linux-x86_64, linux-aarch64, linux-riscv64, darwin-x86_64, darwin-aarch64, windows-x86_64)
Several artifacts or profiles with none marked default = true are refused where a command must pick one, and table order never decides. See the build matrix.
Reports
A build's readout, its diagnostics and its closing summary go to stderr. --report chooses how they are written, and the information is the same in all three modes. json and lines turn stderr into records and leave stdout as it is.
human (the default) prints each diagnostic with its key, its place and the source it points at, then a tally:
$ mach check .
error[name.unresolved] unresolved identifier `undefined_name`
@ src/bin/main.mach:8:9
8 | ret undefined_name;
| ^^^^^^^^^^^^^^
1 error / 0 warnings
json writes one JSON object per line (NDJSON), each naming its schema and record kind:
$ mach check . --report json
{"schema":3,"record":"diagnostic","severity":"error","code":"name.unresolved","message":"unresolved identifier `undefined_name`","origin":"resolve","primary":{"file":"src/bin/main.mach","line":8,"column":9,"end_line":8,"end_column":23,"byte_start":153,"byte_end":167},"related":[],"notes":[],"help":[],"fixes":[]}
{"schema":3,"record":"summary","errors":1,"warnings":0,"notes":0,"outcome":"failure","exit_code":1}
lines writes the same records as tab-separated key=value fields, the record kind first, for tools that read a line at a time:
$ mach check . --report lines
diagnostic schema=3 severity=error code=name.unresolved message=unresolved identifier `undefined_name` origin=resolve primary.file=src/bin/main.mach primary.line=8 primary.column=9 primary.end_line=8 primary.end_column=23 primary.byte_start=153 primary.byte_end=167
summary schema=3 errors=1 warnings=0 notes=0 outcome=failure exit_code=1
Every progress line has one shape: a verb right-aligned in a ten-column gutter, then what it acted on, with any detail on an indented line beneath. When stderr is a terminal and the report is human text, a build also shows a live compiling line that it clears before writing anything else.
build
mach build [options] <path|entry.mach> [inputs...]
Compiles every selected cell of the project at <path> (for example mach build .), one cell being one artifact for one target and one profile. Every module the artifact's entry reaches is compiled to an object under the work directory, and the objects are linked into the artifact's out path. A build says what it did:
$ mach build .
built demo linux-x86_64 debug
./out/linux-x86_64/debug/bin/demo 51 KiB 51 modules 476ms
$ mach build .
unchanged demo linux-x86_64 debug 160ms
A selection of several cells prints one table when the last closes, and a cell that took objects from the cache says how many it rebuilt:
$ mach build . --all
built target artifact profile modules size time
linux-x86_64 demo debug 0 of 51 modules rebuilt 51 KiB 163ms
linux-aarch64 demo debug 51 modules 54 KiB 631ms
linux-riscv64 demo debug 51 modules 54 KiB 391ms
darwin-x86_64 demo debug 51 modules 287 KiB 281ms
darwin-aarch64 demo debug 51 modules 342 KiB 598ms
windows-x86_64 demo debug 52 modules 89 KiB 501ms
linux-x86_64 demo release 0 of 51 modules rebuilt 10 KiB 79ms
linux-aarch64 demo release 51 modules 12 KiB 1.4s
linux-riscv64 demo release 51 modules 12 KiB 383ms
darwin-x86_64 demo release 51 modules 22 KiB 304ms
darwin-aarch64 demo release 51 modules 51 KiB 600ms
windows-x86_64 demo release 52 modules 11 KiB 513ms
Every cell is attempted, and a failure does not abandon the ones after it. A build never fetches a dependency. One that is not checked out is refused naming the command that brings it:
error[dep.unrealized] dependency 'std' is not checked out at './dep/std'. run `mach dep pull .`
| Flag | Value | Effect |
|---|---|---|
-O, --optimize, --no-optimize | - | run the optimization passes or not, over the profile's optimize |
--debug, --no-debug | - | emit debug information or not, over the profile's debug |
--set <key=value> | manifest key | write a manifest key for this invocation, such as --set simd=require or --set passes.licm=false. Repeats. |
-o, --output <path> | path | write the linked artifact here. Accepted only when the selection is one cell. |
-w, --work-dir <path> | path | use another work directory for this build |
--emit <product> | asm, ir, obj, exe | a product to write, once each (default: exe). A set without exe stops at the objects and links nothing. |
--threads <n> | count | how many threads the build compiles and links on (default: the host's CPUs) |
--verify | - | run the module and code generation verifiers |
--passes | - | print each optimization pass that changed each function, and what it did |
--plan | - | print the build plan and exit without building |
--no-cache | - | rebuild without reusing cached objects or steps |
-L, --library-dir <dir> | dir | add a library search directory. Repeats. |
-l, --library <name> | name | link a library, object or archive by name. Repeats. |
A flag that changes the selected profile, -O, --debug or a profile key written with --set, makes a differently configured build, so the build must say where it goes with -o or -w:
$ mach build . -O
error[cli.flag_conflict] these options would overwrite the profile's artifacts with a differently configured build. name an artifact path with -o or a work directory with -w
The work directory is the object cache: a module whose object there was built under the same key is reused, and an edit rebuilds only that module and the modules whose view of it changed. --passes writes one line per pass and function on stdout, the count where the pass counts what it did:
$ mach build main.mach --source std=dep/std -O --passes -o main
promote std.system.panic.panic_sink
effects std.system.panic.panic_sink
inline std.system.panic.panic 1
The verbose readout
-v prints, for each cell, a table of its stages, the counters of the code it made and the memory it took. A row marked * is summed over the threads. -vv adds a line for each module under each stage.
$ mach build . -v
stage count time
target 1 target 14ms #
load 55 modules 57ms ######
resolve 55 modules 15ms #
sema 55 modules 60ms ######
lower 51 objects 156ms * ################
verify 51 objects 0us *
codegen 51 objects 152ms ###############
passes 51 objects 94ms * #########
select 51 objects 173ms * #################
alloc 51 objects 240ms * ########################
frame 51 objects 153ms * ###############
mirl emit 51 objects 189ms * ###################
layout 51 objects 60ms * ######
encode 51 objects 10ms * #
emit 51 objects 93ms #########
link 1 binary 31ms ###
other 1.1ms
total 422ms
* summed over 16 threads
code 876 functions 30,591 ir 41,404 selected 42,198 allocated 1,032 alloc rounds
memory 26 MiB peak 348 MiB allocated in 481,045 blocks
built demo linux-x86_64 debug
./out/linux-x86_64/debug/bin/demo 51 KiB 51 modules 422ms
Entry builds
mach build <entry>.mach builds one file no manifest declares. The command line states the project: --source <name>=<dir> binds a module prefix to a directory, -k names what it makes, and -t <sys>-<arch> names the one target. The entry's own project is named by the file's stem, so hello.mach builds project hello. -a, -p and --all are refused here.
$ mach build hello.mach --source std=dep/std
built hello linux-x86_64 debug
./hello 54 KiB 51 modules 406ms
| Flag | Value | Effect |
|---|---|---|
--source <name=dir> | prefix and directory | bind a module prefix to a directory (default: the entry's directory). A bound directory that holds a mach.toml also brings its exported [link] entries. Repeats. |
-k, --kind <kind> | bin, static, shared, object, module | the product to make (default: bin) |
--linkage <name> | pie, dynamic, static | how a bin links (default: the target's) |
--env <name> | name | the environment, such as gnu on linux (default: the target's) |
--abi <name> | name | the calling convention (default: the target's) |
--isa <selection> | selection | the instruction set, such as x86-64-v3. A target marked needs --isa must name one. |
--ext <name> | name | add an instruction-set extension, once each |
--code-model <name> | name | the code model (default: the target's) |
mach build main.mach -k static # a static library
mach build main.mach --isa x86-64-v3 --ext sha # assume more of the machine
mach build main.mach -t linux-riscv64 -O # cross-compile, optimized
Link inputs
ext fun declarations are resolved at link time against precompiled code: a loose .o object, a static .a archive, or a shared library. Inputs come from the command line and from the artifact's [link.*] entries, and both are linked. An input that resolves to no file is an error, so a typo never silently drops a dependency.
- An operand after the project is a link input named by its path: it holds a
/or is named as an object, archive or shared library (x/libfoo.a,foo.o). A relative input is read against the working directory. Any other operand is refused, since a library named without a path is-l <name>. -l <name>links a library found by name in each-Ldirectory, then the working directory, then the system directories of the target.-L <dir>adds a search directory. Both repeat.
check
mach check [options] <path>
Parses, resolves and type-checks the source the selected artifacts reach. Nothing is generated, linked or written, so it is the fast loop during development. It takes the project alone, no link inputs. --declassified lists every declassification in the project's source once it checks.
$ mach check .
checked demo linux-x86_64 debug 132ms
run
mach run [options] <path> [-- args...]
Runs the program mach build already wrote for one cell. It never builds, and with nothing at the cell's output path it is refused:
$ mach run .
error[artifact.not_built] no built artifact at './out/linux-x86_64/debug/bin/demo'. run `mach build` first
Arguments after -- are passed to the program, and the program's exit status is the command's. -a, -t and -p select the cell, and a selection of several cells is refused. With -o it runs the output a build's -o named.
| Flag | Value | Effect |
|---|---|---|
--runner <cmd> | command | launch the program as <cmd> <program> <args>, in place of the target's runner |
--timeout <duration> | duration | stop the program after a duration such as 30s |
A target that is not the host's runs only through a runner, the target's runner key or --runner:
$ mach run . -t windows-x86_64
error[target.not_runnable] target 'windows-x86_64' does not run on this host and declares no runner. declare `runner` in [target.windows-x86_64] or pass --runner
$ mach run . -t windows-x86_64 --runner wine
Hello, World!
test
mach test [options] <path> [inputs...]
Builds a test dispatcher for the selected artifact as mach build would build it, and runs each test the artifact reaches in its own process, up to --threads at once. A test is named by its qualified name, <module>#<identifier>. Without -v each module gets one row, and each failure is shown once with the command that reruns it:
$ mach test .
demo.util.math 311us 1 ok 1 FAIL
FAIL demo.util.math#clamp__outside src/util/math.mach:12 (exit 1)
rerun: ./out/linux-x86_64/debug/test/demo/demo 1
failed 1 of 2 tests 369us
It takes the build flags (-O, --debug, --set, -w, --threads, --no-cache, -L, -l) and these:
| Flag | Value | Effect |
|---|---|---|
--filter <substr> | substring | run only tests whose name contains the substring |
--list | - | list the tests and their test objects without running them |
--include-deps | - | also run the dependencies' tests |
--runner <cmd> | command | launch every test through this command, in place of the target's runner |
--timeout <duration> | duration | stop each test after a duration (default: 60s) |
--allow-unrunnable | - | succeed when nothing selected can run on this host |
The test results go to stdout, and under --report json or lines each test is also a record on stderr. See Testing for writing tests.
fmt
mach fmt [options] <path>|<file>|-
Rewrites source in the one canonical layout. There is no configuration. The operand decides what is formatted:
- a directory or
mach.tomlrewrites that project's own source files in place. Dependencies are never visited. - any other file rewrites that one source file, with no manifest read.
-reads source from stdin and writes its canonical layout to stdout, touching nothing on disk.
Each file it rewrites is named on a formatted line. With --check nothing is written, each input whose layout differs is named on a differs line, and the command exits 1.
mach fmt . # the whole project
mach fmt src/bin/main.mach # one file
mach fmt - < draft.mach # stdin to stdout, for editors
mach fmt --check . # ci: fail if anything is unformatted
migrate
mach migrate [options] <path>
Moves a project from the previous major to this one. It rewrites the root project's mach.toml, then every source file under its src, names each file it rewrote on a migrated line, and reports what it cannot rewrite. It never edits a dependency. A migrated project changes nothing. With --check it writes nothing, prints each change as a unified diff, and exits 1 when anything would change:
$ mach migrate --check .
--- ./mach.toml
+++ ./mach.toml
@@ -4,1 +4,1 @@ [project]
-mach = "^6.0"
+mach = "^7"
@@ -6,1 +6,1 @@ [project]
-out = "out/{target.name}/{profile.name}"
+work = "out/{target.name}/{profile.name}"
...
--- ./src/root.mach
+++ ./src/root.mach
@@ -3,1 +3,1 @@
-#[oblivious]
+#[constant_time("soft")]
clean
mach clean [options] <path>
Removes the build output of every cell mach build --all would plan: the work directory's obj/, ir/, asm/, .cache/, .stage/, test/ and dep/, and every artifact output, each named on a removed line. The build after it reuses nothing. What a build wrote under -o or -w is not reached. See the work directory.
dep
mach dep <command> [options] <path> [args]
Manages the project's dependencies. A dependency is the files in dep/<id>/: a git dependency is checked out there at its pin, and a path dependency is a copy of another project tree. Each command writes its progress lines to stdout and takes -q.
| Command | Args | Effect |
|---|---|---|
list | <path> | list each declared dependency with its source, selection, recorded pin, and state (realized or missing) |
add | <path> <name> (--git <url> [--ref <ref> | --range <range>] | --path <dir>) | declare a dependency and realize it. With --git alone it writes version = "^X.Y.Z" of the newest release every range and the running compiler accept. Takes --offline. |
remove | <path> <name> | remove the declaration. --purge also deletes the dependency's directory. |
update | <path> (<name> | --all) | the only command that moves a pin: advance branch refs, move exact refs to the commit they name, resolve ranges, and refresh path copies. --lowest selects the lowest release every range accepts. --offline resolves from releases already fetched. |
pull | <path> | realize every dependency at its pin, and sync each path copy with its source |
outdated | <path> | report each version-selected dependency's pinned, highest compatible and latest release |
verify | <path> | check the realized dependencies against the manifests without changing anything. --release also requires every dependency to select a release. |
--ref is spelled branch/<name>, tag/<name> or commit/<id>. add takes exactly one of --git or --path, and update takes a name or --all, not both.
$ mach dep pull .
realized std a78d528f4b89 branch/dev
$ mach dep add . helper --path ../helper
realized helper ./../helper (copy made from its source)
added [dep.helper]
$ mach dep list .
std git=https://github.com/briar-systems/mach-std ref=branch/dev pin=a78d528f4b8925ff586573f357b6b5a1a2163068 realized
helper path=../helper realized
$ mach dep verify .
verified .
Resolution runs only in add, update and outdated. mach build never fetches, never resolves and never writes under dep/. It builds what dep/ holds. See Dependencies for ranges, resolution and the full rules.
Dependency pins
In a Git repository a git dependency is a submodule, and its pin is the gitlink the repository records under dep/. A project need not be a repository: without a gitlink the dependency is a plain checkout. There is no lock file. Every dependency in the transitive closure lives directly under dep/<id>/, where the manifest key, the directory name and the project id are one name.
init
mach init [options] <dir>
Creates a project in <dir>, made when it does not exist (mach init . names the working directory). It writes a complete mach.toml, with a [project] whose mach range is the running compiler's major (mach = "^7"), six targets (linux on x86_64, aarch64 and riscv64, darwin on x86_64 and aarch64, windows on x86_64), one artifact, and debug and release profiles, and the entry src/bin/main.mach. Unless --no-git, it makes the directory a Git repository, writes a .gitignore holding /out/, and checks std out as a submodule. std is declared at the selection the running compiler states for it.
$ mach init demo
created demo
realized std a78d528f4b89 branch/dev
| Flag | Value | Effect |
|---|---|---|
--name <id> | id | the project id (default: the directory name, each - written as _). An id starts with a letter or _ and holds letters, digits and _. |
--force | - | scaffold over an existing mach.toml or entry file |
--lib | - | a library project: a kind = "static" artifact with export = true and the entry src/lib/<id>.mach |
--no-deps | - | declare no dependency, so nothing is read from the network. It prints the mach dep add command that declares std. |
--no-git | - | skip Git initialization |
-q, --quiet | - | suppress non-error output |
A refused init writes nothing: an existing mach.toml is refused ('mach.toml' already exists. --force replaces it), and so is an id that is not one. A failure after the files are written, in git init or in checking std out, leaves the project complete and says which step to rerun. The scaffold builds and runs from mach build . and mach run . with no further edits. See Project layout for the files it writes.
doc, info, and help
mach doc <path> documents every source file of the project as Markdown, from its doc comments, into doc/<project id>/, a directory it owns: a page it did not write in the run is removed. It parses and runs nothing more, so it needs no target and no pulled dependency, and -a and -t are refused. -o <dir> writes under <dir>/<project id>/ instead.
$ mach doc .
documented ./doc/demo
1 source file 3 pages 3 written 0 removed
mach info prints the compiler's version and the host target, one part per line. It needs no project. On an x86_64 Linux machine:
mach
host: linux-x86_64
isa: cmov,cx8,x87,fxsr,osfxsr,mmx,sce,sse,sse2
sys: linux
env: none
abi: sysv64
code_model: small
linkage: static
object: elf
mach info targets prints every target as <sys>-<arch>, the spelling -t takes in an entry build, with the environments, ABIs, code models and linkages it accepts. A * marks each default, and needs --isa marks a target with no default instruction set. Abridged:
linux-x86_64 env=*none,gnu abi=*sysv64 code_model=*small linkage=*static gnu:*pie,dynamic,static
linux-aarch64 env=*none,gnu abi=*aapcs64 code_model=*small linkage=*static gnu:*pie,dynamic,static
linux-riscv64 env=*none,gnu abi=*lp64d,lp64,lp64f code_model=*medany linkage=*static gnu:*pie,dynamic,static
darwin-aarch64 env=*macos abi=*darwinpcs code_model=*small linkage=macos:*pie
windows-x86_64 env=*msvc abi=*win64 code_model=*small linkage=msvc:*pie,dynamic
uefi-x86_64 env=*none abi=*win64 code_model=*small linkage=*pie
freestanding-riscv32 env=*none abi=ilp32,ilp32f,ilp32d code_model=*medlow linkage=*static needs --isa
wasi-wasm32 env=*preview1 abi=*basic code_model=- linkage=preview1:*static
vulkan-spirv env=*none,vulkan1.0,vulkan1.1,vulkan1.2,vulkan1.3 abi=*shader code_model=- linkage=- vulkan1.0:- vulkan1.1:- vulkan1.2:- vulkan1.3:-
mach --version prints the version alone on one line, for tooling.
mach help [command] prints the top-level usage, or with a command that command's usage and flags.
Exit codes
The commands share one convention for scripting:
| Code | Meaning |
|---|---|
0 | success (for test, every test passed) |
1 | a refusal: a usage error, a bad manifest, a compile error, a failed test, an input that differs under --check |
2 | an internal failure |
3 | a failure of the environment, such as the filesystem |
When several cells fail, the build exits with the worst: 2 when any failed internally, else 3, else 1. mach run exits with the program's own status.
See also
- Manifest - the
mach.tomlschema and keys - Dependencies - version ranges, resolution, and git and path dependencies in detail
- Testing - writing tests and the
mach testrunner