]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_metadata/src/native_libs.rs
Merge tag 'debian/1.52.1+dfsg1-1_exp2' into proxmox/buster
[rustc.git] / compiler / rustc_metadata / src / native_libs.rs
CommitLineData
74b04a01 1use rustc_attr as attr;
dfeec247
XL
2use rustc_data_structures::fx::FxHashSet;
3use rustc_errors::struct_span_err;
4use rustc_hir as hir;
5use rustc_hir::itemlikevisit::ItemLikeVisitor;
f9f354fc 6use rustc_middle::middle::cstore::NativeLib;
ba9703b0
XL
7use rustc_middle::ty::TyCtxt;
8use rustc_session::parse::feature_err;
f9f354fc 9use rustc_session::utils::NativeLibKind;
ba9703b0 10use rustc_session::Session;
dfeec247
XL
11use rustc_span::source_map::Span;
12use rustc_span::symbol::{kw, sym, Symbol};
83c7162d 13use rustc_target::spec::abi::Abi;
60c5eb7d 14
f9f354fc 15crate fn collect(tcx: TyCtxt<'_>) -> Vec<NativeLib> {
dfeec247 16 let mut collector = Collector { tcx, libs: Vec::new() };
0731742a 17 tcx.hir().krate().visit_all_item_likes(&mut collector);
ea8adc8c 18 collector.process_command_line();
ba9703b0 19 collector.libs
ea8adc8c
XL
20}
21
f9f354fc 22crate fn relevant_lib(sess: &Session, lib: &NativeLib) -> bool {
ea8adc8c
XL
23 match lib.cfg {
24 Some(ref cfg) => attr::cfg_matches(cfg, &sess.parse_sess, None),
25 None => true,
26 }
27}
28
dc9dc135
XL
29struct Collector<'tcx> {
30 tcx: TyCtxt<'tcx>,
f9f354fc 31 libs: Vec<NativeLib>,
ea8adc8c
XL
32}
33
dc9dc135 34impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
dfeec247 35 fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
fc512014
XL
36 let abi = match it.kind {
37 hir::ItemKind::ForeignMod { abi, .. } => abi,
ea8adc8c
XL
38 _ => return,
39 };
40
fc512014 41 if abi == Abi::Rust || abi == Abi::RustIntrinsic || abi == Abi::PlatformIntrinsic {
dfeec247 42 return;
ea8adc8c
XL
43 }
44
45 // Process all of the #[link(..)]-style arguments
3dfed10e 46 let sess = &self.tcx.sess;
6a06907d
XL
47 for m in self.tcx.hir().attrs(it.hir_id()).iter().filter(|a| sess.check_name(a, sym::link))
48 {
ea8adc8c
XL
49 let items = match m.meta_item_list() {
50 Some(item) => item,
51 None => continue,
52 };
f9f354fc 53 let mut lib = NativeLib {
8faf50e0 54 name: None,
f9f354fc 55 kind: NativeLibKind::Unspecified,
8faf50e0 56 cfg: None,
6a06907d 57 foreign_module: Some(it.def_id.to_def_id()),
8faf50e0 58 wasm_import_module: None,
ea8adc8c 59 };
8faf50e0
XL
60 let mut kind_specified = false;
61
62 for item in items.iter() {
3dfed10e 63 if item.has_name(sym::kind) {
8faf50e0
XL
64 kind_specified = true;
65 let kind = match item.value_str() {
66 Some(name) => name,
67 None => continue, // skip like historical compilers
68 };
60c5eb7d 69 lib.kind = match &*kind.as_str() {
f9f354fc
XL
70 "static" => NativeLibKind::StaticBundle,
71 "static-nobundle" => NativeLibKind::StaticNoBundle,
72 "dylib" => NativeLibKind::Dylib,
73 "framework" => NativeLibKind::Framework,
74 "raw-dylib" => NativeLibKind::RawDylib,
8faf50e0 75 k => {
3dfed10e
XL
76 struct_span_err!(sess, item.span(), E0458, "unknown kind: `{}`", k)
77 .span_label(item.span(), "unknown kind")
78 .span_label(m.span, "")
79 .emit();
f9f354fc 80 NativeLibKind::Unspecified
8faf50e0
XL
81 }
82 };
3dfed10e 83 } else if item.has_name(sym::name) {
8faf50e0 84 lib.name = item.value_str();
3dfed10e 85 } else if item.has_name(sym::cfg) {
8faf50e0
XL
86 let cfg = match item.meta_item_list() {
87 Some(list) => list,
88 None => continue, // skip like historical compilers
89 };
90 if cfg.is_empty() {
3dfed10e 91 sess.span_err(item.span(), "`cfg()` must have an argument");
8faf50e0
XL
92 } else if let cfg @ Some(..) = cfg[0].meta_item() {
93 lib.cfg = cfg.cloned();
94 } else {
3dfed10e 95 sess.span_err(cfg[0].span(), "invalid argument for `cfg(..)`");
8faf50e0 96 }
3dfed10e 97 } else if item.has_name(sym::wasm_import_module) {
8faf50e0
XL
98 match item.value_str() {
99 Some(s) => lib.wasm_import_module = Some(s),
100 None => {
416331ca 101 let msg = "must be of the form `#[link(wasm_import_module = \"...\")]`";
3dfed10e 102 sess.span_err(item.span(), msg);
8faf50e0
XL
103 }
104 }
2c00a5a8 105 } else {
8faf50e0
XL
106 // currently, like past compilers, ignore unknown
107 // directives here.
2c00a5a8 108 }
8faf50e0
XL
109 }
110
111 // In general we require #[link(name = "...")] but we allow
112 // #[link(wasm_import_module = "...")] without the `name`.
113 let requires_name = kind_specified || lib.wasm_import_module.is_none();
114 if lib.name.is_none() && requires_name {
dfeec247 115 struct_span_err!(
3dfed10e 116 sess,
dfeec247
XL
117 m.span,
118 E0459,
119 "`#[link(...)]` specified without \
120 `name = \"foo\"`"
121 )
122 .span_label(m.span, "missing `name` argument")
123 .emit();
8faf50e0 124 }
ea8adc8c
XL
125 self.register_native_lib(Some(m.span), lib);
126 }
127 }
128
dfeec247
XL
129 fn visit_trait_item(&mut self, _it: &'tcx hir::TraitItem<'tcx>) {}
130 fn visit_impl_item(&mut self, _it: &'tcx hir::ImplItem<'tcx>) {}
fc512014 131 fn visit_foreign_item(&mut self, _it: &'tcx hir::ForeignItem<'tcx>) {}
ea8adc8c
XL
132}
133
dc9dc135 134impl Collector<'tcx> {
f9f354fc 135 fn register_native_lib(&mut self, span: Option<Span>, lib: NativeLib) {
5869c6ff 136 if lib.name.as_ref().map_or(false, |&s| s == kw::Empty) {
ea8adc8c
XL
137 match span {
138 Some(span) => {
dfeec247
XL
139 struct_span_err!(
140 self.tcx.sess,
141 span,
142 E0454,
143 "`#[link(name = \"\")]` given with empty name"
144 )
145 .span_label(span, "empty name given")
146 .emit();
ea8adc8c
XL
147 }
148 None => {
149 self.tcx.sess.err("empty library name given via `-l`");
150 }
151 }
dfeec247 152 return;
ea8adc8c 153 }
29967ef6 154 let is_osx = self.tcx.sess.target.is_like_osx;
f9f354fc 155 if lib.kind == NativeLibKind::Framework && !is_osx {
ea8adc8c
XL
156 let msg = "native frameworks are only available on macOS targets";
157 match span {
dfeec247 158 Some(span) => struct_span_err!(self.tcx.sess, span, E0455, "{}", msg).emit(),
ea8adc8c
XL
159 None => self.tcx.sess.err(msg),
160 }
161 }
0531ce1d 162 if lib.cfg.is_some() && !self.tcx.features().link_cfg {
ba9703b0
XL
163 feature_err(
164 &self.tcx.sess.parse_sess,
165 sym::link_cfg,
166 span.unwrap(),
167 "kind=\"link_cfg\" is unstable",
168 )
169 .emit();
ea8adc8c 170 }
f9f354fc 171 if lib.kind == NativeLibKind::StaticNoBundle && !self.tcx.features().static_nobundle {
60c5eb7d
XL
172 feature_err(
173 &self.tcx.sess.parse_sess,
174 sym::static_nobundle,
1b1a35ee 175 span.unwrap_or(rustc_span::DUMMY_SP),
dfeec247 176 "kind=\"static-nobundle\" is unstable",
60c5eb7d
XL
177 )
178 .emit();
ea8adc8c 179 }
f9f354fc 180 if lib.kind == NativeLibKind::RawDylib && !self.tcx.features().raw_dylib {
60c5eb7d
XL
181 feature_err(
182 &self.tcx.sess.parse_sess,
183 sym::raw_dylib,
1b1a35ee 184 span.unwrap_or(rustc_span::DUMMY_SP),
dfeec247 185 "kind=\"raw-dylib\" is unstable",
60c5eb7d
XL
186 )
187 .emit();
e74abb32 188 }
ea8adc8c
XL
189 self.libs.push(lib);
190 }
191
192 // Process libs passed on the command line
193 fn process_command_line(&mut self) {
194 // First, check for errors
0bf4aa26 195 let mut renames = FxHashSet::default();
5869c6ff
XL
196 for (name, new_name, _) in &self.tcx.sess.opts.libs {
197 if let Some(ref new_name) = new_name {
dfeec247
XL
198 let any_duplicate = self
199 .libs
8faf50e0
XL
200 .iter()
201 .filter_map(|lib| lib.name.as_ref())
5869c6ff 202 .any(|n| &n.as_str() == name);
ea8adc8c 203 if new_name.is_empty() {
dfeec247
XL
204 self.tcx.sess.err(&format!(
205 "an empty renaming target was specified for library `{}`",
206 name
207 ));
8faf50e0 208 } else if !any_duplicate {
dfeec247
XL
209 self.tcx.sess.err(&format!(
210 "renaming of the library `{}` was specified, \
416331ca 211 however this crate contains no `#[link(...)]` \
dfeec247
XL
212 attributes referencing this library.",
213 name
214 ));
e74abb32 215 } else if !renames.insert(name) {
dfeec247
XL
216 self.tcx.sess.err(&format!(
217 "multiple renamings were \
ea8adc8c 218 specified for library `{}` .",
dfeec247
XL
219 name
220 ));
ea8adc8c
XL
221 }
222 }
223 }
224
b7449926 225 // Update kind and, optionally, the name of all native libraries
532ac7d7
XL
226 // (there may be more than one) with the specified name. If any
227 // library is mentioned more than once, keep the latest mention
228 // of it, so that any possible dependent libraries appear before
229 // it. (This ensures that the linker is able to see symbols from
230 // all possible dependent libraries before linking in the library
231 // in question.)
ea8adc8c 232 for &(ref name, ref new_name, kind) in &self.tcx.sess.opts.libs {
532ac7d7
XL
233 // If we've already added any native libraries with the same
234 // name, they will be pulled out into `existing`, so that we
235 // can move them to the end of the list below.
dfeec247
XL
236 let mut existing = self
237 .libs
238 .drain_filter(|lib| {
239 if let Some(lib_name) = lib.name {
240 if lib_name.as_str() == *name {
f9f354fc
XL
241 if kind != NativeLibKind::Unspecified {
242 lib.kind = kind;
dfeec247 243 }
5869c6ff 244 if let Some(new_name) = new_name {
dfeec247
XL
245 lib.name = Some(Symbol::intern(new_name));
246 }
247 return true;
532ac7d7 248 }
ea8adc8c 249 }
dfeec247
XL
250 false
251 })
252 .collect::<Vec<_>>();
532ac7d7 253 if existing.is_empty() {
ea8adc8c
XL
254 // Add if not found
255 let new_name = new_name.as_ref().map(|s| &**s); // &Option<String> -> Option<&str>
f9f354fc 256 let lib = NativeLib {
8faf50e0 257 name: Some(Symbol::intern(new_name.unwrap_or(name))),
f9f354fc 258 kind,
ea8adc8c 259 cfg: None,
0531ce1d 260 foreign_module: None,
8faf50e0 261 wasm_import_module: None,
ea8adc8c
XL
262 };
263 self.register_native_lib(None, lib);
532ac7d7
XL
264 } else {
265 // Move all existing libraries with the same name to the
266 // end of the command line.
267 self.libs.append(&mut existing);
ea8adc8c
XL
268 }
269 }
270 }
271}