-
Notifications
You must be signed in to change notification settings - Fork 61
Fix cargo-gpu in build script failing when called by Miri or Clippy #335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tuguzT
wants to merge
2
commits into
Rust-GPU:main
Choose a base branch
from
tuguzT:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+166
−32
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
use std::collections::HashSet; | ||
use std::env; | ||
use std::ffi::{OsStr, OsString}; | ||
use std::fmt::{Display, Formatter}; | ||
use std::ops::{Deref, DerefMut}; | ||
use std::process::Command; | ||
|
||
/// Filters the various env vars that a `cargo` child process would receive and reports back | ||
/// what was inherited and what was removed. By default, removes all env vars that influences | ||
/// the cargo invocations of a spirv-builder's build or cargo-gpu's install action. | ||
pub struct CargoCmd { | ||
cargo: Command, | ||
vars_os: Vec<(OsString, OsString)>, | ||
removed: HashSet<OsString>, | ||
} | ||
|
||
impl CargoCmd { | ||
pub fn new() -> Self { | ||
let mut cargo = CargoCmd::new_no_filtering(); | ||
|
||
// Clear Cargo environment variables that we don't want to leak into the | ||
// inner invocation of Cargo (because e.g. build scripts might read them), | ||
// before we set any of our own below. | ||
cargo.retain_vars_os(|(key, _)| { | ||
!key.to_str() | ||
.is_some_and(|s| s.starts_with("CARGO_FEATURES_") || s.starts_with("CARGO_CFG_")) | ||
}); | ||
|
||
// NOTE(eddyb) Cargo caches some information it got from `rustc` in | ||
// `.rustc_info.json`, and assumes it only depends on the `rustc` binary, | ||
// but in our case, `rustc_codegen_spirv` changes are also relevant, | ||
// so we turn off that caching with an env var, just to avoid any issues. | ||
cargo.env("CARGO_CACHE_RUSTC_INFO", "0"); | ||
|
||
// NOTE(firestar99) If you call SpirvBuilder in a build script, it will | ||
// set `RUSTC` before calling it. And if we were to propagate it to our | ||
// cargo invocation, it will take precedence over the `+toolchain` we | ||
// previously set. | ||
cargo.env_remove("RUSTC"); | ||
|
||
// NOTE(tuguzT) Used by Cargo to call executables of Clippy, Miri | ||
// (and maybe other Cargo subcommands) instead of `rustc` | ||
// which could affect its functionality and break the build process. | ||
cargo.env_remove("RUSTC_WRAPPER"); | ||
|
||
// overwritten by spirv-builder anyway | ||
cargo.env_remove("CARGO_ENCODED_RUSTFLAGS"); | ||
|
||
cargo | ||
.env_remove("RUSTC") | ||
.env_remove("RUSTC_WRAPPER") | ||
.env_remove("RUSTC_WORKSPACE_WRAPPER") | ||
.env_remove("RUSTFLAGS") | ||
.env_remove("CARGO") | ||
.env_remove("RUSTUP_TOOLCHAIN"); | ||
|
||
// ignore any externally supplied target dir | ||
// spirv-builder: we overwrite it with `SpirvBuilder.target_dir_path` anyway | ||
// cargo-gpu: we want to build it in our cache dir | ||
cargo.env_remove("CARGO_TARGET_DIR"); | ||
|
||
cargo | ||
} | ||
|
||
pub fn new_no_filtering() -> Self { | ||
Self { | ||
cargo: Command::new("cargo"), | ||
vars_os: env::vars_os().collect(), | ||
removed: HashSet::default(), | ||
} | ||
} | ||
|
||
pub fn retain_vars_os(&mut self, mut f: impl FnMut((&OsString, &OsString)) -> bool) { | ||
for (key, value) in &self.vars_os { | ||
if !f((key, value)) { | ||
self.removed.insert(key.clone()); | ||
self.cargo.env_remove(key); | ||
} | ||
} | ||
} | ||
|
||
pub fn env_remove(&mut self, key: impl AsRef<OsStr>) -> &mut Self { | ||
self.removed.insert(key.as_ref().to_os_string()); | ||
self.cargo.env_remove(key); | ||
self | ||
} | ||
|
||
pub fn env(&mut self, key: impl AsRef<OsStr>, val: impl AsRef<OsStr>) -> &mut Self { | ||
self.removed.remove(key.as_ref()); | ||
self.cargo.env(key, val); | ||
self | ||
} | ||
|
||
pub fn env_var_report(&self) -> EnvVarReport { | ||
let mut inherited = self.vars_os.clone(); | ||
inherited.retain(|(key, _)| !self.removed.contains(key)); | ||
EnvVarReport { | ||
inherited, | ||
removed: self.removed.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl Default for CargoCmd { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl Display for CargoCmd { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("CargoCmd") | ||
.field("cargo", &self.cargo) | ||
.field("env_vars", &self.env_var_report()) | ||
.finish() | ||
} | ||
} | ||
|
||
impl Deref for CargoCmd { | ||
type Target = Command; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.cargo | ||
} | ||
} | ||
|
||
impl DerefMut for CargoCmd { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.cargo | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, Default)] | ||
pub struct EnvVarReport { | ||
pub inherited: Vec<(OsString, OsString)>, | ||
pub removed: HashSet<OsString>, | ||
} | ||
|
||
impl Display for EnvVarReport { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
let removed = self | ||
.removed | ||
.iter() | ||
.map(|key| format!("{}", key.to_string_lossy())) | ||
.collect::<Vec<_>>(); | ||
let inherited = self | ||
.inherited | ||
.iter() | ||
.map(|(key, value)| format!("{}: {}", key.to_string_lossy(), value.to_string_lossy())) | ||
.collect::<Vec<_>>(); | ||
|
||
f.debug_struct("EnvVarReport") | ||
.field("removed", &removed) | ||
.field("inherited", &inherited) | ||
.finish() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These seem to be added with no comment, and
RUSTC_WRAPPER
is both here and above.