Skip to content

Commit 1f93c8a

Browse files
authored
Merge pull request ClickHouse#79965 from ClickHouse/fix-chcache-rust-sequential-runs
chcache: fix rebuilding Rust projects on sequential CMake runs
2 parents 122dfc8 + 6bacd88 commit 1f93c8a

File tree

3 files changed

+21
-30
lines changed

3 files changed

+21
-30
lines changed

rust/chcache/src/compilers/clang.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ impl ClangLike {
169169
preprocess_args.remove(output_flag_index);
170170
preprocess_args.remove(output_flag_index);
171171

172+
let input_flag_index = preprocess_args.iter().position(|x| x == "-c").unwrap();
173+
preprocess_args.remove(input_flag_index);
174+
172175
let output = std::process::Command::new(self.compiler_path.clone())
173176
.args(preprocess_args)
174177
.output()

rust/chcache/src/compilers/rustc.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
use blake3::Hasher;
22
use log::trace;
33
use std::error::Error;
4-
use std::fs;
4+
use std::fs::{self};
55
use std::io::Cursor;
66
use std::path::Path;
77

8-
use crate::{compilers::clang::ClangLike, traits::compiler::{Compiler, CompilerMeta}};
8+
use crate::{
9+
compilers::clang::ClangLike,
10+
traits::compiler::{Compiler, CompilerMeta},
11+
};
912

1013
pub struct RustC {
1114
compiler_path: String,
@@ -32,10 +35,6 @@ impl CompilerMeta for RustC {
3235
}
3336
}
3437

35-
// [thevar1able@homebox memchr-2.7.4]$ /home/thevar1able/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc --crate-name memchr --edition=2021 /home/thevar1able/.cargo/registry/src/-6df83624996e3d27/memchr-2.7.4/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=117 --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 --cfg feature=\"alloc\" --cfg feature=\"std\" --check-cfg "cfg(docsrs,test)" --check-cfg "cfg(feature, values(\"alloc\", \"compiler_builtins\", \"core\", \"default\", \"libc\", \"logging\", \"rustc-dep-of-std\", \"std\", \"use_std\"))" -C metadata=f0ff90587188d79c -C extra-filename=-5282d705ff339125 --out-dir /home/thevar1able/nvmemount/clickhouse/cmake-build-debug/./cargo/build/x86_64-unknown-linux-gnu/debug/deps --target x86_64-unknown-linux-gnu -C linker=/usr/bin/clang -L dependency=/home/thevar1able/nvmemount/clickhouse/cmake-build-debug/./cargo/build/x86_64-unknown-linux-gnu/debug/deps -L dependency=/home/thevar1able/nvmemount/clickhouse/cmake-build-debug/./cargo/build/debug/deps --cap-lints allow -C link-arg=-fuse-ld=lld | head -n1
36-
// {"$message_type":"artifact","artifact":"/home/thevar1able/nvmemount/clickhouse/cmake-build-debug/./cargo/build/x86_64-unknown-linux-gnu/debug/deps/memchr-5282d705ff339125.d","emit":"dep-info"}
37-
// {"$message_type":"artifact","artifact":"/home/thevar1able/nvmemount/clickhouse/cmake-build-debug/./cargo/build/x86_64-unknown-linux-gnu/debug/deps/libmemchr-5282d705ff339125.rmeta","emit":"metadata"}
38-
// {"$message_type":"artifact","artifact":"/home/thevar1able/nvmemount/clickhouse/cmake-build-debug/./cargo/build/x86_64-unknown-linux-gnu/debug/deps/libmemchr-5282d705ff339125.rlib","emit":"link"}
3938
impl Compiler for RustC {
4039
fn cache_key(&self) -> String {
4140
let mut maybe_basepath: Vec<String> = vec![];
@@ -130,18 +129,18 @@ impl Compiler for RustC {
130129
stripped_args.remove(index);
131130
}
132131

133-
if let Some(index) = stripped_args.iter().position(|x| x.starts_with("--diagnostic-width")) {
132+
if let Some(index) = stripped_args
133+
.iter()
134+
.position(|x| x.starts_with("--diagnostic-width"))
135+
{
134136
stripped_args.remove(index);
135137
}
136138

137139
// May have different linkers between developers.
138140
// - "-C", "linker=/src/llvm/llvm-project/.cmake/bin/clang",
139141
// + "-C", "linker=/usr/bin/clang",
140142
let mut clang_linker_version: Option<String> = None;
141-
if let Some(index) = stripped_args
142-
.iter()
143-
.position(|x| x.contains("linker="))
144-
{
143+
if let Some(index) = stripped_args.iter().position(|x| x.contains("linker=")) {
145144
let linker = stripped_args[index].replace("linker=", "");
146145
assert!(!linker.is_empty());
147146
assert!(linker.ends_with("clang"));
@@ -208,6 +207,7 @@ impl Compiler for RustC {
208207

209208
let cursor = Cursor::new(bytes);
210209
let mut archive = tar::Archive::new(cursor);
210+
archive.set_preserve_mtime(false);
211211
archive.unpack(&self.out_dir)?;
212212

213213
Ok(())
@@ -226,15 +226,9 @@ impl Compiler for RustC {
226226
}
227227

228228
let files_to_pack = String::from_utf8_lossy(&output.stderr);
229-
// eprintln!("{}", String::from_utf8_lossy(&output.stdout));
230-
231229
let files_to_pack = files_to_pack
232230
.lines()
233231
.filter(|line| line.starts_with("{\"$message_type\":\"artifact\""))
234-
.collect::<Vec<&str>>();
235-
236-
let files_to_pack = files_to_pack
237-
.iter()
238232
.map(|x| {
239233
let json: serde_json::Value = serde_json::from_str(x).unwrap();
240234
let artifact = json["artifact"].as_str().unwrap();
@@ -244,24 +238,19 @@ impl Compiler for RustC {
244238
.collect::<Vec<String>>();
245239

246240
trace!("Files to pack: {:?}", files_to_pack);
247-
for (key, value) in std::env::vars() {
248-
// trace!("Env var: {}: {}", key, value);
249-
if key.starts_with("CARGO_") || key == "RUSTFLAGS" || key == "TARGET" {
250-
trace!("Maybe interesting env var {}: {}", key, value);
251-
}
252-
}
253241

254242
let mut buffer = Vec::new();
255243
let cursor = Cursor::new(&mut buffer);
256244
let mut archive = tar::Builder::new(cursor);
257-
for file in files_to_pack {
245+
for file in &files_to_pack {
258246
let file = Path::new(&file);
259247
let filename = file.strip_prefix(&self.out_dir).unwrap();
260248
let filename = filename.to_str().unwrap();
261249
trace!("Packing file: {}", file.display());
262250
let mut packed_file = fs::File::open(file).unwrap();
263251
archive.append_file(filename, &mut packed_file).unwrap();
264252
}
253+
265254
archive.finish().unwrap();
266255
drop(archive);
267256

rust/chcache/src/main.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ async fn compiler_cache_entrypoint(config: &Config) -> Result<(), Box<dyn Error>
8181
let total_hash = compiler.cache_key();
8282
let compiler_version = compiler.version();
8383

84-
let mut did_load_from_cache = false;
84+
let mut did_load_from_local_cache = false;
8585
let mut did_load_from_clickhouse = false;
8686

8787
let compiled_bytes: Vec<u8> = match local_disk.read(&total_hash).await {
@@ -90,8 +90,8 @@ async fn compiler_cache_entrypoint(config: &Config) -> Result<(), Box<dyn Error>
9090

9191
compiler
9292
.apply_cache(&bytes)
93-
.expect("Unable to apply local cache");
94-
did_load_from_cache = true;
93+
.expect(&("Unable to apply local cache for hash ".to_owned() + &total_hash));
94+
did_load_from_local_cache = true;
9595

9696
bytes
9797
}
@@ -107,7 +107,6 @@ async fn compiler_cache_entrypoint(config: &Config) -> Result<(), Box<dyn Error>
107107
.apply_cache(&bytes)
108108
.expect("Unable to apply cache from ClickHouse");
109109

110-
did_load_from_cache = true;
111110
did_load_from_clickhouse = true;
112111

113112
bytes
@@ -122,7 +121,7 @@ async fn compiler_cache_entrypoint(config: &Config) -> Result<(), Box<dyn Error>
122121
}
123122
};
124123

125-
if !did_load_from_clickhouse {
124+
if !did_load_from_local_cache {
126125
local_disk
127126
.write(&total_hash, &compiled_bytes)
128127
.await
@@ -132,7 +131,7 @@ async fn compiler_cache_entrypoint(config: &Config) -> Result<(), Box<dyn Error>
132131
let should_upload = {
133132
let default_config = Config::default();
134133

135-
!did_load_from_cache && config.user != default_config.user
134+
!did_load_from_local_cache && !did_load_from_clickhouse && config.user != default_config.user
136135
};
137136

138137
if should_upload {

0 commit comments

Comments
 (0)