1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use rustc::hir::itemlikevisit::ItemLikeVisitor;
use rustc::hir;
use rustc::middle::cstore::{self, NativeLibrary};
use rustc::session::Session;
use rustc::ty::TyCtxt;
use rustc::util::nodemap::FxHashSet;
use rustc_target::spec::abi::Abi;
use syntax::attr;
use syntax::codemap::Span;
use syntax::feature_gate::{self, GateIssue};
use syntax::symbol::Symbol;
pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Vec<NativeLibrary> {
let mut collector = Collector {
tcx,
libs: Vec::new(),
};
tcx.hir.krate().visit_all_item_likes(&mut collector);
collector.process_command_line();
return collector.libs
}
pub fn relevant_lib(sess: &Session, lib: &NativeLibrary) -> bool {
match lib.cfg {
Some(ref cfg) => attr::cfg_matches(cfg, &sess.parse_sess, None),
None => true,
}
}
struct Collector<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
libs: Vec<NativeLibrary>,
}
impl<'a, 'tcx> ItemLikeVisitor<'tcx> for Collector<'a, 'tcx> {
fn visit_item(&mut self, it: &'tcx hir::Item) {
let fm = match it.node {
hir::ItemForeignMod(ref fm) => fm,
_ => return,
};
if fm.abi == Abi::Rust ||
fm.abi == Abi::RustIntrinsic ||
fm.abi == Abi::PlatformIntrinsic {
return
}
for m in it.attrs.iter().filter(|a| a.check_name("link")) {
let items = match m.meta_item_list() {
Some(item) => item,
None => continue,
};
let kind = items.iter().find(|k| {
k.check_name("kind")
}).and_then(|a| a.value_str()).map(Symbol::as_str);
let kind = match kind.as_ref().map(|s| &s[..]) {
Some("static") => cstore::NativeStatic,
Some("static-nobundle") => cstore::NativeStaticNobundle,
Some("dylib") => cstore::NativeUnknown,
Some("framework") => cstore::NativeFramework,
Some(k) => {
struct_span_err!(self.tcx.sess, m.span, E0458,
"unknown kind: `{}`", k)
.span_label(m.span, "unknown kind").emit();
cstore::NativeUnknown
}
None => cstore::NativeUnknown
};
let n = items.iter().find(|n| {
n.check_name("name")
}).and_then(|a| a.value_str());
let n = match n {
Some(n) => n,
None => {
struct_span_err!(self.tcx.sess, m.span, E0459,
"#[link(...)] specified without `name = \"foo\"`")
.span_label(m.span, "missing `name` argument").emit();
Symbol::intern("foo")
}
};
let cfg = items.iter().find(|k| {
k.check_name("cfg")
}).and_then(|a| a.meta_item_list());
let cfg = if let Some(list) = cfg {
if list.is_empty() {
self.tcx.sess.span_err(m.span(), "`cfg()` must have an argument");
return;
} else if let cfg @ Some(..) = list[0].meta_item() {
cfg.cloned()
} else {
self.tcx.sess.span_err(list[0].span(), "invalid argument for `cfg(..)`");
return;
}
} else {
None
};
let lib = NativeLibrary {
name: n,
kind,
cfg,
foreign_module: Some(self.tcx.hir.local_def_id(it.id)),
};
self.register_native_lib(Some(m.span), lib);
}
}
fn visit_trait_item(&mut self, _it: &'tcx hir::TraitItem) {}
fn visit_impl_item(&mut self, _it: &'tcx hir::ImplItem) {}
}
impl<'a, 'tcx> Collector<'a, 'tcx> {
fn register_native_lib(&mut self, span: Option<Span>, lib: NativeLibrary) {
if lib.name.as_str().is_empty() {
match span {
Some(span) => {
struct_span_err!(self.tcx.sess, span, E0454,
"#[link(name = \"\")] given with empty name")
.span_label(span, "empty name given")
.emit();
}
None => {
self.tcx.sess.err("empty library name given via `-l`");
}
}
return
}
let is_osx = self.tcx.sess.target.target.options.is_like_osx;
if lib.kind == cstore::NativeFramework && !is_osx {
let msg = "native frameworks are only available on macOS targets";
match span {
Some(span) => span_err!(self.tcx.sess, span, E0455, "{}", msg),
None => self.tcx.sess.err(msg),
}
}
if lib.cfg.is_some() && !self.tcx.features().link_cfg {
feature_gate::emit_feature_err(&self.tcx.sess.parse_sess,
"link_cfg",
span.unwrap(),
GateIssue::Language,
"is feature gated");
}
if lib.kind == cstore::NativeStaticNobundle &&
!self.tcx.features().static_nobundle {
feature_gate::emit_feature_err(&self.tcx.sess.parse_sess,
"static_nobundle",
span.unwrap(),
GateIssue::Language,
"kind=\"static-nobundle\" is feature gated");
}
self.libs.push(lib);
}
fn process_command_line(&mut self) {
let mut renames = FxHashSet();
for &(ref name, ref new_name, _) in &self.tcx.sess.opts.libs {
if let &Some(ref new_name) = new_name {
if new_name.is_empty() {
self.tcx.sess.err(
&format!("an empty renaming target was specified for library `{}`",name));
} else if !self.libs.iter().any(|lib| lib.name == name as &str) {
self.tcx.sess.err(&format!("renaming of the library `{}` was specified, \
however this crate contains no #[link(...)] \
attributes referencing this library.", name));
} else if renames.contains(name) {
self.tcx.sess.err(&format!("multiple renamings were \
specified for library `{}` .",
name));
} else {
renames.insert(name);
}
}
}
for &(ref name, ref new_name, kind) in &self.tcx.sess.opts.libs {
let mut found = false;
for lib in self.libs.iter_mut() {
if lib.name == name as &str {
let mut changed = false;
if let Some(k) = kind {
lib.kind = k;
changed = true;
}
if let &Some(ref new_name) = new_name {
lib.name = Symbol::intern(new_name);
changed = true;
}
if !changed {
let msg = format!("redundant linker flag specified for \
library `{}`", name);
self.tcx.sess.warn(&msg);
}
found = true;
}
}
if !found {
let new_name = new_name.as_ref().map(|s| &**s);
let lib = NativeLibrary {
name: Symbol::intern(new_name.unwrap_or(name)),
kind: if let Some(k) = kind { k } else { cstore::NativeUnknown },
cfg: None,
foreign_module: None,
};
self.register_native_lib(None, lib);
}
}
}
}