mbx 1.6.0

A build cache for Rust projects
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
//! Profile-aware linker selection and verified GitHub release installation.

use crate::config::{HttpSettings, LinkerSettings};
use eyre::{Context, Result, bail};
use flate2::read::GzDecoder;
use reqwest::blocking::Client;
use reqwest::header::{AUTHORIZATION, HeaderMap, HeaderValue, USER_AGENT};
use serde::Deserialize;
use sha2::{Digest as _, Sha256};
use std::fs::{self, File};
use std::io::{BufReader, Read as _, Write as _};
use std::path::{Path, PathBuf};
use std::process::Command;

const LINKER_ENV: &str = "MBX_LINKER";

/// A linker selected for one Cargo invocation.
#[derive(Debug, Clone)]
pub(crate) struct Selection {
    pub(crate) executable: PathBuf,
}

/// Resolve and, where necessary, install the linker selected for this build.
pub(crate) fn resolve(
    settings: &LinkerSettings,
    cache_dir: &Path,
    http: &HttpSettings,
    cargo_arguments: &[String],
) -> Result<Option<Selection>> {
    let profile = cargo_profile(cargo_arguments);
    let environment = std::env::var(LINKER_ENV)
        .ok()
        .filter(|value| !value.trim().is_empty());
    let target = if environment.is_none() && settings.profiles.contains_key(&profile) {
        cargo_target(cargo_arguments)?.or_else(|| host_target(cargo_arguments))
    } else {
        cargo_target(cargo_arguments)?
    };
    let configured = environment
        .or_else(|| settings.for_build(&profile, target.as_deref()))
        .unwrap_or_else(|| settings.default.clone());
    let spec = Spec::parse(&configured)?;
    match spec {
        Spec::System => Ok(None),
        Spec::Path(path) => Ok(Some(Selection {
            executable: which::which(&path)
                .wrap_err_with(|| format!("failed to find selected linker `{}`", path.display()))?,
        })),
        Spec::RustLld => Ok(Some(Selection {
            executable: rust_lld(target.as_deref(), cache_dir, cargo_arguments)?,
        })),
        Spec::Managed { provider, version } => {
            let installed = install(provider, &version, cache_dir, http)?;
            Ok(Some(Selection {
                executable: installed,
            }))
        }
    }
}

/// Validate a selector supplied by repository-owned workspace policy.
pub(crate) fn validate_workspace_selector(value: &str) -> Result<()> {
    match Spec::parse(value)? {
        Spec::Path(_) => bail!("workspace linker policy may not select an executable path"),
        _ => Ok(()),
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Provider {
    Mold,
    Wild,
}

impl Provider {
    fn parse(name: &str) -> Option<Self> {
        match name {
            "mold" => Some(Self::Mold),
            "wild" => Some(Self::Wild),
            _ => None,
        }
    }

    fn name(self) -> &'static str {
        match self {
            Self::Mold => "mold",
            Self::Wild => "wild",
        }
    }

    fn repository(self) -> &'static str {
        match self {
            Self::Mold => "rui314/mold",
            Self::Wild => "wild-linker/wild",
        }
    }

    fn tag(self, version: &str) -> String {
        match self {
            Self::Wild => version.to_owned(),
            Self::Mold => format!("v{version}"),
        }
    }

    fn asset(self, version: &str) -> Result<String> {
        let arch = std::env::consts::ARCH;
        match self {
            Self::Mold if cfg!(target_os = "linux") => {
                let arch = match arch {
                    "x86_64" | "aarch64" | "riscv64" | "s390x" => arch,
                    "powerpc64" if cfg!(target_endian = "little") => "ppc64le",
                    other => bail!("mold does not publish a managed binary for {other}-linux"),
                };
                Ok(format!("mold-{version}-{arch}-linux.tar.gz"))
            }
            Self::Wild if cfg!(target_os = "linux") => {
                let arch = match arch {
                    "x86_64" | "aarch64" => arch,
                    "riscv64" => "riscv64gc",
                    other => bail!("Wild does not publish a managed binary for {other}-linux"),
                };
                let environment = if cfg!(target_env = "musl") {
                    "musl"
                } else {
                    "gnu"
                };
                Ok(format!(
                    "wild-linker-{version}-{arch}-unknown-linux-{environment}.tar.gz"
                ))
            }
            _ => bail!(
                "{} does not publish a managed binary for {}-{}",
                self.name(),
                arch,
                std::env::consts::OS
            ),
        }
    }

    fn executable_suffix(self) -> String {
        match self {
            Self::Mold => "/bin/mold".into(),
            Self::Wild => "/wild".into(),
        }
    }
}

