diff --git a/docs/launcher-commands.md b/docs/launcher-commands.md index d5ed3491a..aae91339d 100644 --- a/docs/launcher-commands.md +++ b/docs/launcher-commands.md @@ -763,6 +763,30 @@ Turn off a mod the browser installed; it stays installed. MCP: `mods_disable`. +## `horizon-launcher rust status` + +Whether it is installed, its version and whether this computer can link with it. + + horizon-launcher rust status + +MCP: `rust_status`. + +## `horizon-launcher rust install` + +Download the pinned toolchain from static.rust-lang.org (once), checked by SHA-256. + + horizon-launcher rust install + +MCP: `rust_install`. + +## `horizon-launcher rust remove` + +Delete the toolchain and the native builds' Cargo home. + + horizon-launcher rust remove + +MCP: `rust_remove`. + ## `horizon-launcher servers list` The servers, their folder and whether they run. diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index 4e2a1b11b..65832f4f9 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -500,15 +500,24 @@ fn signed_digest(tag: &str, asset: &str) -> Result { ) } -/// Downloads `url` into `to`; the file is kept only if its SHA-256 is `sha256`, the signed one. -fn download( +/// Downloads `url` into `to`; the file is kept only if its SHA-256 is `sha256`, the signed (or +/// pinned) one. A `size` of 0 takes the server's Content-Length for the progress. +pub(crate) fn download( url: &str, size: u64, sha256: &str, to: &Path, progress: &impl Fn(f32), ) -> Result<(), String> { - let mut reader = get(url)?.into_reader(); + let response = get(url)?; + let size = match size { + 0 => response + .header("Content-Length") + .and_then(|n| n.parse().ok()) + .unwrap_or(0), + n => n, + }; + let mut reader = response.into_reader(); let mut file = fs::File::create(to).map_err(|e| e.to_string())?; let mut hash = horizon_release_sign::Sha256::default(); let (mut done, mut buf) = (0u64, vec![0u8; 1 << 16]); diff --git a/launcher/src/cli.rs b/launcher/src/cli.rs index 7d71084f3..45c880990 100644 --- a/launcher/src/cli.rs +++ b/launcher/src/cli.rs @@ -23,6 +23,16 @@ struct Cli { command: Option, } +#[derive(Subcommand)] +enum RustCmd { + /// Whether it is installed, its version and whether this computer can link with it + Status, + /// Download the pinned toolchain from static.rust-lang.org (once), checked by SHA-256 + Install, + /// Delete the toolchain and the native builds' Cargo home + Remove, +} + #[derive(Subcommand)] enum ServersCmd { /// The servers, their folder and whether they run @@ -704,6 +714,12 @@ enum Cmd { #[command(subcommand)] action: Option, }, + /// The portable Rust toolchain native mods compile with (docs/native-mods.md): the launcher's + /// own, in its data folder, never installed on the system + Rust { + #[command(subcommand)] + action: Option, + }, /// Servers hosted with horizon-server (the Servers page): list them, create one, or run any /// horizon-server command on one Servers { @@ -1261,11 +1277,60 @@ fn execute(command: Cmd, as_json: bool) -> Result<(), String> { Cmd::Modpack { action } => modpack(action.unwrap_or(ModpackCmd::List), as_json)?, Cmd::Mods { action } => mods(action.unwrap_or(ModsCmd::List { instance: None }), as_json)?, Cmd::Servers { action } => servers(action.unwrap_or(ServersCmd::List), as_json)?, + Cmd::Rust { action } => rust(action.unwrap_or(RustCmd::Status), as_json)?, Cmd::Mcp => unreachable!(), } Ok(()) } +fn rust(action: RustCmd, as_json: bool) -> Result<(), String> { + use horizon_launcher::rust_toolchain as rust; + match action { + RustCmd::Status => { + let installed = rust::installed(); + let version = installed.as_ref().map(|t| t.version()).transpose()?; + let linker = rust::linker().err(); + if as_json { + json(serde_json::json!({ + "pinned": rust::VERSION, + "host": rust::host().map(|h| h.target), + "installed": installed.as_ref().map(|t| t.dir.display().to_string()), + "version": version, + "linker": linker, + })); + } else { + match (&installed, &version) { + (Some(t), Some(v)) => println!("{v} in {}", t.dir.display()), + _ => println!( + "Rust {} is not installed (`horizon-launcher rust install`).", + rust::VERSION + ), + } + println!( + "{}", + linker.unwrap_or_else(|| "This computer can link the game.".into()) + ); + } + } + RustCmd::Install => { + let shown = std::cell::Cell::new(0); + let toolchain = rust::install(|f| { + let percent = (f * 100.0) as u32; + if percent >= shown.get() + 10 { + shown.set(percent); + eprintln!("{percent}%"); + } + })?; + println!("{} in {}", toolchain.version()?, toolchain.dir.display()); + } + RustCmd::Remove => { + rust::remove()?; + println!("Removed the launcher's Rust toolchain and Cargo home."); + } + } + Ok(()) +} + fn update(check: bool, as_json: bool) -> Result<(), String> { let updates = backend::check_updates()?; if as_json && check { diff --git a/launcher/src/lib.rs b/launcher/src/lib.rs index d3c498ec2..bf258c6a0 100644 --- a/launcher/src/lib.rs +++ b/launcher/src/lib.rs @@ -21,6 +21,7 @@ pub mod nightly; pub mod packs; pub mod process; pub mod proxy; +pub mod rust_toolchain; pub mod screenshots; pub mod server_list; pub mod servers; diff --git a/launcher/src/rust_toolchain.rs b/launcher/src/rust_toolchain.rs new file mode 100644 index 000000000..f7be88dfe --- /dev/null +++ b/launcher/src/rust_toolchain.rs @@ -0,0 +1,407 @@ +//! A portable Rust toolchain of the launcher's own, for native mods (docs/native-mods.md), which +//! compile the game. levzzz5154 (2026-10-27): players combine many native mods per build, so they +//! build on their own computer, and the launcher brings the compiler. +//! +//! It downloads only the official components a build needs (`rustc`, `cargo`, `rust-std`, and on +//! Windows `rust-mingw`, the linker of the `windows-gnu` host, which needs no Visual Studio) from +//! static.rust-lang.org, each pinned by its SHA-256 below, and unpacks them into the launcher's data +//! folder. No rustup, nothing installed on the system or added to the PATH: the launcher calls this +//! cargo with its own `CARGO_HOME`. The pins come from the channel manifest of [`VERSION`] +//! (`https://static.rust-lang.org/dist/channel-rust-.toml`, `[pkg..target.]`). +use crate::backend; +use std::fs; +use std::path::{Path, PathBuf}; +use std::process::Command; + +/// The Rust release native mods build with: the stable release when it was pinned (CI builds the +/// game on stable; horizon-mixins needs 1.95 or newer). +pub const VERSION: &str = "1.99.1"; +const DIST: &str = "https://static.rust-lang.org/dist/2026-10-15"; + +/// One platform's components: (name, SHA-256 of `--.tar.gz`). +pub struct Host { + pub target: &'static str, + components: &'static [(&'static str, &'static str)], +} + +const HOSTS: &[Host] = &[ + Host { + target: "aarch64-apple-darwin", + components: &[ + ( + "rustc", + "cc998c1dc8df006106472357aa87838b2da3f7b25446b98a94cfd58796cb0610", + ), + ( + "cargo", + "b5cc7634f919949c3e5e131123f813416dc47f6b809a5371d12f1f593595ae13", + ), + ( + "rust-std", + "c124006d63885ba1029aca49efd59e93be6b2b171c379318ff94a00ef5eba664", + ), + ], + }, + Host { + target: "x86_64-unknown-linux-gnu", + components: &[ + ( + "rustc", + "85e7c0e6f21636f714e2a42966011364a6cc56b6548126d32144d6ba90904966", + ), + ( + "cargo", + "47babc028247e26577f8a1bf390b11f447ad9b7ab93f5c9a0dda078935fc149b", + ), + ( + "rust-std", + "d1290c05994a59c029e562cb75be1bddc6fb11a4c501e1b7b0f61d119c6cbb12", + ), + ], + }, + Host { + target: "x86_64-pc-windows-gnu", + components: &[ + ( + "rustc", + "43b17e9a9134ea72562bfbb23728a830f671ea9be1db6a72597a8d240e986b35", + ), + ( + "cargo", + "57e54617be4420dbb967ca8293f5ace7168104ac12efa9dc7c5c3bda112f8b36", + ), + ( + "rust-std", + "6546651238a3ac092b763511be524ca6eda6103e5d47342416923be1b3ce837c", + ), + ( + "rust-mingw", + "30263d7bb4fd0bf63a153afe36d7fc102f929a7d42781651c6b3a03a414ba428", + ), + ], + }, +]; + +/// This computer's host, if the launcher ships a toolchain for it (the platforms it is released +/// for: macOS on Apple silicon, Linux x86-64, Windows x64). +pub fn host() -> Option<&'static Host> { + let target = match (std::env::consts::OS, std::env::consts::ARCH) { + ("macos", "aarch64") => "aarch64-apple-darwin", + ("linux", "x86_64") => "x86_64-unknown-linux-gnu", + ("windows", "x86_64") => "x86_64-pc-windows-gnu", + _ => return None, + }; + HOSTS.iter().find(|h| h.target == target) +} + +fn exe(name: &str) -> String { + format!("{name}{}", std::env::consts::EXE_SUFFIX) +} + +/// An unpacked toolchain. +#[derive(Clone, Debug, PartialEq)] +pub struct Toolchain { + pub dir: PathBuf, +} + +impl Toolchain { + pub fn cargo(&self) -> PathBuf { + self.dir.join("bin").join(exe("cargo")) + } + + /// `program` (this toolchain's cargo, or a tool built with it) set up to use only this + /// toolchain: its rustc, the launcher's Cargo home, and none of the player's rustup or + /// compiler-wrapper settings. + pub fn command(&self, program: &Path) -> Command { + let mut cmd = Command::new(program); + let path = std::env::var_os("PATH").unwrap_or_default(); + let paths = std::iter::once(self.dir.join("bin")).chain(std::env::split_paths(&path)); + cmd.env("PATH", std::env::join_paths(paths).unwrap_or(path)) + .env("RUSTC", self.dir.join("bin").join(exe("rustc"))) + .env("CARGO_HOME", cargo_home()); + for name in [ + "RUSTUP_TOOLCHAIN", + "RUSTC_WRAPPER", + "RUSTC_WORKSPACE_WRAPPER", + "CARGO_BUILD_RUSTC_WRAPPER", + "CARGO_TARGET_DIR", + ] { + cmd.env_remove(name); + } + cmd + } + + /// `rustc --version`, once the toolchain answers. + pub fn version(&self) -> Result { + let out = self + .command(&self.dir.join("bin").join(exe("rustc"))) + .arg("--version") + .output() + .map_err(|e| format!("Rust {VERSION} doesn't start: {e}"))?; + if !out.status.success() { + return Err(format!( + "Rust {VERSION} doesn't start: {}", + String::from_utf8_lossy(&out.stderr).trim() + )); + } + Ok(String::from_utf8_lossy(&out.stdout).trim().to_owned()) + } +} + +/// Where toolchains live: `/rust/`. +pub fn root() -> PathBuf { + backend::data_dir().join("rust") +} + +/// The Cargo home of native builds (downloaded crates, horizon-mixins), never the player's own. +pub fn cargo_home() -> PathBuf { + backend::data_dir().join("cargo") +} + +fn dir_for(host: &Host) -> PathBuf { + root().join(format!("{VERSION}-{}", host.target)) +} + +const COMPLETE: &str = ".installed"; + +/// The toolchain, when it is installed. +pub fn installed() -> Option { + let dir = dir_for(host()?); + dir.join(COMPLETE).is_file().then_some(Toolchain { dir }) +} + +/// Downloads, checks and unpacks the pinned toolchain (once); `progress` gets the fraction done. +pub fn install(progress: impl Fn(f32)) -> Result { + if let Some(done) = installed() { + return Ok(done); + } + let host = host().ok_or_else(|| { + format!( + "Native mods need Rust, and the launcher has none for {} {}.", + std::env::consts::OS, + std::env::consts::ARCH + ) + })?; + let dir = dir_for(host); + let partial = root().join(format!(".{VERSION}-{}.partial", host.target)); + let _ = fs::remove_dir_all(&partial); + fs::create_dir_all(&partial).map_err(|e| e.to_string())?; + let count = host.components.len() as f32; + for (i, (name, sha256)) in host.components.iter().enumerate() { + let file = format!("{name}-{VERSION}-{}.tar.gz", host.target); + let archive = root().join(format!(".{file}")); + let step = |f: f32| progress((i as f32 + f) / count); + backend::download(&format!("{DIST}/{file}"), 0, sha256, &archive, &step) + .map_err(|e| format!("Rust {VERSION} ({name}): {e}"))?; + let unpacked = unpack(&archive, &partial); + let _ = fs::remove_file(&archive); + unpacked.map_err(|e| format!("Rust {VERSION} ({name}): {e}"))?; + } + fs::write(partial.join(COMPLETE), VERSION).map_err(|e| e.to_string())?; + let _ = fs::remove_dir_all(&dir); + fs::rename(&partial, &dir).map_err(|e| e.to_string())?; + progress(1.0); + Ok(Toolchain { dir }) +} + +/// Unpacks one component archive into `prefix`, as its installer would: an archive holds +/// `//` per component, and each component's files go under `prefix`. Its +/// installer script, lists and `manifest.in` stay out. +fn unpack(archive: &Path, prefix: &Path) -> Result<(), String> { + let file = fs::File::open(archive).map_err(|e| e.to_string())?; + let mut tar = tar::Archive::new(flate2::read::GzDecoder::new(file)); + for entry in tar.entries().map_err(|e| e.to_string())? { + let mut entry = entry.map_err(|e| e.to_string())?; + let path = entry.path().map_err(|e| e.to_string())?.into_owned(); + let Some(relative) = component_path(&path) else { + continue; + }; + let to = prefix.join(&relative); + if let Some(parent) = to.parent() { + fs::create_dir_all(parent).map_err(|e| e.to_string())?; + } + entry + .unpack(&to) + .map_err(|e| format!("{}: {e}", relative.display()))?; + } + Ok(()) +} + +/// Where a file of a component archive goes, relative to the prefix: `rustc-1.99.1-x/rustc/bin/rustc` +/// → `bin/rustc`. None for the archive's own files and anything that would leave the prefix. +fn component_path(path: &Path) -> Option { + use std::path::Component; + let parts: Vec<&std::ffi::OsStr> = path + .components() + .map(|c| match c { + Component::Normal(name) => Some(name), + _ => None, + }) + .collect::>()?; + match parts.as_slice() { + [_, _, rest @ ..] if !rest.is_empty() && rest != ["manifest.in"] => { + Some(rest.iter().collect()) + } + _ => None, + } +} + +/// Whether this computer can link what the toolchain compiles: Rust brings its compiler, but uses +/// the system's linker on macOS (the Command Line Tools) and Linux (`cc`). Windows' `windows-gnu` +/// toolchain brings its own. +pub fn linker() -> Result<(), String> { + let ok = |program: &str, args: &[&str]| { + Command::new(program) + .args(args) + .output() + .is_ok_and(|o| o.status.success()) + }; + match std::env::consts::OS { + // `xcode-select -p` only says where they are; running `cc` without them would open + // Apple's install dialog by surprise. + "macos" if !ok("xcode-select", &["-p"]) => Err( + "Native mods need Apple's Command Line Tools to link the game. Run `xcode-select --install` in Terminal, then build again." + .into(), + ), + "linux" if !ok("cc", &["--version"]) => Err( + "Native mods need a C compiler (`cc`) to link the game. Install gcc or clang with your distribution's package manager, then build again." + .into(), + ), + _ => Ok(()), + } +} + +/// Removes every toolchain and the native builds' Cargo home. +pub fn remove() -> Result<(), String> { + for dir in [root(), cargo_home()] { + if dir.exists() { + fs::remove_dir_all(&dir).map_err(|e| format!("{}: {e}", dir.display()))?; + } + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Write; + + #[test] + fn every_host_pins_each_component_once_with_a_sha256() { + for host in HOSTS { + let names: Vec<&str> = host.components.iter().map(|c| c.0).collect(); + assert_eq!( + &names[..3], + ["rustc", "cargo", "rust-std"], + "{}", + host.target + ); + for (name, sha) in host.components { + assert!( + sha.len() == 64 && sha.bytes().all(|b| b.is_ascii_hexdigit()), + "{name} {}", + host.target + ); + } + } + let windows = HOSTS.iter().find(|h| h.target.contains("windows")).unwrap(); + assert!(windows.target.ends_with("-gnu"), "no Visual Studio needed"); + assert!(windows.components.iter().any(|c| c.0 == "rust-mingw")); + if cfg!(all(target_os = "macos", target_arch = "aarch64")) { + assert_eq!(host().unwrap().target, "aarch64-apple-darwin"); + } + } + + #[test] + fn component_files_land_under_the_prefix_and_nothing_else_does() { + let at = |p: &str| component_path(Path::new(p)); + assert_eq!( + at("rustc-1.99.1-x/rustc/bin/rustc"), + Some(PathBuf::from("bin/rustc")) + ); + assert_eq!( + at("rust-std-1.99.1-x/rust-std-x/lib/rustlib/x/lib/libstd.rlib"), + Some(PathBuf::from("lib/rustlib/x/lib/libstd.rlib")) + ); + for skipped in [ + "rustc-1.99.1-x/install.sh", + "rustc-1.99.1-x/components", + "rustc-1.99.1-x/rustc/manifest.in", + "rustc-1.99.1-x/rustc", + "rustc-1.99.1-x/../../etc/passwd", + "/rustc-1.99.1-x/rustc/bin/rustc", + ] { + assert_eq!(at(skipped), None, "{skipped}"); + } + } + + #[test] + fn a_component_archive_unpacks_as_its_installer_would() { + let dir = std::env::temp_dir().join(format!("horizon-rust-unpack-{}", std::process::id())); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(&dir).unwrap(); + let archive = dir.join("cargo.tar.gz"); + let mut tar = tar::Builder::new(flate2::write::GzEncoder::new( + fs::File::create(&archive).unwrap(), + flate2::Compression::fast(), + )); + for (path, body, mode) in [ + ("cargo-1.99.1-x/install.sh", "#!/bin/sh", 0o755), + ("cargo-1.99.1-x/cargo/manifest.in", "file:bin/cargo", 0o644), + ("cargo-1.99.1-x/cargo/bin/cargo", "binary", 0o755), + ] { + let mut header = tar::Header::new_gnu(); + header.set_size(body.len() as u64); + header.set_mode(mode); + header.set_cksum(); + tar.append_data(&mut header, path, body.as_bytes()).unwrap(); + } + tar.into_inner().unwrap().finish().unwrap().flush().unwrap(); + let prefix = dir.join("prefix"); + unpack(&archive, &prefix).unwrap(); + assert_eq!( + fs::read_to_string(prefix.join("bin/cargo")).unwrap(), + "binary" + ); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mode = fs::metadata(prefix.join("bin/cargo")) + .unwrap() + .permissions() + .mode(); + assert_eq!(mode & 0o111, 0o111, "cargo stays executable"); + } + assert!(!prefix.join("install.sh").exists() && !prefix.join("manifest.in").exists()); + fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn the_command_uses_only_the_launchers_toolchain() { + let toolchain = Toolchain { + dir: PathBuf::from("/rust/1.99.1-x"), + }; + let cmd = toolchain.command(&toolchain.cargo()); + let env: Vec<(String, Option)> = cmd + .get_envs() + .map(|(k, v)| { + ( + k.to_string_lossy().into_owned(), + v.map(|v| v.to_string_lossy().into_owned()), + ) + }) + .collect(); + let get = |name: &str| env.iter().find(|(k, _)| k == name).map(|(_, v)| v.clone()); + assert_eq!(get("RUSTUP_TOOLCHAIN"), Some(None)); + assert_eq!(get("RUSTC_WRAPPER"), Some(None)); + assert!(get("RUSTC") + .flatten() + .unwrap() + .starts_with("/rust/1.99.1-x/bin/rustc")); + assert!(get("PATH") + .flatten() + .unwrap() + .starts_with("/rust/1.99.1-x/bin")); + assert!(get("CARGO_HOME").flatten().unwrap().ends_with("cargo")); + } +}