Skip to content

Commit e30caca

Browse files
committed
Move toolchain install handling out of CLI
1 parent c423dd0 commit e30caca

File tree

9 files changed

+59
-49
lines changed

9 files changed

+59
-49
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cargo-gpu/src/build.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
//! `cargo gpu build`, analogous to `cargo build`
2+
13
#![allow(clippy::shadow_reuse, reason = "let's not be silly")]
24
#![allow(clippy::unwrap_used, reason = "this is basically a test")]
3-
//! `cargo gpu build`, analogous to `cargo build`
45

5-
use crate::install::Install;
6-
use crate::linkage::Linkage;
7-
use crate::lockfile::LockfileMismatchHandler;
6+
use std::{io::Write as _, path::PathBuf};
7+
88
use anyhow::Context as _;
9+
use rustc_codegen_spirv_cache::user_output;
910
use spirv_builder::{CompileResult, ModuleResult, SpirvBuilder};
10-
use std::io::Write as _;
11-
use std::path::PathBuf;
11+
12+
use crate::{install::Install, linkage::Linkage, lockfile::LockfileMismatchHandler};
1213

1314
/// Args for just a build
1415
#[derive(clap::Parser, Debug, Clone, serde::Deserialize, serde::Serialize)]
@@ -102,10 +103,11 @@ impl Build {
102103
.context("unreachable")??;
103104
std::thread::park();
104105
} else {
105-
crate::user_output!(
106+
user_output!(
107+
std::io::stdout(),
106108
"Compiling shaders at {}...\n",
107109
self.install.shader_crate.display()
108-
);
110+
)?;
109111
let result = self.build.spirv_builder.build()?;
110112
self.parse_compilation_result(&result)?;
111113
}

crates/cargo-gpu/src/dump_usage.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! Convenience function for internal use. Dumps all the CLI usage instructions. Useful for
22
//! updating the README.
33
4-
use crate::{user_output, Cli};
4+
use rustc_codegen_spirv_cache::user_output;
5+
6+
use crate::Cli;
57

68
/// main dump usage function
79
pub fn dump_full_usage_for_readme() -> anyhow::Result<()> {
@@ -12,7 +14,8 @@ pub fn dump_full_usage_for_readme() -> anyhow::Result<()> {
1214
command.build();
1315

1416
write_help(&mut buffer, &mut command, 0)?;
15-
user_output!("{}", String::from_utf8(buffer)?);
17+
let message = String::from_utf8(buffer)?;
18+
user_output!(std::io::stdout(), "{message}")?;
1619

1720
Ok(())
1821
}

crates/cargo-gpu/src/install.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_codegen_spirv_cache::{
1010
SpirvSource,
1111
},
1212
target_specs::update_target_specs_files,
13+
toolchain, user_output,
1314
};
1415
use spirv_builder::SpirvBuilder;
1516

@@ -273,7 +274,7 @@ package = "rustc_codegen_spirv"
273274
.context("writing target spec files")?;
274275

275276
log::debug!("ensure_toolchain_and_components_exist");
276-
crate::install_toolchain::ensure_toolchain_and_components_exist(
277+
toolchain::ensure_toolchain_and_components_exist(
277278
&toolchain_channel,
278279
self.auto_install_rust_toolchain,
279280
)
@@ -287,7 +288,10 @@ package = "rustc_codegen_spirv"
287288
.context("remove Cargo.lock")?;
288289
}
289290

290-
crate::user_output!("Compiling `rustc_codegen_spirv` from source {}\n", source);
291+
user_output!(
292+
std::io::stdout(),
293+
"Compiling `rustc_codegen_spirv` from source {source}\n"
294+
)?;
291295
let mut cargo = spirv_builder::cargo_cmd::CargoCmd::new();
292296
cargo
293297
.current_dir(&install_dir)

crates/cargo-gpu/src/lib.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ mod build;
5858
mod config;
5959
mod dump_usage;
6060
mod install;
61-
mod install_toolchain;
6261
mod linkage;
6362
mod lockfile;
6463
mod metadata;
@@ -68,30 +67,6 @@ mod test;
6867
pub use install::*;
6968
pub use spirv_builder;
7069

71-
/// Central function to write to the user.
72-
#[macro_export]
73-
macro_rules! user_output {
74-
($($args: tt)*) => {
75-
#[allow(
76-
clippy::allow_attributes,
77-
clippy::useless_attribute,
78-
unused_imports,
79-
reason = "`std::io::Write` is only sometimes called??"
80-
)]
81-
use std::io::Write as _;
82-
83-
#[expect(
84-
clippy::non_ascii_literal,
85-
reason = "CRAB GOOD. CRAB IMPORTANT."
86-
)]
87-
{
88-
print!("🦀 ");
89-
}
90-
print!($($args)*);
91-
std::io::stdout().flush().unwrap();
92-
}
93-
}
94-
9570
/// All of the available subcommands for `cargo gpu`
9671
#[derive(clap::Subcommand)]
9772
#[non_exhaustive]