enum Spec {
    System,
    RustLld,
    Path(PathBuf),
    Managed { provider: Provider, version: String },
}

impl Spec {
    fn parse(value: &str) -> Result<Self> {
        let value = value.trim();
        if value == "system" {
            return Ok(Self::System);
        }
        if matches!(value, "lld" | "rust-lld") {
            return Ok(Self::RustLld);
        }
        if let Some(path) = value.strip_prefix("path:") {
            if path.is_empty() {
                bail!("linker selector `path:` requires an executable path");
            }
            return Ok(Self::Path(path.into()));
        }
        let Some((name, version)) = value.split_once('@') else {
            bail!(
                "unknown linker selector `{value}`; use system, rust-lld, path:<executable>, or mold|wild@<version>"
            );
        };
        let Some(provider) = Provider::parse(name) else {
            bail!("unknown managed linker `{name}`; supported linkers are mold and wild");
        };
        let version = version.trim_start_matches('v');
        if version.is_empty() || matches!(version, "." | ".." | "latest") {
            bail!("managed linker `{name}` must name an exact version");
        }
        if !version
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'-' | b'_' | b'+'))
        {
            bail!("managed linker `{name}` has an invalid version `{version}`");
        }
        Ok(Self::Managed {
            provider,
            version: version.to_owned(),
        })
    }
}

#[derive(Deserialize)]
struct Release {
    assets: Vec<ReleaseAsset>,
}

#[derive(Deserialize)]
struct ReleaseAsset {
    name: String,
    browser_download_url: String,
    digest: Option<String>,
}

fn install(
    provider: Provider,
    version: &str,
    cache_dir: &Path,
    http: &HttpSettings,
) -> Result<PathBuf> {
    let install_dir = cache_dir
        .join("tools")
        .join(provider.name())
        .join(version)
        .join(host_install_name());
    let executable = install_dir.join(executable_filename(provider.name()));
    if executable.is_file() {
        return Ok(executable);
    }
    fs::create_dir_all(&install_dir)?;
    let lock_path = install_dir.join("install.lock");
    let mut lock = fslock::LockFile::open(&lock_path)?;
    lock.lock()?;
    if executable.is_file() {
        return Ok(executable);
    }

    let client = github_client(http)?;
    let tag = provider.tag(version);
    let release_url = format!(
        "https://api.github.com/repos/{}/releases/tags/{tag}",
        provider.repository()
    );
    let release: Release = client
        .get(&release_url)
        .send()
        .wrap_err_with(|| format!("failed to query {release_url}"))?
        .error_for_status()?
        .json()
        .wrap_err("invalid GitHub release response")?;
    let asset_name = provider.asset(version)?;
    let asset = release
        .assets
        .into_iter()
        .find(|asset| asset.name == asset_name)
        .ok_or_else(|| {
            eyre::eyre!(
                "{} {version} has no release asset `{asset_name}`",
                provider.name()
            )
        })?;
    let expected = asset
        .digest
        .as_deref()
        .and_then(|digest| digest.strip_prefix("sha256:"))
        .ok_or_else(|| eyre::eyre!("GitHub reported no SHA-256 digest for `{asset_name}`"))?;

    crate::session::note(&format!(
        "mbx[linker]: downloading {} {version} for {}",
        provider.name(),
        host_install_name()
    ));
    let mut response = client
        .get(&asset.browser_download_url)
        .send()
        .wrap_err_with(|| format!("failed to download `{asset_name}`"))?
        .error_for_status()?;
    let archive_path = install_dir.join(format!(".{asset_name}.download"));
    let mut archive = File::create(&archive_path)?;
    let mut hasher = Sha256::new();
    let mut buffer = [0_u8; 64 * 1024];
    loop {
        let count = response.read(&mut buffer)?;
        if count == 0 {
            break;
        }
        archive.write_all(&buffer[..count])?;
        hasher.update(&buffer[..count]);
    }
    archive.sync_all()?;
    let actual = hex::encode(hasher.finalize());
    if !actual.eq_ignore_ascii_case(expected) {
        bail!("checksum mismatch for `{asset_name}`: expected {expected}, got {actual}");
    }

    let temporary = install_dir.join(format!(".{}.new", provider.name()));
    extract_executable(&archive_path, &temporary, &provider.executable_suffix())?;
    fs::rename(&temporary, &executable)?;
    let _ = fs::remove_file(&archive_path);
    Ok(executable)
}

