machdocs
Home Ecosystem Changelog

Project layout

A mach project is a tree of .mach files under a source directory, anchored by a mach.toml manifest at the root. The project's id is the root of every module path the project exposes.

What mach init writes

mach init demo creates this tree. dep/std is the standard library, checked out as a Git submodule, and .gitignore holds /out/, the work directory every build writes under:

demo/
  .git/
  .gitignore
  .gitmodules
  mach.toml
  dep/
    std/               # the standard library
  src/
    bin/
      main.mach        # the executable's entry

Two directories appear later, each owned by one command. out/ is written by mach build and mach test, one work directory per target and profile, and mach clean removes it. doc/demo/ is written by mach doc, which owns doc/<project id>/ and removes any page there it did not write in the run:

$ mach build .
     built  demo  linux-x86_64  debug
            ./out/linux-x86_64/debug/bin/demo   51 KiB   51 modules   1.4s
$ mach doc .
documented  ./doc/demo
            1 source file   3 pages   3 written   0 removed

dep/ is not ignored, since each checkout there is a submodule its gitlink pins. With --no-git there is no .git, .gitignore or .gitmodules, and with --no-deps no dep/.

Source files

Source files use the .mach extension. By convention an entry sits in a directory of its own, so nothing else under src reads as one:

These names are convention only, and the compiler never looks for a function called main. An entry is the source file an [artifact.*] names with its entry key, and what marks a function as the program's entry point is its exported symbol, covered in Hello world. The artifact's build follows use and fwd from its entry, so a file no entry reaches is not part of it. A project may declare several artifacts, each with its own entry.

The project root

Every project has a mach.toml at its root, and every command takes the project explicitly as a directory or a manifest path. mach init writes a complete one. Abridged to one of its six targets:

[project]
id = "demo"                          # root of every module path
version = "0.1.0"
mach = "^7"                          # the compiler versions this project builds with
src = "src"                          # source dir
work = "out/{target.name}/{profile.name}"

[target.linux-x86_64]
arch = "x86_64"
sys  = "linux"
abi  = "sysv64"
# ... linux-aarch64, linux-riscv64, darwin-x86_64, darwin-aarch64, windows-x86_64

[artifact.demo]
kind = "bin"
entry = "bin/main.mach"               # executable entry, relative to src
out = "{project.work}/bin/demo{artifact.suffix}"
targets = ["*"]

[profile.debug]
default = true
optimize = false
debug = true
simd = "scalarize"
# ... and [profile.release]

[dep.std]
git = "https://github.com/briar-systems/mach-std"
ref = "branch/dev"

[project] requires id, version, mach, src and work, in every manifest. src is where module paths resolve, and dependencies live under dep/. The manifest needs no [link.*] table: the system libraries a program needs on darwin and windows come from std. The full schema lives on the Manifest page, and version ranges on Dependencies.

Files map to module paths

The path separator is .. A file at <src>/foo/bar.mach in a project with id = "myproj" is reachable as myproj.foo.bar.

myproj/
  mach.toml
  src/
    bin/
      main.mach        # myproj.bin.main
    foo/
      bar.mach         # myproj.foo.bar

There is no self-prefix. Within a project, modules always reference each other by their full project-rooted path, so every reference reads the same no matter where it appears. A dependency's modules are reached the same way, by its id and its source path: use std.print; is src/print.mach of the project std.

Surface and split modules

A file foo.mach may sit beside a directory foo/. The file is the surface module, the public face of foo. The directory holds split implementations that the surface loads and re-exports.

src/
  foo.mach             # surface: myproj.foo
  foo/
    a.mach             # split: myproj.foo.a
    b.mach             # split: myproj.foo.b

The surface loads each split with use and re-exports its public symbols with fwd. Consumers use myproj.foo and reach symbols through the surface, never naming the split files.

use myproj.foo.a;

fwd a.X;             # re-export a's public symbol through the surface

Topical splits

Organize a large module by topic. Every split is forwarded unconditionally, keeping one logical module spread across readable files.

Multiplatform splits

Keep one implementation per target and select it with $if on $mach.build.sys or $mach.build.arch, so the surface forwards only the split that matches the build.

See also