Skip to content

Commit cc5ac27

Browse files
committed
use LinkerFlavorCli for -C linker-flavor
1 parent 8ef3d5b commit cc5ac27

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ pub fn ignored_for_lto(sess: &Session, info: &CrateInfo, cnum: CrateNum) -> bool
11301130
&& (info.compiler_builtins == Some(cnum) || info.is_no_builtins.contains(&cnum))
11311131
}
11321132

1133-
// This functions tries to determine the appropriate linker (and corresponding LinkerFlavor) to use
1133+
/// This function tries to determine the appropriate linker (and corresponding LinkerFlavor) to use
11341134
pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
11351135
fn infer_from(
11361136
sess: &Session,
@@ -1203,9 +1203,13 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
12031203
}
12041204
}
12051205

1206-
// linker and linker flavor specified via command line have precedence over what the target
1207-
// specification specifies
1208-
if let Some(ret) = infer_from(sess, sess.opts.cg.linker.clone(), sess.opts.cg.linker_flavor) {
1206+
// Lower the potential `-C linker-flavor` CLI flag to its principal linker-flavor
1207+
let linker_flavor =
1208+
sess.opts.cg.linker_flavor.as_ref().map(|surface_flavor| surface_flavor.to_flavor());
1209+
1210+
// The `-C linker` and `-C linker-flavor` CLI flags have higher priority than what the target
1211+
// specification declares.
1212+
if let Some(ret) = infer_from(sess, sess.opts.cg.linker.clone(), linker_flavor) {
12091213
return ret;
12101214
}
12111215

compiler/rustc_interface/src/tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::interface::parse_cfgspecs;
33
use rustc_data_structures::fx::FxHashSet;
44
use rustc_errors::{emitter::HumanReadableErrorType, registry, ColorConfig};
55
use rustc_session::config::InstrumentCoverage;
6+
use rustc_session::config::LinkerFlavorCli;
67
use rustc_session::config::Strip;
78
use rustc_session::config::{build_configuration, build_session_options, to_crate_config};
89
use rustc_session::config::{
@@ -551,7 +552,7 @@ fn test_codegen_options_tracking_hash() {
551552
untracked!(link_args, vec![String::from("abc"), String::from("def")]);
552553
untracked!(link_self_contained, Some(true));
553554
untracked!(linker, Some(PathBuf::from("linker")));
554-
untracked!(linker_flavor, Some(LinkerFlavor::Gcc));
555+
untracked!(linker_flavor, Some(LinkerFlavorCli::WellKnown(LinkerFlavor::Gcc)));
555556
untracked!(no_stack_check, true);
556557
untracked!(remark, Passes::Some(vec![String::from("pass1"), String::from("pass2")]));
557558
untracked!(rpath, true);

compiler/rustc_session/src/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2432,7 +2432,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
24322432
}
24332433
}
24342434

2435-
if cg.linker_flavor == Some(LinkerFlavor::L4Bender)
2435+
if cg.linker_flavor == Some(LinkerFlavorCli::WellKnown(LinkerFlavor::L4Bender))
24362436
&& !nightly_options::is_unstable_enabled(matches)
24372437
{
24382438
early_error(

compiler/rustc_session/src/options.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::lint;
55
use crate::search_paths::SearchPath;
66
use crate::utils::NativeLib;
77
use rustc_errors::LanguageIdentifier;
8-
use rustc_target::spec::{CodeModel, LinkerFlavor, MergeFunctions, PanicStrategy, SanitizerSet};
8+
use rustc_target::spec::{CodeModel, MergeFunctions, PanicStrategy, SanitizerSet};
99
use rustc_target::spec::{
1010
RelocModel, RelroLevel, SplitDebuginfo, StackProtector, TargetTriple, TlsModel,
1111
};
@@ -383,7 +383,9 @@ mod desc {
383383
"either a boolean (`yes`, `no`, `on`, `off`, etc), `checks`, or `nochecks`";
384384
pub const parse_cfprotection: &str = "`none`|`no`|`n` (default), `branch`, `return`, or `full`|`yes`|`y` (equivalent to `branch` and `return`)";
385385
pub const parse_strip: &str = "either `none`, `debuginfo`, or `symbols`";
386-
pub const parse_linker_flavor: &str = ::rustc_target::spec::LinkerFlavor::one_of();
386+
pub const parse_linker_flavor: &str = "one of: `em`, `gcc`, `l4-bender`, `ld`, `msvc`, \
387+
`ptx-linker`, `bpf-linker`, `wasm-ld`, `ld64.lld`, `ld.lld`, `lld-link`, \
388+
or a `gcc:`-prefixed linker to use with the gcc flavor, like `gcc:lld` or `gcc:gold,`";
387389
pub const parse_optimization_fuel: &str = "crate=integer";
388390
pub const parse_mir_spanview: &str = "`statement` (default), `terminator`, or `block`";
389391
pub const parse_instrument_coverage: &str =
@@ -760,8 +762,8 @@ mod parse {
760762
true
761763
}
762764

763-
pub(crate) fn parse_linker_flavor(slot: &mut Option<LinkerFlavor>, v: Option<&str>) -> bool {
764-
match v.and_then(LinkerFlavor::from_str) {
765+
pub(crate) fn parse_linker_flavor(slot: &mut Option<LinkerFlavorCli>, v: Option<&str>) -> bool {
766+
match v.and_then(|s| LinkerFlavorCli::from_str(s).ok()) {
765767
Some(lf) => *slot = Some(lf),
766768
_ => return false,
767769
}
@@ -1120,7 +1122,7 @@ options! {
11201122
on C toolchain installed in the system"),
11211123
linker: Option<PathBuf> = (None, parse_opt_pathbuf, [UNTRACKED],
11221124
"system linker to link outputs with"),
1123-
linker_flavor: Option<LinkerFlavor> = (None, parse_linker_flavor, [UNTRACKED],
1125+
linker_flavor: Option<LinkerFlavorCli> = (None, parse_linker_flavor, [UNTRACKED],
11241126
"linker flavor"),
11251127
linker_plugin_lto: LinkerPluginLto = (LinkerPluginLto::Disabled,
11261128
parse_linker_plugin_lto, [TRACKED],

compiler/rustc_target/src/spec/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ macro_rules! flavor_mappings {
165165
)
166166
}
167167

168+
// Note: until string concatenation can be done in const contexts, remember to update the
169+
// `parse_linker_flavor` error message in rustc_session/src/options.rs whenever adding a new allowed
170+
// value here, since it also mentions enriched CLI-allowed values like `gcc:lld` that are then
171+
// lowered to these target specs `LinkerFlavor`s.
168172
flavor_mappings! {
169173
((LinkerFlavor::Em), "em"),
170174
((LinkerFlavor::Gcc), "gcc"),

0 commit comments

Comments
 (0)