Skip to content

Commit 0f6287b

Browse files
committed
move target specs to new rustc_codegen_spirv_target_specs crate
1 parent fcb4fdf commit 0f6287b

26 files changed

+68
-24
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ members = [
1919

2020
"crates/rustc_codegen_spirv",
2121
"crates/rustc_codegen_spirv-types",
22+
"crates/rustc_codegen_spirv_target_specs",
2223
"crates/spirv-builder",
2324
"crates/spirv-std",
2425
"crates/spirv-std/shared",
@@ -46,6 +47,7 @@ spirv-std-macros = { path = "./crates/spirv-std/macros", version = "=0.9.0" }
4647
spirv-tools = { version = "0.11", default-features = false }
4748
rustc_codegen_spirv = { path = "./crates/rustc_codegen_spirv", version = "=0.9.0", default-features = false }
4849
rustc_codegen_spirv-types = { path = "./crates/rustc_codegen_spirv-types", version = "=0.9.0" }
50+
rustc_codegen_spirv_target_specs = { path = "./crates/rustc_codegen_spirv_target_specs", version = "=0.9.0" }
4951
tracing = "0.1"
5052
tracing-subscriber = { version = "0.3.3", features = ["env-filter", "json"] }
5153

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "rustc_codegen_spirv_target_specs"
3+
description = "target spec json files of rust-gpu for the rustc compiler"
4+
version.workspace = true
5+
authors.workspace = true
6+
edition.workspace = true
7+
license.workspace = true
8+
repository.workspace = true
9+
10+
[features]
11+
include_str = []
12+
dir_path = []
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# `rustc_codegen_spirv_target_spec`
2+
3+
The target spec json files of rust-gpu to hand to the rustc compiler, declaring various metadata about our codegen backend.
4+
5+
## Features
6+
* `include_str`: include target specs as string constants, for bundling with `cargo-gpu`
7+
* `dir_path`: export a path to the target specs dir, for `spirv-builder` and `compiletest` in this repo
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![doc = include_str!("../README.md")]
2+
3+
/// directory with all the `target-specs` jsons for our codegen backend
4+
#[cfg(feature = "dir_path")]
5+
pub const TARGET_SPEC_DIR_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/target-specs");
6+
7+
#[cfg(feature = "include_str")]
8+
mod include_str;
9+
#[cfg(feature = "include_str")]
10+
pub use include_str::TARGET_SPECS;

crates/spirv-builder/Cargo.toml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,23 @@ default = ["use-compiled-tools"]
2222
# Compile `rustc_codegen_spirv`, allows constructing SpirvBuilder without
2323
# explicitly passing in a path to a compiled `rustc_codegen_spirv.so` (or dll)
2424
rustc_codegen_spirv = ["dep:rustc_codegen_spirv"]
25+
# Inclide target spec json files, allows constructing SpirvBuilder without
26+
# explicitly passing a path to the target spec json
27+
include_target_specs = ["dep:rustc_codegen_spirv_target_specs"]
2528
# See `rustc_codegen_spirv/Cargo.toml` for details on these features.
26-
use-installed-tools = ["rustc_codegen_spirv", "rustc_codegen_spirv?/use-installed-tools"]
27-
use-compiled-tools = ["rustc_codegen_spirv", "rustc_codegen_spirv?/use-compiled-tools"]
29+
# We add new "default" features to `use-installed-tools` and `use-compiled-tools` to keep
30+
# backwards compat with `default-features = false, features = "use-installed-tools"` setups
31+
use-installed-tools = ["rustc_codegen_spirv", "include_target_specs", "rustc_codegen_spirv?/use-installed-tools"]
32+
use-compiled-tools = ["rustc_codegen_spirv", "include_target_specs", "rustc_codegen_spirv?/use-compiled-tools"]
2833
skip-toolchain-check = ["rustc_codegen_spirv?/skip-toolchain-check"]
2934

3035
watch = ["dep:notify"]
3136
clap = ["dep:clap"]
3237

3338
[dependencies]
34-
# See comment in `src/lib.rs` `invoke_rustc` regarding `rustc_codegen_spirv` dep.
35-
rustc_codegen_spirv.workspace = true
36-
# HACK(eddyb) see `docs.rs`-related comment above for why this is optional.
37-
rustc_codegen_spirv.optional = true
38-
39-
rustc_codegen_spirv-types.workspace = true
39+
rustc_codegen_spirv = { workspace = true, optional = true }
40+
rustc_codegen_spirv-types = { workspace = true }
41+
rustc_codegen_spirv_target_specs = { workspace = true, features = ["dir_path"], optional = true }
4042

4143
memchr = "2.4"
4244
raw-string = "0.3.5"

crates/spirv-builder/src/lib.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
#![doc = include_str!("../README.md")]
7474

7575
mod depfile;
76-
mod target_specs;
7776
#[cfg(feature = "watch")]
7877
mod watch;
7978

@@ -91,7 +90,9 @@ use thiserror::Error;
9190

9291
pub use rustc_codegen_spirv_types::Capability;
9392
pub use rustc_codegen_spirv_types::{CompileResult, ModuleResult};
94-
pub use target_specs::TARGET_SPECS;
93+
94+
#[cfg(feature = "include_target_specs")]
95+
pub use rustc_codegen_spirv_target_specs::TARGET_SPEC_DIR_PATH;
9596

9697
#[derive(Debug, Error)]
9798
#[non_exhaustive]
@@ -107,11 +108,16 @@ pub enum SpirvBuilderError {
107108
#[error("crate path '{0}' does not exist")]
108109
CratePathDoesntExist(PathBuf),
109110
#[error(
110-
"Without feature `rustc_codegen_spirv`, you need to set the path of the dylib using `rustc_codegen_spirv_location(...)`"
111+
"Without feature `rustc_codegen_spirv`, you need to set the path of the dylib with `rustc_codegen_spirv_location`"
111112
)]
112113
MissingRustcCodegenSpirvDylib,
113114
#[error("`rustc_codegen_spirv_location` path '{0}' is not a file")]
114115
RustcCodegenSpirvDylibDoesNotExist(PathBuf),
116+
#[error(
117+
"Without feature `include_target_specs`, instead of setting a `target`, \
118+
you need to set the path of the target spec file of your particular target with `path_to_target_spec`"
119+
)]
120+
MissingTargetSpec,
115121
#[error("build failed")]
116122
BuildFailed,
117123
#[error("multi-module build cannot be used with print_metadata = MetadataPrintout::Full")]
@@ -975,13 +981,13 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
975981
// target_spec jsons, some later version requires them, some earlier
976982
// version fails with them (notably our 0.9.0 release)
977983
if toolchain_rustc_version >= Version::new(1, 76, 0) {
978-
cargo
979-
.arg("--target")
980-
.arg(builder.path_to_target_spec.clone().unwrap_or_else(|| {
981-
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
982-
.join("target-specs")
983-
.join(format!("{}.json", target))
984-
}));
984+
let path = builder.path_to_target_spec.clone();
985+
let path = if cfg!(feature = "include_target_specs") {
986+
path.unwrap_or_else(|| PathBuf::from(format!("{TARGET_SPEC_DIR_PATH}/{target}.json")))
987+
} else {
988+
path.ok_or(SpirvBuilderError::MissingTargetSpec)?
989+
};
990+
cargo.arg("--target").arg(path);
985991
} else {
986992
cargo.arg("--target").arg(target);
987993
}

