|
1 | 1 | use std::any::Any; |
| 2 | +use std::env::VarError; |
2 | 3 | use std::ffi::OsString; |
3 | 4 | use std::io::{self, BufWriter, Write}; |
4 | 5 | use std::path::{Path, PathBuf}; |
@@ -321,6 +322,24 @@ fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) { |
321 | 322 | ) |
322 | 323 | } |
323 | 324 |
|
| 325 | +fn env_var(tcx: TyCtxt<'_>, key: Symbol) -> Option<Symbol> { |
| 326 | + let var = match env::var(key.as_str()) { |
| 327 | + Ok(var) => Some(Symbol::intern(&var)), |
| 328 | + Err(VarError::NotPresent) => None, |
| 329 | + Err(VarError::NotUnicode(var)) => { |
| 330 | + tcx.dcx().emit_err(errors::EnvVarNotUnicode { key, var }); |
| 331 | + None |
| 332 | + } |
| 333 | + }; |
| 334 | + // Also add the variable to Cargo's dependency tracking |
| 335 | + // |
| 336 | + // NOTE: This only works for passes run before `write_dep_info`. See that |
| 337 | + // for extension points for configuring environment variables to be |
| 338 | + // properly change-tracked. |
| 339 | + tcx.sess.psess.env_depinfo.borrow_mut().insert((key, var)); |
| 340 | + var |
| 341 | +} |
| 342 | + |
324 | 343 | // Returns all the paths that correspond to generated files. |
325 | 344 | fn generated_output_paths( |
326 | 345 | tcx: TyCtxt<'_>, |
@@ -632,6 +651,9 @@ pub fn write_dep_info(tcx: TyCtxt<'_>) { |
632 | 651 | // the side-effect of providing a complete set of all |
633 | 652 | // accessed files and env vars. |
634 | 653 | let _ = tcx.resolver_for_lowering(); |
| 654 | + // Similarly, analysis, codegen and linking should state which environment |
| 655 | + // variables they depend on here, as those passes are run after this pass, |
| 656 | + // but we'll need the information when emitting dependency info to Cargo. |
635 | 657 |
|
636 | 658 | let sess = tcx.sess; |
637 | 659 | let _timer = sess.timer("write_dep_info"); |
@@ -685,6 +707,7 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| { |
685 | 707 | |tcx, _| tcx.arena.alloc_from_iter(tcx.resolutions(()).stripped_cfg_items.steal()); |
686 | 708 | providers.resolutions = |tcx, ()| tcx.resolver_for_lowering_raw(()).1; |
687 | 709 | providers.early_lint_checks = early_lint_checks; |
| 710 | + providers.env_var = env_var; |
688 | 711 | proc_macro_decls::provide(providers); |
689 | 712 | rustc_const_eval::provide(providers); |
690 | 713 | rustc_middle::hir::provide(providers); |
|
0 commit comments