crates/cargo-gpu/src/lockfile.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! present. This module takes care of warning the user and potentially downgrading the lockfile.
44
55
use anyhow::Context as _;
6+
use rustc_codegen_spirv_cache::user_output;
67
use semver::Version;
78
use spirv_builder::query_rustc_version;
89
use std::io::Write as _;
@@ -188,7 +189,7 @@ impl LockfileMismatchHandler {
188189
is_force_overwrite_lockfiles_v4_to_v3: bool,
189190
) -> anyhow::Result<()> {
190191
if !is_force_overwrite_lockfiles_v4_to_v3 {
191-
Self::exit_with_v3v4_hack_suggestion();
192+
Self::exit_with_v3v4_hack_suggestion()?;
192193
}
193194

194195
Self::replace_cargo_lock_manifest_version(offending_cargo_lock, "4", "3")
@@ -240,9 +241,9 @@ impl LockfileMismatchHandler {
240241

241242
/// Exit and give the user advice on how to deal with the infamous v3/v4 Cargo lockfile version
242243
/// problem.
243-
#[expect(clippy::non_ascii_literal, reason = "It's CLI output")]
244-
fn exit_with_v3v4_hack_suggestion() {
245-
crate::user_output!(
244+
fn exit_with_v3v4_hack_suggestion() -> anyhow::Result<()> {
245+
user_output!(
246+
std::io::stdout(),
246247
"Conflicting `Cargo.lock` versions detected ⚠️\n\
247248
Because `cargo gpu` uses a dedicated Rust toolchain for compiling shaders\n\
248249
it's possible that the `Cargo.lock` manifest version of the shader crate\n\
@@ -259,7 +260,7 @@ impl LockfileMismatchHandler {
259260
\n\
260261
See `cargo gpu build --help` for more information.\n\
261262
"
262-
);
263+
)?;
263264
std::process::exit(1);
264265
}
265266
}

crates/rustc_codegen_spirv-cache/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ anyhow.workspace = true
1414
log.workspace = true
1515
directories.workspace = true
1616
cargo_metadata.workspace = true
17+
crossterm.workspace = true
1718

1819
[dev-dependencies]
1920
test-log.workspace = true

crates/rustc_codegen_spirv-cache/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,24 @@
1717
pub mod cache;
1818
pub mod spirv_source;
1919
pub mod target_specs;
20+
pub mod toolchain;
21+
22+
/// Writes formatted user output into a [writer](std::io::Write).
23+
#[macro_export]
24+
macro_rules! user_output {
25+
($dst:expr, $($args:tt)*) => {{
26+
#[allow(
27+
clippy::allow_attributes,
28+
clippy::useless_attribute,
29+
unused_imports,
30+
reason = "`std::io::Write` is only sometimes called??"
31+
)]
32+
use ::std::io::Write as _;
33+
34+
let mut writer = $dst;
35+
#[expect(clippy::non_ascii_literal, reason = "CRAB GOOD. CRAB IMPORTANT.")]
36+
::std::write!(writer, "🦀 ")
37+
.and_then(|()| ::std::write!(writer, $($args)*))
38+
.and_then(|()| ::std::io::Write::flush(&mut writer))
39+
}};
40+
}

crates/cargo-gpu/src/install_toolchain.rs renamed to crates/rustc_codegen_spirv-cache/src/toolchain.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
use anyhow::Context as _;
44
use crossterm::tty::IsTty as _;
55

6-
use crate::user_output;
7-
86
/// Use `rustup` to install the toolchain and components, if not already installed.
97
///
108
/// Pretty much runs:
119
///
1210
/// * rustup toolchain add nightly-2024-04-24
1311
/// * rustup component add --toolchain nightly-2024-04-24 rust-src rustc-dev llvm-tools
12+
#[inline]
1413
pub fn ensure_toolchain_and_components_exist(
1514
channel: &str,
1615
skip_toolchain_install_consent: bool,
@@ -36,7 +35,7 @@ pub fn ensure_toolchain_and_components_exist(
3635
format!("Install {message}").as_ref(),
3736
skip_toolchain_install_consent,
3837
)?;
39-
crate::user_output!("Installing {message}\n");
38+
crate::user_output!(std::io::stdout(), "Installing {message}\n")?;
4039

4140
let output_toolchain_add = std::process::Command::new("rustup")
4241
.args(["toolchain", "add"])
@@ -79,7 +78,7 @@ pub fn ensure_toolchain_and_components_exist(
7978
format!("Install {message}").as_ref(),
8079
skip_toolchain_install_consent,
8180
)?;
82-
crate::user_output!("Installing {message}\n");
81+
crate::user_output!(std::io::stdout(), "Installing {message}\n")?;
8382

8483
let output_component_add = std::process::Command::new("rustup")
8584
.args(["component", "add", "--toolchain"])
@@ -108,15 +107,18 @@ fn get_consent_for_toolchain_install(
108107
}
109108

110109
if !std::io::stdout().is_tty() {
111-
user_output!("No TTY detected so can't ask for consent to install Rust toolchain.");
110+
crate::user_output!(
111+
std::io::stdout(),
112+
"No TTY detected so can't ask for consent to install Rust toolchain."
113+
)?;
112114
log::error!("Attempted to ask for consent when there's no TTY");
113115
#[expect(clippy::exit, reason = "can't ask for user consent if there's no TTY")]
114116
std::process::exit(1);
115117
}
116118

117119
log::debug!("asking for consent to install the required toolchain");
118120
crossterm::terminal::enable_raw_mode().context("enabling raw mode")?;
119-
crate::user_output!("{prompt} [y/n]: ");
121+
crate::user_output!(std::io::stdout(), "{prompt} [y/n]: ")?;
120122
let mut input = crossterm::event::read().context("reading crossterm event")?;
121123

122124
if let crossterm::event::Event::Key(crossterm::event::KeyEvent {
@@ -138,7 +140,7 @@ fn get_consent_for_toolchain_install(
138140
{
139141
Ok(())
140142
} else {
141-
crate::user_output!("Exiting...\n");
143+
crate::user_output!(std::io::stdout(), "Exiting...\n")?;
142144
#[expect(clippy::exit, reason = "user requested abort")]
143145
std::process::exit(0);
144146
}

0 commit comments

Comments
 (0)