zlayer-toolchain 0.14.1

Runtime toolchain provisioning (macOS Homebrew bottle resolver/installer) for ZLayer
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
//! Build runtime toolchains **from source** into a self-contained, absolute
//! cache keg — "our apt-get" for the macOS sandbox, which has no package
//! manager.
//!
//! # Why source-build instead of a Homebrew bottle
//!
//! Relocating a Homebrew *bottle* (rewriting its baked `@@HOMEBREW_PREFIX@@`
//! install-name placeholders) is a dead end: the rewrite is length-preserving
//! and silently skips placeholders shorter than the cache prefix, so the keg's
//! binary keeps `@@HOMEBREW_PREFIX@@/...` `LC_LOAD_DYLIB` paths. Under a darwin
//! Seatbelt container those paths don't exist and macOS strips `DYLD_*` from
//! the signed binary, so dyld aborts (`Symbol not found … Abort trap: 6`).
//!
//! Building from source at an absolute keg prefix sidesteps both failure modes:
//! every `LC_LOAD_DYLIB` is an absolute path to a macOS system library
//! (`/usr/lib/...`) or an absolute sibling-keg path — never `@@HOMEBREW@@` — and
//! the compiled `sysconfdir`/`prefix` live inside the keg, so the tool reads its
//! own config instead of `/etc/*` (which EPERMs under the deny-default profile).
//!
//! # A fully generic, data-driven build
//!
//! There is **no per-formula recipe table**. Everything the build needs is
//! derived from the Homebrew formula JSON we already parse plus the extracted
//! source tree:
//!
//! - **Dependencies come from the formula's data.** Anything in
//!   `uses_from_macos` is provided by macOS itself (the Seatbelt profile already
//!   grants `/usr/lib` + `/usr/include` via the Command Line Tools) so it is
//!   skipped. Every *other* `dependency` / `build_dependency` is resolved
//!   **recursively** as a sibling keg via [`crate::ensure_macos_keg`] and wired
//!   onto the build with *absolute* keg paths — build tools land on `PATH`,
//!   libraries contribute `-I<keg>/include` / `-L<keg>/lib` /
//!   `-Wl,-rpath,<keg>/lib` / `PKG_CONFIG_PATH=<keg>/lib/pkgconfig`. So git's
//!   `gettext`/`pcre2` and jq's `oniguruma` become resolved kegs automatically —
//!   no `NO_GETTEXT`, no `--with-oniguruma=builtin`, no hardcoded skip lists.
//! - **The build system is autodetected** from the extracted tree (a generated
//!   `configure`, a `CMake` self-host `bootstrap`, a `CMakeLists.txt`, a bare
//!   `Makefile`, or an autotools project shipped as `configure.ac`).
//! - **Irreducible env is derived from the install layout, not the name.** A keg
//!   that installed `<keg>/libexec/git-core` gets `GIT_EXEC_PATH` pointed there —
//!   true for any git-exec-helper tool, asserted by layout, never by `== "git"`.
//!
//! If a formula's generic build fails (a custom `install do` / patches the
//! generic build can't reproduce), [`ensure_from_source`] falls through to
//! [`crate::brew_emulate`], which runs the formula's *real* Homebrew recipe at
//! the keg prefix — so genuinely-custom formulae still work with **zero**
//! hardcoding.

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use tracing::{info, warn};

use crate::error::{Result, ToolchainError};
use crate::formula::{self, Formula};
use crate::manifest::{KegManifest, KegSource};

/// Resolved source-build coordinates for a tool.
#[derive(Debug, Clone)]
pub struct SourceSpec {
    /// Stable version string (e.g. `2.55.0`).
    pub version: String,
    /// URL of the source tarball.
    pub tarball_url: String,
    /// The source tarball's sha256 (bare hex, `sha256:` prefix stripped). Empty
    /// when the formula publishes no `urls.stable.checksum`.
    pub sha256: String,
    /// Runtime dependency formula names.
    pub dependencies: Vec<String>,
    /// Build-only dependency formula names.
    pub build_dependencies: Vec<String>,
    /// Dependency names macOS itself provides (need no keg).
    pub macos_provided: Vec<String>,
}

