|
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};
|
@@ -336,6 +337,24 @@ fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
|
336 | 337 | )
|
337 | 338 | }
|
338 | 339 |
|
| 340 | +fn env_var(tcx: TyCtxt<'_>, key: Symbol) -> Option<Symbol> { |
| 341 | + let var = match std::env::var(key.as_str()) { |
| 342 | + Ok(var) => Some(Symbol::intern(&var)), |
| 343 | + Err(VarError::NotPresent) => None, |
| 344 | + Err(VarError::NotUnicode(var)) => { |
| 345 | + tcx.dcx().emit_err(errors::EnvVarNotUnicode { key, var }); |
| 346 | + None |
| 347 | + } |
| 348 | + }; |
| 349 | + // Also add the variable to Cargo's dependency tracking |
| 350 | + // |
| 351 | + // NOTE: This only works for passes run before `write_dep_info`. See that |
| 352 | + // for extension points for configuring environment variables to be |
| 353 | + // properly change-tracked. |
| 354 | + tcx.sess.psess.env_depinfo.borrow_mut().insert((key, var)); |
| 355 | + var |
| 356 | +} |
| 357 | + |
339 | 358 | // Returns all the paths that correspond to generated files.
|
340 | 359 | fn generated_output_paths(
|
341 | 360 | tcx: TyCtxt<'_>,
|
@@ -647,6 +666,10 @@ pub fn write_dep_info(tcx: TyCtxt<'_>) {
|
647 | 666 | // the side-effect of providing a complete set of all
|
648 | 667 | // accessed files and env vars.
|
649 | 668 | let _ = tcx.resolver_for_lowering();
|
| 669 | + // Similarly, allow codegen and linking passes to state which environment |
| 670 | + // variables they depend on, as those passes are run after this pass, but |
| 671 | + // we'll need the information when emitting dependency info to Cargo. |
| 672 | + rustc_codegen_ssa::register_environment_variables(tcx); |
650 | 673 |
|
651 | 674 | let sess = tcx.sess;
|
652 | 675 | let _timer = sess.timer("write_dep_info");
|
@@ -700,6 +723,7 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
|
700 | 723 | |tcx, _| tcx.arena.alloc_from_iter(tcx.resolutions(()).stripped_cfg_items.steal());
|
701 | 724 | providers.resolutions = |tcx, ()| tcx.resolver_for_lowering_raw(()).1;
|
702 | 725 | providers.early_lint_checks = early_lint_checks;
|
| 726 | + providers.env_var = env_var; |
703 | 727 | proc_macro_decls::provide(providers);
|
704 | 728 | rustc_const_eval::provide(providers);
|
705 | 729 | rustc_middle::hir::provide(providers);
|
|
0 commit comments