Skip to content

Commit 5261a59

Browse files
committed
Centralize CFG_* values in rustc_session crate instead of calling option_env!() from multiple crates
1 parent afca636 commit 5261a59

File tree

12 files changed

+47
-22
lines changed

12 files changed

+47
-22
lines changed

src/librustc_ast_passes/feature_gate.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_errors::{struct_span_err, Handler};
66
use rustc_feature::{AttributeGate, BUILTIN_ATTRIBUTE_MAP};
77
use rustc_feature::{Features, GateIssue, UnstableFeatures};
88
use rustc_session::parse::{feature_err, feature_err_issue, ParseSess};
9+
use rustc_session::CFG_RELEASE_CHANNEL;
910
use rustc_span::source_map::Spanned;
1011
use rustc_span::symbol::sym;
1112
use rustc_span::Span;
@@ -657,7 +658,7 @@ fn maybe_stage_features(span_handler: &Handler, krate: &ast::Crate, unstable: Un
657658
attr.span,
658659
E0554,
659660
"`#![feature]` may not be used on the {} release channel",
660-
option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)")
661+
CFG_RELEASE_CHANNEL.unwrap_or("(unknown)")
661662
)
662663
.emit();
663664
}

src/librustc_codegen_llvm/debuginfo/metadata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use rustc_middle::ty::Instance;
4040
use rustc_middle::ty::{self, AdtKind, ParamEnv, Ty, TyCtxt};
4141
use rustc_middle::{bug, span_bug};
4242
use rustc_session::config::{self, DebugInfo};
43+
use rustc_session::CFG_VERSION;
4344
use rustc_span::symbol::{Interner, Symbol};
4445
use rustc_span::{self, SourceFile, SourceFileHash, Span};
4546
use rustc_target::abi::{Abi, Align, DiscriminantKind, HasDataLayout, Integer, LayoutOf};
@@ -909,8 +910,7 @@ pub fn compile_unit_metadata(
909910
}
910911

911912
debug!("compile_unit_metadata: {:?}", name_in_debuginfo);
912-
let rustc_producer =
913-
format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"),);
913+
let rustc_producer = format!("rustc version {}", CFG_VERSION.expect("CFG_VERSION"),);
914914
// FIXME(#41252) Remove "clang LLVM" if we can get GDB and LLVM to play nice.
915915
let producer = format!("clang LLVM ({})", rustc_producer);
916916

