cabinpkg 0.17.0

A package manager and build system for C/C++
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
//! Orchestration for invoking Ninja and post-processing its output.
//!
//! The build planner (`cabin-build`) writes a Ninja file; this
//! module spawns Ninja against it, tees the child's stdout/stderr
//! through the user's terminal while capturing every byte for
//! post-failure diagnostics, and emits the cargo-style
//! `Compiling <pkg> v<ver>` headers off Ninja's `[N/M] …` progress
//! lines.  When Ninja exits non-zero,
//! [`emit_link_diagnostic_if_applicable`] inspects the captured
//! streams and prints a one-shot link-failure hint when the
//! recognizable shape is present.
//!
//! Shared by `cabin build` / `cabin clean` (in
//! [`crate::cli`]), `cabin run` (in [`crate::cli::run`]), and
//! `cabin test` (in [`crate::cli::test`]) so each command renders
//! Ninja output the same way.

use std::collections::{BTreeSet, HashMap, HashSet};

use anyhow::Context as _;

use crate::cli::term_verbosity::Reporter;

/// Run Ninja and filter its housekeeping lines (`ninja: Entering
/// directory …`, `ninja: no work to do.`, `[N/M] …` progress)
/// from stdout so the default surface stays terse.  Compiler
/// warnings, errors, and any non-housekeeping line from Ninja or
/// the toolchain pass through unchanged on stdout (Ninja funnels
/// each action's own stdout/stderr onto its stdout stream).
///
/// Verbose mode (`-v`) restores the full Ninja output so users
/// who want to inspect the backend's progress have a knob.
/// Outcome of one [`run_ninja`] invocation.  Both `stdout` and
/// `stderr` are captured-and-teed: every line the child wrote
/// was streamed to the user's terminal in real time AND
/// accumulated here, so post-failure diagnostics (e.g.
/// [`cabin_build::link_diagnostics::diagnose`]) have something
/// to parse.
///
/// Ninja sends *all* of a failed action's output - the
/// `FAILED:` banner, the recreated command line, and the
/// compiler/linker diagnostics - to stdout, not stderr.
/// Diagnostics that care about link failures therefore need
/// the captured stdout and stderr.
pub(crate) struct NinjaRun {
    pub status: std::process::ExitStatus,
    pub stdout: String,
    pub stderr: String,
}

/// Path to the running `cabin` executable, used as the `cabin stamp`
/// runner the syntax-check Ninja rule invokes to run the compiler and
/// stamp its output without a shell (see [`crate::stamp`]).  Falls back to
/// the bare name `cabin` (resolved via `PATH`) only if the current-exe
/// lookup fails, which should not happen in practice.
pub(crate) fn check_stamp_runner() -> std::path::PathBuf {
    std::env::current_exe().unwrap_or_else(|_| std::path::PathBuf::from("cabin"))
}

/// Whether to overlay the *auto-discovered* MSVC install's
/// `INCLUDE` / `LIB` / `PATH` onto the Ninja child. `cxx_kind` is the
/// detected compiler family.
///
/// - `clang-cl` ships no CRT/SDK headers of its own, so it *always*
///   borrows an installed MSVC toolset's `INCLUDE` / `LIB` / `PATH`,
///   however it was spelled - apply the discovered overlay unconditionally
///   for it.  This is the same environment a bare-name `clang-cl` already
///   receives via the arm below; without it, an explicitly-*pinned*
///   `clang-cl` path silently falls through to the `Path` arm (a
///   `clang-cl.exe` can never equal the discovered `cl.exe`) and loses the
///   overlay, so the same compiler builds or fails based only on whether
///   it was named or pathed.  Applying it also means `clang-cl` uses
///   Cabin's discovered toolset rather than its own auto-probe - the
///   intended unification with the other two cases, and safe because the
///   overlay only fires when no MSVC environment was supplied at all.
/// - A bare-name / auto-discovered compiler *is* the discovered install,
///   so its overlay is the right environment - apply it.
/// - An explicitly pinned `cl` path takes the overlay only when it is the
///   discovered install.  Unlike `clang-cl`, `cl.exe` *is* a complete
///   toolset, so a separately discovered install could be a different
///   Visual Studio toolset whose headers/libs must not be mixed into the
///   user's chosen compiler.  But when the pinned path *is* the discovered
///   install - the common case on Windows outside a Developer Command
///   Prompt - the compile still needs that install's
///   `INCLUDE` / `LIB` / `PATH`, so apply it there too.
///
/// The overlay only ever has an effect when an install was discovered
/// (Windows, outside an activated environment - see
/// [`cabin_toolchain::msvc_environment`]); inside a Developer Command
/// Prompt it is a no-op, so this never overrides a deliberately activated
/// toolset. (`VSLANG` is applied regardless.)
pub(crate) fn discovered_msvc_install_applies(
    toolchain: &cabin_core::ResolvedToolchain,
    cxx_kind: cabin_core::CompilerKind,
) -> bool {
    if cxx_kind == cabin_core::CompilerKind::ClangCl {
        return true;
    }
    match &toolchain.cxx.spec {
        cabin_core::ToolSpec::Name(_) => true,
        cabin_core::ToolSpec::Path(_) => {
            cabin_toolchain::path_is_discovered_msvc_cl(toolchain.cxx.path.as_std_path())
        }
    }
}

