Skip to content

Commit 1dffc91

Browse files
committed
Move CRT Objects to stateful MaybeLazy
1 parent 1973e9d commit 1dffc91

File tree

4 files changed

+73
-71
lines changed

4 files changed

+73
-71
lines changed

compiler/rustc_target/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
1313
#![doc(rust_logo)]
1414
#![feature(assert_matches)]
15+
#![feature(fn_traits)]
1516
#![feature(iter_intersperse)]
1617
#![feature(let_chains)]
1718
#![feature(min_exhaustive_patterns)]
1819
#![feature(rustc_attrs)]
1920
#![feature(rustdoc_internals)]
21+
#![feature(unboxed_closures)]
2022
// tidy-alphabetical-end
2123

2224
use std::path::{Path, PathBuf};

compiler/rustc_target/src/spec/base/aix.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::abi::Endian;
2-
use crate::spec::{crt_objects, cvs, Cc, CodeModel, LinkOutputKind, LinkerFlavor};
3-
use crate::spec::{MaybeLazy, TargetOptions};
2+
use crate::spec::{crt_objects, cvs, Cc, CodeModel, LinkOutputKind, LinkerFlavor, TargetOptions};
43

54
pub fn opts() -> TargetOptions {
65
TargetOptions {
@@ -23,12 +22,10 @@ pub fn opts() -> TargetOptions {
2322
is_like_aix: true,
2423
default_dwarf_version: 3,
2524
function_sections: true,
26-
pre_link_objects: MaybeLazy::lazy(|| {
27-
crt_objects::new(&[
28-
(LinkOutputKind::DynamicNoPicExe, &["/usr/lib/crt0_64.o", "/usr/lib/crti_64.o"]),
29-
(LinkOutputKind::DynamicPicExe, &["/usr/lib/crt0_64.o", "/usr/lib/crti_64.o"]),
30-
])
31-
}),
25+
pre_link_objects: crt_objects::new(&[
26+
(LinkOutputKind::DynamicNoPicExe, &["/usr/lib/crt0_64.o", "/usr/lib/crti_64.o"]),
27+
(LinkOutputKind::DynamicPicExe, &["/usr/lib/crt0_64.o", "/usr/lib/crti_64.o"]),
28+
]),
3229
dll_suffix: ".a".into(),
3330
..Default::default()
3431
}

compiler/rustc_target/src/spec/base/fuchsia.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,12 @@ pub fn opts() -> TargetOptions {
3434
dynamic_linking: true,
3535
families: cvs!["unix"],
3636
pre_link_args,
37-
pre_link_objects: MaybeLazy::lazy(|| {
38-
crt_objects::new(&[
39-
(LinkOutputKind::DynamicNoPicExe, &["Scrt1.o"]),
40-
(LinkOutputKind::DynamicPicExe, &["Scrt1.o"]),
41-
(LinkOutputKind::StaticNoPicExe, &["Scrt1.o"]),
42-
(LinkOutputKind::StaticPicExe, &["Scrt1.o"]),
43-
])
44-
}),
37+
pre_link_objects: crt_objects::new(&[
38+
(LinkOutputKind::DynamicNoPicExe, &["Scrt1.o"]),
39+
(LinkOutputKind::DynamicPicExe, &["Scrt1.o"]),
40+
(LinkOutputKind::StaticNoPicExe, &["Scrt1.o"]),
41+
(LinkOutputKind::StaticPicExe, &["Scrt1.o"]),
42+
]),
4543
position_independent_executables: true,
4644
has_thread_local: true,
4745
frame_pointer: FramePointer::NonLeaf,

compiler/rustc_target/src/spec/crt_objects.rs

Lines changed: 60 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -44,89 +44,94 @@ use crate::spec::{LinkOutputKind, MaybeLazy};
4444
use std::borrow::Cow;
4545
use std::collections::BTreeMap;
4646

47-
pub type CrtObjects = BTreeMap<LinkOutputKind, Vec<Cow<'static, str>>>;
48-
pub type LazyCrtObjects = MaybeLazy<CrtObjects>;
47+
type LazyCrtObjectsArgs = &'static [(LinkOutputKind, &'static [&'static str])];
48+
pub struct LazyCrtObjectsState(LazyCrtObjectsArgs);
4949

50-
pub(super) fn new(obj_table: &[(LinkOutputKind, &[&'static str])]) -> CrtObjects {
51-
obj_table.iter().map(|(z, k)| (*z, k.iter().map(|b| (*b).into()).collect())).collect()
50+
impl FnOnce<()> for LazyCrtObjectsState {
51+
type Output = CrtObjects;
52+
extern "rust-call" fn call_once(self, _args: ()) -> Self::Output {
53+
self.0.iter().map(|(z, k)| (*z, k.iter().map(|b| (*b).into()).collect())).collect()
54+
}
5255
}
5356

54-
pub(super) fn all(obj: &'static str) -> CrtObjects {
55-
new(&[
56-
(LinkOutputKind::DynamicNoPicExe, &[obj]),
57-
(LinkOutputKind::DynamicPicExe, &[obj]),
58-
(LinkOutputKind::StaticNoPicExe, &[obj]),
59-
(LinkOutputKind::StaticPicExe, &[obj]),
60-
(LinkOutputKind::DynamicDylib, &[obj]),
61-
(LinkOutputKind::StaticDylib, &[obj]),
62-
])
57+
pub type CrtObjects = BTreeMap<LinkOutputKind, Vec<Cow<'static, str>>>;
58+
pub type LazyCrtObjects = MaybeLazy<CrtObjects, LazyCrtObjectsState>;
59+
60+
#[inline]
61+
pub(super) fn new(obj_table: LazyCrtObjectsArgs) -> LazyCrtObjects {
62+
MaybeLazy::lazied(LazyCrtObjectsState(obj_table))
6363
}
6464

65-
pub(super) fn pre_musl_self_contained() -> LazyCrtObjects {
66-
MaybeLazy::lazy(|| {
65+
macro_rules! all {
66+
($obj: literal) => {
6767
new(&[
68-
(LinkOutputKind::DynamicNoPicExe, &["crt1.o", "crti.o", "crtbegin.o"]),
69-
(LinkOutputKind::DynamicPicExe, &["Scrt1.o", "crti.o", "crtbeginS.o"]),
70-
(LinkOutputKind::StaticNoPicExe, &["crt1.o", "crti.o", "crtbegin.o"]),
71-
(LinkOutputKind::StaticPicExe, &["rcrt1.o", "crti.o", "crtbeginS.o"]),
72-
(LinkOutputKind::DynamicDylib, &["crti.o", "crtbeginS.o"]),
73-
(LinkOutputKind::StaticDylib, &["crti.o", "crtbeginS.o"]),
68+
(LinkOutputKind::DynamicNoPicExe, &[$obj]),
69+
(LinkOutputKind::DynamicPicExe, &[$obj]),
70+
(LinkOutputKind::StaticNoPicExe, &[$obj]),
71+
(LinkOutputKind::StaticPicExe, &[$obj]),
72+
(LinkOutputKind::DynamicDylib, &[$obj]),
73+
(LinkOutputKind::StaticDylib, &[$obj]),
7474
])
75-
})
75+
};
76+
}
77+
78+
pub(super) fn pre_musl_self_contained() -> LazyCrtObjects {
79+
new(&[
80+
(LinkOutputKind::DynamicNoPicExe, &["crt1.o", "crti.o", "crtbegin.o"]),
81+
(LinkOutputKind::DynamicPicExe, &["Scrt1.o", "crti.o", "crtbeginS.o"]),
82+
(LinkOutputKind::StaticNoPicExe, &["crt1.o", "crti.o", "crtbegin.o"]),
83+
(LinkOutputKind::StaticPicExe, &["rcrt1.o", "crti.o", "crtbeginS.o"]),
84+
(LinkOutputKind::DynamicDylib, &["crti.o", "crtbeginS.o"]),
85+
(LinkOutputKind::StaticDylib, &["crti.o", "crtbeginS.o"]),
86+
])
7687
}
7788

7889
pub(super) fn post_musl_self_contained() -> LazyCrtObjects {
79-
MaybeLazy::lazy(|| {
80-
new(&[
81-
(LinkOutputKind::DynamicNoPicExe, &["crtend.o", "crtn.o"]),
82-
(LinkOutputKind::DynamicPicExe, &["crtendS.o", "crtn.o"]),
83-
(LinkOutputKind::StaticNoPicExe, &["crtend.o", "crtn.o"]),
84-
(LinkOutputKind::StaticPicExe, &["crtendS.o", "crtn.o"]),
85-
(LinkOutputKind::DynamicDylib, &["crtendS.o", "crtn.o"]),
86-
(LinkOutputKind::StaticDylib, &["crtendS.o", "crtn.o"]),
87-
])
88-
})
90+
new(&[
91+
(LinkOutputKind::DynamicNoPicExe, &["crtend.o", "crtn.o"]),
92+
(LinkOutputKind::DynamicPicExe, &["crtendS.o", "crtn.o"]),
93+
(LinkOutputKind::StaticNoPicExe, &["crtend.o", "crtn.o"]),
94+
(LinkOutputKind::StaticPicExe, &["crtendS.o", "crtn.o"]),
95+
(LinkOutputKind::DynamicDylib, &["crtendS.o", "crtn.o"]),
96+
(LinkOutputKind::StaticDylib, &["crtendS.o", "crtn.o"]),
97+
])
8998
}
9099

91100
pub(super) fn pre_mingw_self_contained() -> LazyCrtObjects {
92-
MaybeLazy::lazy(|| {
93-
new(&[
94-
(LinkOutputKind::DynamicNoPicExe, &["crt2.o", "rsbegin.o"]),
95-
(LinkOutputKind::DynamicPicExe, &["crt2.o", "rsbegin.o"]),
96-
(LinkOutputKind::StaticNoPicExe, &["crt2.o", "rsbegin.o"]),
97-
(LinkOutputKind::StaticPicExe, &["crt2.o", "rsbegin.o"]),
98-
(LinkOutputKind::DynamicDylib, &["dllcrt2.o", "rsbegin.o"]),
99-
(LinkOutputKind::StaticDylib, &["dllcrt2.o", "rsbegin.o"]),
100-
])
101-
})
101+
new(&[
102+
(LinkOutputKind::DynamicNoPicExe, &["crt2.o", "rsbegin.o"]),
103+
(LinkOutputKind::DynamicPicExe, &["crt2.o", "rsbegin.o"]),
104+
(LinkOutputKind::StaticNoPicExe, &["crt2.o", "rsbegin.o"]),
105+
(LinkOutputKind::StaticPicExe, &["crt2.o", "rsbegin.o"]),
106+
(LinkOutputKind::DynamicDylib, &["dllcrt2.o", "rsbegin.o"]),
107+
(LinkOutputKind::StaticDylib, &["dllcrt2.o", "rsbegin.o"]),
108+
])
102109
}
103110

104111
pub(super) fn post_mingw_self_contained() -> LazyCrtObjects {
105-
MaybeLazy::lazy(|| all("rsend.o"))
112+
all!("rsend.o")
106113
}
107114

108115
pub(super) fn pre_mingw() -> LazyCrtObjects {
109-
MaybeLazy::lazy(|| all("rsbegin.o"))
116+
all!("rsbegin.o")
110117
}
111118

112119
pub(super) fn post_mingw() -> LazyCrtObjects {
113-
MaybeLazy::lazy(|| all("rsend.o"))
120+
all!("rsend.o")
114121
}
115122

116123
pub(super) fn pre_wasi_self_contained() -> LazyCrtObjects {
117124
// Use crt1-command.o instead of crt1.o to enable support for new-style
118125
// commands. See https://reviews.llvm.org/D81689 for more info.
119-
MaybeLazy::lazy(|| {
120-
new(&[
121-
(LinkOutputKind::DynamicNoPicExe, &["crt1-command.o"]),
122-
(LinkOutputKind::DynamicPicExe, &["crt1-command.o"]),
123-
(LinkOutputKind::StaticNoPicExe, &["crt1-command.o"]),
124-
(LinkOutputKind::StaticPicExe, &["crt1-command.o"]),
125-
(LinkOutputKind::WasiReactorExe, &["crt1-reactor.o"]),
126-
])
127-
})
126+
new(&[
127+
(LinkOutputKind::DynamicNoPicExe, &["crt1-command.o"]),
128+
(LinkOutputKind::DynamicPicExe, &["crt1-command.o"]),
129+
(LinkOutputKind::StaticNoPicExe, &["crt1-command.o"]),
130+
(LinkOutputKind::StaticPicExe, &["crt1-command.o"]),
131+
(LinkOutputKind::WasiReactorExe, &["crt1-reactor.o"]),
132+
])
128133
}
129134

130135
pub(super) fn post_wasi_self_contained() -> LazyCrtObjects {
131-
MaybeLazy::lazy(|| new(&[]))
136+
new(&[])
132137
}

0 commit comments

Comments
 (0)