Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ Note that some env variables may need sccache server restart to take effect.
* `SCCACHE_ALLOW_CORE_DUMPS` to enable core dumps by the server
* `SCCACHE_ALLOW_INCREMENTAL` set to `1` to allow Rust builds with `CARGO_INCREMENTAL=1`, leaving incremental crates uncached, or to `cache` to cache them too. See [Rust caveats](Rust.md)
* `SCCACHE_CONF` configuration file path
* `SCCACHE_BASEDIRS` base directory (or directories) to strip from paths for cache key computation. This is similar to ccache's `CCACHE_BASEDIR` and enables cache hits across different absolute paths when compiling the same source code. Multiple directories can be separated by `;` on Windows hosts and by `:` on any other operating system. When multiple directories are specified, the longest matching prefix is used. Path matching is **case-insensitive** on Windows and **case-sensitive** on other operating systems. Environment variable takes precedence over file configuration. Only absolute paths are supported; relative paths will cause an error and prevent the server from start.
* `SCCACHE_BASEDIRS` base directory (or directories) to strip from paths for cache key computation. This is similar to ccache's `CCACHE_BASEDIR` and enables cache hits across different absolute paths when compiling the same source code. Multiple directories can be separated by `;` on Windows hosts and by `:` on any other operating system. When multiple directories are specified, the longest matching prefix is used. Path matching is **case-insensitive** on Windows and **case-sensitive** on other operating systems. Environment variable takes precedence over file configuration. Only absolute paths are supported; relative paths will cause an error and prevent the server from start. Also read from the environment of each compile, where it replaces the server's basedirs for that compile, so each checkout can set its own root; an empty value turns basedirs off, and a value with a relative path or not valid UTF-8 is ignored with a warning. Names are case-insensitive on Windows.
* `SCCACHE_BASEDIRS_VERBATIM_ENV_DEPS` comma-separated names of variables that a Rust crate reads with `env!` or `option_env!` whose values are hashed without stripping `SCCACHE_BASEDIRS`, e.g. `CARGO_MANIFEST_DIR,CARGO_MANIFEST_PATH`. Read from the environment of each compile; empty or not valid UTF-8 means none. Names are case-insensitive on Windows. See [Rust caveats](Rust.md)
* `SCCACHE_CACHED_CONF`
* `SCCACHE_DIST_CLIENT_CACHE_DIR` distributed toolchain cache directory; overrides `[dist].cache_dir` from the configuration file.
Expand Down
11 changes: 6 additions & 5 deletions src/compiler/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::compiler::{
};
#[cfg(feature = "dist-client")]
use crate::compiler::{DistPackagers, NoopOutputsRewriter};
use crate::config::PreprocessorCacheModeConfig;
use crate::config::{PreprocessorCacheModeConfig, compile_basedirs};
use crate::dist;
#[cfg(feature = "dist-client")]
use crate::dist::pkg;
Expand Down Expand Up @@ -442,6 +442,7 @@ where
// Try to look for a cached preprocessing step for this compilation
// request.
let preprocessor_cache_mode_config = storage.preprocessor_cache_mode_config();
let basedirs = compile_basedirs(&env_vars, storage.basedirs());
let too_hard_for_preprocessor_cache_mode = self
.parsed_args
.too_hard_for_preprocessor_cache_mode
Expand Down Expand Up @@ -502,7 +503,7 @@ where
self.compiler.plusplus(),
self.parsed_args.is_multiarch(),
preprocessor_cache_mode_config,
storage.basedirs(),
&basedirs,
)?
} else {
None
Expand All @@ -513,7 +514,7 @@ where
// records have to be checked against this tree's copies and not against
// the ones that happened to write the entry.
let tree_root = if preprocessor_key.is_some() {
compilation_tree_root(&absolute_input_path, &cwd, storage.basedirs())?
compilation_tree_root(&absolute_input_path, &cwd, &basedirs)?
} else {
None
};
Expand Down Expand Up @@ -693,7 +694,7 @@ where
.with_extra_hashes(&extra_hashes)
.with_env_vars(&env_vars)
.with_plusplus(self.compiler.plusplus())
.with_basedirs(storage.basedirs())
.with_basedirs(&basedirs)
.with_assembler_digest(assembler_digest.as_deref())
.with_input_file_name(hashed_input_file_name(
self.parsed_args.language,
Expand All @@ -715,7 +716,7 @@ where
start_of_compilation,
&key,
files,
storage.basedirs(),
&basedirs,
tree_root.as_deref(),
);

Expand Down
63 changes: 57 additions & 6 deletions src/compiler/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::compiler::{
};
#[cfg(feature = "dist-client")]
use crate::compiler::{DistPackagers, NoopOutputsRewriter};
use crate::config::compile_basedirs;
#[cfg(feature = "dist-client")]
use crate::dist::pkg;
#[cfg(feature = "dist-client")]
Expand Down Expand Up @@ -1994,7 +1995,7 @@ where
_cache_control: CacheControl,
) -> Result<HashResult<T>> {
trace!("[{}]: generate_hash_key", self.parsed_args.crate_name);
let basedirs = storage.basedirs();
let basedirs = compile_basedirs(&env_vars, storage.basedirs());
// Keep the incremental directory out of the key, and out of the dep-info and
// file-names runs so they don't touch it.
let incremental = self.parsed_args.arguments.iter().any(is_incremental_arg);
Expand Down Expand Up @@ -2196,7 +2197,7 @@ where
.chain(sortables)
.flat_map(|(arg, val)| iter::once(arg).chain(val.as_ref()))
.fold(OsString::new(), |mut a, b| {
a.push(strip_basedirs_os(b, basedirs));
a.push(strip_basedirs_os(b, &basedirs));
a
})
};
Expand All @@ -2223,7 +2224,7 @@ where
// A crate that bakes a listed env-dep such as `env!("CARGO_MANIFEST_DIR")` into its
// outputs must not hit across checkouts. Stripped values start with a NUL, unlike these.
env_deps.sort();
let verbatim = basedirs_verbatim_env_deps(&env_vars, basedirs);
let verbatim = basedirs_verbatim_env_deps(&env_vars, &basedirs);
for (var, val) in env_deps.iter() {
var.hash(&mut HashToDigest { digest: &mut m });
let Some(val) = val else {
Expand All @@ -2236,7 +2237,7 @@ where
if verbatim(var) {
val.hash(&mut HashToDigest { digest: &mut m });
} else {
strip_basedirs_os(val, basedirs).hash(&mut HashToDigest { digest: &mut m });
strip_basedirs_os(val, &basedirs).hash(&mut HashToDigest { digest: &mut m });
}
}
let mut env_vars: Vec<_> = env_vars
Expand Down Expand Up @@ -2271,10 +2272,10 @@ where

var.hash(&mut HashToDigest { digest: &mut m });
m.update(b"=");
strip_basedirs_os(val, basedirs).hash(&mut HashToDigest { digest: &mut m });
strip_basedirs_os(val, &basedirs).hash(&mut HashToDigest { digest: &mut m });
}
// 9. The cwd of the compile. This will wind up in the rlib.
Path::new(&strip_basedirs_os(cwd.as_os_str(), basedirs))
Path::new(&strip_basedirs_os(cwd.as_os_str(), &basedirs))
.hash(&mut HashToDigest { digest: &mut m });
// 10. The version of the compiler.
self.version.hash(&mut HashToDigest { digest: &mut m });
Expand Down Expand Up @@ -5017,6 +5018,56 @@ foo.rs:
assert_eq!(key(&a, "x", vec![]), key(&a, "x", vec![elsewhere]));
}