pub(crate) fn run_ninja(
    cmd: &mut std::process::Command,
    profile_build_root: &std::path::Path,
    reporter: Reporter,
    graph: &cabin_workspace::PackageGraph,
    dialect: cabin_build::Dialect,
    apply_discovered_msvc_install: bool,
) -> std::io::Result<NinjaRun> {
    use std::io::{BufRead, BufReader, Write as _};
    use std::process::Stdio;

    // Only an MSVC build graph gets the MSVC environment overlay
    // (`VSLANG`, and the auto-discovered `INCLUDE` / `LIB` / `PATH`).
    // A GNU-style toolchain on Windows must run in the environment it
    // was resolved under: overlaying MSVC headers/libs and PATH could
    // silently switch `clang++` / `g++` to MSVC behavior while Cabin is
    // still emitting `.a` archives and GNU link lines.  Empty (a no-op)
    // off Windows.  See `cabin_toolchain::msvc_environment`.
    if dialect == cabin_build::Dialect::Msvc {
        for (key, value) in cabin_toolchain::msvc_environment(apply_discovered_msvc_install) {
            cmd.env(key, value);
        }
    }

    // Verbose modes (`-v` / `-vv`) keep every line Ninja
    // emits - the `[N/M] …` progress prefix, the `Entering
    // directory` banner, the `no work to do.` reassurance - so
    // raising verbosity never makes the surface smaller.  The
    // `Compiling` banner is still printed in those modes from
    // the same per-package detection used at the default
    // verbosity; users opting into `-v` see the cargo-style
    // headers interleaved with the raw Ninja output.
    let keep_ninja_chatter = reporter.verbosity().shows_verbose();

    // Lookup table from package name to the workspace `WorkspacePackage`
    // entry.  We resolve each `[N/M] …` progress line back to its
    // owning package by the `/packages/<name>/` segment the
    // planner embeds in every output path, then announce the
    // `Compiling` banner the first time a given package shows
    // up.  Tying announcements to Ninja's own progress keeps the
    // banner temporally accurate - header-only libraries that
    // contribute no actions never get a `Compiling` line because
    // they never appear in Ninja's output.
    let pkg_by_name: HashMap<&str, &cabin_workspace::WorkspacePackage> = graph
        .packages
        .iter()
        .map(|pkg| (pkg.package.name.as_str(), pkg))
        .collect();
    let mut announced: HashSet<String> = HashSet::new();
    // Non-UTF-8 build roots fall back to the unanchored search;
    // the planner rejects non-UTF-8 paths long before Ninja runs.
    let profile_root_str = profile_build_root.to_str();

    let mut child = cmd
        .stdout(Stdio::piped())
        // `stderr` is piped so cabin can tee it: every line
        // streams to the user's terminal in real time AND is
        // accumulated in a buffer so post-failure diagnostics
        // can parse it.  The cost (one extra read per stderr
        // line) is invisible at typical build scale; the win
        // is that the linker output the diagnostic layer needs
        // is sitting in memory the moment ninja exits.
        .stderr(Stdio::piped())
        .spawn()?;

    let stderr_thread = child.stderr.take().map(|stderr| {
        std::thread::spawn(move || {
            let mut captured = String::new();
            let real_stderr = std::io::stderr();
            let mut sink = real_stderr.lock();
            for line in BufReader::new(stderr).lines().map_while(Result::ok) {
                let _ = writeln!(sink, "{line}");
                captured.push_str(&line);
                captured.push('\n');
            }
            captured
        })
    });

    let mut captured_stdout = String::new();
    if let Some(stdout) = child.stdout.take() {
        let stdout_handle = std::io::stdout();
        let mut sink = stdout_handle.lock();
        for line in BufReader::new(stdout).lines().map_while(Result::ok) {
            // Every line is captured regardless of the
            // verbose/progress filtering below, so
            // post-failure diagnostics see what ninja
            // emitted - including the `FAILED:`
            // banner and the linker's "undefined symbol"
            // block that ninja sends to stdout.
            captured_stdout.push_str(&line);
            captured_stdout.push('\n');
            if let Some(path) = ninja_progress_path(&line) {
                if let Some(pkg_name) = package_segment_from_path(path, profile_root_str)
                    && announced.insert(pkg_name.to_owned())
                    && let Some(pkg) = pkg_by_name.get(pkg_name)
                {
                    announce_compiling(reporter, pkg);
                }
                if keep_ninja_chatter {
                    let _ = writeln!(sink, "{line}");
                }
                continue;
            }
            if is_ninja_chatter(&line) {
                if keep_ninja_chatter {
                    let _ = writeln!(sink, "{line}");
                }
                continue;
            }
            let _ = writeln!(sink, "{line}");
        }
    }
    let status = child.wait()?;
    let stderr = stderr_thread
        .and_then(|t| t.join().ok())
        .unwrap_or_default();
    Ok(NinjaRun {
        status,
        stdout: captured_stdout,
        stderr,
    })
}

