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;
13 pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Vec<NativeLibrary> {
14 let mut collector = Collector {
18 tcx.hir().krate().visit_all_item_likes(&mut collector);
19 collector.process_command_line();
23 pub fn relevant_lib(sess: &Session, lib: &NativeLibrary) -> bool {
25 Some(ref cfg) => attr::cfg_matches(cfg, &sess.parse_sess, None),
30 struct Collector<'a, 'tcx: 'a> {
31 tcx: TyCtxt<'a, 'tcx, 'tcx>,
32 libs: Vec<NativeLibrary>,
35 impl<'a, 'tcx> ItemLikeVisitor<'tcx> for Collector<'a, 'tcx> {
36 fn visit_item(&mut self, it: &'tcx hir::Item) {
37 let fm = match it.node {
38 hir::ItemKind::ForeignMod(ref fm) => fm,
42 if fm.abi == Abi::Rust ||
43 fm.abi == Abi::RustIntrinsic ||
44 fm.abi == Abi::PlatformIntrinsic {
48 // Process all of the #[link(..)]-style arguments
49 for m in it.attrs.iter().filter(|a| a.check_name("link")) {
50 let items = match m.meta_item_list() {
54 let mut lib = NativeLibrary {
56 kind: cstore::NativeUnknown,
58 foreign_module: Some(self.tcx.hir().local_def_id(it.id)),
59 wasm_import_module: None,
61 let mut kind_specified = false;
63 for item in items.iter() {
64 if item.check_name("kind") {
65 kind_specified = true;
66 let kind = match item.value_str() {
68 None => continue, // skip like historical compilers
70 lib.kind = match &kind.as_str()[..] {
71 "static" => cstore::NativeStatic,
72 "static-nobundle" => cstore::NativeStaticNobundle,
73 "dylib" => cstore::NativeUnknown,
74 "framework" => cstore::NativeFramework,
76 struct_span_err!(self.tcx.sess, m.span, E0458,
77 "unknown kind: `{}`", k)
78 .span_label(item.span, "unknown kind").emit();
82 } else if item.check_name("name") {
83 lib.name = item.value_str();
84 } else if item.check_name("cfg") {
85 let cfg = match item.meta_item_list() {
87 None => continue, // skip like historical compilers
90 self.tcx.sess.span_err(
92 "`cfg()` must have an argument",
94 } else if let cfg @ Some(..) = cfg[0].meta_item() {
95 lib.cfg = cfg.cloned();
97 self.tcx.sess.span_err(cfg[0].span(), "invalid argument for `cfg(..)`");
99 } else if item.check_name("wasm_import_module") {
100 match item.value_str() {
101 Some(s) => lib.wasm_import_module = Some(s),
103 let msg = "must be of the form #[link(wasm_import_module = \"...\")]";
104 self.tcx.sess.span_err(item.span(), msg);
108 // currently, like past compilers, ignore unknown
113 // In general we require #[link(name = "...")] but we allow
114 // #[link(wasm_import_module = "...")] without the `name`.
115 let requires_name = kind_specified || lib.wasm_import_module.is_none();
116 if lib.name.is_none() && requires_name {
117 struct_span_err!(self.tcx.sess, m.span, E0459,
118 "#[link(...)] specified without \
120 .span_label(m.span, "missing `name` argument")
123 self.register_native_lib(Some(m.span), lib);
127 fn visit_trait_item(&mut self, _it: &'tcx hir::TraitItem) {}
128 fn visit_impl_item(&mut self, _it: &'tcx hir::ImplItem) {}
131 impl<'a, 'tcx> Collector<'a, 'tcx> {
132 fn register_native_lib(&mut self, span: Option<Span>, lib: NativeLibrary) {
133 if lib.name.as_ref().map(|s| s.as_str().is_empty()).unwrap_or(false) {
136 struct_span_err!(self.tcx.sess, span, E0454,
137 "#[link(name = \"\")] given with empty name")
138 .span_label(span, "empty name given")
142 self.tcx.sess.err("empty library name given via `-l`");
147 let is_osx = self.tcx.sess.target.target.options.is_like_osx;
148 if lib.kind == cstore::NativeFramework && !is_osx {
149 let msg = "native frameworks are only available on macOS targets";
151 Some(span) => span_err!(self.tcx.sess, span, E0455, "{}", msg),
152 None => self.tcx.sess.err(msg),
155 if lib.cfg.is_some() && !self.tcx.features().link_cfg {
156 feature_gate::emit_feature_err(&self.tcx.sess.parse_sess,
162 if lib.kind == cstore::NativeStaticNobundle &&
163 !self.tcx.features().static_nobundle {
164 feature_gate::emit_feature_err(&self.tcx.sess.parse_sess,
168 "kind=\"static-nobundle\" is feature gated");
173 // Process libs passed on the command line
174 fn process_command_line(&mut self) {
175 // First, check for errors
176 let mut renames = FxHashSet::default();
177 for &(ref name, ref new_name, _) in &self.tcx.sess.opts.libs {
178 if let &Some(ref new_name) = new_name {
179 let any_duplicate = self.libs
181 .filter_map(|lib| lib.name.as_ref())
183 if new_name.is_empty() {
185 &format!("an empty renaming target was specified for library `{}`",name));
186 } else if !any_duplicate {
187 self.tcx.sess.err(&format!("renaming of the library `{}` was specified, \
188 however this crate contains no #[link(...)] \
189 attributes referencing this library.", name));
190 } else if renames.contains(name) {
191 self.tcx.sess.err(&format!("multiple renamings were \
192 specified for library `{}` .",
195 renames.insert(name);
200 // Update kind and, optionally, the name of all native libraries
201 // (there may be more than one) with the specified name.
202 for &(ref name, ref new_name, kind) in &self.tcx.sess.opts.libs {
203 let mut found = false;
204 for lib in self.libs.iter_mut() {
205 let lib_name = match lib.name {
209 if lib_name == name as &str {
210 let mut changed = false;
211 if let Some(k) = kind {
215 if let &Some(ref new_name) = new_name {
216 lib.name = Some(Symbol::intern(new_name));
220 let msg = format!("redundant linker flag specified for \
221 library `{}`", name);
222 self.tcx.sess.warn(&msg);
230 let new_name = new_name.as_ref().map(|s| &**s); // &Option<String> -> Option<&str>
231 let lib = NativeLibrary {
232 name: Some(Symbol::intern(new_name.unwrap_or(name))),
233 kind: if let Some(k) = kind { k } else { cstore::NativeUnknown },
235 foreign_module: None,
236 wasm_import_module: None,
238 self.register_native_lib(None, lib);