/// `SCCACHE_BASEDIRS` in the compile environment replaces the configured basedirs.
#[test]
fn test_hash_basedirs_from_compile_env() {
fn key(f: &TestFixture, env_basedirs: Option<&OsStr>, configured: Vec<Vec<u8>>) -> String {
let root = f.tempdir.path();
let mut env_vars: Vec<(OsString, OsString)> =
vec![("CARGO_MANIFEST_DIR".into(), root.into())];
env_vars.extend(env_basedirs.map(|v| ("SCCACHE_BASEDIRS".into(), v.into())));
hash_key_with(
f,
&[
"--emit",
"link",
"foo.rs",
"--out-dir",
"out",
"--crate-name",
"foo",
"--crate-type",
"lib",
],
&env_vars,
&[],
nothing,
MockStorage::new(None, false).with_basedirs(configured),
)
}

let a = TestFixture::new();
let b = TestFixture::new();
let a_root = a.tempdir.path().as_os_str();
let b_root = b.tempdir.path().as_os_str();
let both = vec![basedir(a.tempdir.path()), basedir(b.tempdir.path())];
// Each checkout names only its own root and still shares the key.
assert_eq!(key(&a, Some(a_root), vec![]), key(&b, Some(b_root), vec![]));
assert_eq!(key(&a, Some(a_root), vec![]), key(&a, None, both.clone()));
// An empty value turns the configured basedirs off.
assert_ne!(
key(&a, Some(OsStr::new("")), both.clone()),
key(&b, Some(OsStr::new("")), both.clone())
);
assert_eq!(
key(&a, Some(OsStr::new("")), both.clone()),
key(&a, None, vec![])
);
// A relative path is ignored in favor of the configured basedirs.
let relative = Some(OsStr::new("relative"));
assert_eq!(key(&a, relative, both.clone()), key(&b, relative, both));
}

#[test]
fn test_hash_key_cache_incremental() {
let f = TestFixture::new();
Expand Down
Loading
Loading