/// Inspect Ninja's captured stderr after a non-zero exit and, if
/// it looks like a recognizable link failure, print a one-shot
/// `hint:` block to stderr pointing the user at the missing
/// `deps =` entry (or the un-declared bundled port).
///
/// Quiet on inputs that don't look like link failures - the
/// diagnostic is purely additive, never replaces the underlying
/// Ninja error.  Failures inside the diagnostic itself (e.g. a
/// package name in the link error that isn't in the loaded
/// graph) silently do nothing rather than spew on top of the
/// real error.
pub(crate) fn emit_link_diagnostic_if_applicable(
    run: &NinjaRun,
    graph: &cabin_workspace::PackageGraph,
    feature_resolution: &cabin_feature::FeatureResolution,
    include_dev_for: &BTreeSet<String>,
    reporter: Reporter,
) {
    use cabin_build::link_diagnostics::{TargetDepInfo, diagnose, render};

    // Ninja sends the `FAILED:` banner and the failing action's
    // stdout/stderr to *its* stdout, then any wrapper diagnostics
    // (e.g. `ninja: build stopped`) also to stdout.  Concatenate
    // both captured streams so the parser sees whichever stream
    // the platform's linker used.
    let combined = if run.stderr.is_empty() {
        run.stdout.clone()
    } else if run.stdout.is_empty() {
        run.stderr.clone()
    } else {
        format!("{}\n{}", run.stdout, run.stderr)
    };

    let host_platform = cabin_core::TargetPlatform::current();
    let lookup = |pkg_name: &str, target_name: &str| -> Option<TargetDepInfo> {
        let pkg_idx = graph.index_of(pkg_name)?;
        let wp = &graph.packages[pkg_idx];
        let target = wp
            .package
            .targets
            .iter()
            .find(|t| t.name.as_str() == target_name)?;
        // Mirror the workspace loader's active-edge filter so
        // the hint only points at deps that would
        // appear on the link command for this invocation:
        //
        // * Skip cfg-gated entries that do not match the host
        //   platform - they never become graph edges.
        // * Skip `[dev-dependencies]` unless the owning package
        //   activated them for this invocation
        //   (`cabin test` populates `include_dev_for` with the
        //   selected test runners; ordinary builds leave it
        //   empty, matching the loader's policy).
        // * Skip `optional = true` entries whose features are
        //   not enabled by the current resolution - suggesting
        //   "add this to target.deps" for a disabled optional
        //   dep would not change the link command.
        let dev_active = include_dev_for.contains(pkg_name);
        let features = feature_resolution.for_package(pkg_idx);
        let package_deps: BTreeSet<String> = wp
            .package
            .dependencies
            .iter()
            .filter(|d| {
                if !d.matches_platform(&host_platform) {
                    return false;
                }
                let kind_active = d.kind.is_resolved_by_default()
                    || (dev_active && d.kind == cabin_core::DependencyKind::Dev);
                if !kind_active {
                    return false;
                }
                if d.optional && !features.enabled_optional_deps.contains(d.name.as_str()) {
                    return false;
                }
                true
            })
            .map(|d| d.name.as_str().to_owned())
            .collect();
        // `target.deps` entries are either a bare name (same-package
        // target or the same-name shorthand on a dependency package)
        // or a qualified `package:target` reference.  We only care
        // about whether the *package* appears, so the suffix gets
        // stripped.
        let target_deps: BTreeSet<String> = target
            .deps
            .iter()
            .map(|d| {
                d.reference
                    .split_once(':')
                    .map_or(d.reference.as_str(), |(pkg, _)| pkg)
                    .to_owned()
            })
            .collect();
        Some(TargetDepInfo {
            package_deps,
            target_deps,
        })
    };

    if let Some(diag) = diagnose(&combined, lookup) {
        reporter.help(&render(&diag));
    }
}

