]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_metadata/src/dependency_format.rs
Rollup merge of #106823 - m-ou-se:format-args-as-str-guarantees, r=dtolnay
[rust.git] / compiler / rustc_metadata / src / dependency_format.rs
1 //! Resolution of mixing rlibs and dylibs
2 //!
3 //! When producing a final artifact, such as a dynamic library, the compiler has
4 //! a choice between linking an rlib or linking a dylib of all upstream
5 //! dependencies. The linking phase must guarantee, however, that a library only
6 //! show up once in the object file. For example, it is illegal for library A to
7 //! be statically linked to B and C in separate dylibs, and then link B and C
8 //! into a crate D (because library A appears twice).
9 //!
10 //! The job of this module is to calculate what format each upstream crate
11 //! should be used when linking each output type requested in this session. This
12 //! generally follows this set of rules:
13 //!
14 //! 1. Each library must appear exactly once in the output.
15 //! 2. Each rlib contains only one library (it's just an object file)
16 //! 3. Each dylib can contain more than one library (due to static linking),
17 //!    and can also bring in many dynamic dependencies.
18 //!
19 //! With these constraints in mind, it's generally a very difficult problem to
20 //! find a solution that's not "all rlibs" or "all dylibs". I have suspicions
21 //! that NP-ness may come into the picture here...
22 //!
23 //! The current selection algorithm below looks mostly similar to:
24 //!
25 //! 1. If static linking is required, then require all upstream dependencies
26 //!    to be available as rlibs. If not, generate an error.
27 //! 2. If static linking is requested (generating an executable), then
28 //!    attempt to use all upstream dependencies as rlibs. If any are not
29 //!    found, bail out and continue to step 3.
30 //! 3. Static linking has failed, at least one library must be dynamically
31 //!    linked. Apply a heuristic by greedily maximizing the number of
32 //!    dynamically linked libraries.
33 //! 4. Each upstream dependency available as a dynamic library is
34 //!    registered. The dependencies all propagate, adding to a map. It is
35 //!    possible for a dylib to add a static library as a dependency, but it
36 //!    is illegal for two dylibs to add the same static library as a
37 //!    dependency. The same dylib can be added twice. Additionally, it is
38 //!    illegal to add a static dependency when it was previously found as a
39 //!    dylib (and vice versa)
40 //! 5. After all dynamic dependencies have been traversed, re-traverse the
41 //!    remaining dependencies and add them statically (if they haven't been
42 //!    added already).
43 //!
44 //! While not perfect, this algorithm should help support use-cases such as leaf
45 //! dependencies being static while the larger tree of inner dependencies are
46 //! all dynamic. This isn't currently very well battle tested, so it will likely
47 //! fall short in some use cases.
48 //!
49 //! Currently, there is no way to specify the preference of linkage with a
50 //! particular library (other than a global dynamic/static switch).
51 //! Additionally, the algorithm is geared towards finding *any* solution rather
52 //! than finding a number of solutions (there are normally quite a few).
53
54 use crate::creader::CStore;
55 use crate::errors::{
56     BadPanicStrategy, CrateDepMultiple, IncompatiblePanicInDropStrategy, LibRequired,
57     RequiredPanicStrategy, RlibRequired, RustcLibRequired, TwoPanicRuntimes,
58 };
59
60 use rustc_data_structures::fx::FxHashMap;
61 use rustc_hir::def_id::CrateNum;
62 use rustc_middle::middle::dependency_format::{Dependencies, DependencyList, Linkage};
63 use rustc_middle::ty::TyCtxt;
64 use rustc_session::config::CrateType;
65 use rustc_session::cstore::CrateDepKind;
66 use rustc_session::cstore::LinkagePreference::{self, RequireDynamic, RequireStatic};
67
68 pub(crate) fn calculate(tcx: TyCtxt<'_>) -> Dependencies {
69     tcx.sess
70         .crate_types()
71         .iter()
72         .map(|&ty| {
73             let linkage = calculate_type(tcx, ty);
74             verify_ok(tcx, &linkage);
75             (ty, linkage)
76         })
77         .collect::<Vec<_>>()
78 }
79
80 fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
81     let sess = &tcx.sess;
82
83     if !sess.opts.output_types.should_codegen() {
84         return Vec::new();
85     }
86
87     let preferred_linkage = match ty {
88         // Generating a dylib without `-C prefer-dynamic` means that we're going
89         // to try to eagerly statically link all dependencies. This is normally
90         // done for end-product dylibs, not intermediate products.
91         //
92         // Treat cdylibs similarly. If `-C prefer-dynamic` is set, the caller may
93         // be code-size conscious, but without it, it makes sense to statically
94         // link a cdylib.
95         CrateType::Dylib | CrateType::Cdylib if !sess.opts.cg.prefer_dynamic => Linkage::Static,
96         CrateType::Dylib | CrateType::Cdylib => Linkage::Dynamic,
97
98         // If the global prefer_dynamic switch is turned off, or the final
99         // executable will be statically linked, prefer static crate linkage.
100         CrateType::Executable if !sess.opts.cg.prefer_dynamic || sess.crt_static(Some(ty)) => {
101             Linkage::Static
102         }
103         CrateType::Executable => Linkage::Dynamic,
104
105         // proc-macro crates are mostly cdylibs, but we also need metadata.
106         CrateType::ProcMacro => Linkage::Static,
107
108         // No linkage happens with rlibs, we just needed the metadata (which we
109         // got long ago), so don't bother with anything.
110         CrateType::Rlib => Linkage::NotLinked,
111
112         // staticlibs must have all static dependencies.
113         CrateType::Staticlib => Linkage::Static,
114     };
115
116     if preferred_linkage == Linkage::NotLinked {
117         // If the crate is not linked, there are no link-time dependencies.
118         return Vec::new();
119     }
120
121     if preferred_linkage == Linkage::Static {
122         // Attempt static linkage first. For dylibs and executables, we may be
123         // able to retry below with dynamic linkage.
124         if let Some(v) = attempt_static(tcx) {
125             return v;
126         }
127
128         // Staticlibs and static executables must have all static dependencies.
129         // If any are not found, generate some nice pretty errors.
130         if ty == CrateType::Staticlib
131             || (ty == CrateType::Executable
132                 && sess.crt_static(Some(ty))
133                 && !sess.target.crt_static_allows_dylibs)
134         {
135             for &cnum in tcx.crates(()).iter() {
136                 if tcx.dep_kind(cnum).macros_only() {
137                     continue;
138                 }
139                 let src = tcx.used_crate_source(cnum);
140                 if src.rlib.is_some() {
141                     continue;
142                 }
143                 sess.emit_err(RlibRequired { crate_name: tcx.crate_name(cnum) });
144             }
145             return Vec::new();
146         }
147     }
148
149     let mut formats = FxHashMap::default();
150
151     // Sweep all crates for found dylibs. Add all dylibs, as well as their
152     // dependencies, ensuring there are no conflicts. The only valid case for a
153     // dependency to be relied upon twice is for both cases to rely on a dylib.
154     for &cnum in tcx.crates(()).iter() {
155         if tcx.dep_kind(cnum).macros_only() {
156             continue;
157         }
158         let name = tcx.crate_name(cnum);
159         let src = tcx.used_crate_source(cnum);
160         if src.dylib.is_some() {
161             info!("adding dylib: {}", name);
162             add_library(tcx, cnum, RequireDynamic, &mut formats);
163             let deps = tcx.dylib_dependency_formats(cnum);
164             for &(depnum, style) in deps.iter() {
165                 info!("adding {:?}: {}", style, tcx.crate_name(depnum));
166                 add_library(tcx, depnum, style, &mut formats);
167             }
168         }
169     }
170
171     // Collect what we've got so far in the return vector.
172     let last_crate = tcx.crates(()).len();
173     let mut ret = (1..last_crate + 1)
174         .map(|cnum| match formats.get(&CrateNum::new(cnum)) {
175             Some(&RequireDynamic) => Linkage::Dynamic,
176             Some(&RequireStatic) => Linkage::IncludedFromDylib,
177             None => Linkage::NotLinked,
178         })
179         .collect::<Vec<_>>();
180
181     // Run through the dependency list again, and add any missing libraries as
182     // static libraries.
183     //
184     // If the crate hasn't been included yet and it's not actually required
185     // (e.g., it's an allocator) then we skip it here as well.
186     for &cnum in tcx.crates(()).iter() {
187         let src = tcx.used_crate_source(cnum);
188         if src.dylib.is_none()
189             && !formats.contains_key(&cnum)
190             && tcx.dep_kind(cnum) == CrateDepKind::Explicit
191         {
192             assert!(src.rlib.is_some() || src.rmeta.is_some());
193             info!("adding staticlib: {}", tcx.crate_name(cnum));
194             add_library(tcx, cnum, RequireStatic, &mut formats);
195             ret[cnum.as_usize() - 1] = Linkage::Static;
196         }
197     }
198
199     // We've gotten this far because we're emitting some form of a final
200     // artifact which means that we may need to inject dependencies of some
201     // form.
202     //
203     // Things like allocators and panic runtimes may not have been activated
204     // quite yet, so do so here.
205     activate_injected_dep(CStore::from_tcx(tcx).injected_panic_runtime(), &mut ret, &|cnum| {
206         tcx.is_panic_runtime(cnum)
207     });
208
209     // When dylib B links to dylib A, then when using B we must also link to A.
210     // It could be the case, however, that the rlib for A is present (hence we
211     // found metadata), but the dylib for A has since been removed.
212     //
213     // For situations like this, we perform one last pass over the dependencies,
214     // making sure that everything is available in the requested format.
215     for (cnum, kind) in ret.iter().enumerate() {
216         let cnum = CrateNum::new(cnum + 1);
217         let src = tcx.used_crate_source(cnum);
218         match *kind {
219             Linkage::NotLinked | Linkage::IncludedFromDylib => {}
220             Linkage::Static if src.rlib.is_some() => continue,
221             Linkage::Dynamic if src.dylib.is_some() => continue,
222             kind => {
223                 let kind = match kind {
224                     Linkage::Static => "rlib",
225                     _ => "dylib",
226                 };
227                 let crate_name = tcx.crate_name(cnum);
228                 if crate_name.as_str().starts_with("rustc_") {
229                     sess.emit_err(RustcLibRequired { crate_name, kind });
230                 } else {
231                     sess.emit_err(LibRequired { crate_name, kind });
232                 }
233             }
234         }
235     }
236
237     ret
238 }
239
240 fn add_library(
241     tcx: TyCtxt<'_>,
242     cnum: CrateNum,
243     link: LinkagePreference,
244     m: &mut FxHashMap<CrateNum, LinkagePreference>,
245 ) {
246     match m.get(&cnum) {
247         Some(&link2) => {
248             // If the linkages differ, then we'd have two copies of the library
249             // if we continued linking. If the linkages are both static, then we
250             // would also have two copies of the library (static from two
251             // different locations).
252             //
253             // This error is probably a little obscure, but I imagine that it
254             // can be refined over time.
255             if link2 != link || link == RequireStatic {
256                 tcx.sess.emit_err(CrateDepMultiple { crate_name: tcx.crate_name(cnum) });
257             }
258         }
259         None => {
260             m.insert(cnum, link);
261         }
262     }
263 }
264
265 fn attempt_static(tcx: TyCtxt<'_>) -> Option<DependencyList> {
266     let all_crates_available_as_rlib = tcx
267         .crates(())
268         .iter()
269         .copied()
270         .filter_map(|cnum| {
271             if tcx.dep_kind(cnum).macros_only() {
272                 return None;
273             }
274             Some(tcx.used_crate_source(cnum).rlib.is_some())
275         })
276         .all(|is_rlib| is_rlib);
277     if !all_crates_available_as_rlib {
278         return None;
279     }
280
281     // All crates are available in an rlib format, so we're just going to link
282     // everything in explicitly so long as it's actually required.
283     let mut ret = tcx
284         .crates(())
285         .iter()
286         .map(|&cnum| {
287             if tcx.dep_kind(cnum) == CrateDepKind::Explicit {
288                 Linkage::Static
289             } else {
290                 Linkage::NotLinked
291             }
292         })
293         .collect::<Vec<_>>();
294
295     // Our allocator/panic runtime may not have been linked above if it wasn't
296     // explicitly linked, which is the case for any injected dependency. Handle
297     // that here and activate them.
298     activate_injected_dep(CStore::from_tcx(tcx).injected_panic_runtime(), &mut ret, &|cnum| {
299         tcx.is_panic_runtime(cnum)
300     });
301
302     Some(ret)
303 }
304
305 // Given a list of how to link upstream dependencies so far, ensure that an
306 // injected dependency is activated. This will not do anything if one was
307 // transitively included already (e.g., via a dylib or explicitly so).
308 //
309 // If an injected dependency was not found then we're guaranteed the
310 // metadata::creader module has injected that dependency (not listed as
311 // a required dependency) in one of the session's field. If this field is not
312 // set then this compilation doesn't actually need the dependency and we can
313 // also skip this step entirely.
314 fn activate_injected_dep(
315     injected: Option<CrateNum>,
316     list: &mut DependencyList,
317     replaces_injected: &dyn Fn(CrateNum) -> bool,
318 ) {
319     for (i, slot) in list.iter().enumerate() {
320         let cnum = CrateNum::new(i + 1);
321         if !replaces_injected(cnum) {
322             continue;
323         }
324         if *slot != Linkage::NotLinked {
325             return;
326         }
327     }
328     if let Some(injected) = injected {
329         let idx = injected.as_usize() - 1;
330         assert_eq!(list[idx], Linkage::NotLinked);
331         list[idx] = Linkage::Static;
332     }
333 }
334
335 // After the linkage for a crate has been determined we need to verify that
336 // there's only going to be one allocator in the output.
337 fn verify_ok(tcx: TyCtxt<'_>, list: &[Linkage]) {
338     let sess = &tcx.sess;
339     if list.is_empty() {
340         return;
341     }
342     let mut panic_runtime = None;
343     for (i, linkage) in list.iter().enumerate() {
344         if let Linkage::NotLinked = *linkage {
345             continue;
346         }
347         let cnum = CrateNum::new(i + 1);
348
349         if tcx.is_panic_runtime(cnum) {
350             if let Some((prev, _)) = panic_runtime {
351                 let prev_name = tcx.crate_name(prev);
352                 let cur_name = tcx.crate_name(cnum);
353                 sess.emit_err(TwoPanicRuntimes { prev_name, cur_name });
354             }
355             panic_runtime = Some((
356                 cnum,
357                 tcx.required_panic_strategy(cnum).unwrap_or_else(|| {
358                     bug!("cannot determine panic strategy of a panic runtime");
359                 }),
360             ));
361         }
362     }
363
364     // If we found a panic runtime, then we know by this point that it's the
365     // only one, but we perform validation here that all the panic strategy
366     // compilation modes for the whole DAG are valid.
367     if let Some((runtime_cnum, found_strategy)) = panic_runtime {
368         let desired_strategy = sess.panic_strategy();
369
370         // First up, validate that our selected panic runtime is indeed exactly
371         // our same strategy.
372         if found_strategy != desired_strategy {
373             sess.emit_err(BadPanicStrategy {
374                 runtime: tcx.crate_name(runtime_cnum),
375                 strategy: desired_strategy,
376             });
377         }
378
379         // Next up, verify that all other crates are compatible with this panic
380         // strategy. If the dep isn't linked, we ignore it, and if our strategy
381         // is abort then it's compatible with everything. Otherwise all crates'
382         // panic strategy must match our own.
383         for (i, linkage) in list.iter().enumerate() {
384             if let Linkage::NotLinked = *linkage {
385                 continue;
386             }
387             let cnum = CrateNum::new(i + 1);
388             if cnum == runtime_cnum || tcx.is_compiler_builtins(cnum) {
389                 continue;
390             }
391
392             if let Some(found_strategy) = tcx.required_panic_strategy(cnum) && desired_strategy != found_strategy {
393                 sess.emit_err(RequiredPanicStrategy {
394                     crate_name: tcx.crate_name(cnum),
395                     found_strategy,
396                     desired_strategy});
397             }
398
399             let found_drop_strategy = tcx.panic_in_drop_strategy(cnum);
400             if tcx.sess.opts.unstable_opts.panic_in_drop != found_drop_strategy {
401                 sess.emit_err(IncompatiblePanicInDropStrategy {
402                     crate_name: tcx.crate_name(cnum),
403                     found_strategy: found_drop_strategy,
404                     desired_strategy: tcx.sess.opts.unstable_opts.panic_in_drop,
405                 });
406             }
407         }
408     }
409 }