1 use rustc::hir::itemlikevisit::ItemLikeVisitor;
3 use rustc::middle::cstore::{self, NativeLibrary};
4 use rustc::session::Session;
6 use rustc::util::nodemap::FxHashSet;
7 use rustc_target::spec::abi::Abi;
9 use syntax::source_map::Span;
10 use syntax::feature_gate::{self, GateIssue};
11 use syntax::symbol::Symbol;
12 use syntax::{span_err, struct_span_err};
14 pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Vec<NativeLibrary> {
15 let mut collector = Collector {
19 tcx.hir().krate().visit_all_item_likes(&mut collector);
20 collector.process_command_line();
24 pub fn relevant_lib(sess: &Session, lib: &NativeLibrary) -> bool {
26 Some(ref cfg) => attr::cfg_matches(cfg, &sess.parse_sess, None),
31 struct Collector<'a, 'tcx: 'a> {
32 tcx: TyCtxt<'a, 'tcx, 'tcx>,
33 libs: Vec<NativeLibrary>,
36 impl<'a, 'tcx> ItemLikeVisitor<'tcx> for Collector<'a, 'tcx> {
37 fn visit_item(&mut self, it: &'tcx hir::Item) {
38 let fm = match it.node {
39 hir::ItemKind::ForeignMod(ref fm) => fm,
43 if fm.abi == Abi::Rust ||
44 fm.abi == Abi::RustIntrinsic ||
45 fm.abi == Abi::PlatformIntrinsic {
49 // Process all of the #[link(..)]-style arguments
50 for m in it.attrs.iter().filter(|a| a.check_name("link")) {
51 let items = match m.meta_item_list() {
55 let mut lib = NativeLibrary {
57 kind: cstore::NativeUnknown,
59 foreign_module: Some(self.tcx.hir().local_def_id(it.id)),
60 wasm_import_module: None,
62 let mut kind_specified = false;
64 for item in items.iter() {
65 if item.check_name("kind") {
66 kind_specified = true;
67 let kind = match item.value_str() {
69 None => continue, // skip like historical compilers
71 lib.kind = match &kind.as_str()[..] {
72 "static" => cstore::NativeStatic,
73 "static-nobundle" => cstore::NativeStaticNobundle,
74 "dylib" => cstore::NativeUnknown,
75 "framework" => cstore::NativeFramework,
77 struct_span_err!(self.tcx.sess, m.span, E0458,
78 "unknown kind: `{}`", k)
79 .span_label(item.span, "unknown kind").emit();
83 } else if item.check_name("name") {
84 lib.name = item.value_str();
85 } else if item.check_name("cfg") {
86 let cfg = match item.meta_item_list() {
88 None => continue, // skip like historical compilers
91 self.tcx.sess.span_err(
93 "`cfg()` must have an argument",
95 } else if let cfg @ Some(..) = cfg[0].meta_item() {
96 lib.cfg = cfg.cloned();
98 self.tcx.sess.span_err(cfg[0].span(), "invalid argument for `cfg(..)`");
100 } else if item.check_name("wasm_import_module") {
101 match item.value_str() {
102 Some(s) => lib.wasm_import_module = Some(s),
104 let msg = "must be of the form #[link(wasm_import_module = \"...\")]";
105 self.tcx.sess.span_err(item.span(), msg);
109 // currently, like past compilers, ignore unknown
114 // In general we require #[link(name = "...")] but we allow
115 // #[link(wasm_import_module = "...")] without the `name`.
116 let requires_name = kind_specified || lib.wasm_import_module.is_none();
117 if lib.name.is_none() && requires_name {
118 struct_span_err!(self.tcx.sess, m.span, E0459,
119 "#[link(...)] specified without \
121 .span_label(m.span, "missing `name` argument")
124 self.register_native_lib(Some(m.span), lib);
128 fn visit_trait_item(&mut self, _it: &'tcx hir::TraitItem) {}
129 fn visit_impl_item(&mut self, _it: &'tcx hir::ImplItem) {}
132 impl<'a, 'tcx> Collector<'a, 'tcx> {
133 fn register_native_lib(&mut self, span: Option<Span>, lib: NativeLibrary) {
134 if lib.name.as_ref().map(|s| s.as_str().is_empty()).unwrap_or(false) {
137 struct_span_err!(self.tcx.sess, span, E0454,
138 "#[link(name = \"\")] given with empty name")
139 .span_label(span, "empty name given")
143 self.tcx.sess.err("empty library name given via `-l`");
148 let is_osx = self.tcx.sess.target.target.options.is_like_osx;
149 if lib.kind == cstore::NativeFramework && !is_osx {
150 let msg = "native frameworks are only available on macOS targets";
152 Some(span) => span_err!(self.tcx.sess, span, E0455, "{}", msg),
153 None => self.tcx.sess.err(msg),
156 if lib.cfg.is_some() && !self.tcx.features().link_cfg {
157 feature_gate::emit_feature_err(&self.tcx.sess.parse_sess,
163 if lib.kind == cstore::NativeStaticNobundle &&
164 !self.tcx.features().static_nobundle {
165 feature_gate::emit_feature_err(&self.tcx.sess.parse_sess,
167 span.unwrap_or_else(|| syntax_pos::DUMMY_SP),
169 "kind=\"static-nobundle\" is feature gated");
174 // Process libs passed on the command line
175 fn process_command_line(&mut self) {
176 // First, check for errors
177 let mut renames = FxHashSet::default();
178 for &(ref name, ref new_name, _) in &self.tcx.sess.opts.libs {
179 if let &Some(ref new_name) = new_name {
180 let any_duplicate = self.libs
182 .filter_map(|lib| lib.name.as_ref())
184 if new_name.is_empty() {
186 &format!("an empty renaming target was specified for library `{}`",name));
187 } else if !any_duplicate {
188 self.tcx.sess.err(&format!("renaming of the library `{}` was specified, \
189 however this crate contains no #[link(...)] \
190 attributes referencing this library.", name));
191 } else if renames.contains(name) {
192 self.tcx.sess.err(&format!("multiple renamings were \
193 specified for library `{}` .",
196 renames.insert(name);
201 // Update kind and, optionally, the name of all native libraries
202 // (there may be more than one) with the specified name.
203 for &(ref name, ref new_name, kind) in &self.tcx.sess.opts.libs {
204 let mut found = false;
205 for lib in self.libs.iter_mut() {
206 let lib_name = match lib.name {
210 if lib_name == name as &str {
211 let mut changed = false;
212 if let Some(k) = kind {
216 if let &Some(ref new_name) = new_name {
217 lib.name = Some(Symbol::intern(new_name));
221 let msg = format!("redundant linker flag specified for \
222 library `{}`", name);
223 self.tcx.sess.warn(&msg);
231 let new_name = new_name.as_ref().map(|s| &**s); // &Option<String> -> Option<&str>
232 let lib = NativeLibrary {
233 name: Some(Symbol::intern(new_name.unwrap_or(name))),
234 kind: if let Some(k) = kind { k } else { cstore::NativeUnknown },
236 foreign_module: None,
237 wasm_import_module: None,
239 self.register_native_lib(None, lib);