/// Emit the cargo-style `Compiling <name> v<ver> (<dir>)`
/// header for a single package.  Local packages render their
/// manifest directory in parentheses; registry packages drop
/// the path because the workspace user did not bring them in
/// by hand.
fn announce_compiling(reporter: Reporter, pkg: &cabin_workspace::WorkspacePackage) {
    let name = pkg.package.name.as_str();
    let version = &pkg.package.version;
    match pkg.kind {
        cabin_workspace::PackageKind::Local => {
            reporter.status(
                "Compiling",
                format_args!("{} v{} ({})", name, version, pkg.manifest_dir.display()),
            );
        }
        cabin_workspace::PackageKind::Registry => {
            reporter.status("Compiling", format_args!("{name} v{version}"));
        }
    }
}

/// Return true for lines that are pure Ninja housekeeping:
/// `Entering directory` and `no work to do.`.  Any other line
/// (including compiler-emitted diagnostics, blank lines, and
/// Ninja error reports) returns `false` so it passes through
/// unchanged.  Progress lines (`[N/M] …`) are handled separately
/// - `run_ninja` extracts the package name from them before
///   dropping the line.
fn is_ninja_chatter(line: &str) -> bool {
    line == "ninja: no work to do." || line.starts_with("ninja: Entering directory")
}

/// Parse a Ninja `[<finished>/<total>] <action> <path>` progress
/// line.  Returns the trailing `<path>` slice on a successful
/// match, or `None` for any other input.  Both progress numbers
/// must be non-empty decimal integers; both are decimal positive
/// integers in practice.
fn ninja_progress_path(line: &str) -> Option<&str> {
    let rest = line.strip_prefix('[')?;
    let (finished, rest) = rest.split_once('/')?;
    if finished.is_empty() || !finished.chars().all(|c| c.is_ascii_digit()) {
        return None;
    }
    let (total, after) = rest.split_once("] ")?;
    if total.is_empty() || !total.chars().all(|c| c.is_ascii_digit()) {
        return None;
    }
    // `after` is `<action> <path>`; the action token is the
    // single word the planner records as the description prefix
    // (`CXX`, `AR`, `LINK`, …), so the path starts immediately
    // after the first space.
    let (_action, path) = after.split_once(' ')?;
    Some(path)
}

/// Extract the package name from a planner-emitted build path.
/// Every per-package artifact lives under `<build_dir>/<profile>
/// /packages/<name>/…`, so locating the first `/packages/`
/// segment after the profile build root and taking the next path
/// component yields the owning package's name.  The search is
/// anchored past `profile_root` (when it prefixes the path) so a
/// `packages/` directory in the user's own build path - e.g. a
/// project checked out under `~/packages/` - cannot shadow the
/// planner's per-package segment.  Returns `None` when the path
/// lacks the segment (a custom-command output the planner did not
/// route through the per-package tree).
fn package_segment_from_path<'a>(path: &'a str, profile_root: Option<&str>) -> Option<&'a str> {
    const SEGMENT: &str = "/packages/";
    let search = profile_root
        .and_then(|root| path.strip_prefix(root))
        .unwrap_or(path);
    let after = search.find(SEGMENT)?;
    let tail = &search[after + SEGMENT.len()..];
    tail.split('/').next().filter(|s| !s.is_empty())
}

