WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 2c620bd

Browse files
committed
target-spec: accept both targets and target envs as valid targets
1 parent 823b0c2 commit 2c620bd

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

crates/rustc_codegen_spirv-types/src/target.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,29 @@ pub const SPIRV_TARGET_PREFIX: &str = "spirv-unknown-";
55

66
/// A well-formed rust-gpu target.
77
///
8-
/// The constructors [`Self::from_target`] or [`Self::from_env`] only check whether the target is well-formed, not
9-
/// whether it is valid. Since `spirv-builder` is backwards compatible with older rust-gpu compilers, only the compiler
10-
/// itself knows what targets it can and cannot support. This also allows adding new targets to the compiler without
11-
/// having to update `spirv-builder` and `cargo-gpu`.
8+
/// The constructors only check whether the target is well-formed, not whether it is valid. Since `spirv-builder` is
9+
/// backwards compatible with older rust-gpu compilers, only the compiler itself knows what targets it can and cannot
10+
/// support. This also allows adding new targets to the compiler without having to update `spirv-builder` and
11+
/// `cargo-gpu`.
12+
///
13+
/// Differentiates between a full target (e.g. `spirv-unknown-vulkan1.3`) and a target env (e.g. `vulkan1.3`). Use
14+
/// [`Self::parse_target`] and [`Self::target`] to parse or format a full target, or use [`Self::parse_env`] and
15+
/// [`Self::env`] when dealing with just target envs. The convenience function [`Self::parse`] accepts both targets and
16+
/// target envs. Does not implement `Display`, since it is unclear whether the user wants a target or target env, though
17+
/// a `Debug` implementation is provided.
1218
#[derive(Clone)]
1319
pub struct SpirvTarget {
1420
target: String,
1521
}
1622

1723
impl SpirvTarget {
18-
pub fn from_target(target: &str) -> Result<Self, TargetError> {
24+
/// Try to parse either a full target or a target env
25+
pub fn parse(target_or_env: &str) -> Result<Self, TargetError> {
26+
Self::parse_target(target_or_env).or_else(|_| Self::parse_env(target_or_env))
27+
}
28+
29+
/// Parse a full target, e.g. `spirv-unknown-vulkan1.3`
30+
pub fn parse_target(target: &str) -> Result<Self, TargetError> {
1931
let _target_env = target.strip_prefix(SPIRV_TARGET_PREFIX).ok_or_else(|| {
2032
TargetError::NonSpirvTarget {
2133
target: target.to_string(),
@@ -26,17 +38,20 @@ impl SpirvTarget {
2638
})
2739
}
2840

29-
pub fn from_env(target_env: &str) -> Result<Self, TargetError> {
41+
/// Parse a target env, e.g. `vulkan1.3`
42+
pub fn parse_env(target_env: &str) -> Result<Self, TargetError> {
3043
Ok(Self {
3144
target: format!("{SPIRV_TARGET_PREFIX}{target_env}"),
3245
})
3346
}
3447

48+
/// returns the full target, e.g. `spirv-unknown-vulkan1.3`
3549
pub fn target(&self) -> &str {
3650
&self.target
3751
}
3852

39-
pub fn target_env(&self) -> &str {
53+
/// returns the target env, e.g. `vulkan1.3`
54+
pub fn env(&self) -> &str {
4055
&self.target[SPIRV_TARGET_PREFIX.len()..]
4156
}
4257
}

crates/rustc_codegen_spirv-types/src/target_spec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl TargetSpecVersion {
5050

5151
/// format the target spec json
5252
pub fn format_spec(&self, target: &SpirvTarget) -> String {
53-
let target_env = target.target_env();
53+
let target_env = target.env();
5454
let extra = match self {
5555
TargetSpecVersion::Rustc_1_85_0 => r#""crt-static-respected": true,"#,
5656
TargetSpecVersion::Rustc_1_76_0 => r#""os": "unknown","#,

crates/spirv-builder/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
784784
.target
785785
.as_ref()
786786
.ok_or(SpirvBuilderError::MissingTarget)?;
787-
target = SpirvTarget::from_target(target_str)?;
787+
target = SpirvTarget::parse(target_str)?;
788788

789789
if (builder.print_metadata == MetadataPrintout::Full) && builder.multimodule {
790790
return Err(SpirvBuilderError::MultiModuleWithPrintMetadata);

tests/compiletests/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl Runner {
156156

157157
println!("Testing env: {stage_id}\n");
158158

159-
let target = SpirvTarget::from_env(env).unwrap();
159+
let target = SpirvTarget::parse(env).unwrap();
160160
let libs = self.build_deps(&target);
161161
let mut flags = test_rustc_flags(
162162
&self.codegen_backend_path,

0 commit comments

Comments
 (0)