/// Which build system a recipe drives, autodetected from the extracted tree.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum BuildSystem {
    /// `./configure --prefix=<keg> && make && make install` (a generated
    /// `configure`, or `configure.ac`/`autogen.sh` regenerated first).
    Autotools,
    /// `cmake -DCMAKE_INSTALL_PREFIX=<keg> … && cmake --build && cmake --install`
    /// using a `cmake` resolved as a build-dependency keg.
    CMake,
    /// A self-hosting `CMake` (`bootstrap` + `CMakeLists.txt`, i.e. cmake itself):
    /// `./bootstrap --prefix=<keg> --parallel=N && make && make install`.
    CMakeBootstrap,
    /// A bare hand-written `Makefile`: `make prefix=<keg> … install`.
    MakePrefix,
}

/// Host architecture token used in cache keys (`arm64` / `x86_64`).
fn arch_token() -> &'static str {
    match std::env::consts::ARCH {
        "aarch64" => "arm64",
        other => other,
    }
}

/// Fetch the formula and extract its source-build coordinates.
///
/// # Errors
///
/// Returns [`ToolchainError::RegistryError`] if the formula cannot be fetched
/// or carries no stable version / source URL.
pub async fn resolve_source_spec(formula: &str) -> Result<SourceSpec> {
    let info: Formula = formula::fetch_formula(formula).await?;

    let version = info
        .stable_version()
        .ok_or_else(|| ToolchainError::RegistryError {
            message: format!("formula {formula} has no stable version"),
        })?
        .to_string();

    let tarball_url = info
        .stable_url()
        .ok_or_else(|| ToolchainError::RegistryError {
            message: format!("formula {formula} has no stable source URL"),
        })?
        .to_string();

    Ok(SourceSpec {
        version,
        tarball_url,
        sha256: info.stable_checksum().unwrap_or_default(),
        dependencies: info.dependencies.clone(),
        build_dependencies: info.build_dependencies.clone(),
        macos_provided: info.macos_provided(),
    })
}

/// Build `formula` from source into a self-contained keg under `cache_dir`,
/// writing a [`KegManifest`] and returning the keg path.
///
/// Idempotent: a `<keg>/.ready` marker (written LAST, after the manifest)
/// short-circuits a populated keg. A cold build removes any partial keg, builds
/// into a scratch dir, installs into the keg prefix, writes the manifest, then
/// stamps `.ready`.
///
/// # Errors
///
/// Propagates formula-resolution, download, extraction, dependency, and build
/// failures.
pub async fn ensure_from_source(
    formula: &str,
    cache_dir: &Path,
    lockfile: Option<&crate::ToolchainLockfile>,
) -> Result<PathBuf> {
    let mut spec = resolve_source_spec(formula).await?;

    // A lock hit pins the exact version + source URL + digest (consume-only).
    // The dependency graph still comes from the resolved formula.
    if let Some(locked) = lockfile.and_then(|l| {
        use crate::ToolchainLockfileExt;
        l.lookup(formula, "macos", arch_token())
    }) {
        spec.version = locked.version.clone();
        spec.tarball_url = locked.url.clone();
        spec.sha256 = locked.sha256.clone();
    }

    let keg = cache_dir.join(format!("{formula}-{}-{}", spec.version, arch_token()));
    let ready_marker = keg.join(".ready");

    if tokio::fs::try_exists(&ready_marker).await.unwrap_or(false) {
        return Ok(keg);
    }

    // Try the generic build first. If it can't build this formula — no build
    // system detected (a custom `install do` with no
    // `configure`/`CMakeLists.txt`/`Makefile`, e.g. a `cargo install` / `go
    // build` formula), a required dependency keg won't build, or the build
    // errors in a way the generic flags don't cover — fall back to running the
    // formula's REAL Homebrew recipe at the keg prefix (`brew_emulate`).
    match try_generic_source_build(formula, &spec, &keg, cache_dir, lockfile).await {
        Ok(()) => Ok(keg),
        Err(e) => {
            warn!(
                formula,
                error = %e,
                "generic source build failed; falling back to brew-emulate at the keg prefix"
            );
            // Drop the partial generic keg so the brew-emulate install starts
            // from a clean prefix (it reuses the same `<keg>` cache key).
            let _ = tokio::fs::remove_dir_all(&keg).await;
            crate::brew_emulate::ensure_via_brew(formula, &spec, cache_dir).await
        }
    }
}

