Skip to content

Commit

Permalink
Added env var to disable multi arch caching
Browse files Browse the repository at this point in the history
Added an environment variable SCCACHE_NOCACHE_MULTIARCH to allow disabling caching of multiarch compilations.
  • Loading branch information
harryt@coretechsec committed May 26, 2023
1 parent b2faf16 commit 495c057
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ itertools = "0.10"
predicates = "=3.0.2"
thirtyfour_sync = "0.27"
serial_test = "2.0"
temp-env = "0.3.4"

[target.'cfg(unix)'.dependencies]
daemonize = "0.5"
Expand Down
4 changes: 3 additions & 1 deletion docs/Caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ In parallel, we also take into account in the hash:
For C/C++, the hash is generated with a blake3 digest of the preprocessed
file (-E with gcc/clang). For compilations that specify multiple `-arch` flags,
these flags are rewritten to their corresponding preprocessor defines to allow
pre-processing the file (e.g `-arch x86_64` is rewritten to `-D__X86_64__=1`).
pre-processing the file (e.g `-arch x86_64` is rewritten to `-D__X86_64__=1`),
this can be disabled by setting the environment variable
`SCCACHE_NOCACHE_MULTIARCH` as it may not work in all cases.

We also take into account in the hash:
* Hash of the compiler binary
Expand Down
1 change: 1 addition & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ configuration variables
* `SCCACHE_STARTUP_NOTIFY` specify a path to a socket which will be used for server completion notification
* `SCCACHE_MAX_FRAME_LENGTH` how much data can be transferred between client and server
* `SCCACHE_NO_DAEMON` set to `1` to disable putting the server to the background
* `SCCACHE_NOCACHE_MULTIARCH` to disable caching of multi architecture builds.

### cache configs

Expand Down
33 changes: 32 additions & 1 deletion src/compiler/gcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use fs::File;
use fs_err as fs;
use log::Level::Trace;
use std::collections::HashMap;
use std::env;
use std::ffi::OsString;
use std::io::Read;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -275,6 +276,8 @@ where
let mut outputs_gcno = false;
let mut xclangs: Vec<OsString> = vec![];
let mut color_mode = ColorMode::Auto;
let mut seen_arch = None;
let dont_cache_multiarch = env::var("SCCACHE_NOCACHE_MULTIARCH").is_ok();

// Custom iterator to expand `@` arguments which stand for reading a file
// and interpreting it as a list of more arguments.
Expand Down Expand Up @@ -365,7 +368,15 @@ where
_ => cannot_cache!("-x"),
};
}
Some(Arch(_)) => {}
Some(Arch(arch)) => {
match seen_arch {
Some(s) if &s != arch && dont_cache_multiarch => {
cannot_cache!("multiple different -arch, and SCCACHE_NOCACHE_MULTIARCH set")
}
_ => {}
};
seen_arch = Some(arch.clone());
}
Some(XClang(s)) => xclangs.push(s.clone()),
None => match arg {
Argument::Raw(ref val) => {
Expand Down Expand Up @@ -884,6 +895,8 @@ mod test {
use crate::mock_command::*;
use crate::test::utils::*;

use temp_env::with_var;

fn parse_arguments_(
arguments: Vec<String>,
plusplus: bool,
Expand Down Expand Up @@ -1681,6 +1694,24 @@ mod test {
);
}

#[test]
fn test_parse_arguments_multiarch_cache_disabled() {
with_var("SCCACHE_NOCACHE_MULTIARCH", Some("1"), || {
assert_eq!(
CompilerArguments::CannotCache(
"multiple different -arch, and SCCACHE_NOCACHE_MULTIARCH set",
None
),
parse_arguments_(
stringvec![
"-fPIC", "-arch", "arm64", "-arch", "i386", "-o", "foo.o", "-c", "foo.cpp"
],
false
)
)
});
}

#[test]
fn test_parse_arguments_multiple_arch() {
match parse_arguments_(
Expand Down

0 comments on commit 495c057

Please sign in to comment.