Skip to content
Closed
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ This is similar to ccache's `CCACHE_BASEDIR` and helps when:
* Multiple developers working with different username paths
* Working with multiple project checkouts simultaneously

**Note:** Only absolute paths are supported. Relative paths will prevent server from starting.
**Note:** Only absolute paths are supported. If a build passes a relative path, sccache compiles without caching. A relative path in the config file stops the server from starting.

You can also configure this in the sccache config file:

Expand All @@ -379,6 +379,10 @@ basedirs = ["/home/user/project"]
basedirs = ["/home/user/project", "/home/user/workspace"]
```

The environment variable applies to each compiler invocation. Its value overrides the config-file value for that request, so a persistent sccache daemon can serve builds from different checkout roots without a restart. Set `SCCACHE_BASEDIRS=""` on an invocation to disable the config-file fallback for that request.

Only the config file defines the daemon's fallback. A `SCCACHE_BASEDIRS` value in the environment that starts the daemon does not become global daemon state. Statistics report the config-file fallback and do not accumulate paths from individual requests.

---

Known Caveats
Expand Down
2 changes: 1 addition & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,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`; incremental crates go uncached. 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. The environment variable applies to each compiler invocation and overrides the config-file fallback for that request. `SCCACHE_BASEDIRS=""` disables the fallback. Setting `SCCACHE_BASEDIRS` when starting the server has no effect. Put `basedirs` in the config file to set a default for every build. `sccache --show-stats` shows the config file's `basedirs`, not the values builds pass. Only absolute paths are supported; see the note in the README.
* `SCCACHE_CACHED_CONF`
* `SCCACHE_DIST_CLIENT_CACHE_DIR` distributed toolchain cache directory; overrides `[dist].cache_dir` from the configuration file.
* `SCCACHE_IDLE_TIMEOUT` how long the local daemon process waits for more client requests before exiting, in seconds. Set to `0` to run sccache permanently
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ where
pool: &tokio::runtime::Handle,
rewrite_includes_only: bool,
storage: Arc<dyn Storage>,
basedirs: &[Vec<u8>],
cache_control: CacheControl,
) -> Result<HashResult<T>> {
let start_of_compilation = std::time::SystemTime::now();
Expand Down Expand Up @@ -490,7 +491,7 @@ where
self.compiler.plusplus(),
self.parsed_args.is_multiarch(),
preprocessor_cache_mode_config,
storage.basedirs(),
basedirs,
)?
} else {
None
Expand Down Expand Up @@ -663,7 +664,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 Down
86 changes: 86 additions & 0 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ where
pool: &tokio::runtime::Handle,
rewrite_includes_only: bool,
storage: Arc<dyn Storage>,
basedirs: &[Vec<u8>],
cache_control: CacheControl,
) -> Result<HashResult<T>>;

Expand All @@ -579,6 +580,7 @@ where
dist_client: Option<Arc<dyn dist::Client>>,
creator: T,
storage: Arc<dyn Storage>,
basedirs: &[Vec<u8>],
arguments: Vec<OsString>,
cwd: PathBuf,
env_vars: Vec<(OsString, OsString)>,
Expand All @@ -602,6 +604,7 @@ where
&pool,
rewrite_includes_only,
storage.clone(),
basedirs,
cache_control,
)
.await;
Expand Down Expand Up @@ -769,6 +772,7 @@ where
dist_client,
creator,
storage,
basedirs,
arguments,
cwd,
env_vars,
Expand Down Expand Up @@ -2644,6 +2648,7 @@ LLVM version: 6.0",
pool,
false,
Arc::new(MockStorage::new(None, preprocessor_cache_mode)),
&[],
CacheControl::Default,
)
.wait()
Expand All @@ -2654,6 +2659,72 @@ LLVM version: 6.0",
assert_ne!(results[0].key, results[1].key);
}

/// The basedirs passed to `generate_hash_key` key two C checkouts alike.
#[test]
fn test_basedirs_argument_reaches_c_hash() {
let f = TestFixture::new();
let clang = f.mk_bin("clang").unwrap();
let runtime = single_threaded_runtime();
let pool = runtime.handle();

let key = |root: &str, basedirs: &[Vec<u8>]| {
let creator = new_creator();
next_command(
&creator,
Ok(MockChild::new(
exit_status(0),
"compiler_id=clang\ncompiler_version=\"16.0.0\"",
"",
)),
);
next_assembler(&creator, "GNU assembler (GNU Binutils) 2.42", "");
let c = detect_compiler(
creator.clone(),
&clang,
f.tempdir.path(),
&[],
&[],
pool,
None,
)
.wait()
.unwrap()
.0;
next_command(
&creator,
Ok(MockChild::new(
exit_status(0),
format!("# 1 \"{root}/foo.c\"\n"),
"",
)),
);
let arguments = ovec!["-c", "foo.c", "-o", "foo.o", format!("-I{root}/include")];
let mut hasher = match c.parse_arguments(&arguments, ".".as_ref(), &[]) {
CompilerArguments::Ok(h) => h,
o => panic!("Bad result from parse_arguments: {:?}", o),
};
hasher
.generate_hash_key(
&creator,
f.tempdir.path().to_path_buf(),
vec![],
false,
pool,
false,
Arc::new(MockStorage::new(None, false)),
basedirs,
CacheControl::Default,
)
.wait()
.unwrap()
.key
};

let basedirs = [b"/checkout/a/".to_vec(), b"/checkout/b/".to_vec()];
assert_ne!(key("/checkout/a", &[]), key("/checkout/b", &[]));
assert_eq!(key("/checkout/a", &basedirs), key("/checkout/b", &basedirs));
}