/// Attempt the generic (autotools/CMake/Makefile) source build into `keg`.
///
/// On success the keg is fully populated: manifest written and `.ready` stamped.
/// On any failure (detection or build) the caller falls back to
/// [`crate::brew_emulate`]. Kept as a separate fallible step so the fallback
/// decision is a single `match` at the call site.
async fn try_generic_source_build(
    formula: &str,
    spec: &SourceSpec,
    keg: &Path,
    cache_dir: &Path,
    lockfile: Option<&crate::ToolchainLockfile>,
) -> Result<()> {
    // Fresh build. Remove any partial keg so a crashed prior attempt can't leave
    // a half-installed prefix that confuses the installer.
    let _ = tokio::fs::remove_dir_all(keg).await;
    tokio::fs::create_dir_all(keg).await?;

    let scratch = keg.join(".build");
    tokio::fs::create_dir_all(&scratch).await?;

    // 1. Resolve dependencies as sibling kegs and assemble the build env. macOS-
    //    provided deps (`uses_from_macos`) are skipped; everything else is built
    //    recursively as a keg with absolute paths.
    let (build_env, resolved_build_deps) =
        resolve_dependencies(formula, spec, cache_dir, lockfile).await?;

    // 2. Download + extract the source tarball into `<scratch>/src`.
    let src_dir = download_and_extract(formula, spec, &scratch).await?;

    // 3. Autodetect the build system and run it into the keg prefix.
    let system = detect_build_system(&src_dir).await?;
    info!(formula, ?system, "detected build system");
    run_build(formula, &src_dir, keg, system, &build_env).await?;

    // 4. Write the manifest, clean scratch, then stamp `.ready` LAST.
    let manifest = build_manifest(formula, spec, keg, resolved_build_deps).await;
    manifest.write_to_keg(keg).await?;

    if let Err(e) = tokio::fs::remove_dir_all(&scratch).await {
        warn!(error = %e, "failed to clean source scratch dir (non-fatal)");
    }
    tokio::fs::write(keg.join(".ready"), b"").await?;

    Ok(())
}

/// Build the [`KegManifest`] for a freshly-installed source keg, deriving any
/// irreducible runtime env from the **installed layout** (never the formula
/// name): a keg that produced `<keg>/libexec/git-core` gets `GIT_EXEC_PATH`
/// pointed there, so git discovers its exec-helpers out of the keg.
async fn build_manifest(
    formula: &str,
    spec: &SourceSpec,
    keg: &Path,
    build_deps: Vec<String>,
) -> KegManifest {
    let mut path_dirs = Vec::new();
    let bin = keg.join("bin");
    if tokio::fs::try_exists(&bin).await.unwrap_or(false) {
        path_dirs.push(bin.display().to_string());
    }

    let mut env: HashMap<String, String> = HashMap::new();
    // Layout-derived, not name-derived: any tool that installs git's exec-helper
    // dir needs `GIT_EXEC_PATH` to find them out of the keg rather than the host.
    let git_exec = keg.join("libexec/git-core");
    if tokio::fs::try_exists(&git_exec).await.unwrap_or(false) {
        env.insert("GIT_EXEC_PATH".to_string(), git_exec.display().to_string());
    }

    KegManifest {
        tool: formula.to_string(),
        version: spec.version.clone(),
        arch: arch_token().to_string(),
        platform: "macos".to_string(),
        path_dirs,
        env,
        source: KegSource::SourceBuild {
            url: spec.tarball_url.clone(),
            sha256: spec.sha256.clone(),
        },
        build_deps,
        provisioned_at: chrono::Utc::now().to_rfc3339(),
    }
}

/// Environment accumulated for a source build: `PATH` plus linker/include/
/// pkg-config flags pointing at resolved dependency kegs.
#[derive(Debug, Default, Clone)]
struct BuildEnv {
    path_prefix: Vec<String>,
    cppflags: Vec<String>,
    ldflags: Vec<String>,
    pkg_config_path: Vec<String>,
}

