@@ -1323,7 +1323,7 @@ pub fn rustc_cargo_env(
13231323 builder : & Builder < ' _ > ,
13241324 cargo : & mut Cargo ,
13251325 target : TargetSelection ,
1326- build_stage : u32 ,
1326+ _build_stage : u32 ,
13271327) {
13281328 // Set some configuration variables picked up by build scripts and
13291329 // the compiler alike
@@ -1379,18 +1379,24 @@ pub fn rustc_cargo_env(
13791379 cargo. rustflag ( "--cfg=llvm_enzyme" ) ;
13801380 }
13811381
1382- // Note that this is disabled if LLVM itself is disabled or we're in a check
1383- // build. If we are in a check build we still go ahead here presuming we've
1384- // detected that LLVM is already built and good to go which helps prevent
1385- // busting caches (e.g. like #71152).
1382+ // These conditionals represent a tension between three forces:
1383+ // - For non-check builds, we need to define some LLVM-related environment
1384+ // variables, requiring LLVM to have been built.
1385+ // - For check builds, we want to avoid building LLVM if possible.
1386+ // - Check builds and non-check builds should have the same environment if
1387+ // possible, to avoid unnecessary rebuilds due to cache-busting.
1388+ //
1389+ // Therefore we try to avoid building LLVM for check builds, but only if
1390+ // building LLVM would be expensive. If "building" LLVM is cheap
1391+ // (i.e. it's already built or is downloadable), we prefer to maintain a
1392+ // consistent environment between check and non-check builds.
13861393 if builder. config . llvm_enabled ( target) {
1387- let building_is_expensive =
1394+ let building_llvm_is_expensive =
13881395 crate :: core:: build_steps:: llvm:: prebuilt_llvm_config ( builder, target, false )
13891396 . should_build ( ) ;
1390- // `top_stage == stage` might be false for `check --stage 1`, if we are building the stage 1 compiler
1391- let can_skip_build = builder. kind == Kind :: Check && builder. top_stage == build_stage;
1392- let should_skip_build = building_is_expensive && can_skip_build;
1393- if !should_skip_build {
1397+
1398+ let skip_llvm = ( builder. kind == Kind :: Check ) && building_llvm_is_expensive;
1399+ if !skip_llvm {
13941400 rustc_llvm_env ( builder, cargo, target)
13951401 }
13961402 }
@@ -1407,6 +1413,9 @@ pub fn rustc_cargo_env(
14071413
14081414/// Pass down configuration from the LLVM build into the build of
14091415/// rustc_llvm and rustc_codegen_llvm.
1416+ ///
1417+ /// Note that this has the side-effect of _building LLVM_, which is sometimes
1418+ /// unwanted (e.g. for check builds).
14101419fn rustc_llvm_env ( builder : & Builder < ' _ > , cargo : & mut Cargo , target : TargetSelection ) {
14111420 if builder. config . is_rust_llvm ( target) {
14121421 cargo. env ( "LLVM_RUSTLLVM" , "1" ) ;
0 commit comments