Skip to content

Commit 825ba07

Browse files
committed
make Install usable from a lib
1 parent 46ba9d5 commit 825ba07

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

crates/cargo-gpu/src/install.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pub struct Install {
4444
pub rebuild_codegen: bool,
4545

4646
/// Assume "yes" to "Install Rust toolchain: [y/n]" prompt.
47+
///
48+
/// Defaults to `false` in cli, `true` in [`Default`]
4749
#[clap(long, action)]
4850
pub auto_install_rust_toolchain: bool,
4951

@@ -77,6 +79,21 @@ pub struct Install {
7779
pub force_overwrite_lockfiles_v4_to_v3: bool,
7880
}
7981

82+
impl Install {
83+
/// Create a default install for a shader crate of some path
84+
pub fn from_shader_crate(shader_crate: PathBuf) -> Self {
85+
Self {
86+
shader_crate,
87+
spirv_builder_source: None,
88+
spirv_builder_version: None,
89+
rebuild_codegen: false,
90+
auto_install_rust_toolchain: true,
91+
clear_target: true,
92+
force_overwrite_lockfiles_v4_to_v3: false,
93+
}
94+
}
95+
}
96+
8097
/// Represents a functional backend installation, whether it was cached or just installed.
8198
#[derive(Clone, Debug)]
8299
pub struct InstalledBackend {
@@ -89,6 +106,18 @@ pub struct InstalledBackend {
89106
}
90107

91108
impl InstalledBackend {
109+
/// Creates a new `SpirvBuilder` configured to use this installed backend.
110+
pub fn to_spirv_builder(
111+
&self,
112+
path_to_crate: impl AsRef<Path>,
113+
target: impl Into<String>,
114+
) -> SpirvBuilder {
115+
let mut builder = SpirvBuilder::new(path_to_crate, target);
116+
self.configure_spirv_builder(&mut builder)
117+
.expect("unreachable");
118+
builder
119+
}
120+
92121
/// Configures the supplied [`SpirvBuilder`]. `SpirvBuilder.target` must be set and must not change after calling this function.
93122
pub fn configure_spirv_builder(&self, builder: &mut SpirvBuilder) -> anyhow::Result<()> {
94123
builder.rustc_codegen_spirv_location = Some(self.rustc_codegen_spirv_location.clone());
@@ -101,21 +130,6 @@ impl InstalledBackend {
101130
}
102131
}
103132

104-
impl Default for Install {
105-
#[inline]
106-
fn default() -> Self {
107-
Self {
108-
shader_crate: PathBuf::from("./"),
109-
spirv_builder_source: None,
110-
spirv_builder_version: None,
111-
rebuild_codegen: false,
112-
auto_install_rust_toolchain: false,
113-
clear_target: true,
114-
force_overwrite_lockfiles_v4_to_v3: false,
115-
}
116-
}
117-
}
118-
119133
impl Install {
120134
/// Create the `rustc_codegen_spirv_dummy` crate that depends on `rustc_codegen_spirv`
121135
fn write_source_files(source: &SpirvSource, checkout: &Path) -> anyhow::Result<()> {

crates/cargo-gpu/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ use anyhow::Context as _;
5252

5353
use crate::dump_usage::dump_full_usage_for_readme;
5454
use build::Build;
55-
use install::Install;
5655
use show::Show;
5756

5857
mod build;
@@ -68,6 +67,9 @@ mod show;
6867
mod spirv_source;
6968
mod test;
7069

70+
pub use install::*;
71+
pub use spirv_builder;
72+
7173
/// Central function to write to the user.
7274
#[macro_export]
7375
macro_rules! user_output {

0 commit comments

Comments
 (0)