docs/src/writing-shader-crates.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ how to build SPIR-V. Here are a few things we need to mention there.
120120

121121
- Path to a spec of a target you're compiling for (see [platform support](./platform-support.md)).
122122
These specs reside in a directory inside the `spirv-builder` crate and an example relative path
123-
could look like `../rust-gpu/crates/spirv-builder/target-specs/spirv-unknown-spv1.3.json`.
123+
could look like `../rust-gpu/crates/rustc_codegen_spirv-types/target-specs/spirv-unknown-spv1.3.json`.
124124
- Absolute path to the `rustc_codegen_spirv` dynamic library that we built above.
125125
- Some additional options.
126126

tests/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use-compiled-tools = ["rustc_codegen_spirv/use-compiled-tools"]
1515

1616
[dependencies]
1717
compiletest = { version = "0.9.0", package = "compiletest_rs" }
18-
rustc_codegen_spirv.workspace = true
18+
rustc_codegen_spirv = { workspace = true }
19+
rustc_codegen_spirv_target_specs = { workspace = true, features = ["dir_path"] }
1920
clap = { version = "4", features = ["derive"] }
2021
itertools = "0.10.5"

tests/src/main.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clap::Parser;
22
use itertools::Itertools as _;
3+
use rustc_codegen_spirv_target_specs::TARGET_SPEC_DIR_PATH;
34
use std::{
45
env, io,
56
path::{Path, PathBuf},
@@ -30,10 +31,7 @@ impl Opt {
3031
const SPIRV_TARGET_PREFIX: &str = "spirv-unknown-";
3132

3233
fn target_spec_json(target: &str) -> String {
33-
format!(
34-
"{}/../crates/spirv-builder/target-specs/{target}.json",
35-
env!("CARGO_MANIFEST_DIR")
36-
)
34+
format!("{TARGET_SPEC_DIR_PATH}/{target}.json")
3735
}
3836

3937
#[derive(Copy, Clone)]

0 commit comments

Comments
 (0)