The driver turns a command line into a batch: it parses flags into a CompilerInvocation, finds the toolchain's roots and sysroot, runs each input through the pipeline (or hands it to clang), and links. The pipeline stages it calls are specified in Pipeline.md; the link it plans is specified in Linking.md.
| Binary | Source | Role |
|---|---|---|
kairo |
Compiler/Driver/Kairo.k |
The compiler driver. |
kcc |
Compiler/Driver/Kcc.k |
clang-compatible driver over the same CompilerInstance; see Interop.md. |
kld |
Linker/Driver/kld.k |
Out-of-process linker; see Linking.md. |
main in Kairo.k is deliberately thin; driver behaviour lives in CompilerInstance (Compiler/Driver/CompilerInstance.k) and the option structs in Compiler/Config/.
- Scan argv by hand for
--time-passes,--print-stats,--stats-fileand--time-trace. The timer must be armed beforeCompilerInstanceis constructed (the constructor starts the thread pool), and flags are not parsed until then. The scan only decides whether to arm; the trace path comes from the real parse. - Initialize LLVM (all targets, MC layers, asm parsers and printers), the process locale (
LC_CTYPEforced to UTF-8), and the logger. TheInitLLVMguard is a heap object destroyed last inmain, sollvm_shutdownruns while lld's parallel executor is still alive. - Construct
CompilerInstancefrom argv. Flag errors are reported here. - Identity exits:
--help,--version,--explain <code>. - Info queries, which exit without compiling:
--print-targets,--print-cpus,--print-target-info,--print-dirs,--print-clang-args,--print-jobs,--print-from-clang,--print-cfg,--print-runtimes-revision(the LLVM runtimes revision the compiler was built from;<prefix>/share/kairo/runtimes/REVISIONmust equal it before the sysroot generator builds anything). args_ok(): any flag error exits 1.apply_input_options():--input-encodingother than UTF-8 is U009E;--working-dirchanges the process directory (U010E if it cannot), before anything reads the current directory.initialize(cwd): report queued root warnings, build theImportResolver(include dirs, macro search paths, then thebuiltinandstdroots; see Imports.md), start the sharedClangBackendon the pool, and create the header-set cache, header index, foreign registry andPreProcessor.execute()runs the batch.- Write the timing report,
--stats-fileand--time-traceoutputs. An unwritable path is U023E and fails the run.
The exit status is 0 when BatchResult::ok() holds: no error anywhere in the batch. Warnings alone exit 0. Otherwise it is 1, or the sysroot hook's own status (2 refusal, 3 build failure). The numbers are named once: EXIT_OK / EXIT_FAILURE in Compiler/Support/ExitCodes.k, SYS_STATUS_REFUSED / SYS_STATUS_FAILED in Linker/Sysroot/Outcome.k.
CompilerInstance has three constructors, all ending in _adopt:
| Constructor | Used by |
|---|---|
(argc, argv) |
kairo. Parses with CompilerInvocation::from_args. |
(words) |
kcc: kairo CLI words translated from a clang command line, through the same parser. |
(inv) |
Programmatic callers with a built CompilerInvocation. The invocation is copied; print_help is a no-op. |
_adopt copies the invocation, applies it (_apply_invocation) when it parsed cleanly, then creates the thread pool (--threads, 0 = hardware concurrency, capped by --cmax-threads), SourceManager, TokenCacheManager and GlobalDisambigTable. _apply_invocation maps diagnostic policy onto the engine (only pretty, basic and json error formats are accepted; msvc is U021E) and then resolves the environment in this order:
_check_host_flags:--nativewith--targetis U016E;--nativeon a non-Linux host is U017E._use_bundled_sysroot(Sysroot selection)._absolutize_paths: every path option made absolute against the cwd._discover_roots(Root discovery)._check_sysroot._apply_host_system: host include and library dirs, and--pkg-config.
main (Compiler/Driver/Kairo.k) runs execute() on a joined thread with the pool workers' stack, Tuning::WORKER_STACK_BYTES (32 MB; kairo::execute_on_big_stack, Compiler/Native/Thread.hh), so the entry file's parse, Sema and codegen do not depend on the main thread's stack, which the OS sizes (8 MB on Linux) and only a Linux-only setrlimit could raise (a left-deep 30000-term + chain overflowed it in Sema's recursive visitors; release now handles 30000, debug 7500). An exception on that thread is rethrown on the main one. Where no thread with a stack size can be made (Windows, wasm, a failing pthread_create) execute() runs on the calling thread.
execute():
- No inputs is I004F.
_plan_outputsclassifies every input and fixes the output plan (Outputs and the object directory). An unusable input stops the run before anything compiles, every one reported._discover_includesfinds.kfiles that C/C++ inputs include (Compiler/Driver/KairoIncludes.k; see Interop.md)._settle_global_allocator(whether every Kairo file is compiled with__KAIRO_GLOBAL_ALLOCATOR_SET:--global-allocator-setor a-Dof it, a linked object whose ABI note says its file set it, or a file the inputs reach that writes@set_global_allocator(at file scope; a file whose bytes name the attribute is lexed, and only@, the name and(outside every brace count, so a comment, a string or a function body naming it does not), then the Kairo library when linking under--kairo-liband the link holds Kairo code (_link_has_kairo: a.kinput, a C/C++ input that includes a.k, or an object or archive carrying the ABI note,LinkABI::carries_note; a pure C/C++ link is exactly clang's) (Runtime and the Kairo library).- One
FrontendActionper Kairo input, in_kairo_order(). Link inputs go to the link list; C/C++ sources and headers are collected for step 7. Every input runs even after an earlier one failed, as clang does; a fatal setsaborted, which stops what runs after the loop. - When linking: a second script (each script is a
main; see ScriptMode.md) or a second global allocator is an error. - With no errors: compile native inputs (
_compile_sources), then the runtime when Kairo objects were emitted. - Drain and release every clang header set.
- Link (
_link) when the plan links and nothing failed; remove a temporary object directory unless--save-temps. A link input in a run that does not link is L006W.
The diagnostic engine is shared by the whole batch and never reset, so its final counts are the batch totals.
FrontendAction::run advances a TUStage (Compiler/Driver/TUStage.k) strictly forward: Loading, Preprocessing, then parse, Sema and codegen, ending in Done or Failed. Each stage gate compares the engine's error count with the count the previous input left (errors_at_start), so one input's errors never stop the next input's checks.
| Stage | Work | Stops here for |
|---|---|---|
| Load | SourceManager::load_file; failure is I005F. |
|
| Preprocess | The batch-scoped PreProcessor walks the input's import graph once per file across the batch; claim_fid gives each file to the first input that reaches it. Header sets start parsing on the pool. |
--preprocess, --dump-preprocessed |
| Parse | --syntax-only |
|
| Sema | Import groups in dependency order (Pipeline.md). | --type-check-only |
| Codegen | Codegen.md. |
--syntax-only is per file: it parses its inputs and none of their imports, and drops the C++ header sets, but still preprocesses the import closure so imported macros expand. When a C/C++ input includes a Kairo file (kairo_feeds_includes), the Kairo inputs run past the preprocess and parse stops, because the include needs them analysed.
With exactly one Kairo input and no C/C++ input including a .k, the clang header sets are released after Sema, before codegen fans out.
_plan_outputs decides, once per batch:
-onames one output: the executable or library when the run links, the single object (or.s,.ll,.bc,.pch) otherwise.-onaming a directory is I004F. Under a non-linking run, more than one output with-ois I004F; the count is(kairo + source inputs) × emitted kinds + headers, taken before any TU writes.- The run links when it is not
-cor-S, does not stop before codegen, has at least one Kairo, source or link input, and its--emitset links (EmitKinds::links,Compiler/Config/Frontend.k):objalone links; one other kind does not (-onames that file); two or more kinds link only with-o, which then names the executable. The set counts distinct kinds, the one parse codegen also uses, so an empty item or a repeat changes nothing:obj,,,objandobj,objareobj, andasm,asmisasm. An empty set (--emit=) links nothing and is I004F. A build of headers alone writes their.pchfiles and stops. The default link output isa.out. - Objects go to
--cache-dir(kept). Without it, a linking run uses a temporary directory removed after the link, or<output>.kobjunder--save-temps; a non-linking run uses the working directory. - With
--emitnaming kinds besidesobjon a linking run, objects are still produced for the link; the listed kinds are outputs, written beside the executable when the object directory is temporary. --emit-scope=graph(default) writes objects for every Kairo file the run reaches;inputswrites them for the command line's own files only, asclang -cdoes.- Two C/C++ inputs with one stem (or two headers with one file name) are U013E; clang silently overwrites, Kairo refuses. A Kairo entry TU of the same stem writes
<stem>.k.<ext>instead and does not clash.
The runtime is the C and C++ sources directly in <runtime root>/rt, where the runtime root is the parent of the builtin directory. They compile on Kairo's own hand-built cc1 line (ClangBackend::native_invocation), not clang's driver, because they are part of Kairo's code and must be built as Kairo TUs are. Runtime objects never take -o. The batch starts the runtime compile on the pool as soon as the first action reaches Sema (prestart_runtime) and joins it after codegen.
Runtime objects are cached under <cache root>/rt, keyed by the exact cc1 line and a fingerprint: the compiler binary, every file in rt/, and the vendored mimalloc sources, each stamped with size and mtime. Objects are copied in and out of the cache, never linked from it.
--kairo-lib (set by kcc when it links) links builtin, std and the runtime lazily from a library built once per target and option set, under <cache root>/lib/<triple>-<hash>. The hash covers the compiler path, the triple, the target, codegen and optimization words, debug info, the builtin and std directories, and --global-allocator-set. A manifest lists the objects, an optional archive, and every source with its mtime; any change rebuilds. The library is built into a private directory and renamed into place, so concurrent builds never see half of one. Its objects are linked lazily (Linking.md), so a run's own copy of builtin wins.
The cache root is $KAIRO_CACHE_DIR, else $XDG_CACHE_HOME/kairo, else ~/.cache/kairo, else %LOCALAPPDATA%/kairo, with / separators on every host. No root means nothing is cached.
Every kairo flag is defined in register_kairo_flags (Compiler/Command/FlagRegistry.k); --help is generated from that table, so this document does not list flags. The parser is Command (Compiler/Command/Command.k), its results are ParsedArgs (Compiler/Command/ParsedArgs.k), and the typed option structs are built from them in Compiler/Config/ (FrontendOptions, LanguageOptions, TargetOptions, CodeGenOptions, LinkerOptions, PreprocessorOptions, DiagnosticOptions, gathered in CompilerInvocation).
Flag kinds (FlagKind, Compiler/Command/FlagUtils.k):
| Kind | Shape |
|---|---|
Bool |
presence |
String, Path |
one value; str_c restricts it to a choice list |
I32, F64 |
number with optional bounds |
StringList |
repeatable; each occurrence appends |
PathList |
the positional inputs |
Feature |
-f<name> / -fno-<name>, read as -1 unset, 0 off, 1 on |
Config limits (define_cfg) are stored under the key c:<name> and spelled --c<name>=<value> (for example --cmax-errors=50).
Accepted spellings: --flag, --flag=value, --flag value, short flags and clusters (-abc), joined short values (-ofile), and long single-dash flags for the prefixes W O f m X s D A E F g I L l Z (-Wall, -O2, -march=..., -DFOO=1). -- ends options. - is the stdin input. -x <lang> (--language) tags every following input until the next -x, with
clang's positional semantics (Command::tag_positionals_with, ParsedArgs::file_langs); -x none restores classification by extension.
Several letters keep Kairo meanings that differ from clang's: -I is a Kairo import root and a clang -I; -D is a Kairo macro, never forwarded to clang (--cxx-define is clang's); -E, -A, -F, -W<code> are the deny, allow, forbid and warn diagnostic policies (Diagnostics.md).
Groups in the registry: identity and info queries, input, output, target, build mode, optimization, reproducible builds, language semantics (hard switches), language feature toggles (-f), defines and config, warnings and diagnostics, BCIR/AMT internals, incremental compilation, module system, linking, per-platform link options (Darwin, Windows, WASM, embedded), testing, verbosity, developer dumps, and compiler limits.
Two feature toggles split the conversion rules: -f[no-]implicit-conv is the Kairo-side implicit conversions (the primitive lattice's W class and ABI widening; on by default), and -f[no-]cxx-implicit-conv lets C++'s own implicit conversions apply where a value crosses into C++ (one user-defined conversion, a literal crossing as const char[N]; off by default, so such a crossing needs as T). Neither implies the other (ExprTyping.md).
A flag that changes a language rule (-fno-implicit-conv, -fcxx-implicit-conv, -funwind, -fduck-generics, -fstrict-cxx-moves (on unless -fno-strict-cxx-moves), the --no-* gates and --max-* limits) applies to the user's files only. Lib/builtin and Lib/std always compile in one fixed mode, whatever the user passes: every pass asks SemaContext::in_library (VisibilityFacts::is_library_tu, the driver's stamp). So under -funwind a library function is still noexcept unless it is marked @unwind (FunctionDecl::no_unwind, which every TU that declares the function reads), and a C++ exception thrown through such a frame (a callback the library calls) terminates. Codegen's own -funwind reading (EmitOptions::unwind: closures' call operators, __kairo_fn's call path, witness and role functions) still follows the user's flag everywhere: those are spelled by every TU that emits them, so splitting them by TU could give one entity two spellings. Not yet under this rule: -finfer-purity (-fno-infer-purity turns inference off in the library too; exempting it changes Tests/Codegen/purity/infer_purity.k).
A flag the parser accepts but nothing consumes is registered with cmd->mark_unimplemented(key) at the end of register_kairo_flags (c:<name> for config limits, the bare name for features). Setting one emits U005W ("accepted but not yet implemented; it has no effect"), once per flag, and --help renders it with [not implemented], so a flag never appears to do something it does not. The entry is removed in the same change that wires the flag's consumer. An unknown key in the list is ignored, so a typo there cannot abort the compiler.
The Sema agreement pass (SemaAgreement.md) runs in each TU's clang invocation unless -fno-sema-agreement is given. -fsema-agreement makes it report (I012W); with neither flag a debug-built compiler reports and a release-built one only counts. -fsema-agreement-repair (on) lets it point a call clang bound differently at Kairo's declaration; -fno-sema-agreement-repair leaves every call alone. All are CodeGenOptions fields and apply to every TU, the library's included. --test-sema-agreement-swap=<from>:<to>, --test-sema-agreement-quiet and --test-sema-agreement-no-adl are the pass's hidden test hooks. --sema-agreement-summary (hidden) prints kairo: sema agreement: N finding(s) in M of T file(s) (kind n, ...) after codegen, or off. With -v the driver logs incremental: not stored <file>: <reason> for each object it compiled and did not store.
An internal flag, one that exists for the compiler's own build rather than for users, is registered with cmd->mark_hidden(key) (same key forms). It parses and works as usual; --help leaves it out. The first is -fduck-generics (off by default so user code gets real bounds; build.py passes it for the compiler's own targets).
--help-hidden lists the flags a user is not offered or cannot use yet: the internal ones, tagged [internal], and the not-implemented ones, tagged [not implemented] (these also appear in --help). It exits like --help.
The list covers: the BCIR, pass-pipeline, regalloc, generics and type-info dumps and verifiers; the module cache (incremental compilation phase 1 is implemented: --cache-dir, Incremental.md); the test runner (--test*); --host and --cxx-abi; --borrow-check, --amt-*, --panic-handler, --cfg, --edition (validated, never read); the language gates with no check point (--no-assert, --no-eval, --no-generics, -fverify-interfaces, -fstrict-null, ...); sanitizers, coverage, PGO, XRay, LTO (objects are never bitcode) and other codegen features with no cc1 spelling or no runtime archive in the bundled sysroots; WASI and some wasm feature flags; --ctab-width, --diag-cascade; and config limits nothing enforces.
The primary action is one FrontendActionKind (Compiler/Config/Frontend.k), chosen by the highest-priority flag given:
| Priority | Flag | Action |
|---|---|---|
| 1 | --preprocess, --dump-preprocessed |
Preprocess |
| 2 | --syntax-only |
SyntaxOnly |
| 3 | --type-check-only |
TypeCheckOnly |
| 4 | -c |
EmitObjOnly |
| 5 | -S, or --emit=asm alone |
EmitAsm |
| 6 | --emit=llvm-ir alone |
EmitLLVMIR |
| 7 | --emit=llvm-bc alone |
EmitLLVMBC |
| default | EmitObj (compile and link) |
--emit takes a comma list of obj, asm, llvm-ir, llvm-bc; any other item is U003E. -S beside an explicit --emit adds asm; alone it replaces obj. Each TU writes <module>.o/.s/.ll/.bc under the object directory.
Dump flags are orthogonal to the action and fire at fixed points:
| Flag | Output | Point |
|---|---|---|
--dump-preprocessed, --print-tokens, --print-macros, --print-imports |
token stream, macros, import graph and overlays | after preprocessing |
--emit-ast-bin=<path>, --ast-roundtrip, --verify-ast-roundtrip |
the entry file's serialized AST; every parsed tree replaced by, or compared with, its reloaded one (Serialization.md) | after parsing, before the AST dumps |
--print-ast=tree|flat|canonical, --print-symbols |
AST, frozen symbol tables | after parsing |
--print-sema |
every resolved binding | after the checks, before any lowering |
--print-lowered |
the same dump after the Lower/ passes |
after lowering |
--print-unchecked-assumptions |
per TU, one line per C++ call that borrows a MOVE argument although its body could not be inspected: file:line:col: unchecked assumption: 'f' is assumed not to move out of 'h': <why>, or, for a call into the toolchain's own standard library, 'f' is a C++ standard library function whose body is not visible: assumed not to move out of 'h' (Ownership.md). The standard library's calls are listed under the default -fstrict-cxx-moves too; the others are listed under -fno-strict-cxx-moves, and in Lib/builtin / Lib/std, which always borrow, since strict moves take such a call to move the argument instead of assuming |
after the checks, beside --print-sema |
--dump-interface=<path.json>, --dump-deps=<path.json> |
every file's interface hashes and name sets; every file's dependency rows (IncrementalSema.md) | the interface at Checked, before any lowering; the rows and both files at the end of Sema |
--emit-headers-line-directives (hidden) |
with --emit-headers: #line before each header line naming its Kairo file and line, invented text under the header's own name; an unchanged header is not rewritten |
for kals' clangd bridge only (LanguageServer.md section 15); a plain --emit-headers stays directive-free |
--emit-headers-layout (hidden) |
with --emit-headers: each declaration on the same physical line as in its .k file, invented text in <header>.prelude included from line 1; for clangd, which ignores #line |
kals only |
--print-emit-plan, --print-cxx, --emit-headers=<dir> |
EmitPlan, C++ preamble text, interop headers | after Sema; headers only without errors, each header and prelude written to a temporary beside it and renamed into place |
--print-cg, --print-ir |
codegen token stream, generated C++ | in codegen (forces one worker) |
--print-panic-sets |
every Panic<T, E...> instance |
after Sema |
--dump-interface and --dump-deps are the facts of incremental Sema's phase 1, for Scripts/incr_oracle.py and the tests under Tests/Sema/Incremental/; nothing reads them back. Either one turns dependency logging on for the run (about 7% more CPU on the compiler's own tree); without them no log exists and Sema does what it did. Both files are sorted throughout and hold no absolute path but a top-level root, the working directory: a file's path is relative to it, and empty for a file outside it, which its module path still names. A file that cannot be written is U001E.
The three serialized-AST flags are for testing the format, not for output a user keeps: --emit-ast-bin writes only the entry file's tree, --ast-roundtrip (also KAIRO_AST_ROUNDTRIP=1 in the environment, which is how Scripts/lit_ast_roundtrip.py runs the unmodified test suite under it) makes Sema and every later stage, the AST dumps included, run on trees the reader built, and --verify-ast-roundtrip reports a tree that does not survive the round trip as I002F and leaves the parsed tree in place.
Each dump covers every TU, including builtin and std. Its exact pinning to Sema phases is specified in Pipeline.md.
What were environment variables during the compile-time and parallel-Sema measurements are flags, so a run is reproducible from its command line:
| Flag | Meaning |
|---|---|
--sema-threads=N |
threads that run Sema over the import groups; 0 (default) is --threads, 1 is the one-thread loop (the byte-identity baseline) |
--codegen-threads=N |
threads of the per-TU C++ compiles; 0 (default) is --threads; always capped at the TU count |
--interface-pch=auto|on|off |
the per-flavour interface PCH (Codegen.md); auto (default) builds it only for 2+ TUs and 1 MiB of Kairo; under --verbose a failed build's clang diagnostics are shown |
-fno-shared-header-set |
one clang parse per header list instead of one for the union (Interop.md) |
--print-stats also prints the memory probes (arena bytes by kind, largest TU, header-set AST bytes, per-stage Sema arena growth) and, with -finfer-purity, the inferred const / pure counts (Performance.md), and, when the build has an import cycle and no error, [stats] import-cycle refolds: N, the re-export fixpoint rounds summed over the cycles' files (one per file per round of the group's I; the same at every thread count). NO_COLOR, KAIRO_DEBUG_FORCE_ICE (test hook) and KAIRO_CLANG_TRACE_DIR stay environment variables.
--progress=auto|always|never|lines (Driver/Progress.k, engine in Native/Progress.hh) reports how far a run is, on stderr. Three phases report: parse (one unit per file), sema (one per file, counted when its import group finishes) and codegen (one per Kairo TU). Counters are atomics bumped when a task ends.
| Value | Behaviour |
|---|---|
auto (default) |
a pinned status line whenever stderr is a terminal and TERM is not dumb (NO_COLOR and --verbose do not turn it off); nothing otherwise, so piped output and the lit tests see none |
always |
the pinned line regardless of those checks (a console that cannot take ANSI sequences still gets none) |
never |
nothing |
lines |
one plain line per event, below |
The pinned line is [sema 212/338] Sema/Type/CallTyping.k 0:09, redrawn about every 100 ms from one thread, never wrapped. In the codegen phase it reads [codegen 41/338, 297 reused]: done includes the TUs the incremental object cache already held (Incremental.md). The line is erased before the diagnostics print and stays off until the last one is out, so diagnostic text never shares its row; it is also gone before the codegen post-pass prints, and on an ICE. On Windows the driver enables virtual-terminal processing on the console (Native/Win32.cpp), and falls back to never where that fails.
lines writes events to stderr, each one whole line. The format is stable:
@progress phase=<parse|sema|codegen> done=<n> total=<n> [reused=<n>] [file=<path>]
- The prefix
@progressstarts no diagnostic (those begin with a path orkairo:). Every other stderr line is not an event. - Keys come in this order, separated by one space.
reusedappears oncodegenlines only (always, 0 when nothing was reused).fileis last and runs to the end of the line (it may hold spaces); it is the file that just finished, relative to the working directory when under it, and is absent on the event that opens a phase and onreusedupdates. - A phase opens with
done=0.doneis monotonic within a phase and reachestotalwhen the phase ends. A run with several inputs repeats each phase per input. - A
semaevent names the first file of the import group that finished and advancesdoneby the group's size, sodonecan step by more than one.
On a terminal build.py runs every compiler that lists the flag in --help-hidden on a pty with --progress=always, passes the compiler's own redraws through to the terminal and keeps every other stderr line in the log; without a pty (Windows) it passes --progress=lines and draws its own line from the events. Off a terminal it passes --progress=lines and only the log is written. An unknown value is U001E.
Compiler/Command/FlagTranslator.k is the only file that spells a clang flag for a Kairo option. Each concern (target, codegen, optimization, debug info, warnings, preprocessor, language standard) is one function that decides once what was asked for and writes it in a requested spelling:
| Spelling | Consumer |
|---|---|
| cc1 | Kairo's own TUs and the ffi header sets (ClangBackend::_cc1_common). |
| Driver | C/C++ inputs handed to clang's driver, and the target-defaults probe. |
reverse (from_driver) |
kcc: a clang command line parsed with clang's own option table into Kairo words, which then go through the ordinary parser. |
Order is part of the contract: cc1 takes the last of a repeated option, so broad switches come before specific ones and --mllvm words come last. --print-from-clang -- <clang argv> prints the reverse translation. The header-search roots are not flags and are not spelled here: they are a property of the sysroot, owned by ClangBackend (Interop.md).
A staged tree (BuildSystem.md) runs with no root flags. Everything is found relative to the toolchain prefix, the directory above the resolved binary (<prefix>/bin/kairo; a symlink on PATH still finds its tree):
| Root | Default | Flag |
|---|---|---|
| clang resource dir | highest-versioned <prefix>/lib/clang/<ver> holding include/stddef.h (versions compare numerically) |
--resource-dir |
| builtin tree | <prefix>/lib/builtin |
--builtins-dir |
| standard library | <prefix>/lib/std |
--std-dir |
| bundled sysroots | <prefix>/sysroot/<triple> |
--sysroot |
| out-of-process linker | <prefix>/bin/kld[.exe] |
|
| sysroot generator inputs | <prefix>/share/kairo/sysroot |
$KAIRO_SYSROOT_SHARE |
| sysroot generator cache | <toolchain cache root>/sysroot |
$KAIRO_SYSROOT_CACHE |
_discover_roots fills the roots that were not given. A flag always wins. A given --resource-dir keeps its older meaning too: its builtin/ and std/ are used when they exist, before the layout. No builtin root is searched under --no-prelude (or --freestanding), and no std root under --no-stdlib.
Between the flag and those defaults comes the library an input lies in (_input_library_dir). A Kairo input whose nearest pinned module.k (walking up from its directory, as ImportResolver::_library_of does) says module builtin or module kairo::std makes that directory the builtin or std root. A source checkout's Lib/std/io.k is therefore checked against that checkout's Lib/std, and the toolchain's staged copy is not registered beside it: the two would claim one root name (R018E). Directories compare by real path. Inputs in two different copies of one library select nothing, so the default is registered and the resolver reports the copies. When only one library is selected this way, the directory beside it (same parent) whose module.k pins the other driver library is registered as that library's root too (_sibling_library_dir; the first by name if several qualify), so a checkout's Lib/std is checked against that checkout's Lib/builtin. A flag for a library still wins for that library, and a copy of the other library found as an input is used as it is. The current directory's pinned subdirectories never replace a toolchain library. A file outside every library is unaffected. The runtime root (_runtime_root, the builtin root's parent) follows the builtin root, so a checkout's builtin uses that checkout's Lib/rt headers.
A root still missing is P008W with a P008N naming the flag to pass. The warnings are queued and reported by initialize(), so --help, --version and the info queries never print them. Without the builtin tree every lang item is unbound and string does not resolve; the warning keeps that from reading as a Sema bug.
Without --target, the build compiles for get_default_target_triple(), the host's architecture and OS with a C library the toolchain ships; the table per host and the reason the Linux vendor is always unknown are in Portability.md. An explicit --target is canonicalized once, first in _apply_invocation (_canonical_target): a glibc version (-glibc2.28, Zig's .2.28) moves to TargetOptions::glibc, where only the sysroot lookups read it, and a macOS triple with no OS version gets its deployment floor (11.0 arm64, 10.15 x86_64; Toolchain::with_os_floor), since clang's default has no thread-local storage. _effective_triple then applies Toolchain::canonical_triple so clang, codegen and the linker see one float ABI; the default target on 32-bit Arm Linux is hard-float for the same reason. -m32/-m64 rewrite the triple's architecture, as clang does.
The sysroot is chosen by _use_bundled_sysroot:
--native: the host is the sysroot (/); the bundle for the host's C library still supplies libc++ and compiler-rt.--target, or no--sysroot:<prefix>/sysroot/<triple>, or the one directory there naming the same target in another spelling (same arch, sub-arch, OS and environment). A bundle that is not a directory, lacksSYSROOT.toml, or has a malformed one is P007E.--sysrootwithout--target: that directory, as given.- No bundle and no
--sysroot, for--targetand for the default target alike (a macOS or BSD archive ships no root for its own host): the sysroot generator's cache, then itsensurein-process; macOS and MSVC roots are staged from the installed SDK plus the shipped overlay (SysRoot.md) (_sysroot_from_generator; SysRoot.md). A refusal is P009E and exit 2;--no-sysroot-buildmakes the missing root that refusal instead of a build. Without a share tree to build from, the run keeps the P008W below. The order is: a bundled<prefix>/sysroot/<name>, then the cache'sby-name/<name>(also reported by--version, which never builds), then the build; the build is skipped for--help,--version,--explainand the info queries. A build that fails is P010E and exit 3. When a sysroot'sSYSROOT.tomlrecordsclang_triple, clang's-triple, the driver probe and C/C++--targetuse it instead of the name's triple. The target-spelling grammar, the stages and the cache layout are SysRoot.md; the user-facing view is theksyspage of the website docs.
_check_sysroot then reports P008W when no sysroot was found or the given one is not a directory, unless --freestanding asked for none or --macos-sdk stands in for one.
_apply_host_system sets TargetOptions::host_mode:
| Mode | When | Effect |
|---|---|---|
floor |
Linux host, same arch, no --target, --sysroot or --freestanding |
Host system include and library dirs are searched after the bundled sysroot, so a header or library the sysroot lacks comes from the host while libc never does. LIBRARY_PATH, NIX_CFLAGS_COMPILE and NIX_LDFLAGS are read. |
native |
--native |
The host's C library, headers and libraries, with the bundled libc++ and compiler-rt. |
| hermetic | anything else | No host dir and no host environment search path. A cross build is the same on every machine. |
--pkg-config <name> runs pkg-config and routes its words: -I/-isystem to --ffi-include, -D/-U to --cxx-define/--cxx-undefine, -L, -l, -pthread, -Wl, to the link. A -I or -L naming a host system dir is dropped (it would come before the sysroot). With --target it requires PKG_CONFIG_SYSROOT_DIR or PKG_CONFIG_LIBDIR (U018E); a failed query is U019E.
classify_input (Compiler/Driver/InputKinds.k) is the one classifier for command-line inputs and runtime sources. It uses clang's own type table (clang::driver::types), so an input clang would accept is classified as clang classifies it and -x takes clang's type names. Kairo adds .k and the link inputs clang has no type for.
| Class | Inputs | Handling |
|---|---|---|
Kairo |
.k, or -x kairo/-x k on a .k |
FrontendAction |
Source |
C, C++, ObjC/ObjC++, OpenCL, HLSL, CUDA, HIP, LLVM IR, preprocessed, assembly (.s, .S), .pcm, .ast, .pch |
clang's driver (below) |
Header |
clang's precompile-only types (.h, .hh, .hpp, ...) |
precompiled to <file>.pch; never linked |
LinkInput |
.o, .obj, .lib (clang's TY_Object), .a, .so, .so.N..., .dylib, .dll, .tbd, .res |
the link |
Unsupported |
a clang type clang does not compile (Fortran), or -x kairo on a non-.k |
U011E |
Unknown |
no recognized extension and no -x; - without -x |
U011E, naming -x |
BadLanguage |
-x naming no language |
U011E |
An unknown extension is an error, a deliberate deviation from clang, which treats it as an object and lets the linker print an unreadable message. Path::extension() keeps the dot; input_ext strips it once, and every comparison uses the bare spelling.
-x kairo is refused on any extension but .k: module naming, --emit-headers paths and the object-directory mirror all derive from the .k stem.
ClangJobs (Compiler/Interop/Clang/ClangJobs.k) compiles every Source and Header input. It builds a clang driver argv (FlagTranslator::to_driver plus the backend's sysroot words), calls Driver::BuildCompilation, and runs each planned job itself:
| Job | Execution |
|---|---|
clang -cc1 |
In process: CompilerInvocation::CreateFromArgs, a fresh CompilerInstance over the backend's shared VFS, and ExecuteCompilerInvocation with the #include "foo.k" hook (Interop.md). |
clang -cc1as |
In process, through clang's own cc1as_main, compiled into every Kairo binary (Native/Cc1As.hh). An .S is a cc1 -E job followed by a cc1as job. |
| any other tool | Spawned with Command::Execute, exactly as clang's driver would (ptxas and fatbinary for CUDA, the offload tools for HIP, an external as under -fno-integrated-as). |
The jobs are read from Compilation::getJobs(); Command::Execute is used only for external tools, because clang forces out-of-process execution whenever a compilation has more than one job. Every argv carries -c, -S, -E or -fsyntax-only, so clang never plans a link: Kairo's linker is the only linker. -disable-free is cleared, because this process keeps compiling after the job. -mllvm words are kept and re-parsed per job, as under clang. On a hermetic --target build, the words clang's driver took from CPATH, C_INCLUDE_PATH, CPLUS_INCLUDE_PATH and OBJC_INCLUDE_PATH are removed. Each cc1 job has its own ClangDiagAdapter, so a C/C++ input's errors render like Kairo's and one bad input does not fail the next.
Defaults for a C/C++ input are clang's: the translator forwards only Kairo flags the user gave, plus --target, which is always the build's effective triple (the driver would otherwise pick the host's, and the objects would not match the Kairo objects in the same link). --cxx-flags words are appended last and reach only the C/C++ compiles. --c-std (default c17) and --cxx-std (default c++23) select the standard per input language. IR and preprocessed inputs get no header roots.
Native inputs follow the batch's action: -c writes .o, -S .s, --emit kinds .ll/.bc (one driver run per kind), --preprocess runs -E to stdout or -o, and --syntax-only and --type-check-only both run -fsyntax-only. An input with no such phase (IR and .i/.ii under --preprocess, IR under a check-only run) is skipped with U012W, as clang does. Outputs are named <object dir>/<stem>.<ext>; a header's is <file>.pch (foo.h writes foo.h.pch, so it never collides with foo.c), kept in the working directory when the object directory is temporary. --print-clang-args prints each native input's planned cc1 line after the Kairo line; --print-jobs adds the planned link (kcc's -###).
-E <code> denies a diagnostic. A clang user typing kairo -E foo.cc gets "deny foo.cc"; --preprocess is the preprocess spelling.
Off by default; --incremental-sema or KAIRO_INCR_SEMA=1 turns it on (IncrementalSema.md). With --cache-dir, a run of one Kairo input whose only frontend outputs are the link's objects (or none, under --type-check-only) keeps a manifest and one record per module under <cache-dir>/kairo-cache-v<CACHE_SCHEMA>/mod/. CompilerInstance::execute decides before the action loop: when every recorded input is unchanged the action is replaced by the recorded result (objects, ffi_pthread, the frontend's rendered diagnostics, its warning count) and the library, C/C++ inputs, runtime and link run as always. Any miss runs the full pipeline and rewrites the records; a run that reported an error writes them marked, as the baseline for the next run's plan and never as a hit (miss: the recorded run had errors).
--print-incr-statusprints the verdict to stdout:incremental: hit, N modules,incremental: miss: <first reason>(no record,flags changed,environment changed,f.diag changed,compiler changed: <path>,changed: <path>,removed: <path>,added: <path>,graph changed: <dir>,object changed: <path>) orincremental: miss: not eligible (<why>).--incr-verify: on a would-be hit the full pipeline runs anyway and its diagnostics, warning count, object list, per-module object keys andI(m)are compared with the records; a difference is U001E (--incr-verify: the records claim a hit, but a full run differs in ...). A verified hit printsincremental: hit, N modules, verified.--print-incr-plan: after a miss, the re-check set the stored dependency rows give for what changed (IncrementalSema.md):incremental: plan: re-check K of N modules, then oneincremental: plan: <module>: <rule and what it matched>line per module, sorted; orincremental: plan: re-check all N modules (<why>)when something moved that the rows cannot see. Nothing is reused on it: the run is a full run. With--incr-verifythe plan is proven against that run (every module left out has the interface and diagnostics of its record:incremental: plan: verified, M modules as recorded), and a module that differs is U001E (--incr-verify: the dependency rows leave a module out of the re-check set ...), with the records left as they were.--incr-replay=<script>(with--incr-replay-dump=<dir>): a test mode, independent of--incremental-sema. The entry is type-checked once, then each step of the script edits files on disk and the same process brings the result up to date throughTooling::LiveCheck(IncrementalSema.md). Per step:incr-replay: step <n> <name>: <mode> (<why>), <files> files, <errors> errors, <ms> mson stdout,== incr-replay step <n> <name>and the diagnostics on stderr.Scripts/incr_replay_check.pycompares every step with a fresh run. With--incr-replay-in-placean edit is re-checked in place where that is sound (refresh (K of N files)). Three more, all test modes (IncrementalSema.md):--incr-replay-overlayhands each edit of an existing.kfile to the live check as a buffer and leaves the disk as it was;--incr-replay-observeinstalls a Sema observer and printschecked <file>/lowered <file>for each call;--incr-replay-two-tierre-checks each edited file as its edit is read (incr-replay: fg <file>: <mode> (<why>), <ms> ms, and== incr-replay fg <n> <file>with that file's diagnostics on stderr), queues the files that import it, honoursdrain <n>lines in the script, and prints the step once the queue is empty.--incr-paranoidhashes every recorded file instead of trusting an unchanged size and mtime.- Not eligible (always a full run): no
--cache-dir, more than one Kairo input or one found through an include, a C++ input that includes a.k, objects in a temporary directory,--emitbeside the link or of.s/.ll/.bc,--print-cg/--print-ir/--force-recompile, any--print-*/--dump-*flag, a run stopping before Sema.