2017-08-30 16:48:57 -05:00
|
|
|
use rustc::hir;
|
2019-12-22 16:42:04 -06:00
|
|
|
use rustc::hir::itemlikevisit::ItemLikeVisitor;
|
2017-08-30 16:48:57 -05:00
|
|
|
use rustc::middle::cstore::{self, NativeLibrary};
|
|
|
|
use rustc::session::Session;
|
|
|
|
use rustc::ty::TyCtxt;
|
|
|
|
use rustc::util::nodemap::FxHashSet;
|
2020-01-01 12:25:28 -06:00
|
|
|
use rustc_span::source_map::Span;
|
2020-01-01 12:30:57 -06:00
|
|
|
use rustc_span::symbol::{kw, sym, Symbol};
|
2018-04-25 11:30:39 -05:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2017-08-30 16:48:57 -05:00
|
|
|
use syntax::attr;
|
2019-11-30 00:40:28 -06:00
|
|
|
use syntax::feature_gate::feature_err;
|
2019-02-08 05:50:17 -06:00
|
|
|
use syntax::{span_err, struct_span_err};
|
2017-08-30 16:48:57 -05:00
|
|
|
|
2019-11-11 15:46:56 -06:00
|
|
|
use rustc_error_codes::*;
|
|
|
|
|
2019-10-06 06:30:46 -05:00
|
|
|
crate fn collect(tcx: TyCtxt<'_>) -> Vec<NativeLibrary> {
|
2019-12-22 16:42:04 -06:00
|
|
|
let mut collector = Collector { tcx, libs: Vec::new() };
|
2018-12-04 06:45:36 -06:00
|
|
|
tcx.hir().krate().visit_all_item_likes(&mut collector);
|
2017-08-30 16:48:57 -05:00
|
|
|
collector.process_command_line();
|
2019-06-11 16:11:55 -05:00
|
|
|
return collector.libs;
|
2017-08-30 16:48:57 -05:00
|
|
|
}
|
|
|
|
|
2019-10-06 06:30:46 -05:00
|
|
|
crate fn relevant_lib(sess: &Session, lib: &NativeLibrary) -> bool {
|
2017-08-30 16:48:57 -05:00
|
|
|
match lib.cfg {
|
|
|
|
Some(ref cfg) => attr::cfg_matches(cfg, &sess.parse_sess, None),
|
|
|
|
None => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 14:03:44 -05:00
|
|
|
struct Collector<'tcx> {
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2017-08-30 16:48:57 -05:00
|
|
|
libs: Vec<NativeLibrary>,
|
|
|
|
}
|
|
|
|
|
2019-06-11 14:03:44 -05:00
|
|
|
impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
|
2019-11-28 12:28:50 -06:00
|
|
|
fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
|
2019-09-26 11:51:36 -05:00
|
|
|
let fm = match it.kind {
|
2018-07-11 10:36:06 -05:00
|
|
|
hir::ItemKind::ForeignMod(ref fm) => fm,
|
2017-08-30 16:48:57 -05:00
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
if fm.abi == Abi::Rust || fm.abi == Abi::RustIntrinsic || fm.abi == Abi::PlatformIntrinsic {
|
|
|
|
return;
|
2017-08-30 16:48:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process all of the #[link(..)]-style arguments
|
2019-05-07 22:21:18 -05:00
|
|
|
for m in it.attrs.iter().filter(|a| a.check_name(sym::link)) {
|
2017-08-30 16:48:57 -05:00
|
|
|
let items = match m.meta_item_list() {
|
|
|
|
Some(item) => item,
|
|
|
|
None => continue,
|
|
|
|
};
|
2018-07-16 13:31:14 -05:00
|
|
|
let mut lib = NativeLibrary {
|
|
|
|
name: None,
|
|
|
|
kind: cstore::NativeUnknown,
|
|
|
|
cfg: None,
|
2019-06-27 04:28:14 -05:00
|
|
|
foreign_module: Some(self.tcx.hir().local_def_id(it.hir_id)),
|
2018-07-16 13:31:14 -05:00
|
|
|
wasm_import_module: None,
|
2017-08-30 16:48:57 -05:00
|
|
|
};
|
2018-07-16 13:31:14 -05:00
|
|
|
let mut kind_specified = false;
|
|
|
|
|
|
|
|
for item in items.iter() {
|
2019-05-07 22:21:18 -05:00
|
|
|
if item.check_name(sym::kind) {
|
2018-07-16 13:31:14 -05:00
|
|
|
kind_specified = true;
|
|
|
|
let kind = match item.value_str() {
|
|
|
|
Some(name) => name,
|
|
|
|
None => continue, // skip like historical compilers
|
|
|
|
};
|
2019-10-21 19:04:25 -05:00
|
|
|
lib.kind = match &*kind.as_str() {
|
2018-07-16 13:31:14 -05:00
|
|
|
"static" => cstore::NativeStatic,
|
|
|
|
"static-nobundle" => cstore::NativeStaticNobundle,
|
|
|
|
"dylib" => cstore::NativeUnknown,
|
|
|
|
"framework" => cstore::NativeFramework,
|
2019-08-27 09:42:44 -05:00
|
|
|
"raw-dylib" => cstore::NativeRawDylib,
|
2018-07-16 13:31:14 -05:00
|
|
|
k => {
|
2019-12-22 16:42:04 -06:00
|
|
|
struct_span_err!(
|
|
|
|
self.tcx.sess,
|
|
|
|
item.span(),
|
|
|
|
E0458,
|
|
|
|
"unknown kind: `{}`",
|
|
|
|
k
|
|
|
|
)
|
|
|
|
.span_label(item.span(), "unknown kind")
|
|
|
|
.span_label(m.span, "")
|
|
|
|
.emit();
|
2018-07-16 13:31:14 -05:00
|
|
|
cstore::NativeUnknown
|
|
|
|
}
|
|
|
|
};
|
2019-05-07 22:21:18 -05:00
|
|
|
} else if item.check_name(sym::name) {
|
2018-07-16 13:31:14 -05:00
|
|
|
lib.name = item.value_str();
|
2019-05-07 22:21:18 -05:00
|
|
|
} else if item.check_name(sym::cfg) {
|
2018-07-16 13:31:14 -05:00
|
|
|
let cfg = match item.meta_item_list() {
|
|
|
|
Some(list) => list,
|
|
|
|
None => continue, // skip like historical compilers
|
|
|
|
};
|
|
|
|
if cfg.is_empty() {
|
2019-12-22 16:42:04 -06:00
|
|
|
self.tcx.sess.span_err(item.span(), "`cfg()` must have an argument");
|
2018-07-16 13:31:14 -05:00
|
|
|
} else if let cfg @ Some(..) = cfg[0].meta_item() {
|
|
|
|
lib.cfg = cfg.cloned();
|
|
|
|
} else {
|
|
|
|
self.tcx.sess.span_err(cfg[0].span(), "invalid argument for `cfg(..)`");
|
|
|
|
}
|
2019-05-07 22:21:18 -05:00
|
|
|
} else if item.check_name(sym::wasm_import_module) {
|
2018-07-16 13:31:14 -05:00
|
|
|
match item.value_str() {
|
|
|
|
Some(s) => lib.wasm_import_module = Some(s),
|
|
|
|
None => {
|
2019-07-19 13:08:21 -05:00
|
|
|
let msg = "must be of the form `#[link(wasm_import_module = \"...\")]`";
|
2018-07-16 13:31:14 -05:00
|
|
|
self.tcx.sess.span_err(item.span(), msg);
|
|
|
|
}
|
|
|
|
}
|
2018-01-11 20:41:33 -06:00
|
|
|
} else {
|
2018-07-16 13:31:14 -05:00
|
|
|
// currently, like past compilers, ignore unknown
|
|
|
|
// directives here.
|
2018-01-11 20:41:33 -06:00
|
|
|
}
|
2018-07-16 13:31:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// In general we require #[link(name = "...")] but we allow
|
|
|
|
// #[link(wasm_import_module = "...")] without the `name`.
|
|
|
|
let requires_name = kind_specified || lib.wasm_import_module.is_none();
|
|
|
|
if lib.name.is_none() && requires_name {
|
2019-12-22 16:42:04 -06:00
|
|
|
struct_span_err!(
|
|
|
|
self.tcx.sess,
|
|
|
|
m.span,
|
|
|
|
E0459,
|
|
|
|
"`#[link(...)]` specified without \
|
|
|
|
`name = \"foo\"`"
|
|
|
|
)
|
|
|
|
.span_label(m.span, "missing `name` argument")
|
|
|
|
.emit();
|
2018-07-16 13:31:14 -05:00
|
|
|
}
|
2017-08-30 16:48:57 -05:00
|
|
|
self.register_native_lib(Some(m.span), lib);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 14:47:10 -06:00
|
|
|
fn visit_trait_item(&mut self, _it: &'tcx hir::TraitItem<'tcx>) {}
|
2019-11-28 15:16:44 -06:00
|
|
|
fn visit_impl_item(&mut self, _it: &'tcx hir::ImplItem<'tcx>) {}
|
2017-08-30 16:48:57 -05:00
|
|
|
}
|
|
|
|
|
2019-06-11 14:03:44 -05:00
|
|
|
impl Collector<'tcx> {
|
2017-08-30 16:48:57 -05:00
|
|
|
fn register_native_lib(&mut self, span: Option<Span>, lib: NativeLibrary) {
|
2019-09-04 20:54:01 -05:00
|
|
|
if lib.name.as_ref().map(|&s| s == kw::Invalid).unwrap_or(false) {
|
2017-08-30 16:48:57 -05:00
|
|
|
match span {
|
|
|
|
Some(span) => {
|
2019-12-22 16:42:04 -06:00
|
|
|
struct_span_err!(
|
|
|
|
self.tcx.sess,
|
|
|
|
span,
|
|
|
|
E0454,
|
|
|
|
"`#[link(name = \"\")]` given with empty name"
|
|
|
|
)
|
|
|
|
.span_label(span, "empty name given")
|
|
|
|
.emit();
|
2017-08-30 16:48:57 -05:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
self.tcx.sess.err("empty library name given via `-l`");
|
|
|
|
}
|
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
return;
|
2017-08-30 16:48:57 -05:00
|
|
|
}
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
2018-02-14 09:11:02 -06:00
|
|
|
if lib.cfg.is_some() && !self.tcx.features().link_cfg {
|
2019-11-30 00:40:28 -06:00
|
|
|
feature_err(&self.tcx.sess.parse_sess, sym::link_cfg, span.unwrap(), "is unstable")
|
|
|
|
.emit();
|
2017-08-30 16:48:57 -05:00
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
if lib.kind == cstore::NativeStaticNobundle && !self.tcx.features().static_nobundle {
|
2019-11-30 00:40:28 -06:00
|
|
|
feature_err(
|
|
|
|
&self.tcx.sess.parse_sess,
|
|
|
|
sym::static_nobundle,
|
2019-12-31 11:15:40 -06:00
|
|
|
span.unwrap_or_else(|| rustc_span::DUMMY_SP),
|
2019-12-22 16:42:04 -06:00
|
|
|
"kind=\"static-nobundle\" is unstable",
|
2019-11-30 00:40:28 -06:00
|
|
|
)
|
|
|
|
.emit();
|
2017-08-30 16:48:57 -05:00
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
if lib.kind == cstore::NativeRawDylib && !self.tcx.features().raw_dylib {
|
2019-11-30 00:40:28 -06:00
|
|
|
feature_err(
|
|
|
|
&self.tcx.sess.parse_sess,
|
|
|
|
sym::raw_dylib,
|
2019-12-31 11:15:40 -06:00
|
|
|
span.unwrap_or_else(|| rustc_span::DUMMY_SP),
|
2019-12-22 16:42:04 -06:00
|
|
|
"kind=\"raw-dylib\" is unstable",
|
2019-11-30 00:40:28 -06:00
|
|
|
)
|
|
|
|
.emit();
|
2019-08-27 09:42:44 -05:00
|
|
|
}
|
2017-08-30 16:48:57 -05:00
|
|
|
self.libs.push(lib);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process libs passed on the command line
|
|
|
|
fn process_command_line(&mut self) {
|
|
|
|
// First, check for errors
|
2018-10-16 03:44:26 -05:00
|
|
|
let mut renames = FxHashSet::default();
|
2017-08-30 16:48:57 -05:00
|
|
|
for &(ref name, ref new_name, _) in &self.tcx.sess.opts.libs {
|
|
|
|
if let &Some(ref new_name) = new_name {
|
2019-12-22 16:42:04 -06:00
|
|
|
let any_duplicate = self
|
|
|
|
.libs
|
2018-07-16 13:31:14 -05:00
|
|
|
.iter()
|
|
|
|
.filter_map(|lib| lib.name.as_ref())
|
2019-05-07 01:03:44 -05:00
|
|
|
.any(|n| n.as_str() == *name);
|
2017-08-30 16:48:57 -05:00
|
|
|
if new_name.is_empty() {
|
2019-12-22 16:42:04 -06:00
|
|
|
self.tcx.sess.err(&format!(
|
|
|
|
"an empty renaming target was specified for library `{}`",
|
|
|
|
name
|
|
|
|
));
|
2018-07-16 13:31:14 -05:00
|
|
|
} else if !any_duplicate {
|
2019-12-22 16:42:04 -06:00
|
|
|
self.tcx.sess.err(&format!(
|
|
|
|
"renaming of the library `{}` was specified, \
|
2019-07-19 13:08:21 -05:00
|
|
|
however this crate contains no `#[link(...)]` \
|
2019-12-22 16:42:04 -06:00
|
|
|
attributes referencing this library.",
|
|
|
|
name
|
|
|
|
));
|
2019-10-15 23:48:20 -05:00
|
|
|
} else if !renames.insert(name) {
|
2019-12-22 16:42:04 -06:00
|
|
|
self.tcx.sess.err(&format!(
|
|
|
|
"multiple renamings were \
|
2017-08-30 16:48:57 -05:00
|
|
|
specified for library `{}` .",
|
2019-12-22 16:42:04 -06:00
|
|
|
name
|
|
|
|
));
|
2017-08-30 16:48:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-19 08:30:23 -05:00
|
|
|
// Update kind and, optionally, the name of all native libraries
|
Keep last redundant linker flag, not first
When a library (L1) is passed to the linker multiple times, this is
sometimes purposeful: there might be several other libraries in the
linker command (L2 and L3) that all depend on L1. You'd end up with a
(simplified) linker command that looks like:
-l2 -l1 -l3 -l1
With the previous behavior, when rustc encountered a redundant library,
it would keep the first instance, and remove the later ones, resulting
in:
-l2 -l1 -l3
This can cause a linker error, because on some platforms (e.g. Linux),
the linker will only include symbols from L1 that are needed *at the
point it's referenced in the command line*. So if L3 depends on
additional symbols from L1, which aren't needed by L2, the linker won't
know to include them, and you'll end up with "undefined symbols" errors.
A better behavior is to keep the *last* instance of the library:
-l2 -l3 -l1
This ensures that all "downstream" libraries have been included in the
linker command before the "upstream" library is referenced.
Fixes rust-lang#47989
2018-12-20 14:46:42 -06:00
|
|
|
// (there may be more than one) with the specified name. If any
|
|
|
|
// library is mentioned more than once, keep the latest mention
|
|
|
|
// of it, so that any possible dependent libraries appear before
|
|
|
|
// it. (This ensures that the linker is able to see symbols from
|
|
|
|
// all possible dependent libraries before linking in the library
|
|
|
|
// in question.)
|
2017-08-30 16:48:57 -05:00
|
|
|
for &(ref name, ref new_name, kind) in &self.tcx.sess.opts.libs {
|
Keep last redundant linker flag, not first
When a library (L1) is passed to the linker multiple times, this is
sometimes purposeful: there might be several other libraries in the
linker command (L2 and L3) that all depend on L1. You'd end up with a
(simplified) linker command that looks like:
-l2 -l1 -l3 -l1
With the previous behavior, when rustc encountered a redundant library,
it would keep the first instance, and remove the later ones, resulting
in:
-l2 -l1 -l3
This can cause a linker error, because on some platforms (e.g. Linux),
the linker will only include symbols from L1 that are needed *at the
point it's referenced in the command line*. So if L3 depends on
additional symbols from L1, which aren't needed by L2, the linker won't
know to include them, and you'll end up with "undefined symbols" errors.
A better behavior is to keep the *last* instance of the library:
-l2 -l3 -l1
This ensures that all "downstream" libraries have been included in the
linker command before the "upstream" library is referenced.
Fixes rust-lang#47989
2018-12-20 14:46:42 -06:00
|
|
|
// If we've already added any native libraries with the same
|
2018-12-20 15:19:55 -06:00
|
|
|
// name, they will be pulled out into `existing`, so that we
|
|
|
|
// can move them to the end of the list below.
|
2019-12-22 16:42:04 -06:00
|
|
|
let mut existing = self
|
|
|
|
.libs
|
|
|
|
.drain_filter(|lib| {
|
|
|
|
if let Some(lib_name) = lib.name {
|
|
|
|
if lib_name.as_str() == *name {
|
|
|
|
if let Some(k) = kind {
|
|
|
|
lib.kind = k;
|
|
|
|
}
|
|
|
|
if let &Some(ref new_name) = new_name {
|
|
|
|
lib.name = Some(Symbol::intern(new_name));
|
|
|
|
}
|
|
|
|
return true;
|
Keep last redundant linker flag, not first
When a library (L1) is passed to the linker multiple times, this is
sometimes purposeful: there might be several other libraries in the
linker command (L2 and L3) that all depend on L1. You'd end up with a
(simplified) linker command that looks like:
-l2 -l1 -l3 -l1
With the previous behavior, when rustc encountered a redundant library,
it would keep the first instance, and remove the later ones, resulting
in:
-l2 -l1 -l3
This can cause a linker error, because on some platforms (e.g. Linux),
the linker will only include symbols from L1 that are needed *at the
point it's referenced in the command line*. So if L3 depends on
additional symbols from L1, which aren't needed by L2, the linker won't
know to include them, and you'll end up with "undefined symbols" errors.
A better behavior is to keep the *last* instance of the library:
-l2 -l3 -l1
This ensures that all "downstream" libraries have been included in the
linker command before the "upstream" library is referenced.
Fixes rust-lang#47989
2018-12-20 14:46:42 -06:00
|
|
|
}
|
2017-08-30 16:48:57 -05:00
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
false
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
Keep last redundant linker flag, not first
When a library (L1) is passed to the linker multiple times, this is
sometimes purposeful: there might be several other libraries in the
linker command (L2 and L3) that all depend on L1. You'd end up with a
(simplified) linker command that looks like:
-l2 -l1 -l3 -l1
With the previous behavior, when rustc encountered a redundant library,
it would keep the first instance, and remove the later ones, resulting
in:
-l2 -l1 -l3
This can cause a linker error, because on some platforms (e.g. Linux),
the linker will only include symbols from L1 that are needed *at the
point it's referenced in the command line*. So if L3 depends on
additional symbols from L1, which aren't needed by L2, the linker won't
know to include them, and you'll end up with "undefined symbols" errors.
A better behavior is to keep the *last* instance of the library:
-l2 -l3 -l1
This ensures that all "downstream" libraries have been included in the
linker command before the "upstream" library is referenced.
Fixes rust-lang#47989
2018-12-20 14:46:42 -06:00
|
|
|
if existing.is_empty() {
|
2017-08-30 16:48:57 -05:00
|
|
|
// Add if not found
|
|
|
|
let new_name = new_name.as_ref().map(|s| &**s); // &Option<String> -> Option<&str>
|
|
|
|
let lib = NativeLibrary {
|
2018-07-16 13:31:14 -05:00
|
|
|
name: Some(Symbol::intern(new_name.unwrap_or(name))),
|
2017-08-30 16:48:57 -05:00
|
|
|
kind: if let Some(k) = kind { k } else { cstore::NativeUnknown },
|
|
|
|
cfg: None,
|
2018-02-10 16:28:17 -06:00
|
|
|
foreign_module: None,
|
2018-07-16 13:31:14 -05:00
|
|
|
wasm_import_module: None,
|
2017-08-30 16:48:57 -05:00
|
|
|
};
|
|
|
|
self.register_native_lib(None, lib);
|
Keep last redundant linker flag, not first
When a library (L1) is passed to the linker multiple times, this is
sometimes purposeful: there might be several other libraries in the
linker command (L2 and L3) that all depend on L1. You'd end up with a
(simplified) linker command that looks like:
-l2 -l1 -l3 -l1
With the previous behavior, when rustc encountered a redundant library,
it would keep the first instance, and remove the later ones, resulting
in:
-l2 -l1 -l3
This can cause a linker error, because on some platforms (e.g. Linux),
the linker will only include symbols from L1 that are needed *at the
point it's referenced in the command line*. So if L3 depends on
additional symbols from L1, which aren't needed by L2, the linker won't
know to include them, and you'll end up with "undefined symbols" errors.
A better behavior is to keep the *last* instance of the library:
-l2 -l3 -l1
This ensures that all "downstream" libraries have been included in the
linker command before the "upstream" library is referenced.
Fixes rust-lang#47989
2018-12-20 14:46:42 -06:00
|
|
|
} else {
|
|
|
|
// Move all existing libraries with the same name to the
|
|
|
|
// end of the command line.
|
|
|
|
self.libs.append(&mut existing);
|
2017-08-30 16:48:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|