1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use rustc::hir::itemlikevisit::ItemLikeVisitor;
13 use rustc::middle::cstore::{self, NativeLibrary};
14 use rustc::session::Session;
15 use rustc::ty::TyCtxt;
16 use rustc::util::nodemap::FxHashSet;
19 use syntax::codemap::Span;
20 use syntax::feature_gate::{self, GateIssue};
21 use syntax::symbol::Symbol;
23 pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Vec<NativeLibrary> {
24 let mut collector = Collector {
28 tcx.hir.krate().visit_all_item_likes(&mut collector);
29 collector.process_command_line();
33 pub fn relevant_lib(sess: &Session, lib: &NativeLibrary) -> bool {
35 Some(ref cfg) => attr::cfg_matches(cfg, &sess.parse_sess, None),
40 struct Collector<'a, 'tcx: 'a> {
41 tcx: TyCtxt<'a, 'tcx, 'tcx>,
42 libs: Vec<NativeLibrary>,
45 impl<'a, 'tcx> ItemLikeVisitor<'tcx> for Collector<'a, 'tcx> {
46 fn visit_item(&mut self, it: &'tcx hir::Item) {
47 let fm = match it.node {
48 hir::ItemForeignMod(ref fm) => fm,
52 if fm.abi == Abi::Rust ||
53 fm.abi == Abi::RustIntrinsic ||
54 fm.abi == Abi::PlatformIntrinsic {
58 // Process all of the #[link(..)]-style arguments
59 for m in it.attrs.iter().filter(|a| a.check_name("link")) {
60 let items = match m.meta_item_list() {
64 let kind = items.iter().find(|k| {
66 }).and_then(|a| a.value_str()).map(Symbol::as_str);
67 let kind = match kind.as_ref().map(|s| &s[..]) {
68 Some("static") => cstore::NativeStatic,
69 Some("static-nobundle") => cstore::NativeStaticNobundle,
70 Some("dylib") => cstore::NativeUnknown,
71 Some("framework") => cstore::NativeFramework,
73 struct_span_err!(self.tcx.sess, m.span, E0458,
74 "unknown kind: `{}`", k)
75 .span_label(m.span, "unknown kind").emit();
78 None => cstore::NativeUnknown
80 let n = items.iter().find(|n| {
82 }).and_then(|a| a.value_str());
86 struct_span_err!(self.tcx.sess, m.span, E0459,
87 "#[link(...)] specified without `name = \"foo\"`")
88 .span_label(m.span, "missing `name` argument").emit();
92 let cfg = items.iter().find(|k| {
94 }).and_then(|a| a.meta_item_list());
95 let cfg = cfg.map(|list| {
96 list[0].meta_item().unwrap().clone()
98 let foreign_items = fm.items.iter()
99 .map(|it| self.tcx.hir.local_def_id(it.id))
101 let lib = NativeLibrary {
107 self.register_native_lib(Some(m.span), lib);
111 fn visit_trait_item(&mut self, _it: &'tcx hir::TraitItem) {}
112 fn visit_impl_item(&mut self, _it: &'tcx hir::ImplItem) {}
115 impl<'a, 'tcx> Collector<'a, 'tcx> {
116 fn register_native_lib(&mut self, span: Option<Span>, lib: NativeLibrary) {
117 if lib.name.as_str().is_empty() {
120 struct_span_err!(self.tcx.sess, span, E0454,
121 "#[link(name = \"\")] given with empty name")
122 .span_label(span, "empty name given")
126 self.tcx.sess.err("empty library name given via `-l`");
131 let is_osx = self.tcx.sess.target.target.options.is_like_osx;
132 if lib.kind == cstore::NativeFramework && !is_osx {
133 let msg = "native frameworks are only available on macOS targets";
135 Some(span) => span_err!(self.tcx.sess, span, E0455, "{}", msg),
136 None => self.tcx.sess.err(msg),
139 if lib.cfg.is_some() && !self.tcx.sess.features.borrow().link_cfg {
140 feature_gate::emit_feature_err(&self.tcx.sess.parse_sess,
146 if lib.kind == cstore::NativeStaticNobundle &&
147 !self.tcx.sess.features.borrow().static_nobundle {
148 feature_gate::emit_feature_err(&self.tcx.sess.parse_sess,
152 "kind=\"static-nobundle\" is feature gated");
157 // Process libs passed on the command line
158 fn process_command_line(&mut self) {
159 // First, check for errors
160 let mut renames = FxHashSet();
161 for &(ref name, ref new_name, _) in &self.tcx.sess.opts.libs {
162 if let &Some(ref new_name) = new_name {
163 if new_name.is_empty() {
165 &format!("an empty renaming target was specified for library `{}`",name));
166 } else if !self.libs.iter().any(|lib| lib.name == name as &str) {
167 self.tcx.sess.err(&format!("renaming of the library `{}` was specified, \
168 however this crate contains no #[link(...)] \
169 attributes referencing this library.", name));
170 } else if renames.contains(name) {
171 self.tcx.sess.err(&format!("multiple renamings were \
172 specified for library `{}` .",
175 renames.insert(name);
180 // Update kind and, optionally, the name of all native libaries
181 // (there may be more than one) with the specified name.
182 for &(ref name, ref new_name, kind) in &self.tcx.sess.opts.libs {
183 let mut found = false;
184 for lib in self.libs.iter_mut() {
185 if lib.name == name as &str {
186 let mut changed = false;
187 if let Some(k) = kind {
191 if let &Some(ref new_name) = new_name {
192 lib.name = Symbol::intern(new_name);
196 let msg = format!("redundant linker flag specified for \
197 library `{}`", name);
198 self.tcx.sess.warn(&msg);
206 let new_name = new_name.as_ref().map(|s| &**s); // &Option<String> -> Option<&str>
207 let lib = NativeLibrary {
208 name: Symbol::intern(new_name.unwrap_or(name)),
209 kind: if let Some(k) = kind { k } else { cstore::NativeUnknown },
211 foreign_items: Vec::new(),
213 self.register_native_lib(None, lib);