Skip to content

Commit 755bc0b

Browse files
committed
implement opt out -Clink-self-contained=-linker
record both enabled and disabled components so that they can be merged with the ones that the target spec will define
1 parent 164e172 commit 755bc0b

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -2992,9 +2992,13 @@ fn add_lld_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
29922992
}
29932993

29942994
// 1. Implement the "self-contained" part of this feature by adding rustc distribution
2995-
// directories to the tool's search path.
2996-
let self_contained_linker = sess.opts.cg.link_self_contained.linker() || unstable_use_lld;
2997-
if self_contained_linker {
2995+
// directories to the tool's search path:
2996+
// - if the self-contained linker is enabled on the CLI (or by the unstable CLI flag that will
2997+
// be removed eventually),
2998+
// - and if the self-contained linker is not disabled on the CLI.
2999+
let self_contained_linker =
3000+
sess.opts.cg.link_self_contained.is_linker_enabled() || unstable_use_lld;
3001+
if self_contained_linker && !sess.opts.cg.link_self_contained.is_linker_disabled() {
29983002
for path in sess.get_tools_search_paths(false) {
29993003
cmd.arg({
30003004
let mut arg = OsString::from("-B");
@@ -3005,7 +3009,7 @@ fn add_lld_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
30053009
}
30063010

30073011
// 2. Implement the "linker flavor" part of this feature by asking `cc` to use some kind of
3008-
// `lld` as the linker.
3012+
// `lld` as the linker.
30093013
cmd.arg("-fuse-ld=lld");
30103014

30113015
if !flavor.is_gnu() {

compiler/rustc_session/src/config.rs

+30-12
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,13 @@ pub struct LinkSelfContained {
233233
/// Used for compatibility with the existing opt-in and target inference.
234234
pub explicitly_set: Option<bool>,
235235

236-
/// The components that are enabled.
237-
components: LinkSelfContainedComponents,
236+
/// The components that are enabled on the CLI, using the `+component` syntax or one of the
237+
/// `true` shorcuts.
238+
enabled_components: LinkSelfContainedComponents,
239+
240+
/// The components that are disabled on the CLI, using the `-component` syntax or one of the
241+
/// `false` shortcuts.
242+
disabled_components: LinkSelfContainedComponents,
238243
}
239244

240245
impl LinkSelfContained {
@@ -247,11 +252,13 @@ impl LinkSelfContained {
247252
// `explicitly_set` state.
248253
if let Some(component_to_enable) = component.strip_prefix('+') {
249254
self.explicitly_set = None;
250-
self.components.insert(LinkSelfContainedComponents::from_str(component_to_enable)?);
255+
self.enabled_components
256+
.insert(LinkSelfContainedComponents::from_str(component_to_enable)?);
251257
Some(())
252258
} else if let Some(component_to_disable) = component.strip_prefix('-') {
253259
self.explicitly_set = None;
254-
self.components.remove(LinkSelfContainedComponents::from_str(component_to_disable)?);
260+
self.disabled_components
261+
.insert(LinkSelfContainedComponents::from_str(component_to_disable)?);
255262
Some(())
256263
} else {
257264
None
@@ -262,11 +269,14 @@ impl LinkSelfContained {
262269
/// purposes.
263270
pub(crate) fn set_all_explicitly(&mut self, enabled: bool) {
264271
self.explicitly_set = Some(enabled);
265-
self.components = if enabled {
266-
LinkSelfContainedComponents::all()
272+
273+
if enabled {
274+
self.enabled_components = LinkSelfContainedComponents::all();
275+
self.disabled_components = LinkSelfContainedComponents::empty();
267276
} else {
268-
LinkSelfContainedComponents::empty()
269-
};
277+
self.enabled_components = LinkSelfContainedComponents::empty();
278+
self.disabled_components = LinkSelfContainedComponents::all();
279+
}
270280
}
271281

272282
/// Helper creating a fully enabled `LinkSelfContained` instance. Used in tests.
@@ -280,13 +290,21 @@ impl LinkSelfContained {
280290
/// components was set individually. This would also require the `-Zunstable-options` flag, to
281291
/// be allowed.
282292
fn are_unstable_variants_set(&self) -> bool {
283-
let any_component_set = !self.components.is_empty();
293+
let any_component_set =
294+
!self.enabled_components.is_empty() || !self.disabled_components.is_empty();
284295
self.explicitly_set.is_none() && any_component_set
285296
}
286297

287-
/// Returns whether the self-contained linker component is enabled.
288-
pub fn linker(&self) -> bool {
289-
self.components.contains(LinkSelfContainedComponents::LINKER)
298+
/// Returns whether the self-contained linker component was enabled on the CLI, using the
299+
/// `-C link-self-contained=+linker` syntax, or one of the `true` shorcuts.
300+
pub fn is_linker_enabled(&self) -> bool {
301+
self.enabled_components.contains(LinkSelfContainedComponents::LINKER)
302+
}
303+
304+
/// Returns whether the self-contained linker component was disabled on the CLI, using the
305+
/// `-C link-self-contained=-linker` syntax, or one of the `false` shorcuts.
306+
pub fn is_linker_disabled(&self) -> bool {
307+
self.disabled_components.contains(LinkSelfContainedComponents::LINKER)
290308
}
291309
}
292310

0 commit comments

Comments
 (0)