Skip to content

Commit 1b25b5a

Browse files
committed
create mod target_specs and move all related code there
1 parent b438d7d commit 1b25b5a

File tree

4 files changed

+99
-99
lines changed

4 files changed

+99
-99
lines changed

crates/cargo-gpu/src/install.rs

Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
//! Install a dedicated per-shader crate that has the `rust-gpu` compiler in it.
22
3-
use crate::legacy_target_specs::write_legacy_target_specs;
43
use crate::spirv_source::{
54
get_channel_from_rustc_codegen_spirv_build_script, query_metadata, FindPackage as _,
65
};
6+
use crate::target_specs::update_spec_files;
77
use crate::{cache_dir, spirv_source::SpirvSource};
88
use anyhow::Context as _;
9-
use cargo_metadata::Metadata;
109
use spirv_builder::SpirvBuilder;
1110
use std::path::{Path, PathBuf};
1211

@@ -196,83 +195,6 @@ package = "rustc_codegen_spirv"
196195
Ok(())
197196
}
198197

199-
/// Copy spec files from one dir to another, assuming no subdirectories
200-
fn copy_spec_files(src: &Path, dst: &Path) -> anyhow::Result<()> {
201-
std::fs::create_dir_all(dst)?;
202-
let dir = std::fs::read_dir(src)?;
203-
for dir_entry in dir {
204-
let file = dir_entry?;
205-
let file_path = file.path();
206-
if file_path.is_file() {
207-
std::fs::copy(file_path, dst.join(file.file_name()))?;
208-
}
209-
}
210-
Ok(())
211-
}
212-
213-
/// Add the target spec files to the crate.
214-
fn update_spec_files(
215-
source: &SpirvSource,
216-
install_dir: &Path,
217-
dummy_metadata: &Metadata,
218-
skip_rebuild: bool,
219-
) -> anyhow::Result<PathBuf> {
220-
let mut target_specs_dst = install_dir.join("target-specs");
221-
if !skip_rebuild {
222-
if let Ok(target_specs) =
223-
dummy_metadata.find_package("rustc_codegen_spirv-target-specs")
224-
{
225-
log::info!(
226-
"target-specs: found crate `rustc_codegen_spirv-target-specs` with manifest at `{}`",
227-
target_specs.manifest_path
228-
);
229-
230-
let target_specs_src = target_specs
231-
.manifest_path
232-
.as_std_path()
233-
.parent()
234-
.and_then(|root| {
235-
let src = root.join("target-specs");
236-
src.is_dir().then_some(src)
237-
})
238-
.context("Could not find `target-specs` directory within `rustc_codegen_spirv-target-specs` dependency")?;
239-
if source.is_path() {
240-
// skip copy
241-
log::info!(
242-
"target-specs: source is local path, use target-specs from `{}`",
243-
target_specs_src.display()
244-
);
245-
target_specs_dst = target_specs_src;
246-
} else {
247-
// copy over the target-specs
248-
log::info!(
249-
"target-specs: Copy target specs from `{}`",
250-
target_specs_src.display()
251-
);
252-
Self::copy_spec_files(&target_specs_src, &target_specs_dst)
253-
.context("copying target-specs json files")?;
254-
}
255-
} else {
256-
// use legacy target specs bundled with cargo gpu
257-
if source.is_path() {
258-
// This is a stupid situation:
259-
// * We can't be certain that there are `target-specs` in the local checkout (there may be some in `spirv-builder`)
260-
// * We can't dump our legacy ones into the `install_dir`, as that would modify the local rust-gpu checkout
261-
// -> do what the old cargo gpu did, one global dir for all target specs
262-
// and hope parallel runs don't shred each other
263-
target_specs_dst = cache_dir()?.join("legacy-target-specs-for-local-checkout");
264-
}
265-
log::info!(
266-
"target-specs: Writing legacy target specs to `{}`",
267-
target_specs_dst.display()
268-
);
269-
write_legacy_target_specs(&target_specs_dst)?;
270-
}
271-
}
272-
273-
Ok(target_specs_dst)
274-
}
275-
276198
/// Install the binary pair and return the [`InstalledBackend`], from which you can create [`SpirvBuilder`] instances.
277199
///
278200
/// # Errors
@@ -339,7 +261,7 @@ package = "rustc_codegen_spirv"
339261

340262
log::debug!("update_spec_files");
341263
let target_spec_dir =
342-
Self::update_spec_files(&source, &install_dir, &dummy_metadata, skip_rebuild)
264+
update_spec_files(&source, &install_dir, &dummy_metadata, skip_rebuild)
343265
.context("writing target spec files")?;
344266

