diff --git a/Cargo.lock b/Cargo.lock index 080c4c1f4b..0038aed6fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -498,6 +498,7 @@ dependencies = [ "compiletest_rs", "itertools", "rustc_codegen_spirv", + "rustc_codegen_spirv-target-specs", ] [[package]] @@ -2334,6 +2335,7 @@ dependencies = [ "regex", "rspirv", "rustc-demangle", + "rustc_codegen_spirv-target-specs", "rustc_codegen_spirv-types", "rustix", "sanitize-filename", @@ -2346,6 +2348,10 @@ dependencies = [ "tracing-tree", ] +[[package]] +name = "rustc_codegen_spirv-target-specs" +version = "0.9.0" + [[package]] name = "rustc_codegen_spirv-types" version = "0.9.0" @@ -2661,6 +2667,7 @@ dependencies = [ "notify", "raw-string", "rustc_codegen_spirv", + "rustc_codegen_spirv-target-specs", "rustc_codegen_spirv-types", "semver", "serde", diff --git a/Cargo.toml b/Cargo.toml index 13b1c0337f..d5d439a7a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ members = [ "crates/rustc_codegen_spirv", "crates/rustc_codegen_spirv-types", + "crates/rustc_codegen_spirv-target-specs", "crates/spirv-builder", "crates/spirv-std", "crates/spirv-std/shared", @@ -46,6 +47,7 @@ spirv-std-macros = { path = "./crates/spirv-std/macros", version = "=0.9.0" } spirv-tools = { version = "0.11", default-features = false } rustc_codegen_spirv = { path = "./crates/rustc_codegen_spirv", version = "=0.9.0", default-features = false } rustc_codegen_spirv-types = { path = "./crates/rustc_codegen_spirv-types", version = "=0.9.0" } +rustc_codegen_spirv-target-specs = { path = "crates/rustc_codegen_spirv-target-specs", version = "=0.9.0" } tracing = "0.1" tracing-subscriber = { version = "0.3.3", features = ["env-filter", "json"] } diff --git a/crates/rustc_codegen_spirv-target-specs/Cargo.toml b/crates/rustc_codegen_spirv-target-specs/Cargo.toml new file mode 100644 index 0000000000..76e6e2c106 --- /dev/null +++ b/crates/rustc_codegen_spirv-target-specs/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "rustc_codegen_spirv-target-specs" +description = "target spec json files of rust-gpu for the rustc compiler" +version.workspace = true +authors.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true + +[features] +include_str = [] +dir_path = [] diff --git a/crates/rustc_codegen_spirv-target-specs/README.md b/crates/rustc_codegen_spirv-target-specs/README.md new file mode 100644 index 0000000000..9b0bc4d74a --- /dev/null +++ b/crates/rustc_codegen_spirv-target-specs/README.md @@ -0,0 +1,7 @@ +# `rustc_codegen_spirv-target-specs` + +The target spec json files of rust-gpu to hand to the rustc compiler, declaring various metadata about our codegen backend. + +## Features +* `include_str`: include target specs as string constants, for bundling with `cargo-gpu` +* `dir_path`: export a path to the target specs dir, for `spirv-builder` and `compiletest` in this repo diff --git a/crates/spirv-builder/src/target_specs.rs b/crates/rustc_codegen_spirv-target-specs/src/include_str.rs similarity index 100% rename from crates/spirv-builder/src/target_specs.rs rename to crates/rustc_codegen_spirv-target-specs/src/include_str.rs diff --git a/crates/rustc_codegen_spirv-target-specs/src/lib.rs b/crates/rustc_codegen_spirv-target-specs/src/lib.rs new file mode 100644 index 0000000000..b3033d9fee --- /dev/null +++ b/crates/rustc_codegen_spirv-target-specs/src/lib.rs @@ -0,0 +1,10 @@ +#![doc = include_str!("../README.md")] + +/// directory with all the `target-specs` jsons for our codegen backend +#[cfg(feature = "dir_path")] +pub const TARGET_SPEC_DIR_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/target-specs"); + +#[cfg(feature = "include_str")] +mod include_str; +#[cfg(feature = "include_str")] +pub use include_str::TARGET_SPECS; diff --git a/crates/spirv-builder/target-specs/spirv-unknown-opengl4.0.json b/crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-opengl4.0.json similarity index 100% rename from crates/spirv-builder/target-specs/spirv-unknown-opengl4.0.json rename to crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-opengl4.0.json diff --git a/crates/spirv-builder/target-specs/spirv-unknown-opengl4.1.json b/crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-opengl4.1.json similarity index 100% rename from crates/spirv-builder/target-specs/spirv-unknown-opengl4.1.json rename to crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-opengl4.1.json diff --git a/crates/spirv-builder/target-specs/spirv-unknown-opengl4.2.json b/crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-opengl4.2.json similarity index 100% rename from crates/spirv-builder/target-specs/spirv-unknown-opengl4.2.json rename to crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-opengl4.2.json diff --git a/crates/spirv-builder/target-specs/spirv-unknown-opengl4.3.json b/crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-opengl4.3.json similarity index 100% rename from crates/spirv-builder/target-specs/spirv-unknown-opengl4.3.json rename to crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-opengl4.3.json diff --git a/crates/spirv-builder/target-specs/spirv-unknown-opengl4.5.json b/crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-opengl4.5.json similarity index 100% rename from crates/spirv-builder/target-specs/spirv-unknown-opengl4.5.json rename to crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-opengl4.5.json diff --git a/crates/spirv-builder/target-specs/spirv-unknown-spv1.0.json b/crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-spv1.0.json similarity index 100% rename from crates/spirv-builder/target-specs/spirv-unknown-spv1.0.json rename to crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-spv1.0.json diff --git a/crates/spirv-builder/target-specs/spirv-unknown-spv1.1.json b/crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-spv1.1.json similarity index 100% rename from crates/spirv-builder/target-specs/spirv-unknown-spv1.1.json rename to crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-spv1.1.json diff --git a/crates/spirv-builder/target-specs/spirv-unknown-spv1.2.json b/crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-spv1.2.json similarity index 100% rename from crates/spirv-builder/target-specs/spirv-unknown-spv1.2.json rename to crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-spv1.2.json diff --git a/crates/spirv-builder/target-specs/spirv-unknown-spv1.3.json b/crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-spv1.3.json similarity index 100% rename from crates/spirv-builder/target-specs/spirv-unknown-spv1.3.json rename to crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-spv1.3.json diff --git a/crates/spirv-builder/target-specs/spirv-unknown-spv1.4.json b/crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-spv1.4.json similarity index 100% rename from crates/spirv-builder/target-specs/spirv-unknown-spv1.4.json rename to crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-spv1.4.json diff --git a/crates/spirv-builder/target-specs/spirv-unknown-spv1.5.json b/crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-spv1.5.json similarity index 100% rename from crates/spirv-builder/target-specs/spirv-unknown-spv1.5.json rename to crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-spv1.5.json diff --git a/crates/spirv-builder/target-specs/spirv-unknown-vulkan1.0.json b/crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-vulkan1.0.json similarity index 100% rename from crates/spirv-builder/target-specs/spirv-unknown-vulkan1.0.json rename to crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-vulkan1.0.json diff --git a/crates/spirv-builder/target-specs/spirv-unknown-vulkan1.1.json b/crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-vulkan1.1.json similarity index 100% rename from crates/spirv-builder/target-specs/spirv-unknown-vulkan1.1.json rename to crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-vulkan1.1.json diff --git a/crates/spirv-builder/target-specs/spirv-unknown-vulkan1.1spv1.4.json b/crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-vulkan1.1spv1.4.json similarity index 100% rename from crates/spirv-builder/target-specs/spirv-unknown-vulkan1.1spv1.4.json rename to crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-vulkan1.1spv1.4.json diff --git a/crates/spirv-builder/target-specs/spirv-unknown-vulkan1.2.json b/crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-vulkan1.2.json similarity index 100% rename from crates/spirv-builder/target-specs/spirv-unknown-vulkan1.2.json rename to crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-vulkan1.2.json diff --git a/crates/rustc_codegen_spirv/Cargo.toml b/crates/rustc_codegen_spirv/Cargo.toml index f863d086d9..5af8dbea20 100644 --- a/crates/rustc_codegen_spirv/Cargo.toml +++ b/crates/rustc_codegen_spirv/Cargo.toml @@ -61,6 +61,9 @@ tracing.workspace = true tracing-subscriber.workspace = true tracing-tree = "0.3.0" +# required for cargo gpu to resolve the needed target specs +rustc_codegen_spirv-target-specs.workspace = true + [dev-dependencies] pretty_assertions = "1.0" diff --git a/crates/spirv-builder/Cargo.toml b/crates/spirv-builder/Cargo.toml index 545085c114..d8d2e68c95 100644 --- a/crates/spirv-builder/Cargo.toml +++ b/crates/spirv-builder/Cargo.toml @@ -22,21 +22,23 @@ default = ["use-compiled-tools"] # Compile `rustc_codegen_spirv`, allows constructing SpirvBuilder without # explicitly passing in a path to a compiled `rustc_codegen_spirv.so` (or dll) rustc_codegen_spirv = ["dep:rustc_codegen_spirv"] +# Inclide target spec json files, allows constructing SpirvBuilder without +# explicitly passing a path to the target spec json +include-target-specs = ["dep:rustc_codegen_spirv-target-specs"] # See `rustc_codegen_spirv/Cargo.toml` for details on these features. -use-installed-tools = ["rustc_codegen_spirv", "rustc_codegen_spirv?/use-installed-tools"] -use-compiled-tools = ["rustc_codegen_spirv", "rustc_codegen_spirv?/use-compiled-tools"] +# We add new "default" features to `use-installed-tools` and `use-compiled-tools` to keep +# backwards compat with `default-features = false, features = "use-installed-tools"` setups +use-installed-tools = ["rustc_codegen_spirv", "include-target-specs", "rustc_codegen_spirv?/use-installed-tools"] +use-compiled-tools = ["rustc_codegen_spirv", "include-target-specs", "rustc_codegen_spirv?/use-compiled-tools"] skip-toolchain-check = ["rustc_codegen_spirv?/skip-toolchain-check"] watch = ["dep:notify"] clap = ["dep:clap"] [dependencies] -# See comment in `src/lib.rs` `invoke_rustc` regarding `rustc_codegen_spirv` dep. -rustc_codegen_spirv.workspace = true -# HACK(eddyb) see `docs.rs`-related comment above for why this is optional. -rustc_codegen_spirv.optional = true - -rustc_codegen_spirv-types.workspace = true +rustc_codegen_spirv = { workspace = true, optional = true } +rustc_codegen_spirv-types = { workspace = true } +rustc_codegen_spirv-target-specs = { workspace = true, features = ["dir_path"], optional = true } memchr = "2.4" raw-string = "0.3.5" diff --git a/crates/spirv-builder/src/lib.rs b/crates/spirv-builder/src/lib.rs index ea017ed526..80cc8ad246 100644 --- a/crates/spirv-builder/src/lib.rs +++ b/crates/spirv-builder/src/lib.rs @@ -73,7 +73,6 @@ #![doc = include_str!("../README.md")] mod depfile; -mod target_specs; #[cfg(feature = "watch")] mod watch; @@ -91,7 +90,9 @@ use thiserror::Error; pub use rustc_codegen_spirv_types::Capability; pub use rustc_codegen_spirv_types::{CompileResult, ModuleResult}; -pub use target_specs::TARGET_SPECS; + +#[cfg(feature = "include-target-specs")] +pub use rustc_codegen_spirv_target_specs::TARGET_SPEC_DIR_PATH; #[derive(Debug, Error)] #[non_exhaustive] @@ -107,11 +108,16 @@ pub enum SpirvBuilderError { #[error("crate path '{0}' does not exist")] CratePathDoesntExist(PathBuf), #[error( - "Without feature `rustc_codegen_spirv`, you need to set the path of the dylib using `rustc_codegen_spirv_location(...)`" + "Without feature `rustc_codegen_spirv`, you need to set the path of the dylib with `rustc_codegen_spirv_location`" )] MissingRustcCodegenSpirvDylib, #[error("`rustc_codegen_spirv_location` path '{0}' is not a file")] RustcCodegenSpirvDylibDoesNotExist(PathBuf), + #[error( + "Without feature `include-target-specs`, instead of setting a `target`, \ + you need to set the path of the target spec file of your particular target with `path_to_target_spec`" + )] + MissingTargetSpec, #[error("build failed")] BuildFailed, #[error("multi-module build cannot be used with print_metadata = MetadataPrintout::Full")] @@ -352,13 +358,13 @@ impl Default for ShaderCrateFeatures { } } -#[non_exhaustive] #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[cfg_attr(feature = "clap", derive(clap::Parser))] pub struct SpirvBuilder { #[cfg_attr(feature = "clap", clap(skip))] pub path_to_crate: Option, - /// Whether to print build.rs cargo metadata (e.g. cargo:rustc-env=var=val). Defaults to [`MetadataPrintout::Full`]. + /// Whether to print build.rs cargo metadata (e.g. cargo:rustc-env=var=val). Defaults to [`MetadataPrintout::None`]. + /// Within build scripts, set it to [`MetadataPrintout::DependencyOnly`] or [`MetadataPrintout::Full`] to ensure the build script is rerun on code changes. #[cfg_attr(feature = "clap", clap(skip))] pub print_metadata: MetadataPrintout, /// Build in release. Defaults to true. @@ -975,13 +981,18 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result { // target_spec jsons, some later version requires them, some earlier // version fails with them (notably our 0.9.0 release) if toolchain_rustc_version >= Version::new(1, 76, 0) { - cargo - .arg("--target") - .arg(builder.path_to_target_spec.clone().unwrap_or_else(|| { - PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("target-specs") - .join(format!("{}.json", target)) - })); + let path_opt = builder.path_to_target_spec.clone(); + let path; + #[cfg(feature = "include-target-specs")] + { + path = path_opt + .unwrap_or_else(|| PathBuf::from(format!("{TARGET_SPEC_DIR_PATH}/{target}.json"))); + } + #[cfg(not(feature = "include-target-specs"))] + { + path = path_opt.ok_or(SpirvBuilderError::MissingTargetSpec)?; + } + cargo.arg("--target").arg(path); } else { cargo.arg("--target").arg(target); } diff --git a/docs/src/writing-shader-crates.md b/docs/src/writing-shader-crates.md index 91ef93d081..19d1b4884f 100644 --- a/docs/src/writing-shader-crates.md +++ b/docs/src/writing-shader-crates.md @@ -120,7 +120,7 @@ how to build SPIR-V. Here are a few things we need to mention there. - Path to a spec of a target you're compiling for (see [platform support](./platform-support.md)). These specs reside in a directory inside the `spirv-builder` crate and an example relative path - could look like `../rust-gpu/crates/spirv-builder/target-specs/spirv-unknown-spv1.3.json`. + could look like `../rust-gpu/crates/rustc_codegen_spirv-target-specs/target-specs/spirv-unknown-spv1.3.json`. - Absolute path to the `rustc_codegen_spirv` dynamic library that we built above. - Some additional options. diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 6d12c9a5f3..b03db33b8e 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -15,6 +15,7 @@ use-compiled-tools = ["rustc_codegen_spirv/use-compiled-tools"] [dependencies] compiletest = { version = "0.11.2", package = "compiletest_rs" } -rustc_codegen_spirv.workspace = true +rustc_codegen_spirv = { workspace = true } +rustc_codegen_spirv-target-specs = { workspace = true, features = ["dir_path"] } clap = { version = "4", features = ["derive"] } itertools = "0.10.5" diff --git a/tests/src/main.rs b/tests/src/main.rs index 551e6eb92f..c2947bd35d 100644 --- a/tests/src/main.rs +++ b/tests/src/main.rs @@ -1,5 +1,6 @@ use clap::Parser; use itertools::Itertools as _; +use rustc_codegen_spirv_target_specs::TARGET_SPEC_DIR_PATH; use std::{ env, io, path::{Path, PathBuf}, @@ -30,10 +31,7 @@ impl Opt { const SPIRV_TARGET_PREFIX: &str = "spirv-unknown-"; fn target_spec_json(target: &str) -> String { - format!( - "{}/../crates/spirv-builder/target-specs/{target}.json", - env!("CARGO_MANIFEST_DIR") - ) + format!("{TARGET_SPEC_DIR_PATH}/{target}.json") } #[derive(Copy, Clone)]