#[test_case(true ; "with preprocessor cache")]
#[test_case(false ; "without preprocessor cache")]
fn test_multiarch_hash_covers_every_arch(preprocessor_cache_mode: bool) {
Expand Down Expand Up @@ -2716,6 +2787,7 @@ LLVM version: 6.0",
pool,
false,
Arc::new(MockStorage::new(None, preprocessor_cache_mode)),
&[],
CacheControl::Default,
)
.wait()
Expand Down Expand Up @@ -2793,6 +2865,7 @@ LLVM version: 6.0",
pool,
false,
Arc::new(MockStorage::new(None, preprocessor_cache_mode)),
&[],
CacheControl::Default,
)
.wait()
Expand Down Expand Up @@ -2890,6 +2963,7 @@ LLVM version: 6.0",
pool,
false,
Arc::new(MockStorage::new(None, preprocessor_cache_mode)),
&[],
CacheControl::Default,
)
.wait()
Expand Down Expand Up @@ -2957,6 +3031,7 @@ LLVM version: 6.0",
pool,
false,
Arc::new(MockStorage::new(None, preprocessor_cache_mode)),
&[],
CacheControl::Default,
)
.wait()
Expand Down Expand Up @@ -3065,6 +3140,7 @@ LLVM version: 6.0",
None,
creator.clone(),
storage.clone(),
&[],
arguments.clone(),
cwd.to_path_buf(),
vec![],
Expand Down Expand Up @@ -3102,6 +3178,7 @@ LLVM version: 6.0",
None,
creator,
storage,
&[],
arguments,
cwd.to_path_buf(),
vec![],
Expand Down Expand Up @@ -3196,6 +3273,7 @@ LLVM version: 6.0",
Some(dist_client.clone()),
creator.clone(),
storage.clone(),
&[],
arguments.clone(),
cwd.to_path_buf(),
vec![],
Expand Down Expand Up @@ -3233,6 +3311,7 @@ LLVM version: 6.0",
Some(dist_client.clone()),
creator,
storage,
&[],
arguments,
cwd.to_path_buf(),
vec![],
Expand Down Expand Up @@ -3319,6 +3398,7 @@ LLVM version: 6.0",
None,
creator,
storage,
&[],
arguments.clone(),
cwd.to_path_buf(),
vec![],
Expand Down Expand Up @@ -3412,6 +3492,7 @@ LLVM version: 6.0",
None,
creator,
storage,
&[],
arguments.clone(),
cwd.to_path_buf(),
vec![],
Expand Down Expand Up @@ -3507,6 +3588,7 @@ LLVM version: 6.0",
None,
creator.clone(),
storage.clone(),
&[],
arguments.clone(),
cwd.to_path_buf(),
vec![],
Expand Down Expand Up @@ -3536,6 +3618,7 @@ LLVM version: 6.0",
None,
creator,
storage,
&[],
arguments,
cwd.to_path_buf(),
vec![],
Expand Down Expand Up @@ -3631,6 +3714,7 @@ LLVM version: 6.0",
None,
creator,
storage,
&[],
arguments,
cwd.to_path_buf(),
vec![],
Expand Down Expand Up @@ -3710,6 +3794,7 @@ LLVM version: 6.0",
Some(dist_client.clone()),
creator.clone(),
storage.clone(),
&[],
arguments.clone(),
f.tempdir.path().to_path_buf(),
vec![],
Expand Down Expand Up @@ -3828,6 +3913,7 @@ LLVM version: 6.0",
Some(dist_client.clone()),
creator.clone(),
storage.clone(),
&[],
arguments.clone(),
cwd.to_path_buf(),
vec![],
Expand Down
13 changes: 8 additions & 5 deletions src/compiler/nvcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1325,16 +1325,19 @@ where
},
)),
CompilerArguments::Ok(hasher) => {
let env_vars = env_vars
.iter()
.chain([("SCCACHE_DIRECT".into(), "false".into())].iter())
.cloned()
.collect::<Vec<_>>();
let basedirs = srvc.basedirs_for_request(&env_vars)?;
srvc.start_compile_task(
compiler,
hasher,
args,
cwd.to_owned(),
env_vars
.iter()
.chain([("SCCACHE_DIRECT".into(), "false".into())].iter())
.cloned()
.collect::<Vec<_>>(),
env_vars,
basedirs,
)
.await
}
Expand Down
Loading
Loading