/// Render the optional `-jN` token plus a trailing space for
/// the status line.  Empty when jobs is unset so the message
/// `cabin: invoking ninja -C <dir>` stays byte-identical to
/// the pre-jobs default.
pub(crate) fn ninja_jobs_echo(jobs: Option<cabin_core::BuildJobs>) -> String {
    match jobs {
        Some(j) => format!("-j{j} "),
        None => String::new(),
    }
}

fn ninja_verbose_echo(verbose: bool) -> &'static str {
    if verbose { "-v " } else { "" }
}

/// Ninja argv fragment: a single `-jN` token.  Producing a single
/// fused argument (rather than `-j` + `N`) matches every Ninja
/// `--help` example and supports Ninja versions that historically
/// parsed only the fused form.  Backend-specific conversion lives
/// here, at the call site that spawns Ninja, rather than in
/// `cabin-core`'s [`cabin_core::BuildJobs`] model.
pub(crate) fn ninja_jobs_arg(jobs: cabin_core::BuildJobs) -> std::ffi::OsString {
    std::ffi::OsString::from(format!("-j{}", jobs.get()))
}

/// Inputs for [`invoke_ninja_and_report`]: everything needed to write
/// the Ninja files for a planned graph and drive Ninja to completion.
/// Shared by `cabin build` / `cabin run` / `cabin test`, each of which
/// does its own command-specific work once the build phase is done.
pub(crate) struct NinjaInvocationRequest<'a> {
    /// Resolved, absolute build directory - the parent of the
    /// per-profile build root.
    pub build_dir: &'a std::path::Path,
    /// Active build profile; its name selects the `build/<profile>`
    /// root and labels the `Finished` banner the caller prints.
    pub profile: &'a cabin_core::ResolvedProfile,
    /// Planned build graph to lower into `build.ninja` and
    /// `compile_commands.json`.
    pub plan_graph: &'a cabin_build::BuildGraph,
    /// Loaded workspace graph, used to attribute Ninja progress lines
    /// to packages and to render a link-failure hint.
    pub graph: &'a cabin_workspace::PackageGraph,
    /// Resolved toolchain, used to decide whether the discovered MSVC
    /// environment overlay applies.
    pub toolchain: &'a cabin_core::ResolvedToolchain,
    /// Detected C++ compiler family
    /// (`detection_report.cxx.identity.kind`).
    pub cxx_kind: cabin_core::CompilerKind,
    /// Resolved feature graph, consumed by the link-failure hint.
    pub feature_resolution: &'a cabin_feature::FeatureResolution,
    /// Packages whose `[dev-dependencies]` are active for this
    /// invocation: empty for `build` / `run`, the selected runners for
    /// `test`.
    pub dev_for: &'a BTreeSet<String>,
    /// Located `ninja` executable.
    pub ninja: &'a std::path::Path,
    /// Parallelism for Ninja's `-j` flag, or `None` to let Ninja pick.
    pub jobs: Option<cabin_core::BuildJobs>,
    pub reporter: Reporter,
}

/// Write `build.ninja` + `compile_commands.json` for the planned graph
/// under `build/<profile>/`, invoke Ninja there, and surface a
/// link-failure hint before bailing on a non-zero exit.  Returns the
/// wall-clock time the Ninja invocation took so callers can print
/// their own cargo-style `Finished` banner.
///
/// # Errors
/// Returns an error if the build root cannot be created, the Ninja
/// files cannot be written, Ninja cannot be spawned, or Ninja exits
/// non-zero.
pub(crate) fn invoke_ninja_and_report(
    req: &NinjaInvocationRequest<'_>,
) -> anyhow::Result<std::time::Duration> {
    let profile_build_root = req.build_dir.join(req.profile.name.as_str());
    std::fs::create_dir_all(&profile_build_root).with_context(|| {
        format!(
            "failed to create build directory {}",
            profile_build_root.display()
        )
    })?;

    let ninja_file = profile_build_root.join("build.ninja");
    cabin_ninja::write_build_ninja(&ninja_file, req.plan_graph, &check_stamp_runner())?;
    let ccmd_file = profile_build_root.join("compile_commands.json");
    cabin_ninja::write_compile_commands(&ccmd_file, req.plan_graph)?;

    // Implementation-detail status is verbose-only: under `-v` the
    // user sees which files Cabin wrote and how Ninja was invoked,
    // alongside Ninja's own raw banner.
    req.reporter
        .verbose(format_args!("cabin: wrote {}", ninja_file.display()));
    req.reporter
        .verbose(format_args!("cabin: wrote {}", ccmd_file.display()));
    let ninja_verbose = req.reporter.verbosity().shows_verbose();
    req.reporter.verbose(format_args!(
        "cabin: invoking {} {}{}-C {}",
        req.ninja.display(),
        ninja_jobs_echo(req.jobs),
        ninja_verbose_echo(ninja_verbose),
        profile_build_root.display()
    ));

    let mut ninja_cmd = std::process::Command::new(req.ninja);
    if let Some(jobs) = req.jobs {
        ninja_cmd.arg(ninja_jobs_arg(jobs));
    }
    if ninja_verbose {
        ninja_cmd.arg("-v");
    }
    let build_started = std::time::Instant::now();
    let run = run_ninja(
        ninja_cmd.arg("-C").arg(&profile_build_root),
        &profile_build_root,
        req.reporter,
        req.graph,
        req.plan_graph.dialect,
        discovered_msvc_install_applies(req.toolchain, req.cxx_kind),
    )
    .with_context(|| format!("failed to invoke ninja at {}", req.ninja.display()))?;
    if !run.status.success() {
        emit_link_diagnostic_if_applicable(
            &run,
            req.graph,
            req.feature_resolution,
            req.dev_for,
            req.reporter,
        );
        anyhow::bail!("ninja exited with {}", run.status);
    }
    Ok(build_started.elapsed())
}