fn github_client(http: &HttpSettings) -> Result<Client> {
    let mut headers = HeaderMap::new();
    headers.insert(USER_AGENT, HeaderValue::from_static("mr-boxington"));
    if let Ok(token) = std::env::var("GITHUB_TOKEN")
        && !token.is_empty()
    {
        headers.insert(
            AUTHORIZATION,
            HeaderValue::from_str(&format!("Bearer {token}"))?,
        );
    }
    Ok(Client::builder()
        .default_headers(headers)
        .connect_timeout(http.timeout)
        .timeout(http.download_timeout)
        .build()?)
}

fn extract_executable(archive: &Path, destination: &Path, suffix: &str) -> Result<()> {
    let input = BufReader::new(File::open(archive)?);
    let mut archive = tar::Archive::new(GzDecoder::new(input));
    for entry in archive.entries()? {
        let mut entry = entry?;
        let path = entry.path()?;
        let normalized = path.to_string_lossy();
        if normalized == suffix.trim_start_matches('/') || normalized.ends_with(suffix) {
            let mut output = File::create(destination)?;
            std::io::copy(&mut entry, &mut output)?;
            output.sync_all()?;
            #[cfg(unix)]
            {
                use std::os::unix::fs::PermissionsExt as _;
                fs::set_permissions(destination, fs::Permissions::from_mode(0o755))?;
            }
            return Ok(());
        }
    }
    bail!("release archive did not contain an executable ending in `{suffix}`")
}

fn rust_lld(target: Option<&str>, cache_dir: &Path, cargo_arguments: &[String]) -> Result<PathBuf> {
    if cfg!(windows) {
        bail!("managed rust-lld selection on Windows is not supported yet");
    }
    let output = rustc_command(cargo_arguments)
        .args(["--print", "sysroot"])
        .output()
        .wrap_err("failed to find the active Rust sysroot")?;
    if !output.status.success() {
        bail!("rustc could not report its sysroot");
    }
    let sysroot = String::from_utf8(output.stdout)?;
    let host = host_target(cargo_arguments)
        .ok_or_else(|| eyre::eyre!("rustc did not report its host target"))?;
    let path = PathBuf::from(sysroot.trim())
        .join("lib/rustlib")
        .join(&host)
        .join("bin")
        .join("rust-lld");
    if !path.is_file() {
        bail!(
            "the active Rust toolchain has no bundled linker at `{}`",
            path.display()
        );
    }
    let link_target = target.unwrap_or(&host);
    let flavor = if link_target.contains("apple") {
        "ld64.lld"
    } else {
        "ld.lld"
    };
    let sysroot_key = hex::encode(Sha256::digest(sysroot.trim().as_bytes()));
    let shim_dir = cache_dir
        .join("tools/rust-lld")
        .join(host)
        .join(sysroot_key);
    fs::create_dir_all(&shim_dir)?;
    let shim = shim_dir.join(flavor);
    #[cfg(unix)]
    {
        let mut lock = fslock::LockFile::open(&shim_dir.join("install.lock"))?;
        lock.lock()?;
        if fs::read_link(&shim).is_ok_and(|target| target == path) {
            return Ok(shim);
        }
        let temporary = shim_dir.join(format!(".{flavor}.new"));
        let _ = fs::remove_file(&temporary);
        std::os::unix::fs::symlink(&path, &temporary)?;
        fs::rename(&temporary, &shim)?;
    }
    Ok(shim)
}

fn cargo_profile(arguments: &[String]) -> String {
    let mut arguments = arguments
        .iter()
        .take_while(|argument| argument.as_str() != "--");
    let mut subcommand = None;
    let mut profile = None;
    while let Some(argument) = arguments.next() {
        if argument == "--release" {
            profile = Some("release".to_owned());
        } else if argument == "--profile" {
            profile = arguments.next().cloned();
        } else if let Some(value) = argument.strip_prefix("--profile=") {
            profile = Some(value.to_owned());
        } else if matches!(argument.as_str(), "--config" | "--color" | "-Z") {
            let _ = arguments.next();
        } else if !argument.starts_with('-') && !argument.starts_with('+') && subcommand.is_none() {
            subcommand = Some(argument.as_str());
        }
    }
    profile.unwrap_or_else(|| {
        if subcommand == Some("bench") {
            "bench"
        } else {
            "dev"
        }
        .into()
    })
}