/// Resolve a formula's build + runtime dependencies into sibling kegs and build
/// the [`BuildEnv`], **purely from the formula's data**:
///
/// - Deps macOS itself provides (`uses_from_macos`) are skipped — the Command
///   Line Tools already expose them under `/usr/lib` + `/usr/include`, which the
///   Seatbelt profile grants.
/// - Every other build dependency is **required** (its absence fails the build,
///   routing the formula to brew-emulate) and its `bin` dirs go on `PATH`.
/// - Every other runtime dependency is best-effort (a missing optional library
///   should not abort) and is wired via `-I`/`-L`/`-rpath`/`PKG_CONFIG_PATH`.
///
/// Resolution is **recursive**: [`crate::ensure_macos_keg`] re-enters the whole
/// provisioning pipeline for each dep (prebuilt-lang fetch or another source
/// build), so an arbitrarily deep dependency graph is materialized as kegs.
async fn resolve_dependencies(
    formula: &str,
    spec: &SourceSpec,
    cache_dir: &Path,
    lockfile: Option<&crate::ToolchainLockfile>,
) -> Result<(BuildEnv, Vec<String>)> {
    let mut env = BuildEnv::default();
    let mut resolved_build_deps = Vec::new();

    // A dependency provided by macOS itself needs no keg.
    let is_macos_provided = |dep: &str| spec.macos_provided.iter().any(|d| d == dep);

    // Build dependencies — required; their bin dirs go on PATH.
    for dep in &spec.build_dependencies {
        if is_macos_provided(dep) {
            continue;
        }
        let keg = Box::pin(crate::ensure_macos_keg(dep, cache_dir, lockfile)).await?;
        let manifest = KegManifest::load_or_synthesize(&keg).await?;
        env.path_prefix.extend(manifest.path_dirs.clone());
        add_dep_link_flags(&mut env, &keg);
        resolved_build_deps.push(dep.clone());
        info!(formula, dep, keg = %keg.display(), "resolved build dependency keg");
    }

    // Runtime dependencies — best effort; wire link/include/pkg-config flags so
    // the binary's load commands reference the absolute dependency-keg path.
    for dep in &spec.dependencies {
        if is_macos_provided(dep) {
            continue;
        }
        match Box::pin(crate::ensure_macos_keg(dep, cache_dir, lockfile)).await {
            Ok(keg) => {
                if let Ok(manifest) = KegManifest::load_or_synthesize(&keg).await {
                    env.path_prefix.extend(manifest.path_dirs.clone());
                }
                add_dep_link_flags(&mut env, &keg);
                info!(formula, dep, keg = %keg.display(), "resolved runtime dependency keg");
            }
            Err(e) => warn!(
                formula, dep, error = %e,
                "runtime dependency keg unavailable; continuing without it"
            ),
        }
    }

    Ok((env, resolved_build_deps))
}

/// Add `-I`/`-L`/`-rpath`/pkg-config entries for a dependency keg so the build
/// links against its absolute path (never `@@HOMEBREW@@`).
fn add_dep_link_flags(env: &mut BuildEnv, keg: &Path) {
    let include = keg.join("include");
    let lib = keg.join("lib");
    if include.is_dir() {
        env.cppflags.push(format!("-I{}", include.display()));
    }
    if lib.is_dir() {
        env.ldflags.push(format!("-L{}", lib.display()));
        env.ldflags.push(format!("-Wl,-rpath,{}", lib.display()));
        let pc = lib.join("pkgconfig");
        if pc.is_dir() {
            env.pkg_config_path.push(pc.display().to_string());
        }
    }
}

