Skip to content

Commit b95f88a

Browse files
committed
Decouple target specs handling, add error types for it
1 parent e5539ed commit b95f88a

File tree

10 files changed

+276
-208
lines changed

10 files changed

+276
-208
lines changed

Cargo.lock

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

crates/cargo-gpu-cache/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ rustc_codegen_spirv-cache = { path = "../rustc_codegen_spirv-cache", default-fea
1212
cargo_metadata.workspace = true
1313
anyhow.workspace = true
1414
spirv-builder.workspace = true
15-
legacy_target_specs.workspace = true
1615
clap = { workspace = true, optional = true }
1716
log.workspace = true
1817
relative-path.workspace = true

crates/cargo-gpu-cache/src/install.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ use anyhow::Context as _;
66
use rustc_codegen_spirv_cache::{
77
cache::cache_dir,
88
metadata::{query_metadata, MetadataExt as _},
9-
spirv_source::{get_channel_from_rustc_codegen_spirv_build_script, SpirvSource},
9+
spirv_source::{rust_gpu_toolchain_channel, SpirvSource},
10+
target_specs::update_target_specs_files,
1011
};
1112
use spirv_builder::SpirvBuilder;
1213

13-
use crate::target_specs::update_target_specs_files;
14-
1514
/// Represents a functional backend installation, whether it was cached or just installed.
1615
#[derive(Clone, Debug, Default)]
1716
#[non_exhaustive]
@@ -262,10 +261,9 @@ package = "rustc_codegen_spirv"
262261
let rustc_codegen_spirv = dummy_metadata.find_package("rustc_codegen_spirv").context(
263262
"resolving toolchain version: expected a dependency on `rustc_codegen_spirv`",
264263
)?;
265-
let toolchain_channel =
266-
get_channel_from_rustc_codegen_spirv_build_script(rustc_codegen_spirv).context(
267-
"resolving toolchain version: read toolchain from `rustc_codegen_spirv`'s build.rs",
268-
)?;
264+
let toolchain_channel = rust_gpu_toolchain_channel(rustc_codegen_spirv).context(
265+
"resolving toolchain version: read toolchain from `rustc_codegen_spirv`'s build.rs",
266+
)?;
269267
log::info!("selected toolchain channel `{toolchain_channel:?}`");
270268

271269
log::debug!("update_spec_files");

crates/cargo-gpu-cache/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ mod linkage;
5252
mod lockfile;
5353
mod metadata;
5454
mod show;
55-
mod target_specs;
5655

5756
pub use install::*;
5857
pub use spirv_builder;

crates/cargo-gpu-cache/src/show.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ use std::{fs, path::Path};
55
use anyhow::bail;
66
use rustc_codegen_spirv_cache::{
77
cache::cache_dir, metadata::query_metadata, spirv_source::SpirvSource,
8+
target_specs::update_target_specs_files,
89
};
910

10-
use crate::target_specs::update_target_specs_files;
11-
1211
/// Show the computed source of the spirv-std dependency.
1312
#[derive(Clone, Debug)]
1413
#[cfg_attr(feature = "clap", derive(clap::Parser))]

crates/cargo-gpu-cache/src/target_specs.rs

Lines changed: 0 additions & 137 deletions
This file was deleted.

crates/rustc_codegen_spirv-cache/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ keywords.workspace = true
88
license.workspace = true
99

1010
[dependencies]
11+
legacy_target_specs.workspace = true
1112
thiserror.workspace = true
1213
directories.workspace = true
1314
cargo_metadata.workspace = true

crates/rustc_codegen_spirv-cache/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ pub use cargo_metadata;
2323
pub mod cache;
2424
pub mod metadata;
2525
pub mod spirv_source;
26+
pub mod target_specs;

crates/rustc_codegen_spirv-cache/src/spirv_source.rs

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -251,75 +251,27 @@ pub enum ParseSourceVersionError {
251251
InvalidManifestPath(Utf8PathBuf),
252252
}
253253

254-
/// An error indicating that getting the channel of a Rust toolchain from the package failed.
255-
#[derive(Debug, thiserror::Error)]
256-
#[non_exhaustive]
257-
pub enum GetChannelError {
258-
/// Package is located at the root of the file system
259-
/// and cannot have a parent.
260-
#[error("package located at root")]
261-
PackageAtRoot,
262-
/// Build script file is not valid or does not exist.
263-
#[error("invalid build script {build_script}: {source}")]
264-
InvalidBuildScript {
265-
/// Source of the error.
266-
source: io::Error,
267-
/// Path to the build script file.
268-
build_script: Utf8PathBuf,
269-
},
270-
/// There is no line starting with `channel_start`
271-
/// in the build script file contents.
272-
#[error("`{channel_start}` line in {build_script:?} not found")]
273-
ChannelStartNotFound {
274-
/// Start of the channel line.
275-
channel_start: &'static str,
276-
/// Path to the build script file.
277-
build_script: Utf8PathBuf,
278-
},
279-
/// Channel line does not contain `channel_end`
280-
/// in the build script file contents.
281-
#[error("ending `{channel_end}` of line \"{channel_line}\" in {build_script:?} not found")]
282-
ChannelEndNotFound {
283-
/// End of the channel line.
284-
channel_end: &'static str,
285-
/// The line containing the channel information.
286-
channel_line: String,
287-
/// Path to the build script file.
288-
build_script: Utf8PathBuf,
289-
},
290-
/// The range to slice the channel line is not valid.
291-
#[error("cannot slice line \"{channel_line}\" of {build_script:?} by range {range:?}")]
292-
InvalidRange {
293-
/// The invalid range.
294-
range: Range<usize>,
295-
/// The line containing the channel information.
296-
channel_line: String,
297-
/// Path to the build script file.
298-
build_script: Utf8PathBuf,
299-
},
300-
}
301-
302254
/// Parse the `rust-toolchain.toml` in the working tree of the checked-out version of the `rust-gpu` repo.
303255
///
304256
/// # Errors
305257
///
306258
/// Returns an error if the package is at the root of the filesystem,
307259
/// build script does not exist, or there is no definition of `channel` in it.
308260
#[inline]
309-
pub fn get_channel_from_rustc_codegen_spirv_build_script(
310-
rustc_codegen_spirv_package: &Package,
311-
) -> Result<String, GetChannelError> {
312-
let path = rustc_codegen_spirv_package
261+
pub fn rust_gpu_toolchain_channel(
262+
rustc_codegen_spirv: &Package,
263+
) -> Result<String, RustGpuToolchainChannelError> {
264+
let path = rustc_codegen_spirv
313265
.manifest_path
314266
.parent()
315-
.ok_or(GetChannelError::PackageAtRoot)?;
267+
.ok_or(RustGpuToolchainChannelError::PackageAtRoot)?;
316268
let build_script = path.join("build.rs");
317269

318270
log::debug!("Parsing `build.rs` at {build_script:?} for the used toolchain");
319271
let contents = match fs::read_to_string(&build_script) {
320272
Ok(contents) => contents,
321273
Err(source) => {
322-
let err = GetChannelError::InvalidBuildScript {
274+
let err = RustGpuToolchainChannelError::InvalidBuildScript {
323275
source,
324276
build_script,
325277
};
@@ -332,8 +284,8 @@ pub fn get_channel_from_rustc_codegen_spirv_build_script(
332284
.lines()
333285
.find(|line| line.starts_with(channel_start))
334286
else {
335-
let err = GetChannelError::ChannelStartNotFound {
336-
channel_start,
287+
let err = RustGpuToolchainChannelError::ChannelStartNotFound {
288+
channel_start: channel_start.to_owned(),
337289
build_script,
338290
};
339291
return Err(err);
@@ -346,8 +298,8 @@ pub fn get_channel_from_rustc_codegen_spirv_build_script(
346298
.find(channel_end)
347299
.map(|end| end + start)
348300
else {
349-
let err = GetChannelError::ChannelEndNotFound {
350-
channel_end,
301+
let err = RustGpuToolchainChannelError::ChannelEndNotFound {
302+
channel_end: channel_end.to_owned(),
351303
channel_line: channel_line.to_owned(),
352304
build_script,
353305
};
@@ -356,7 +308,7 @@ pub fn get_channel_from_rustc_codegen_spirv_build_script(
356308

357309
let range = start..end;
358310
let Some(channel) = channel_line.get(range.clone()) else {
359-
let err = GetChannelError::InvalidRange {
311+
let err = RustGpuToolchainChannelError::InvalidRange {
360312
range,
361313
channel_line: channel_line.to_owned(),
362314
build_script,
@@ -366,6 +318,54 @@ pub fn get_channel_from_rustc_codegen_spirv_build_script(
366318
Ok(channel.to_owned())
367319
}
368320

321+
/// An error indicating that getting the channel of a Rust toolchain from the package failed.
322+
#[derive(Debug, thiserror::Error)]
323+
#[non_exhaustive]
324+
pub enum RustGpuToolchainChannelError {
325+
/// Package is located at the root of the file system
326+
/// and cannot have a parent.
327+
#[error("package located at root")]
328+
PackageAtRoot,
329+
/// Build script file is not valid or does not exist.
330+
#[error("invalid build script {build_script}: {source}")]
331+
InvalidBuildScript {
332+
/// Source of the error.
333+
source: io::Error,
334+
/// Path to the build script file.
335+
build_script: Utf8PathBuf,
336+
},
337+
/// There is no line starting with `channel_start`
338+
/// in the build script file contents.
339+
#[error("`{channel_start}` line in {build_script:?} not found")]
340+
ChannelStartNotFound {
341+
/// Start of the channel line.
342+
channel_start: String,
343+
/// Path to the build script file.
344+
build_script: Utf8PathBuf,
345+
},
346+
/// Channel line does not contain `channel_end`
347+
/// in the build script file contents.
348+
#[error("ending `{channel_end}` of line \"{channel_line}\" in {build_script:?} not found")]
349+
ChannelEndNotFound {
350+
/// End of the channel line.
351+
channel_end: String,
352+
/// The line containing the channel information.
353+
channel_line: String,
354+
/// Path to the build script file.
355+
build_script: Utf8PathBuf,
356+
},
357+
/// The range to slice the channel line is not valid.
358+
#[error("cannot slice line \"{channel_line}\" of {build_script:?} by range {range:?}")]
359+
InvalidRange {
360+
/// The invalid range.
361+
range: Range<usize>,
362+
/// The line containing the channel information.
363+
channel_line: String,
364+
/// Path to the build script file.
365+
build_script: Utf8PathBuf,
366+
},
367+
}
368+
369369
/// Returns a string suitable to use as a directory.
370370
///
371371
/// Created from the spirv-builder source dep and the rustc channel.

0 commit comments

Comments
 (0)