Building Fly from source

Fly is self-hosted: the compiler, the runtime and the standard library are written in Fly. Building it therefore needs a Fly compiler to start from. That starting compiler is the seed, and the process that goes from the seed to the released toolchain is the staged bootstrap.

This page describes the bootstrap as it is implemented in ci/linux/*.sh and ci/windows/*.ps1 of the release/0.16.X branch. The scripts are the source of truth; the two sets mirror each other step for step. The POSIX scripts serve both Linux x86_64 and macOS arm64 (from 0.14.9): what differs between the two systems is set once in ci/linux/platform.sh.


The chain of seeds

Each release series is built by one pinned seed, and each series has its own branch, so the whole chain can be rebuilt from sources in order:

SeriesBranchSeedSeed written in
0.14.xrelease/0.14.X0.13.19, the C++ reference (release/0.13.X)C++
0.15.xrelease/0.15.X0.14.9, the last 0.14.x releaseFly

From 0.15 the seed is the previous self-host release: the C++ compiler is frozen at 0.13.19 and only starts the chain from scratch (0.13.19 builds 0.14.x, whose 0.14.9 builds 0.15.x). The rest of this section describes the C++ seed of the 0.14.x series.

The C++ seed is deliberately small. It has no standard library, no package manager and no test runner: it exists to turn the 0.14.x sources into a first working fly. The rule that follows from this is the bootstrap subset:

The sources under compiler/, runtime/ and std/lib may only use syntax the pinned seed can parse, because the seed compiles all of them at stage 1. From 0.15 the limit is what the previous self-host release compiles.

Language features that go beyond the seed are available to user programs and to the tools (which are built at stage 2), not to the compiler tree itself.

The seed pin

The seed version is pinned in ci/linux/stage0.sh and ci/windows/stage0.ps1 (FLY_VERSION: 0.13.19 on release/0.14.X, 0.14.9 on release/0.15.X, 0.15.2 on release/0.16.X), with the SHA-256 of each platform's archive. A C++ seed was re-cut only when the tree needed something from it. The history of the C++ pins documents why each one was needed:

SeedWhat it brought to the tree
0.13.13array subscript a[i], T[N] declarations, reference-counted array buffers
0.13.14first seed with no std: bin/fly + two headers + the C runtime archive
0.13.15no CLI test runner; a lone suite is the test entry
0.13.16a generated .fly.h is a declaration file, with no {} stubs
0.13.17.fly.h is the only textual file in a lib directory (declarations + generic templates)
0.13.18return expr replaces the implicit out variable; every path must return; NRVO
0.13.19macOS arm64 (the language of 0.13.18) — the last C++ seed
0.14.9first self-host seed (release/0.15.X): stage 1 runs the same commands as stage 2
0.15.2release/0.16.X: stage 1 is fly build on the repository's Manifest.fly

stage0 checks the installed seed with --version and replaces it when it does not match the pin; the swap is atomic, so a failed download never leaves a machine without a seed.


The stages

stage 0   download          seed fly (0.15.2 on 0.16.x)  +  LLVM 20.1.8
              │
stage 1   seed fly build    runtime → std → compiler+driver  ──link──▶  .fly/bootstrap/stage1/bin/fly
              │             then stage1 fly rebuilds the std
              │             then: test_runtime, test_std, test_compiler   (run by stage1 fly)
              │
stage 2   stage1 fly build  runtime, std, compiler+driver, tools  ──▶  .fly/bootstrap/stage2/{bin,lib}
          (profile dist)    + the LLVM pieces: the release, laid out
                            then: every suite + ABI, semantic, leak, debugger guards
              │
stage 3   stage2 fly build  the compiler object again, compared byte for byte with stage 2's

Stage 0 — provisioning

Downloads two things into .fly/bootstrap/, and needs no system package manager:

  • LLVM 20.1.8 (upstream LLVM, built and released by fly-lang/llvm) → .fly/bootstrap/llvm: the LLVM shared library, ld.lld, llvm-config, the compiler-rt builtins that every Fly executable links, and the lldb debugger set. On Windows it also provides the llvm-mingw UCRT sysroot, so no Visual Studio is required.
  • The pinned seed → .fly/bootstrap/stage0.

Stage 1 — the seed builds the self-host

STAGE=1. From 0.16 the seed builds the tree the way any Fly project is built: fly build on the repository's own Manifest.fly, one target at a time, with the profile stage1 (stage1-debug when the build embeds DWARF). The seed's fly build writes target/<profile> below the directory it runs in and has no --out-dir, so it runs in a working copy under .fly/bootstrap/: .fly/bootstrap/stage1/work holds a copy of compiler/, driver/, std/ and runtime/ and of the manifest, and the output lands in .fly/bootstrap/stage1/work/target/<profile>. Building Fly writes nothing outside .fly/ (from 0.16.0; up to 0.15.x the bootstrap wrote build/, and stage 1 left a target/ in the checkout). After each target the script copies the result into .fly/bootstrap/stage1, where the next target finds its headers:

  1. runtime — target runtime-<os>: runtime/lib/Runtime<Platform>.fly into fly_runtime_lib, then tls-stub and the real TLS backend (tls-schannel or tls-openssl-<os>) as separate archives.
  2. std — target std: the whole std/lib directory as one library and one .fly.h header per public module.
  3. compiler — target fly. From 0.16.0 the fly program is the driver (driver/lib, whose Driver.fly holds the only main()) linked with the compiler library (compiler/lib, target compiler). The seed cannot link it that way: an archive it writes leaves some generic helpers undefined. So stage 1 gives the seed a manifest whose fly target compiles both trees into one object, which fly build links with std, runtime and LLVM into .fly/bootstrap/stage1/bin/fly. That rewrite goes with the next seed (fly-lang/fly#308).
  4. std again — the fly just linked runs fly build --target std itself and replaces the std in .fly/bootstrap/stage1/lib. The seed's build only served to link that compiler.
  5. LLVM pieces — the same fly runs the copy targets of Manifest.fly with --out-dir .fly/bootstrap/stage1: on Linux always the compiler-rt builtins and, with FLY_BUNDLE_LLVM=1, the LLVM shared library, the linker and lldb (llvm-windows on Windows), laid out as stage 2 lays them out. The seed knows no copy targets.

Up to 0.15 the stage drove the seed with one hand-written compiler command per artifact and linked the compiler in a script; those scripts are gone in 0.16. The profile in Manifest.fly now carries the options, and the stage keeps only the layout. Stage 2 is fly build all the way: see below.

The compiler reads its version from the fly.meta.package module that fly build generates from Manifest.fly. Stage 1 copies the one the seed wrote for the compiler to .fly/bootstrap/stage1/.state/gen/fly, where stage 2 keeps its own, and the compiler suites compile the compiler sources with the copy that belongs to the stage under test.

Only then do the suites run, and they are compiled and executed by the fly that stage 1 just linked — never by the seed. FLY_STAGE1_TESTS=0 skips them for a fast local build.

Stage 2 — the self-host builds what ships

STAGE=2. From 0.16 the stage is one command, run by the stage 1 fly:

.fly/bootstrap/stage1/bin/fly build --profile dist --out-dir .fly/bootstrap/stage2 --keep-objects

run three times: on the root Manifest.fly, then in tools/lsp and in tools/registry, the two tools being packages of their own with their Manifest.fly and the same output directory. The root builds every target of its manifest — runtime, TLS archives, standard library, the compiler as a library (lib/compiler), and fly, the driver linked with that library — and the tools link those libraries: the compiler's sources are compiled once, for fly and fly-lsp alike (from 0.16.0). The root build also copies the LLVM pieces stage 0 downloaded (the LLVM shared library, ld.lld, lldb, the compiler-rt builtins, on Windows the mingw sysroot), laid out as bin/ and lib/. .fly/bootstrap/stage2/{bin,lib} is exactly what a release packages, lib/compiler left out. Both dist profiles optimize at -O2 (from 0.16.0). The profile is dist-debug when the build embeds DWARF (CI on every push), dist for a release; its prefix map writes the repository, the lib/ of the compiling fly and the output directory as ., lib and out, so nothing that ships holds a path of the build machine. The executables' objects are kept (--keep-objects) and set aside in .fly/bootstrap/stage2/obj for stage 3. The guards follow (below).

Stage 3 — the fixpoint

.fly/bootstrap/stage2/bin/fly builds the compiler library and the fly program once more, with the same profile, into .fly/bootstrap/stage3 (whose lib/ starts as a copy of stage 2's). The library archive and the driver's object must be byte-identical to the ones stage 2 produced. If they differ, the compiler's output depends on which compiler built it: a defect no suite can see, because a suite runs a single compiler. CI runs it on every build.


What compiles what

The provenance of every shipped artifact, as of the next release (0.14.8):

ArtifactCompiled byNotes
fly (compiler + driver)stage 1 flya self-host compiled by a self-host that the seed compiled
fly-lsp, fly-registrystage 2 flythe compiler that ships
standard librarystage 1 flybuilt at stage 2 like the compiler; was the seed's build up to 0.14.7
runtimestage 1 flyall Fly on every platform; on Linux it also carried C objects from the seed up to 0.14.7
lldb, ld.lld, LLVMfork LLVM releaseprovisioned, not compiled

The seed still builds a standard library at stage 1, but only to link the stage 1 compiler, which it also compiles. As soon as that compiler exists it rebuilds the standard library in .fly/bootstrap/stage1/lib, and everything after it — the stage 1 suites and all of stage 2 — uses a library laid out the way the self-hosted compiler lays out classes.

Up to 0.14.7 the shipped standard library was the seed's build. The two compilers disagreed on class layout, and the self-hosted compiler resolved an interface call through a vtable slot computed for its whole module — so a program and a library compiled separately would pick different slots. Every class now carries one table of methods per interface it implements, found by the interface's identity, which both sides of any boundary agree on.

Nothing that ships is compiled by the seed any more. The last piece was the Linux runtime: up to 0.14.7 its archive kept the C primitives of the seed's runtime (sources only in the 0.13.x repository) next to the Fly runtime, made weak so the C definitions won. The Fly runtime defines every one of those primitives, and the self-hosted compiler lowers the runtime's atomic operations to LLVM atomic instructions instead of calls into a libatomic, so the stage 2 archive is the Fly runtime alone. Stage 1 still links the seed's C objects: the seed compiles that runtime and cannot lower the atomics itself.


Guards

Some defects are invisible to ordinary suites, which compile valid programs with a single compiler and run their cases inside one process. Two kinds of check cover them.

The probe suites (from 0.16.0). A probe is a whole program under compiler/probes/<family>/<name>/, a directory no build reads. The suites of compiler/test/probes compile each probe with the fly under test, as a process of its own, run the program it makes, and check what only shows from outside. They run with the compiler's suites, at stage 1 and at stage 2:

SuiteProbesWhat it catches
SemaProbeSuitesema/reject, sema/accept, sema/warna compile-time check that went missing: a program that must be rejected with a specific diagnostic, an edge case that must keep compiling, the warnings a program must get. The seed judges the reject probes too; a probe it is known to accept carries a seed-accepts.txt.
TrapProbeSuitetrapsa run-time trap that went missing: the program must stop with its exit code, message and position.
HeapProbeSuiteheapthe object report at the end of main, compared exactly.
LibProbeSuiteliba function a library defines that a program cannot reach through its header: each probe has a library and a program compiled apart; several libraries (lib_<x>/) each get a directory and a -L; dyn/ is built with --lib-dyn next to the program; reject_<x>/ must not compile against the libraries; suite/ runs against them.

Up to 0.15.x these were the scripts test_sema, test_traps, test_heap and part of test_abi, written twice, in PowerShell and in bash, over ci/*_probes. The probes also hold the programs of the differential campaign that compared the C++ compiler with the first self-host, once kept outside the repository: each one is now an accept, reject or trap probe, and none of them still diverges (fly-lang/fly#201).

The guards. What a suite cannot drive stays a script, at the end of stage 2, and stage 3 follows it:

GuardScriptWhat it catches
Fixpointstage3the compiler generating different code depending on which compiler built it.
Seed library boundarytest_abia calling-convention mismatch between two releases: each probe under ci/abi_probes has a library compiled by the seed and a program compiled by the self-host. Suspended in the 0.16 series: the seed spells the symbols differently, so its library does not link into a program of this tree.
Leakstest_leaksan ownership defect in codegen. Each probe loops millions of times over one pattern and fails above a 20 MB peak, measured from outside the program.
Toolstest_lsp, test_registry, test_toolsthe shipped companion binaries.
Debuggertest_dbgthe bundled lldb starts and drives a program.
Hardeningtest_hardeninga program or a shipped tool that lost a protection of its system: the script reads their headers (ASLR, NX and CET compatibility on Windows; PIE, full RELRO, a non-executable stack and the stack probes on Linux; PIE and a non-executable stack on macOS). From 0.16.0 (fly-lang/fly#320).

Test order is always bottom-up — runtime, std, compiler — so a failure is reported at the lowest broken layer.


Building locally

Linux

. ./ci/linux/stage0.sh      # sourced: exports PATH / LIBRARY_PATH
./ci/linux/stage1.sh
./ci/linux/stage2.sh

Requires libc6-dev and gcc for the host C runtime start-up objects, plus ar and objcopy.

Windows — PowerShell 7 (pwsh), not Windows PowerShell 5.1:

. .\ci\windows\stage0.ps1
.\ci\windows\stage1.ps1
.\ci\windows\stage2.ps1

Useful switches, as environment variables:

VariableEffect
FLY_VERSIONoverride the seed pin
FLY_BUNDLE_LLVM=1self-contained stage: LLVM, ld.lld and lldb copied next to fly (what CI and releases use)
FLY_STAGE1_TESTS=0skip the stage 1 suites
FLY_DEBUG_SYMBOLS=1DWARF in every stage: the seed emits it at stage 1, the self-host from stage 2
STAGE=1|2select the stage for an individual test_* script
FLY=<path>test another compiler binary with a test_* script

Each test_* script can be run on its own with STAGE set. There is no per-artifact build script: a single target is fly build --target <name>.

Layout

.fly/bootstrap/
  llvm/            fork LLVM (stage 0)
  stage0/          the seed: bin/fly + lib/
  stage1/          bin/fly0 (the seed, hard-linked), bin/fly, lib/, .state/gen/fly/package.fly
    work/          the copy of the sources the seed builds, and its target/stage1[-debug]/
  stage2/          bin/, lib/   ← the release; obj/ (the objects stage 3 compares), .state/ (build state)
  stage3/          the compiler library and fly again, built by stage 2
  test/            suite binaries and probe output

The bootstrap lives in .fly/bootstrap/, beside what any fly build of the repository writes in .fly/<profile> (the repository is a Fly project too), so nothing else is written in the checkout and .gitignore names .fly/ alone for it (from 0.16.0; up to 0.15.x this was build/).

A fly binary finds its libraries at <exe>/../lib. That is why stage 1 hard links the seed as .fly/bootstrap/stage1/bin/fly0: the seed then picks up .fly/bootstrap/stage1/lib rather than its own.


Releases

release.yml runs on a push to main. It calls the Linux and Windows build workflows without debug symbols, takes the version from the root Manifest.fly — the single source of truth since 0.15.2 (before, it was compiler/lib/basic/Version.fly; the compiler now reads it through the generated fly.meta.package module) — and publishes fly-<version>-linux-x86_64.tar.gz and fly-<version>-win-x64.zip.


Known limits

These are properties of the bootstrap today, tracked in the roadmap:

  • Platforms. CI builds Linux x86_64, Windows x64 and, from 0.14.9, macOS arm64 (Apple Silicon), each through stage 3. Linux aarch64 (ARM 64-bit Linux) is a later platform. On macOS TLS has no backend yet (tlsAvailable() is false).
  • Debug information without class fields. From 0.14.8 the self-host emits DWARF with --debug-symbols: functions, files, source lines, parameters and locals, enough for breakpoints, backtraces, stepping and frame variable. The fields of a class are not described yet.