/// Download the source tarball and extract it (stripping the top-level dir) into
/// `<scratch>/src`. macOS `tar` (libarchive) auto-detects the compression, so a
/// single `tar xf` handles `.tar.xz` / `.tar.gz` / `.tgz` / `.tar.bz2`.
async fn download_and_extract(formula: &str, spec: &SourceSpec, scratch: &Path) -> Result<PathBuf> {
    let tar_name = spec
        .tarball_url
        .rsplit('/')
        .next()
        .filter(|s| !s.is_empty())
        .unwrap_or("source.tar");
    let tar_path = scratch.join(tar_name);
    info!(url = %spec.tarball_url, "downloading {formula} source tarball");
    // Stream + verify against the formula's `urls.stable.checksum` when present;
    // otherwise the digest is computed (trust-on-first-download) and recorded in
    // the manifest. A mismatch deletes the partial and aborts the build.
    let expected = (!spec.sha256.is_empty()).then_some(spec.sha256.as_str());
    crate::package_index::download_verified(&spec.tarball_url, &tar_path, expected).await?;

    let src_dir = scratch.join("src");
    let _ = tokio::fs::remove_dir_all(&src_dir).await;
    tokio::fs::create_dir_all(&src_dir).await?;
    let untar = tokio::process::Command::new("tar")
        .arg("xf")
        .arg(&tar_path)
        .args(["--strip-components", "1", "-C"])
        .arg(&src_dir)
        .output()
        .await?;
    if !untar.status.success() {
        return Err(ToolchainError::RegistryError {
            message: format!(
                "failed to extract {formula} source: {}",
                String::from_utf8_lossy(&untar.stderr)
            ),
        });
    }
    Ok(src_dir)
}

/// Detect the build system of an extracted source tree, preferring the path that
/// needs the least extra tooling and is canonical for the project shape:
///
/// 1. `bootstrap` (+ `CMakeLists.txt`) → a self-hosting `CMake` (cmake itself).
/// 2. a generated `configure` → autotools (ready to run).
/// 3. a hand-written top-level `Makefile`/`GNUmakefile` → make-prefix (git).
/// 4. `CMakeLists.txt` → `CMake` (built with a `cmake` keg).
/// 5. `configure.ac`/`configure.in`/`autogen.sh`/`bootstrap.sh` → autotools
///    (regenerate `configure` first).
///
/// # Errors
///
/// Returns [`ToolchainError::RegistryError`] if no recognised build system is
/// present.
async fn detect_build_system(src_dir: &Path) -> Result<BuildSystem> {
    let exists = |rel: &str| {
        let p = src_dir.join(rel);
        async move { tokio::fs::try_exists(&p).await.unwrap_or(false) }
    };

    let has_bootstrap = exists("bootstrap").await || exists("bootstrap.sh").await;
    let has_cmakelists = exists("CMakeLists.txt").await;

    if has_bootstrap && has_cmakelists {
        // Self-hosting CMake (e.g. cmake's own source): its `bootstrap` builds a
        // minimal cmake then generates the real build — can't use a cmake keg to
        // build cmake. The bootstrap wrapper bakes `--prefix` into the build.
        Ok(BuildSystem::CMakeBootstrap)
    } else if exists("configure").await {
        Ok(BuildSystem::Autotools)
    } else if exists("Makefile").await || exists("GNUmakefile").await {
        // A ready top-level Makefile (not Makefile.in) is a hand-written build
        // (git, simple C tools): `make prefix=<keg> install`.
        Ok(BuildSystem::MakePrefix)
    } else if has_cmakelists {
        Ok(BuildSystem::CMake)
    } else if exists("configure.ac").await
        || exists("configure.in").await
        || exists("autogen.sh").await
        || has_bootstrap
    {
        // Autotools project shipped without a generated `configure`. Generate it
        // (needs autoconf/automake/libtool on PATH from build-dep kegs).
        Ok(BuildSystem::Autotools)
    } else {
        Err(ToolchainError::RegistryError {
            message: format!(
                "could not detect a build system \
                 (configure/CMakeLists.txt/Makefile/bootstrap) in {}",
                src_dir.display()
            ),
        })
    }
}