src/librustc_codegen_ssa/back/link.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_session::output::{check_file_is_writeable, invalid_output_for_target,
1010
use rustc_session::search_paths::PathKind;
1111
/// For all the linkers we support, and information they might
1212
/// need out of the shared crate context before we get rid of it.
13-
use rustc_session::{filesearch, Session};
13+
use rustc_session::{filesearch, Session, CFG_PREFIX, CFG_RELEASE_CHANNEL};
1414
use rustc_span::symbol::Symbol;
1515
use rustc_target::spec::{LinkerFlavor, PanicStrategy, RelroLevel};
1616

@@ -698,9 +698,7 @@ fn link_sanitizer_runtime(sess: &Session, crate_type: config::CrateType, linker:
698698
let default_sysroot = filesearch::get_or_default_sysroot();
699699
let default_tlib =
700700
filesearch::make_target_lib_path(&default_sysroot, sess.opts.target_triple.triple());
701-
let channel = option_env!("CFG_RELEASE_CHANNEL")
702-
.map(|channel| format!("-{}", channel))
703-
.unwrap_or_default();
701+
let channel = CFG_RELEASE_CHANNEL.map(|channel| format!("-{}", channel)).unwrap_or_default();
704702

705703
match sess.opts.target_triple.triple() {
706704
"x86_64-apple-darwin" => {
@@ -1402,7 +1400,7 @@ fn add_rpath_args(
14021400
if sess.opts.cg.rpath {
14031401
let target_triple = sess.opts.target_triple.triple();
14041402
let mut get_install_prefix_lib_path = || {
1405-
let install_prefix = option_env!("CFG_PREFIX").expect("CFG_PREFIX");
1403+
let install_prefix = CFG_PREFIX.expect("CFG_PREFIX");
14061404
let tlib = filesearch::relative_target_lib_path(&sess.sysroot, target_triple);
14071405
let mut path = PathBuf::from(install_prefix);
14081406
path.push(&tlib);

src/librustc_driver/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ use rustc_session::config::nightly_options;
3636
use rustc_session::config::{ErrorOutputType, Input, OutputType, PrintRequest};
3737
use rustc_session::getopts;
3838
use rustc_session::lint::{Lint, LintId};
39-
use rustc_session::{config, DiagnosticOutput, Session};
39+
use rustc_session::{
40+
config, DiagnosticOutput, Session, CFG_RELEASE, CFG_VERSION, CFG_VER_DATE, CFG_VER_HASH,
41+
};
4042
use rustc_session::{early_error, early_warn};
4143
use rustc_span::source_map::{FileLoader, FileName};
4244
use rustc_span::symbol::sym;
@@ -748,24 +750,24 @@ impl RustcDefaultCalls {
748750

749751
/// Returns a version string such as "0.12.0-dev".
750752
fn release_str() -> Option<&'static str> {
751-
option_env!("CFG_RELEASE")
753+
CFG_RELEASE
752754
}
753755

754756
/// Returns the full SHA1 hash of HEAD of the Git repo from which rustc was built.
755757
fn commit_hash_str() -> Option<&'static str> {
756-
option_env!("CFG_VER_HASH")
758+
CFG_VER_HASH
757759
}
758760

759761
/// Returns the "commit date" of HEAD of the Git repo from which rustc was built as a static string.
760762
fn commit_date_str() -> Option<&'static str> {
761-
option_env!("CFG_VER_DATE")
763+
CFG_VER_DATE
762764
}
763765

764766
/// Prints version information
765767
pub fn version(binary: &str, matches: &getopts::Matches) {
766768
let verbose = matches.opt_present("verbose");
767769

768-
println!("{} {}", binary, option_env!("CFG_VERSION").unwrap_or("unknown version"));
770+
println!("{} {}", binary, CFG_VERSION.unwrap_or("unknown version"));
769771

770772
if verbose {
771773
fn unw(x: Option<&str>) -> &str {
@@ -1196,7 +1198,7 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
11961198
format!("we would appreciate a bug report: {}", bug_report_url).into(),
11971199
format!(
11981200
"rustc {} running on {}",
1199-
option_env!("CFG_VERSION").unwrap_or("unknown_version"),
1201+
CFG_VERSION.unwrap_or("unknown_version"),
12001202
config::host_triple()
12011203
)
12021204
.into(),

src/librustc_incremental/persist/file_format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const HEADER_FORMAT_VERSION: u16 = 0;
2626
/// A version string that hopefully is always different for compiler versions
2727
/// with different encodings of incremental compilation artifacts. Contains
2828
/// the Git commit hash.
29-
const RUSTC_VERSION: Option<&str> = option_env!("CFG_VERSION");
29+
use rustc_session::CFG_VERSION as RUSTC_VERSION;
3030

3131
pub fn write_file_header(stream: &mut Encoder) {
3232
stream.emit_raw_bytes(FILE_MAGIC);

src/librustc_metadata/rmeta/decoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_middle::ty::codec::TyDecoder;
3131
use rustc_middle::ty::{self, Ty, TyCtxt};
3232
use rustc_middle::util::common::record_time;
3333
use rustc_serialize::{opaque, Decodable, Decoder, SpecializedDecoder};
34-
use rustc_session::Session;
34+
use rustc_session::{Session, CFG_VIRTUAL_RUST_SOURCE_BASE_DIR};
3535
use rustc_span::source_map::{respan, Spanned};
3636
use rustc_span::symbol::{sym, Symbol};
3737
use rustc_span::{self, hygiene::MacroKind, BytePos, Pos, Span, DUMMY_SP};
@@ -1463,7 +1463,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
14631463
fn imported_source_files(&self, sess: &Session) -> &'a [ImportedSourceFile] {
14641464
// Translate the virtual `/rustc/$hash` prefix back to a real directory
14651465
// that should hold actual sources, where possible.
1466-
let virtual_rust_source_base_dir = option_env!("CFG_VIRTUAL_RUST_SOURCE_BASE_DIR")
1466+
let virtual_rust_source_base_dir = CFG_VIRTUAL_RUST_SOURCE_BASE_DIR
14671467
.map(Path::new)
14681468
.filter(|_| {
14691469
// Only spend time on further checks if we have what to translate *to*.

src/librustc_metadata/rmeta/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_middle::ty::{self, ReprOptions, Ty};
1818
use rustc_serialize::opaque::Encoder;
1919
use rustc_session::config::SymbolManglingVersion;
2020
use rustc_session::CrateDisambiguator;
21+
use rustc_session::CFG_VERSION;
2122
use rustc_span::edition::Edition;
2223
use rustc_span::symbol::Symbol;
2324
use rustc_span::{self, Span};
@@ -34,7 +35,7 @@ mod encoder;
3435
mod table;
3536

3637
crate fn rustc_version() -> String {
37-
format!("rustc {}", option_env!("CFG_VERSION").unwrap_or("unknown version"))
38+
format!("rustc {}", CFG_VERSION.unwrap_or("unknown version"))
3839
}
3940

4041
/// Metadata encoding version.

src/librustc_middle/middle/stability.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_hir::{self, HirId};
1616
use rustc_session::lint::builtin::{DEPRECATED, DEPRECATED_IN_FUTURE, SOFT_UNSTABLE};
1717
use rustc_session::lint::{BuiltinLintDiagnostics, Lint, LintBuffer};
1818
use rustc_session::parse::feature_err_issue;
19+
use rustc_session::CFG_RELEASE;
1920
use rustc_session::{DiagnosticMessageId, Session};
2021
use rustc_span::symbol::{sym, Symbol};
2122
use rustc_span::{MultiSpan, Span};
@@ -136,7 +137,7 @@ pub fn deprecation_in_effect(since: &str) -> bool {
136137
ver.split(|c| c == '.' || c == '-').flat_map(|s| s.parse()).collect()
137138
}
138139

139-
if let Some(rustc) = option_env!("CFG_RELEASE") {
140+
if let Some(rustc) = CFG_RELEASE {
140141
let since: Vec<u32> = parse_version(since);
141142
let rustc: Vec<u32> = parse_version(rustc);
142143
// We simply treat invalid `since` attributes as relating to a previous

src/librustc_session/build.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fn main() {
2+
println!("cargo:rerun-if-changed=build.rs");
3+
println!("cargo:rerun-if-env-changed=CFG_RELEASE");
4+
println!("cargo:rerun-if-env-changed=CFG_RELEASE_CHANNEL");
5+
println!("cargo:rerun-if-env-changed=CFG_VERSION");
6+
println!("cargo:rerun-if-env-changed=CFG_VER_DATE");
7+
println!("cargo:rerun-if-env-changed=CFG_VER_HASH");
8+
println!("cargo:rerun-if-env-changed=CFG_PREFIX");
9+
println!("cargo:rerun-if-env-changed=CFG_VIRTUAL_RUST_SOURCE_BASE_DIR");
10+
println!("cargo:rerun-if-env-changed=CFG_COMPILER_HOST_TRIPLE");
11+
println!("cargo:rerun-if-env-changed=CFG_LIBDIR_RELATIVE");
12+
}

src/librustc_session/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ pub fn host_triple() -> &'static str {
540540
// Instead of grabbing the host triple (for the current host), we grab (at
541541
// compile time) the target triple that this rustc is built with and
542542
// calling that (at runtime) the host triple.
543-
(option_env!("CFG_COMPILER_HOST_TRIPLE")).expect("CFG_COMPILER_HOST_TRIPLE")
543+
option_env!("CFG_COMPILER_HOST_TRIPLE").expect("CFG_COMPILER_HOST_TRIPLE")
544544
}
545545

546546
impl Default for Options {

src/librustc_session/session.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ use std::path::PathBuf;
3232
use std::sync::Arc;
3333
use std::time::Duration;
3434

35+
pub static CFG_VERSION: Option<&str> = option_env!("CFG_VERSION");
36+
pub static CFG_RELEASE: Option<&str> = option_env!("CFG_RELEASE");
37+
pub static CFG_RELEASE_CHANNEL: Option<&str> = option_env!("CFG_RELEASE_CHANNEL");
38+
pub static CFG_VER_HASH: Option<&str> = option_env!("CFG_VER_HASH");
39+
pub static CFG_VER_DATE: Option<&str> = option_env!("CFG_VER_DATE");
40+
41+
pub static CFG_PREFIX: Option<&str> = option_env!("CFG_PREFIX");
42+
pub static CFG_VIRTUAL_RUST_SOURCE_BASE_DIR: Option<&str> =
43+
option_env!("CFG_VIRTUAL_RUST_SOURCE_BASE_DIR");
44+
3545
pub struct OptimizationFuel {
3646
/// If `-zfuel=crate=n` is specified, initially set to `n`, otherwise `0`.
3747
remaining: u64,

src/librustc_typeck/check/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ use rustc_middle::ty::{
128128
use rustc_session::config::{self, EntryFnType};
129129
use rustc_session::lint;
130130
use rustc_session::parse::feature_err;
131-
use rustc_session::Session;
131+
use rustc_session::{Session, CFG_VERSION};
132132
use rustc_span::hygiene::DesugaringKind;
133133
use rustc_span::source_map::{original_sp, DUMMY_SP};
134134
use rustc_span::symbol::{kw, sym, Ident};
@@ -5806,7 +5806,7 @@ fn fatally_break_rust(sess: &Session) {
58065806
);
58075807
handler.note_without_error(&format!(
58085808
"rustc {} running on {}",
5809-
option_env!("CFG_VERSION").unwrap_or("unknown_version"),
5809+
CFG_VERSION.unwrap_or("unknown_version"),
58105810
config::host_triple(),
58115811
));
58125812
}

0 commit comments

Comments
 (0)