#[cfg(test)]
mod tests {
    use super::*;
    use cabin_core::{
        CompilerKind, ResolvedTool, ResolvedToolchain, ToolKind, ToolSource, ToolSpec,
    };
    use camino::Utf8PathBuf;

    fn toolchain_with_pinned_cxx(path: &str) -> ResolvedToolchain {
        let pinned = |kind, p: &str| ResolvedTool {
            kind,
            path: Utf8PathBuf::from(p),
            spec: ToolSpec::Path(Utf8PathBuf::from(p)),
            source: ToolSource::Cli,
        };
        ResolvedToolchain {
            cxx: pinned(ToolKind::CxxCompiler, path),
            ar: pinned(ToolKind::Archiver, "/llvm/bin/llvm-lib.exe"),
            cc: None,
        }
    }

    #[test]
    fn explicit_clang_cl_path_takes_the_discovered_overlay() {
        // An explicitly-pinned `clang-cl` path is MSVC-dialect but is never
        // the discovered `cl.exe`, yet it still needs the discovered
        // INCLUDE/LIB/PATH because `clang-cl` ships no CRT/SDK headers.  The
        // `ClangCl` early return short-circuits before
        // `path_is_discovered_msvc_cl`, so this holds with no Windows host
        // or real install - locking the regression at the only seam that
        // distinguishes `clang-cl` from a foreign-toolset `cl.exe`.
        let toolchain = toolchain_with_pinned_cxx("/llvm/bin/clang-cl.exe");
        assert!(discovered_msvc_install_applies(
            &toolchain,
            CompilerKind::ClangCl
        ));
    }

    #[test]
    fn explicit_non_clang_cl_path_still_defers_to_install_match() {
        // A pinned `cl.exe` from a *different* toolset detects as
        // `CompilerKind::Msvc`, not `ClangCl`, so it skips the early return
        // and falls through to the path comparison, which is false off a
        // matching Windows install - the eo1 "don't mix SDKs" safety stays
        // intact.
        let toolchain = toolchain_with_pinned_cxx("/some/other/vs/cl.exe");
        assert!(!discovered_msvc_install_applies(
            &toolchain,
            CompilerKind::Msvc
        ));
    }

    #[test]
    fn package_segment_anchors_after_the_profile_build_root() {
        // A project checked out under a `packages/` directory: the
        // unanchored search would report `proj` (and mislabel the
        // `Compiling` banner); anchoring past the profile root
        // finds the planner's own segment.
        let path = "/home/u/packages/proj/build/dev/packages/foo/src_main.cc.o";
        assert_eq!(
            package_segment_from_path(path, Some("/home/u/packages/proj/build/dev")),
            Some("foo")
        );
        assert_eq!(package_segment_from_path(path, None), Some("proj"));
        // Paths the planner did not route through the per-package
        // tree stay unattributed.
        assert_eq!(
            package_segment_from_path(
                "/home/u/proj/build/dev/stamp",
                Some("/home/u/proj/build/dev")
            ),
            None
        );
    }
}