345267
if !skip_rebuild {

crates/cargo-gpu/src/legacy_target_specs.rs

Lines changed: 0 additions & 18 deletions
This file was deleted.

crates/cargo-gpu/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ mod config;
6161
mod dump_usage;
6262
mod install;
6363
mod install_toolchain;
64-
mod legacy_target_specs;
6564
mod linkage;
6665
mod lockfile;
6766
mod metadata;
6867
mod show;
6968
mod spirv_source;
69+
mod target_specs;
7070
mod test;
7171

7272
pub use install::*;

crates/cargo-gpu/src/target_specs.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//! Legacy target specs are spec jsons for versions before `rustc_codegen_spirv-target-specs`
2+
//! came bundled with them. Instead, cargo gpu needs to bundle these legacy spec files. Luckily,
3+
//! they are the same for all versions, as bundling target specs with the codegen backend was
4+
//! introduced before the first target spec update.
5+
6+
use crate::cache_dir;
7+
use crate::spirv_source::{FindPackage as _, SpirvSource};
8+
use anyhow::Context as _;
9+
use cargo_metadata::Metadata;
10+
use std::path::{Path, PathBuf};
11+
12+
/// Extract legacy target specs from our executable into some directory
13+
pub fn write_legacy_target_specs(target_spec_dir: &Path) -> anyhow::Result<()> {
14+
std::fs::create_dir_all(target_spec_dir)?;
15+
for (filename, contents) in legacy_target_specs::TARGET_SPECS {
16+
let path = target_spec_dir.join(filename);
17+
std::fs::write(&path, contents.as_bytes())
18+
.with_context(|| format!("writing legacy target spec file at [{}]", path.display()))?;
19+
}
20+
Ok(())
21+
}
22+
23+
/// Copy spec files from one dir to another, assuming no subdirectories
24+
fn copy_spec_files(src: &Path, dst: &Path) -> anyhow::Result<()> {
25+
std::fs::create_dir_all(dst)?;
26+
let dir = std::fs::read_dir(src)?;
27+
for dir_entry in dir {
28+
let file = dir_entry?;
29+
let file_path = file.path();
30+
if file_path.is_file() {
31+
std::fs::copy(file_path, dst.join(file.file_name()))?;
32+
}
33+
}
34+
Ok(())
35+
}
36+
37+
/// Add the target spec files to the crate.
38+
pub fn update_spec_files(
39+
source: &SpirvSource,
40+
install_dir: &Path,
41+
dummy_metadata: &Metadata,
42+
skip_rebuild: bool,
43+
) -> anyhow::Result<PathBuf> {
44+
let mut target_specs_dst = install_dir.join("target-specs");
45+
if !skip_rebuild {
46+
if let Ok(target_specs) = dummy_metadata.find_package("rustc_codegen_spirv-target-specs") {
47+
log::info!(
48+
"target-specs: found crate `rustc_codegen_spirv-target-specs` with manifest at `{}`",
49+
target_specs.manifest_path
50+
);
51+
52+
let target_specs_src = target_specs
53+
.manifest_path
54+
.as_std_path()
55+
.parent()
56+
.and_then(|root| {
57+
let src = root.join("target-specs");
58+
src.is_dir().then_some(src)
59+
})
60+
.context("Could not find `target-specs` directory within `rustc_codegen_spirv-target-specs` dependency")?;
61+
if source.is_path() {
62+
// skip copy
63+
log::info!(
64+
"target-specs: source is local path, use target-specs from `{}`",
65+
target_specs_src.display()
66+
);
67+
target_specs_dst = target_specs_src;
68+
} else {
69+
// copy over the target-specs
70+
log::info!(
71+
"target-specs: Copy target specs from `{}`",
72+
target_specs_src.display()
73+
);
74+
copy_spec_files(&target_specs_src, &target_specs_dst)
75+
.context("copying target-specs json files")?;
76+
}
77+
} else {
78+
// use legacy target specs bundled with cargo gpu
79+
if source.is_path() {
80+
// This is a stupid situation:
81+
// * We can't be certain that there are `target-specs` in the local checkout (there may be some in `spirv-builder`)
82+
// * We can't dump our legacy ones into the `install_dir`, as that would modify the local rust-gpu checkout
83+
// -> do what the old cargo gpu did, one global dir for all target specs
84+
// and hope parallel runs don't shred each other
85+
target_specs_dst = cache_dir()?.join("legacy-target-specs-for-local-checkout");
86+
}
87+
log::info!(
88+
"target-specs: Writing legacy target specs to `{}`",
89+
target_specs_dst.display()
90+
);
91+
write_legacy_target_specs(&target_specs_dst)?;
92+
}
93+
}
94+
95+
Ok(target_specs_dst)
96+
}

0 commit comments

Comments
 (0)