/// Run the detected build system into the keg prefix using the host CLT plus the
/// resolved dependency kegs (no per-formula flags — every quirk is data-driven).
#[allow(clippy::too_many_lines)]
async fn run_build(
    formula: &str,
    src_dir: &Path,
    keg: &Path,
    system: BuildSystem,
    build_env: &BuildEnv,
) -> Result<()> {
    let jobs = std::thread::available_parallelism()
        .map_or(4, std::num::NonZero::get)
        .to_string();
    let keg_str = keg.display().to_string();

    match system {
        BuildSystem::MakePrefix => {
            // Hand-written Makefile: one `make -jN prefix=<keg> install`. The
            // Makefile bakes its own install/sysconfdir from `prefix=`.
            let mut cmd = tokio::process::Command::new("make");
            cmd.current_dir(src_dir)
                .arg(format!("-j{jobs}"))
                .arg(format!("prefix={keg_str}"))
                .arg("install");
            run_cmd(formula, "make install", &mut cmd, build_env).await?;
        }
        BuildSystem::CMakeBootstrap => {
            // Self-hosting CMake: bootstrap (bakes the install prefix), then make.
            run_cmd(
                formula,
                "bootstrap",
                tokio::process::Command::new("./bootstrap")
                    .current_dir(src_dir)
                    .arg(format!("--prefix={keg_str}"))
                    .arg(format!("--parallel={jobs}")),
                build_env,
            )
            .await?;
            run_cmd(
                formula,
                "make",
                tokio::process::Command::new("make")
                    .current_dir(src_dir)
                    .arg(format!("-j{jobs}")),
                build_env,
            )
            .await?;
            run_cmd(
                formula,
                "make install",
                tokio::process::Command::new("make")
                    .current_dir(src_dir)
                    .arg("install"),
                build_env,
            )
            .await?;
        }
        BuildSystem::Autotools => {
            // Generate `configure` if the tarball shipped only configure.ac.
            if !src_dir.join("configure").is_file() {
                let autogen = src_dir.join("autogen.sh");
                if autogen.is_file() {
                    run_cmd(
                        formula,
                        "autogen.sh",
                        tokio::process::Command::new("sh")
                            .current_dir(src_dir)
                            .arg("autogen.sh"),
                        build_env,
                    )
                    .await?;
                } else {
                    run_cmd(
                        formula,
                        "autoreconf",
                        tokio::process::Command::new("autoreconf")
                            .current_dir(src_dir)
                            .arg("-fi"),
                        build_env,
                    )
                    .await?;
                }
            }

            let mut configure = tokio::process::Command::new("./configure");
            configure
                .current_dir(src_dir)
                .arg(format!("--prefix={keg_str}"));
            run_cmd(formula, "configure", &mut configure, build_env).await?;

            let mut make = tokio::process::Command::new("make");
            make.current_dir(src_dir).arg(format!("-j{jobs}"));
            run_cmd(formula, "make", &mut make, build_env).await?;

            run_cmd(
                formula,
                "make install",
                tokio::process::Command::new("make")
                    .current_dir(src_dir)
                    .arg("install"),
                build_env,
            )
            .await?;
        }
        BuildSystem::CMake => {
            let build_dir = src_dir.join("_zl_build");
            let mut configure = tokio::process::Command::new("cmake");
            configure
                .current_dir(src_dir)
                .arg("-S")
                .arg(".")
                .arg("-B")
                .arg(&build_dir)
                .arg(format!("-DCMAKE_INSTALL_PREFIX={keg_str}"))
                .arg("-DCMAKE_BUILD_TYPE=Release");
            run_cmd(formula, "cmake configure", &mut configure, build_env).await?;

            run_cmd(
                formula,
                "cmake build",
                tokio::process::Command::new("cmake")
                    .current_dir(src_dir)
                    .arg("--build")
                    .arg(&build_dir)
                    .arg("-j")
                    .arg(&jobs),
                build_env,
            )
            .await?;

            run_cmd(
                formula,
                "cmake install",
                tokio::process::Command::new("cmake")
                    .current_dir(src_dir)
                    .arg("--install")
                    .arg(&build_dir),
                build_env,
            )
            .await?;
        }
    }
    Ok(())
}