fn cargo_target(arguments: &[String]) -> Result<Option<String>> {
    let mut target = None;
    let mut arguments = arguments
        .iter()
        .take_while(|argument| argument.as_str() != "--");
    while let Some(argument) = arguments.next() {
        let value = if argument == "--target" {
            arguments.next().cloned()
        } else {
            argument.strip_prefix("--target=").map(str::to_owned)
        };
        if let Some(value) = value
            && target.replace(value).is_some()
        {
            bail!("managed linker selection supports one Cargo --target per invocation");
        }
    }
    Ok(target)
}

fn rustc_command(cargo_arguments: &[String]) -> Command {
    let mut command = Command::new(std::env::var_os("RUSTC").unwrap_or_else(|| "rustc".into()));
    if let Some(toolchain) = cargo_arguments
        .first()
        .filter(|argument| argument.starts_with('+'))
    {
        command.arg(toolchain);
    }
    command
}

fn host_target(cargo_arguments: &[String]) -> Option<String> {
    let output = rustc_command(cargo_arguments).arg("-vV").output().ok()?;
    String::from_utf8(output.stdout)
        .ok()?
        .lines()
        .find_map(|line| line.strip_prefix("host: ").map(str::to_owned))
}

fn host_install_name() -> String {
    host_target(&[])
        .unwrap_or_else(|| format!("{}-{}", std::env::consts::ARCH, std::env::consts::OS))
}

fn executable_filename(name: &str) -> String {
    if cfg!(windows) {
        format!("{name}.exe")
    } else {
        name.into()
    }
}

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

    #[test]
    fn parses_profiles_without_reading_program_arguments() {
        assert_eq!(cargo_profile(&["build".into()]), "dev");
        assert_eq!(
            cargo_profile(&["build".into(), "--release".into()]),
            "release"
        );
        assert_eq!(cargo_profile(&["bench".into()]), "bench");
        assert_eq!(
            cargo_profile(&["--config".into(), "build.jobs=1".into(), "bench".into(),]),
            "bench"
        );
        assert_eq!(
            cargo_profile(&["build".into(), "--profile=fast".into()]),
            "fast"
        );
        assert_eq!(
            cargo_profile(&["run".into(), "--".into(), "--profile=ignored".into()]),
            "dev"
        );
    }

    #[test]
    fn parses_supported_selectors() {
        assert!(matches!(Spec::parse("system").unwrap(), Spec::System));
        assert!(matches!(Spec::parse("lld").unwrap(), Spec::RustLld));
        assert!(matches!(
            Spec::parse("path:/tmp/ld").unwrap(),
            Spec::Path(_)
        ));
        assert!(matches!(
            Spec::parse("mold@2.42.0").unwrap(),
            Spec::Managed { provider: Provider::Mold, version } if version == "2.42.0"
        ));
        assert!(Spec::parse("wild@latest").is_err());
        assert!(Spec::parse("mold@../../escape").is_err());
        assert!(Spec::parse("mold@..").is_err());
        assert!(Spec::parse("unknown@1").is_err());
    }

    #[test]
    fn target_flag_is_unambiguous() {
        assert_eq!(cargo_target(&["build".into()]).unwrap(), None);
        assert_eq!(
            cargo_target(&["build".into(), "--target=aarch64-apple-darwin".into()]).unwrap(),
            Some("aarch64-apple-darwin".into())
        );
        assert!(
            cargo_target(&[
                "build".into(),
                "--target".into(),
                "one".into(),
                "--target=two".into(),
            ])
            .is_err()
        );
    }

    #[cfg(unix)]
    #[test]
    fn rust_lld_repairs_a_dangling_cached_shim() {
        let cache = tempfile::tempdir().unwrap();
        let shim = rust_lld(None, cache.path(), &[]).unwrap();
        fs::remove_file(&shim).unwrap();
        std::os::unix::fs::symlink("/missing/rust-lld", &shim).unwrap();

        assert_eq!(rust_lld(None, cache.path(), &[]).unwrap(), shim);
        assert!(fs::read_link(shim).unwrap().is_file());
    }
}