Captain / Documentation / Part 8. Where Captain runs

Forge: build recipes

Contents ↓

Forge is the platform's build service. It runs on each build machine, pulls a pushed commit, compiles that commit's .forge file, and runs one of its entries. The entry may use Forge's own tasks and nothing else. Forge signs only artefacts from full, fresh builds of approved sources, and it never publishes anything: publishing stays with people, from exactly the bytes Forge offered.

A .forge file is Captain source. That is what lets Forge know, before a build starts, every task the recipe can reach.

A recipe

A recipe lives at the repository root, as .forge, and uses only Forge::Tasks. One entry per platform is the convention. This one builds a Rust program for Linux, linked against a glibc 2.31 floor, and offers the binary:

release.forge
module Hello::Forge.

use Forge::Tasks.

;; Linux x86-64, linked against a glibc 2.31 floor.
export release commit: ForgeCommit -> ForgeArtifact [
  crates := fetchCrates commit: commit.
  built := build commit: commit crates: crates
    inputs: (List empty: ForgeInput)
    recipe: RustRecipe {
      package: "hello",
      profile: #Release,
      features: (List empty: Text),
      outputs: #( "hello" ),
      glibc: "2.31"
    }.
  ret offer artifact: (artifact build: built named: "hello").
].

It is the shape of the recipes in the repository's own .forge, cut down to one entry. captain check accepts a recipe like any other module:

Terminal
$ captain check release.forge
ok    release.forge (module Hello::Forge, 1 declaration)
checked 1 file as 1 module, no errors

An entry is an exported procedure that takes exactly commit: ForgeCommit: the commit Forge pulled. A recipe may export other procedures; they are not entries.

Handles, not paths

The values Forge's tasks pass each other are handles that only Forge can make: ForgeZone, ForgeCommit, ForgeCrates, ForgePackages, ForgeBuild and ForgeArtifact. Their fields can be read (an artifact's name and digest, a commit's sha and buildNumber, whether a build was full), but a recipe cannot construct one. No task takes a path where it needs an artifact.

So "Forge signs only what Forge built" is not a rule Forge has to remember at run time. It is a type error to write anything else: sign artifact: takes a ForgeArtifact, and the only way to get one is from artifact build:named:.

The only records a recipe builds are descriptions: RustRecipe, ClangRecipe, ClangLibrary and ForgeInput. They say what to build, and grant nothing.

The tasks

Task Does
zone named:, reset zone: a project's build area on that machine, opened, or emptied
pull zone:from:ref: a commit from a git remote
fetchCrates commit: the commit's Rust dependencies, vendored before the build so the build needs no network
fetchGritpack commit:at: packages from the platform's package registry, as the manifest and lockfile in a directory of the commit pin them; passed to a build through a ForgeInput
build commit:crates:inputs:recipe: a Rust build, from a RustRecipe
build commit:recipe:, buildLibrary commit:recipe: a C program or library, from a ClangRecipe or ClangLibrary
readText commit:path: a small text file from the commit, such as a version file; read, never run
artifact build:named: one output of a build, as an artifact
sign artifact:identity:, notarize artifact:using: signing and notarization, on a machine set up for them; the recipe names an identity by symbol, never a certificate or a key
offer artifact: puts an artifact in Forge's outbox, with its provenance

The authority for the vocabulary is spec/forge/TASKS_V1.md; the declarations as built are Forge::Tasks in prelude.rs.

What forge check adds

captain check proves the recipe is well typed. It does not know which effects Forge is willing to run. Any Captain program may, for example, call the built-in exec effect, so this recipe, which runs a shell before building, passes captain check:

sneaky.forge
module Hello::Forge.

use Forge::Tasks.

;; Not a recipe Forge will run: `exec` is not a Forge task.
export release commit: ForgeCommit -> ForgeArtifact [
  ran := exec program: "/bin/sh"#path
    args: #( "-c", "make" )
    stdoutLimit: 1KiB
    stderrLimit: 1KiB.
  crates := fetchCrates commit: commit.
  built := build commit: commit crates: crates
    inputs: (List empty: ForgeInput)
    recipe: RustRecipe {
      package: "hello",
      profile: #Release,
      features: (List empty: Text),
      outputs: #( "hello" ),
      glibc: "2.31"
    }.
  ret offer artifact: (artifact build: built named: "hello").
].

forge check compiles the recipe, verifies the bytecode, and then reads each export's effect set from the manifest. Every effect an export can reach must be a Forge task:

Terminal
$ forge check release.forge
release.forge: ok
  release commit: ForgeCommit -> ForgeArtifact
$ forge check sneaky.forge
forge: FORGE-NOT-A-TASK: `release commit:` can reach `exec program:args:`, which is not a Forge task; a .forge file may use Forge tasks only

Forge runs the same check on the commit it pulled before running anything, and its host refuses any other effect at run time as well. Because the check reads what an export can reach, a call hidden in a branch that a particular run would not take is refused all the same.

forge check FILE --json prints forge.check/1: the diagnostics, and for each entry the tasks it would call in order, with their arguments, what feeds what, and what the steps add up to (artefacts offered, identities and notaries named, toolchains needed). Forge plans each entry on Captain's own planning interpreter, with the commit as a symbolic value, so nothing runs. captain plan cannot do this itself, because it cannot be given a ForgeCommit.

Running a recipe

A run names a zone, the remote and ref to pull, and the entry:

Terminal
$ forge run --zone hello --from https://example.invalid/hello.git --ref main --entry release --fresh

A zone keeps its build output between runs, so later builds can be incremental. Only a full build, --fresh, can be signed. The run prints each step and the digest of each artefact it offers, which forge artefacts read DIGEST streams out, checked against that digest.