40
40
//! but not gcc's. As a result rustc cannot link with C++ static libraries (#36710)
41
41
//! when linking in self-contained mode.
42
42
43
- use crate :: spec:: LinkOutputKind ;
43
+ use crate :: spec:: { LinkOutputKind , MaybeLazy } ;
44
44
use std:: borrow:: Cow ;
45
45
use std:: collections:: BTreeMap ;
46
46
47
+ type LazyCrtObjectsArgs = & ' static [ ( LinkOutputKind , & ' static [ & ' static str ] ) ] ;
48
+ pub struct LazyCrtObjectsState ( LazyCrtObjectsArgs ) ;
49
+
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
+ }
55
+ }
56
+
47
57
pub type CrtObjects = BTreeMap < LinkOutputKind , Vec < Cow < ' static , str > > > ;
58
+ pub type LazyCrtObjects = MaybeLazy < CrtObjects , LazyCrtObjectsState > ;
48
59
49
- pub ( super ) fn new ( obj_table : & [ ( LinkOutputKind , & [ & ' static str ] ) ] ) -> CrtObjects {
50
- obj_table. iter ( ) . map ( |( z, k) | ( * z, k. iter ( ) . map ( |b| ( * b) . into ( ) ) . collect ( ) ) ) . collect ( )
60
+ #[ inline]
61
+ pub ( super ) fn new ( obj_table : LazyCrtObjectsArgs ) -> LazyCrtObjects {
62
+ MaybeLazy :: lazied ( LazyCrtObjectsState ( obj_table) )
51
63
}
52
64
53
- pub ( super ) fn all ( obj : & ' static str ) -> CrtObjects {
54
- new ( & [
55
- ( LinkOutputKind :: DynamicNoPicExe , & [ obj] ) ,
56
- ( LinkOutputKind :: DynamicPicExe , & [ obj] ) ,
57
- ( LinkOutputKind :: StaticNoPicExe , & [ obj] ) ,
58
- ( LinkOutputKind :: StaticPicExe , & [ obj] ) ,
59
- ( LinkOutputKind :: DynamicDylib , & [ obj] ) ,
60
- ( LinkOutputKind :: StaticDylib , & [ obj] ) ,
61
- ] )
65
+ macro_rules! all {
66
+ ( $obj: literal) => {
67
+ new( & [
68
+ ( LinkOutputKind :: DynamicNoPicExe , & [ $obj] ) ,
69
+ ( LinkOutputKind :: DynamicPicExe , & [ $obj] ) ,
70
+ ( LinkOutputKind :: StaticNoPicExe , & [ $obj] ) ,
71
+ ( LinkOutputKind :: StaticPicExe , & [ $obj] ) ,
72
+ ( LinkOutputKind :: DynamicDylib , & [ $obj] ) ,
73
+ ( LinkOutputKind :: StaticDylib , & [ $obj] ) ,
74
+ ] )
75
+ } ;
62
76
}
63
77
64
- pub ( super ) fn pre_musl_self_contained ( ) -> CrtObjects {
78
+ pub ( super ) fn pre_musl_self_contained ( ) -> LazyCrtObjects {
65
79
new ( & [
66
80
( LinkOutputKind :: DynamicNoPicExe , & [ "crt1.o" , "crti.o" , "crtbegin.o" ] ) ,
67
81
( LinkOutputKind :: DynamicPicExe , & [ "Scrt1.o" , "crti.o" , "crtbeginS.o" ] ) ,
@@ -72,7 +86,7 @@ pub(super) fn pre_musl_self_contained() -> CrtObjects {
72
86
] )
73
87
}
74
88
75
- pub ( super ) fn post_musl_self_contained ( ) -> CrtObjects {
89
+ pub ( super ) fn post_musl_self_contained ( ) -> LazyCrtObjects {
76
90
new ( & [
77
91
( LinkOutputKind :: DynamicNoPicExe , & [ "crtend.o" , "crtn.o" ] ) ,
78
92
( LinkOutputKind :: DynamicPicExe , & [ "crtendS.o" , "crtn.o" ] ) ,
@@ -83,7 +97,7 @@ pub(super) fn post_musl_self_contained() -> CrtObjects {
83
97
] )
84
98
}
85
99
86
- pub ( super ) fn pre_mingw_self_contained ( ) -> CrtObjects {
100
+ pub ( super ) fn pre_mingw_self_contained ( ) -> LazyCrtObjects {
87
101
new ( & [
88
102
( LinkOutputKind :: DynamicNoPicExe , & [ "crt2.o" , "rsbegin.o" ] ) ,
89
103
( LinkOutputKind :: DynamicPicExe , & [ "crt2.o" , "rsbegin.o" ] ) ,
@@ -94,19 +108,19 @@ pub(super) fn pre_mingw_self_contained() -> CrtObjects {
94
108
] )
95
109
}
96
110
97
- pub ( super ) fn post_mingw_self_contained ( ) -> CrtObjects {
98
- all ( "rsend.o" )
111
+ pub ( super ) fn post_mingw_self_contained ( ) -> LazyCrtObjects {
112
+ all ! ( "rsend.o" )
99
113
}
100
114
101
- pub ( super ) fn pre_mingw ( ) -> CrtObjects {
102
- all ( "rsbegin.o" )
115
+ pub ( super ) fn pre_mingw ( ) -> LazyCrtObjects {
116
+ all ! ( "rsbegin.o" )
103
117
}
104
118
105
- pub ( super ) fn post_mingw ( ) -> CrtObjects {
106
- all ( "rsend.o" )
119
+ pub ( super ) fn post_mingw ( ) -> LazyCrtObjects {
120
+ all ! ( "rsend.o" )
107
121
}
108
122
109
- pub ( super ) fn pre_wasi_self_contained ( ) -> CrtObjects {
123
+ pub ( super ) fn pre_wasi_self_contained ( ) -> LazyCrtObjects {
110
124
// Use crt1-command.o instead of crt1.o to enable support for new-style
111
125
// commands. See https://reviews.llvm.org/D81689 for more info.
112
126
new ( & [
@@ -118,6 +132,6 @@ pub(super) fn pre_wasi_self_contained() -> CrtObjects {
118
132
] )
119
133
}
120
134
121
- pub ( super ) fn post_wasi_self_contained ( ) -> CrtObjects {
135
+ pub ( super ) fn post_wasi_self_contained ( ) -> LazyCrtObjects {
122
136
new ( & [ ] )
123
137
}
0 commit comments