/// Apply the [`BuildEnv`] to a command and run it, returning a build error with
/// the tail of stderr on failure. The host environment is inherited; `PATH`,
/// `CPPFLAGS`, `LDFLAGS` and `PKG_CONFIG_PATH` are prepended/augmented.
async fn run_cmd(
    formula: &str,
    step: &str,
    cmd: &mut tokio::process::Command,
    env: &BuildEnv,
) -> Result<()> {
    // PATH: dependency keg bins first, then the host CLT/system paths.
    let host_path = std::env::var("PATH").unwrap_or_default();
    let system_path = "/usr/bin:/bin:/usr/sbin:/sbin";
    let mut path_parts: Vec<String> = env.path_prefix.clone();
    if !host_path.is_empty() {
        path_parts.push(host_path);
    }
    path_parts.push(system_path.to_string());
    cmd.env("PATH", path_parts.join(":"));

    if !env.cppflags.is_empty() {
        cmd.env("CPPFLAGS", env.cppflags.join(" "));
    }
    if !env.ldflags.is_empty() {
        cmd.env("LDFLAGS", env.ldflags.join(" "));
    }
    if !env.pkg_config_path.is_empty() {
        cmd.env("PKG_CONFIG_PATH", env.pkg_config_path.join(":"));
    }

    info!(formula, step, "running source build step");
    let out = cmd.output().await?;
    if !out.status.success() {
        let tail = String::from_utf8_lossy(&out.stderr)
            .lines()
            .rev()
            .take(25)
            .collect::<Vec<_>>()
            .into_iter()
            .rev()
            .collect::<Vec<_>>()
            .join("\n");
        return Err(ToolchainError::RegistryError {
            message: format!("{formula} `{step}` failed:\n{tail}"),
        });
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn detect_autotools_from_configure() {
        let tmp = tempfile::tempdir().unwrap();
        tokio::fs::write(tmp.path().join("configure"), b"#!/bin/sh\n")
            .await
            .unwrap();
        assert_eq!(
            detect_build_system(tmp.path()).await.unwrap(),
            BuildSystem::Autotools
        );
    }

    #[tokio::test]
    async fn detect_cmake_from_cmakelists() {
        let tmp = tempfile::tempdir().unwrap();
        tokio::fs::write(tmp.path().join("CMakeLists.txt"), b"project(x)\n")
            .await
            .unwrap();
        assert_eq!(
            detect_build_system(tmp.path()).await.unwrap(),
            BuildSystem::CMake
        );
    }

    #[tokio::test]
    async fn detect_make_from_bare_makefile() {
        let tmp = tempfile::tempdir().unwrap();
        tokio::fs::write(tmp.path().join("Makefile"), b"all:\n\ttrue\n")
            .await
            .unwrap();
        assert_eq!(
            detect_build_system(tmp.path()).await.unwrap(),
            BuildSystem::MakePrefix
        );
    }

    /// A self-hosting `CMake` (cmake itself ships `bootstrap` + `CMakeLists.txt`)
    /// must bootstrap, NOT try to build with a cmake we don't yet have.
    #[tokio::test]
    async fn detect_cmake_bootstrap_for_self_host() {
        let tmp = tempfile::tempdir().unwrap();
        tokio::fs::write(tmp.path().join("bootstrap"), b"#!/bin/sh\n")
            .await
            .unwrap();
        tokio::fs::write(tmp.path().join("CMakeLists.txt"), b"project(cmake)\n")
            .await
            .unwrap();
        assert_eq!(
            detect_build_system(tmp.path()).await.unwrap(),
            BuildSystem::CMakeBootstrap
        );
    }

    /// A generated `configure` wins over a sibling `Makefile.in`; and a tree with
    /// only `configure.ac` is still autotools (regenerated first).
    #[tokio::test]
    async fn detect_autotools_from_configure_ac_only() {
        let tmp = tempfile::tempdir().unwrap();
        tokio::fs::write(tmp.path().join("configure.ac"), b"AC_INIT([x],[1])\n")
            .await
            .unwrap();
        assert_eq!(
            detect_build_system(tmp.path()).await.unwrap(),
            BuildSystem::Autotools
        );
    }

    /// A hand-written `Makefile` that also ships `configure.ac` (git's shape:
    /// no generated `configure`) takes the ready Makefile path, not the
    /// regenerate-with-autotools path.
    #[tokio::test]
    async fn detect_prefers_ready_makefile_over_configure_ac() {
        let tmp = tempfile::tempdir().unwrap();
        tokio::fs::write(tmp.path().join("Makefile"), b"all:\n\ttrue\n")
            .await
            .unwrap();
        tokio::fs::write(tmp.path().join("configure.ac"), b"AC_INIT([git],[1])\n")
            .await
            .unwrap();
        assert_eq!(
            detect_build_system(tmp.path()).await.unwrap(),
            BuildSystem::MakePrefix
        );
    }

    #[tokio::test]
    async fn detect_fails_on_unknown_tree() {
        let tmp = tempfile::tempdir().unwrap();
        tokio::fs::write(tmp.path().join("README"), b"hi\n")
            .await
            .unwrap();
        assert!(detect_build_system(tmp.path()).await.is_err());
    }

    #[test]
    fn dep_link_flags_use_absolute_keg_paths() {
        let tmp = tempfile::tempdir().unwrap();
        let keg = tmp.path();
        std::fs::create_dir_all(keg.join("include")).unwrap();
        std::fs::create_dir_all(keg.join("lib/pkgconfig")).unwrap();
        let mut env = BuildEnv::default();
        add_dep_link_flags(&mut env, keg);
        assert!(env.cppflags.iter().any(|f| f.contains("/include")));
        assert!(env.ldflags.iter().any(|f| f.starts_with("-L")));
        assert!(env
            .ldflags
            .iter()
            .any(|f| f.contains("-Wl,-rpath,") && !f.contains("@@HOMEBREW")));
        assert!(env.pkg_config_path.iter().any(|p| p.contains("pkgconfig")));
    }

    /// `resolve_dependencies` skips `uses_from_macos` deps without touching the
    /// network: a spec whose every dep is macOS-provided resolves to an empty
    /// build env and no resolved build deps.
    #[tokio::test]
    async fn macos_provided_deps_are_skipped_offline() {
        let tmp = tempfile::tempdir().unwrap();
        let spec = SourceSpec {
            version: "1.0".to_string(),
            tarball_url: "https://example/x.tar.gz".to_string(),
            sha256: String::new(),
            dependencies: vec!["curl".to_string(), "zlib".to_string()],
            build_dependencies: vec!["expat".to_string()],
            macos_provided: vec!["curl".to_string(), "zlib".to_string(), "expat".to_string()],
        };
        let (env, build_deps) = resolve_dependencies("demo", &spec, tmp.path(), None)
            .await
            .expect("all-macos-provided deps resolve offline");
        assert!(build_deps.is_empty(), "no keg deps to resolve");
        assert!(env.path_prefix.is_empty());
        assert!(env.cppflags.is_empty());
        assert!(env.ldflags.is_empty());
        assert!(env.pkg_config_path.is_empty());
    }

    /// The manifest's env is derived from the installed LAYOUT, not the formula
    /// name: a keg with `libexec/git-core` gets `GIT_EXEC_PATH`; one without
    /// gets none — for the same formula name either way.
    #[tokio::test]
    async fn manifest_env_is_layout_derived_not_name_derived() {
        let spec = SourceSpec {
            version: "2.55.0".to_string(),
            tarball_url: "https://example/git.tar.xz".to_string(),
            sha256: String::new(),
            dependencies: vec![],
            build_dependencies: vec![],
            macos_provided: vec![],
        };

        // Keg WITH the git-core exec dir → GIT_EXEC_PATH present.
        let with_dir = tempfile::tempdir().unwrap();
        tokio::fs::create_dir_all(with_dir.path().join("libexec/git-core"))
            .await
            .unwrap();
        let m = build_manifest("git", &spec, with_dir.path(), vec![]).await;
        assert_eq!(
            m.env.get("GIT_EXEC_PATH"),
            Some(
                &with_dir
                    .path()
                    .join("libexec/git-core")
                    .display()
                    .to_string()
            )
        );

        // Same formula NAME, no git-core dir → no GIT_EXEC_PATH (layout-driven).
        let without_dir = tempfile::tempdir().unwrap();
        let m2 = build_manifest("git", &spec, without_dir.path(), vec![]).await;
        assert!(!m2.env.contains_key("GIT_EXEC_PATH"));
    }
}