@@ -34,13 +34,14 @@ use charon_lib::{
3434 export:: { CrateData , multi_target} ,
3535 formatter:: IntoFormatter ,
3636 logger,
37- options:: { CHARON_ARGS , CliOpts , SerializationFormat } ,
37+ options:: { CHARON_ARGS , CliOpts , SerializationFormat , selected_crate_var } ,
3838 pretty:: FmtWithCtx ,
3939 utils:: { arg_value, arg_values} ,
4040} ;
4141use clap:: Parser ;
4242use cli:: { Charon , Cli } ;
4343use itertools:: Itertools ;
44+ use serde:: Deserialize ;
4445use std:: { env, path:: PathBuf , process:: ExitStatus } ;
4546use toolchain:: { CHARON_SYSROOT_VAR , setup_miri_sysroot, toolchain_path, toolchain_version} ;
4647
@@ -211,16 +212,11 @@ fn translate_multi_target(
211212 Ok ( ExitStatus :: default ( ) )
212213}
213214
214- fn translate_with_cargo ( options : CliOpts , cargo_args : Vec < String > ) -> anyhow:: Result < ExitStatus > {
215+ fn translate_with_cargo (
216+ options : CliOpts ,
217+ mut cargo_args : Vec < String > ,
218+ ) -> anyhow:: Result < ExitStatus > {
215219 ensure_rustup ( ) ;
216- let mut cmd = toolchain:: in_toolchain ( "cargo" ) ?;
217- cmd. env ( "RUSTC_WRAPPER" , toolchain:: driver_path ( ) ) ;
218- // We pass a random value to prevent cargo from considering a charon run cacheable.
219- cmd. env ( "RUSTC_WORKSPACE_WRAPPER" , fresh_fingerprint ( ) ) ;
220- cmd. env ( "CHARON_USING_CARGO" , "1" ) ;
221- cmd. env_remove ( "CARGO_PRIMARY_PACKAGE" ) ;
222- cmd. env ( CHARON_ARGS , serde_json:: to_string ( & options) . unwrap ( ) ) ;
223- cmd. arg ( "build" ) ;
224220 let target = match arg_values ( & cargo_args, "--target" ) . collect_vec ( ) . as_slice ( ) {
225221 [ target] => target. to_string ( ) ,
226222 // We resolve a single sysroot for all the crates we build.
@@ -232,11 +228,28 @@ fn translate_with_cargo(options: CliOpts, cargo_args: Vec<String>) -> anyhow::Re
232228 // Make sure the build target is explicitly set. This is needed to detect which crates
233229 // are proc-macro/build-script in `charon-driver`.
234230 let host = get_rustc_version ( ) ?. host ;
235- cmd . arg ( "--target" ) ;
236- cmd . arg ( & host) ;
231+ cargo_args . push ( "--target" . to_owned ( ) ) ;
232+ cargo_args . push ( host. clone ( ) ) ;
237233 host
238234 }
239235 } ;
236+
237+ let mut cmd = toolchain:: in_toolchain ( "cargo" ) ?;
238+ cmd. env ( "RUSTC_WRAPPER" , toolchain:: driver_path ( ) ) ;
239+ cmd. env_remove ( "RUSTC_WORKSPACE_WRAPPER" ) ;
240+ cmd. env ( "CHARON_USING_CARGO" , "1" ) ;
241+ cmd. env_remove ( "CARGO_PRIMARY_PACKAGE" ) ;
242+ cmd. env ( CHARON_ARGS , serde_json:: to_string ( & options) . unwrap ( ) ) ;
243+ // Cargo skips the crates it considers fresh, and charon would emit nothing for a skipped
244+ // crate. `charon-driver` records `selected_crate_var` of each target crate as an env
245+ // dependency, and cargo reruns a crate when the value of such a variable changes. We give a
246+ // fresh value to the variables of the crates this build selects only, so cargo reruns exactly
247+ // them and keeps every dependency cached.
248+ let nonce = fresh_nonce ( ) ;
249+ for crate_name in selected_crates ( & cargo_args) ? {
250+ cmd. env ( selected_crate_var ( & crate_name) , & nonce) ;
251+ }
252+ cmd. arg ( "build" ) ;
240253 if let Some ( sysroot) = miri_sysroot ( options. sysroot . as_deref ( ) , & target) {
241254 cmd. env ( CHARON_SYSROOT_VAR , sysroot) ;
242255 }
@@ -249,6 +262,47 @@ fn translate_with_cargo(options: CliOpts, cargo_args: Vec<String>) -> anyhow::Re
249262 . expect ( "failed to wait for cargo?" ) )
250263}
251264
265+ /// Returns the names of the crates that `cargo build <build_args>` selects, i.e. the root units of
266+ /// its unit graph.
267+ fn selected_crates ( build_args : & [ String ] ) -> anyhow:: Result < Vec < String > > {
268+ /// The parts of cargo's `--unit-graph` output we use.
269+ #[ derive( Deserialize ) ]
270+ struct UnitGraph {
271+ units : Vec < Unit > ,
272+ roots : Vec < usize > ,
273+ }
274+ #[ derive( Deserialize ) ]
275+ struct Unit {
276+ target : UnitTarget ,
277+ }
278+ #[ derive( Deserialize ) ]
279+ struct UnitTarget {
280+ name : String ,
281+ }
282+
283+ let mut cmd = toolchain:: in_toolchain ( "cargo" ) ?;
284+ cmd. env_remove ( "RUSTC_WRAPPER" ) ;
285+ cmd. env_remove ( "RUSTC_WORKSPACE_WRAPPER" ) ;
286+ cmd. args ( [ "build" , "--unit-graph" , "-Z" , "unstable-options" ] ) ;
287+ cmd. args ( build_args) ;
288+ trace ! ( "running {cmd:?}" ) ;
289+ let output = cmd. output ( ) . context ( "could not run cargo" ) ?;
290+ if !output. status . success ( ) {
291+ anyhow:: bail!(
292+ "`cargo build --unit-graph` failed with {}:\n {}" ,
293+ output. status,
294+ String :: from_utf8_lossy( & output. stderr)
295+ ) ;
296+ }
297+ let graph: UnitGraph = serde_json:: from_slice ( & output. stdout )
298+ . context ( "could not parse the output of `cargo build --unit-graph`" ) ?;
299+ Ok ( graph
300+ . roots
301+ . into_iter ( )
302+ . map ( |root| graph. units [ root] . target . name . replace ( '-' , "_" ) )
303+ . collect ( ) )
304+ }
305+
252306fn translate_without_cargo ( mut options : CliOpts ) -> anyhow:: Result < ExitStatus > {
253307 ensure_rustup ( ) ;
254308 let mut cmd = toolchain:: driver_cmd ( ) ?;
@@ -293,12 +347,12 @@ fn get_rustc_version() -> anyhow::Result<rustc_version::VersionMeta> {
293347 Ok ( rustc_version)
294348}
295349
296- /// A sufficiently unique string used to force cargo to re-run charon each time we call it .
297- fn fresh_fingerprint ( ) -> String {
350+ /// A sufficiently unique string, different for each charon run .
351+ fn fresh_nonce ( ) -> String {
298352 let nanos = std:: time:: SystemTime :: now ( )
299353 . duration_since ( std:: time:: UNIX_EPOCH )
300354 . map_or ( 0 , |duration| duration. as_nanos ( ) ) ;
301- format ! ( "charon-dont-cache-this- {}-{nanos}" , std:: process:: id( ) )
355+ format ! ( "{}-{nanos}" , std:: process:: id( ) )
302356}
303357
304358fn ensure_rustup ( ) {
0 commit comments