|
| 1 | +use std::env; |
| 2 | +use std::path::Path; |
| 3 | +use std::process::{Command, Stdio}; |
| 4 | + |
| 5 | +use anyhow::{Context, Result}; |
| 6 | +use log::{info, warn}; |
| 7 | + |
| 8 | +// Runs x.py in the given environment root. Handles the build or dist command, |
| 9 | +// stage limiting, and job config. |
| 10 | +pub fn run_xpy(env_root: &Path, jobs: u32, target: Option<&str>, full_dist: bool) -> Result<()> { |
| 11 | + let x_py = env_root.join("x.py"); |
| 12 | + |
| 13 | + let python = env::var("BOOTSTRAP_PYTHON").unwrap_or_else(|_| { |
| 14 | + if cfg!(windows) { "python".to_string() } else { "python3".to_string() } |
| 15 | + }); |
| 16 | + |
| 17 | + let mut cmd = Command::new(&python); |
| 18 | + cmd.arg(&x_py); |
| 19 | + |
| 20 | + let build_cmd = if full_dist { "dist" } else { "build" }; |
| 21 | + cmd.arg(build_cmd); |
| 22 | + |
| 23 | + if !full_dist { |
| 24 | + cmd.arg("--stage").arg("2"); |
| 25 | + cmd.arg("compiler"); |
| 26 | + } |
| 27 | + |
| 28 | + if let Some(t) = target { |
| 29 | + cmd.arg("--target").arg(t); |
| 30 | + } |
| 31 | + |
| 32 | + cmd.arg("-j").arg(jobs.to_string()); |
| 33 | + cmd.arg("--config").arg("bootstrap.toml"); |
| 34 | + cmd.current_dir(env_root); |
| 35 | + cmd.stdout(Stdio::inherit()); |
| 36 | + cmd.stderr(Stdio::inherit()); |
| 37 | + |
| 38 | + info!("Kicking off: {} {}", python, x_py.display()); |
| 39 | + |
| 40 | + let status = cmd.status().with_context(|| format!("Couldn't run x.py in {:?}", env_root))?; |
| 41 | + if !status.success() { |
| 42 | + return Err(anyhow::anyhow!("Build bombed in {:?}", env_root)); |
| 43 | + } |
| 44 | + |
| 45 | + Ok(()) |
| 46 | +} |
| 47 | + |
| 48 | +// Figures out the host triple by asking rustc, or guessing if that fails. |
| 49 | +pub fn detect_host(src_root: &Path) -> Result<String> { |
| 50 | + let output = Command::new("rustc") |
| 51 | + .arg("-vV") |
| 52 | + .output() |
| 53 | + .context("Couldn't query rustc for version info")?; |
| 54 | + |
| 55 | + if !output.status.success() { |
| 56 | + warn!("rustc -vV didn't work; falling back to a guess."); |
| 57 | + } |
| 58 | + |
| 59 | + let out_str = String::from_utf8_lossy(&output.stdout); |
| 60 | + for line in out_str.lines() { |
| 61 | + if line.starts_with("host: ") { |
| 62 | + return Ok(line.trim_start_matches("host: ").trim().to_string()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + let arch = if cfg!(target_arch = "x86_64") { |
| 67 | + "x86_64" |
| 68 | + } else if cfg!(target_arch = "aarch64") { |
| 69 | + "aarch64" |
| 70 | + } else { |
| 71 | + "unknown" |
| 72 | + }; |
| 73 | + let os = if cfg!(target_os = "windows") { |
| 74 | + "windows" |
| 75 | + } else if cfg!(target_os = "macos") { |
| 76 | + "apple-darwin" |
| 77 | + } else { |
| 78 | + "linux-gnu" |
| 79 | + }; |
| 80 | + info!("Detected host from src root {:?}: {arch}-unknown-{os}", src_root); |
| 81 | + Ok(format!("{arch}-unknown-{os}")) |
| 82 | +} |
0 commit comments