Skip to content
This repository was archived by the owner on Apr 15, 2026. It is now read-only.

Commit 1b62fd4

Browse files
committed
install-crate: move install action to new cargo-gpu-install crate
1 parent 82e8329 commit 1b62fd4

11 files changed

Lines changed: 180 additions & 72 deletions

File tree

Cargo.lock

Lines changed: 23 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
@@ -1,6 +1,7 @@
11
[workspace]
22
members = [
33
"crates/cargo-gpu",
4+
"crates/cargo-gpu-install",
45
"crates/xtask",
56
]
67

@@ -24,6 +25,7 @@ license = "MIT OR Apache-2.0"
2425

2526
[workspace.dependencies]
2627
spirv-builder = { git = "https://github.com/Rust-GPU/rust-gpu", rev = "a30bd43db45f2bfe260051f44141e5eaffcbb4b0", default-features = false }
28+
cargo-gpu-install = { path = "./crates/cargo-gpu-install" }
2729
anyhow = "1.0.98"
2830
clap = { version = "4.5.41", features = ["derive"] }
2931
crossterm = "0.29.0"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[package]
2+
name = "cargo-gpu-install"
3+
version.workspace = true
4+
edition.workspace = true
5+
description = "Install rust-gpu and it's required toolchain automatically"
6+
repository.workspace = true
7+
readme.workspace = true
8+
keywords.workspace = true
9+
license.workspace = true
10+
11+
[features]
12+
test = ["dep:tempfile"]
13+
14+
[dependencies]
15+
cargo_metadata.workspace = true
16+
anyhow.workspace = true
17+
spirv-builder = { workspace = true, features = ["clap", "watch"] }
18+
clap.workspace = true
19+
directories.workspace = true
20+
env_logger.workspace = true
21+
log.workspace = true
22+
relative-path.workspace = true
23+
serde.workspace = true
24+
serde_json.workspace = true
25+
crossterm.workspace = true
26+
semver.workspace = true
27+
dunce.workspace = true
28+
tempfile = { workspace = true, optional = true }
29+
30+
[dev-dependencies]
31+
test-log.workspace = true
32+
cargo_metadata = { workspace = true, features = ["builder"] }
33+
cargo-util-schemas = "0.8.2"
34+
35+
[lints]
36+
workspace = true

crates/cargo-gpu-install/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# cargo-gpu-install
2+
3+
`cargo-gpu-install` is the install action of `cargo-gpu`, separated into its own crate. It's intended to be used
4+
by build scripts and other binaries that need automated installation of rust-gpu and it's required toolchain,
5+
without having to pull all the other cli dependencies of the full `cargo-gpu` (like clap).
6+
7+
## Example
8+
9+
This is an example build script meant to be placed in your "main" std crate, to build a secondary no-std "shader" crate.
10+
But you can just as well use this in your executable directly, with some minor adjustments.
11+
```rust,no_run
12+
# use std::path::PathBuf;
13+
# use cargo_gpu_install::install::Install;
14+
# use cargo_gpu_install::spirv_builder::SpirvMetadata;
15+
16+
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
17+
// path to your shader crate
18+
let shader_crate = PathBuf::from("./shaders");
19+
20+
// install the toolchain and build the `rustc_codegen_spirv` codegen backend with it
21+
let backend = Install::from_shader_crate(shader_crate.clone()).run()?;
22+
23+
// build the shader crate
24+
let mut builder = backend.to_spirv_builder(shader_crate, "spirv-unknown-vulkan1.2");
25+
// set to true when you're in a build script, false when outside one
26+
builder.build_script.defaults = true;
27+
// enable debug information in the shaders
28+
builder.spirv_metadata = SpirvMetadata::Full;
29+
let spv_result = builder.build()?;
30+
let path_to_spv = spv_result.module.unwrap_single();
31+
32+
// emit path to the artifact into env var, use it anywhere in your crate like:
33+
// > include_str!(env!("MY_SHADER_PATH"))
34+
println!(
35+
"cargo::rustc-env=MY_SHADER_PATH={}",
36+
path_to_spv.display()
37+
);
38+
39+
// you could also generate some rust source code into the `std::env::var("OUT_DIR")` dir
40+
// and use `include!(concat!(env!("OUT_DIR"), "/shader_symbols.rs"));` to include it
41+
Ok(())
42+
}
43+
```
File renamed without changes.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#![expect(clippy::pub_use, reason = "pub use for build scripts")]
2+
#![doc = include_str!("../README.md")]
3+
4+
pub mod install;
5+
mod install_toolchain;
6+
pub mod spirv_source;
7+
pub mod test;
8+
9+
pub use spirv_builder;
10+
11+
/// Central function to write to the user.
12+
#[macro_export]
13+
macro_rules! user_output {
14+
($($args: tt)*) => { {
15+
#[allow(
16+
clippy::allow_attributes,
17+
clippy::useless_attribute,
18+
unused_imports,
19+
reason = "`std::io::Write` is only sometimes called??"
20+
)]
21+
use std::io::Write as _;
22+
23+
#[expect(
24+
clippy::non_ascii_literal,
25+
reason = "CRAB GOOD. CRAB IMPORTANT."
26+
)]
27+
{
28+
print!("🦀 ");
29+
}
30+
print!($($args)*);
31+
std::io::stdout().flush().unwrap();
32+
} }
33+
}
34+
35+
/// The central cache directory of cargo gpu
36+
///
37+
/// # Errors
38+
/// may fail if we can't find the user home directory
39+
#[inline]
40+
#[cfg(not(test))]
41+
#[expect(clippy::cfg_not_test, reason = "tests use different cache_dir")]
42+
pub fn cache_dir() -> anyhow::Result<std::path::PathBuf> {
43+
use anyhow::Context as _;
44+
Ok(directories::BaseDirs::new()
45+
.with_context(|| "could not find the user home directory")?
46+
.cache_dir()
47+
.join("rust-gpu"))
48+
}
49+
50+
#[cfg(test)]
51+
pub use test::test_cache_dir as cache_dir;
52+
53+
/// Returns a string suitable to use as a directory.
54+
///
55+
/// Created from the spirv-builder source dep and the rustc channel.
56+
fn to_dirname(text: &str) -> String {
57+
text.replace(
58+
[std::path::MAIN_SEPARATOR, '\\', '/', '.', ':', '@', '='],
59+
"_",
60+
)
61+
.split(['{', '}', ' ', '\n', '"', '\''])
62+
.collect::<Vec<_>>()
63+
.concat()
64+
}
File renamed without changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! utilities for tests
2-
#![cfg(test)]
2+
#![cfg(any(feature = "test", test))]
33

44
use anyhow::Context;
55
use std::cell::RefCell;

crates/cargo-gpu/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ default-run = "cargo-gpu"
1414
cargo_metadata.workspace = true
1515
anyhow.workspace = true
1616
spirv-builder = { workspace = true, features = ["clap", "watch"] }
17+
cargo-gpu-install.workspace = true
1718
clap.workspace = true
1819
directories.workspace = true
1920
env_logger.workspace = true
@@ -26,6 +27,7 @@ semver.workspace = true
2627
dunce.workspace = true
2728

2829
[dev-dependencies]
30+
cargo-gpu-install = { workspace = true, features = ["test"] }
2931
tempfile.workspace = true
3032
test-log.workspace = true
3133
cargo_metadata = { workspace = true, features = ["builder"] }

0 commit comments

Comments
 (0)