Skip to content

Commit 6fef1fd

Browse files
committed
support for local paths, also always build it there instead of our cache
1 parent dda18d9 commit 6fef1fd

File tree

2 files changed

+71
-37
lines changed

2 files changed

+71
-37
lines changed

crates/cargo-gpu/src/install.rs

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//! Install a dedicated per-shader crate that has the `rust-gpu` compiler in it.
22
3-
use anyhow::Context as _;
4-
use std::io::Write as _;
5-
use std::path::Path;
6-
73
use crate::args::InstallArgs;
84
use crate::spirv_source::{
95
get_channel_from_rustc_codegen_spirv_build_script, get_package_from_crate,
106
};
117
use crate::{cache_dir, spirv_source::SpirvSource, target_spec_dir};
8+
use anyhow::Context as _;
9+
use log::trace;
10+
use std::io::Write as _;
11+
use std::path::Path;
1212

1313
/// Metadata for the compile targets supported by `rust-gpu`
1414
const TARGET_SPECS: &[(&str, &str)] = &[
@@ -85,21 +85,32 @@ pub struct Install {
8585
impl Install {
8686
/// Create the `rustc_codegen_spirv_dummy` crate that depends on `rustc_codegen_spirv`
8787
fn write_source_files(source: &SpirvSource, checkout: &Path) -> anyhow::Result<()> {
88+
// skip writing a dummy project if we use a local rust-gpu checkout
89+
if matches!(source, SpirvSource::Path { .. }) {
90+
return Ok(());
91+
}
92+
log::debug!(
93+
"writing `rustc_codegen_spirv_dummy` source files into '{}'",
94+
checkout.display()
95+
);
96+
8897
{
98+
trace!("writing dummy main.rs");
8999
let main = "fn main() {}";
90100
let src = checkout.join("src");
91101
std::fs::create_dir_all(&src).context("creating directory for 'src'")?;
92102
std::fs::write(src.join("main.rs"), main).context("writing 'main.rs'")?;
93103
};
94104

95105
{
106+
trace!("writing dummy Cargo.toml");
96107
let version_spec = match &source {
97108
SpirvSource::CratesIO(version) => {
98109
format!("version = \"{}\"", version)
99110
}
100111
SpirvSource::Git { url, rev } => format!("git = \"{url}\"\nrev = \"{rev}\""),
101112
SpirvSource::Path {
102-
rust_gpu_path,
113+
rust_gpu_repo_root: rust_gpu_path,
103114
version,
104115
} => {
105116
let mut new_path = rust_gpu_path.to_owned();
@@ -157,28 +168,37 @@ package = "rustc_codegen_spirv"
157168
self.spirv_install.spirv_builder_source.as_deref(),
158169
self.spirv_install.spirv_builder_version.as_deref(),
159170
)?;
171+
let source_is_path = matches!(source, SpirvSource::Path { .. });
160172
let checkout = source.install_dir()?;
161173

162174
let dylib_filename = format!(
163175
"{}rustc_codegen_spirv{}",
164176
std::env::consts::DLL_PREFIX,
165177
std::env::consts::DLL_SUFFIX
166178
);
167-
let dest_dylib_path = checkout.join(&dylib_filename);
168-
if dest_dylib_path.is_file() {
169-
log::info!(
170-
"cargo-gpu artifacts are already installed in '{}'",
171-
checkout.display()
172-
);
179+
180+
let dest_dylib_path;
181+
if source_is_path {
182+
dest_dylib_path = checkout
183+
.join("target")
184+
.join("release")
185+
.join(&dylib_filename);
186+
} else {
187+
dest_dylib_path = checkout.join(&dylib_filename);
188+
if dest_dylib_path.is_file() {
189+
log::info!(
190+
"cargo-gpu artifacts are already installed in '{}'",
191+
checkout.display()
192+
);
193+
}
173194
}
174195

175-
if dest_dylib_path.is_file() && !self.spirv_install.force_spirv_cli_rebuild {
196+
if !source_is_path
197+
&& dest_dylib_path.is_file()
198+
&& !self.spirv_install.force_spirv_cli_rebuild
199+
{
176200
log::info!("...and so we are aborting the install step.");
177201
} else {
178-
log::debug!(
179-
"writing `rustc_codegen_spirv_dummy` source files into '{}'",
180-
checkout.display()
181-
);
182202
Self::write_source_files(&source, &checkout).context("writing source files")?;
183203

184204
log::debug!("resolving toolchain version to use");
@@ -196,10 +216,6 @@ package = "rustc_codegen_spirv"
196216
)
197217
.context("ensuring toolchain and components exist")?;
198218

199-
log::debug!("write_target_spec_files");
200-
self.write_target_spec_files()
201-
.context("writing target spec files")?;
202-
203219
crate::user_output!("Compiling `rustc_codegen_spirv` from source {}\n", source,);
204220

205221
let mut build_command = std::process::Command::new("cargo");
@@ -208,6 +224,9 @@ package = "rustc_codegen_spirv"
208224
.arg(format!("+{}", toolchain_channel))
209225
.args(["build", "--release"])
210226
.env_remove("RUSTC");
227+
if source_is_path {
228+
build_command.args(["-p", "rustc_codegen_spirv", "--lib"]);
229+
}
211230

212231
log::debug!("building artifacts with `{build_command:?}`");
213232

@@ -231,11 +250,18 @@ package = "rustc_codegen_spirv"
231250
.join(&dylib_filename);
232251
if dylib_path.is_file() {
233252
log::info!("successfully built {}", dylib_path.display());
234-
std::fs::rename(&dylib_path, &dest_dylib_path).context("renaming dylib path")?;
253+
if !source_is_path {
254+
std::fs::rename(&dylib_path, &dest_dylib_path)
255+
.context("renaming dylib path")?;
256+
}
235257
} else {
236258
log::error!("could not find {}", dylib_path.display());
237259
anyhow::bail!("`rustc_codegen_spirv` build failed");
238260
}
261+
262+
log::debug!("write_target_spec_files");
263+
self.write_target_spec_files()
264+
.context("writing target spec files")?;
239265
}
240266

241267
self.spirv_install.dylib_path = dest_dylib_path;

crates/cargo-gpu/src/spirv_source.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! From there we can look at the source code to get the required Rust toolchain.
66
77
use anyhow::{anyhow, Context as _};
8-
use cargo_metadata::camino::Utf8PathBuf;
8+
use cargo_metadata::camino::{Utf8Path, Utf8PathBuf};
99
use cargo_metadata::semver::Version;
1010
use cargo_metadata::{MetadataCommand, Package};
1111
use std::fs;
@@ -41,7 +41,7 @@ pub enum SpirvSource {
4141
/// then the source of `rust-gpu` is `Path`.
4242
Path {
4343
/// File path of rust-gpu repository
44-
rust_gpu_path: Utf8PathBuf,
44+
rust_gpu_repo_root: Utf8PathBuf,
4545
/// Version of specified rust-gpu repository
4646
version: Version,
4747
},
@@ -57,9 +57,9 @@ impl core::fmt::Display for SpirvSource {
5757
Self::CratesIO(version) => version.fmt(f),
5858
Self::Git { url, rev } => f.write_str(&format!("{url}+{rev}")),
5959
Self::Path {
60-
rust_gpu_path,
60+
rust_gpu_repo_root,
6161
version,
62-
} => f.write_str(&format!("{rust_gpu_path}+{version}")),
62+
} => f.write_str(&format!("{rust_gpu_repo_root}+{version}")),
6363
}
6464
}
6565
}
@@ -102,10 +102,17 @@ impl SpirvSource {
102102
/// It needs to be dynamically created because an end-user might want to swap out the source,
103103
/// maybe using their own fork for example.
104104
pub fn install_dir(&self) -> anyhow::Result<PathBuf> {
105-
let dir = crate::to_dirname(self.to_string().as_ref());
106-
Ok(crate::cache_dir()?
107-
.join("rustc_backend_spirv_install")
108-
.join(dir))
105+
match self {
106+
SpirvSource::Path {
107+
rust_gpu_repo_root, ..
108+
} => Ok(rust_gpu_repo_root.as_std_path().to_owned()),
109+
SpirvSource::CratesIO { .. } | SpirvSource::Git { .. } => {
110+
let dir = crate::to_dirname(self.to_string().as_ref());
111+
Ok(crate::cache_dir()?
112+
.join("rustc_backend_spirv_install")
113+
.join(dir))
114+
}
115+
}
109116
}
110117

111118
/// Parse a string like:
@@ -144,18 +151,19 @@ impl SpirvSource {
144151
}
145152
}
146153
None => {
147-
let rust_gpu_path = spirv_std_package
154+
let rust_gpu_repo_root = spirv_std_package
148155
.manifest_path // rust-gpu/crates/spirv-std/Cargo.toml
149-
.parent()
150-
.unwrap() // rust-gpu/crates/spirv-std
151-
.parent()
152-
.unwrap() // rust-gpu/crates
153-
.parent()
154-
.unwrap() // rust-gpu
156+
.parent() // rust-gpu/crates/spirv-std
157+
.and_then(Utf8Path::parent) // rust-gpu/crates
158+
.and_then(Utf8Path::parent) // rust-gpu
159+
.context("selecting rust-gpu workspace root dir in local path")?
155160
.to_owned();
161+
if !rust_gpu_repo_root.is_dir() {
162+
anyhow::bail!("path {rust_gpu_repo_root} is not a directory");
163+
}
156164
let version = spirv_std_package.version.clone();
157165
Self::Path {
158-
rust_gpu_path,
166+
rust_gpu_repo_root,
159167
version,
160168
}
161169
}

0 commit comments

Comments
 (0)