1 use rustc_attr
as attr
;
2 use rustc_data_structures
::fx
::FxHashSet
;
3 use rustc_errors
::struct_span_err
;
5 use rustc_hir
::itemlikevisit
::ItemLikeVisitor
;
6 use rustc_middle
::middle
::cstore
::NativeLib
;
7 use rustc_middle
::ty
::TyCtxt
;
8 use rustc_session
::parse
::feature_err
;
9 use rustc_session
::utils
::NativeLibKind
;
10 use rustc_session
::Session
;
11 use rustc_span
::source_map
::Span
;
12 use rustc_span
::symbol
::{kw, sym, Symbol}
;
13 use rustc_target
::spec
::abi
::Abi
;
15 crate fn collect(tcx
: TyCtxt
<'_
>) -> Vec
<NativeLib
> {
16 let mut collector
= Collector { tcx, libs: Vec::new() }
;
17 tcx
.hir().krate().visit_all_item_likes(&mut collector
);
18 collector
.process_command_line();
22 crate fn relevant_lib(sess
: &Session
, lib
: &NativeLib
) -> bool
{
24 Some(ref cfg
) => attr
::cfg_matches(cfg
, &sess
.parse_sess
, None
),
29 struct Collector
<'tcx
> {
34 impl ItemLikeVisitor
<'tcx
> for Collector
<'tcx
> {
35 fn visit_item(&mut self, it
: &'tcx hir
::Item
<'tcx
>) {
36 let abi
= match it
.kind
{
37 hir
::ItemKind
::ForeignMod { abi, .. }
=> abi
,
41 if abi
== Abi
::Rust
|| abi
== Abi
::RustIntrinsic
|| abi
== Abi
::PlatformIntrinsic
{
45 // Process all of the #[link(..)]-style arguments
46 let sess
= &self.tcx
.sess
;
47 for m
in self.tcx
.hir().attrs(it
.hir_id()).iter().filter(|a
| sess
.check_name(a
, sym
::link
))
49 let items
= match m
.meta_item_list() {
53 let mut lib
= NativeLib
{
55 kind
: NativeLibKind
::Unspecified
,
57 foreign_module
: Some(it
.def_id
.to_def_id()),
58 wasm_import_module
: None
,
60 let mut kind_specified
= false;
62 for item
in items
.iter() {
63 if item
.has_name(sym
::kind
) {
64 kind_specified
= true;
65 let kind
= match item
.value_str() {
67 None
=> continue, // skip like historical compilers
69 lib
.kind
= match &*kind
.as_str() {
70 "static" => NativeLibKind
::StaticBundle
,
71 "static-nobundle" => NativeLibKind
::StaticNoBundle
,
72 "dylib" => NativeLibKind
::Dylib
,
73 "framework" => NativeLibKind
::Framework
,
74 "raw-dylib" => NativeLibKind
::RawDylib
,
76 struct_span_err
!(sess
, item
.span(), E0458
, "unknown kind: `{}`", k
)
77 .span_label(item
.span(), "unknown kind")
78 .span_label(m
.span
, "")
80 NativeLibKind
::Unspecified
83 } else if item
.has_name(sym
::name
) {
84 lib
.name
= item
.value_str();
85 } else if item
.has_name(sym
::cfg
) {
86 let cfg
= match item
.meta_item_list() {
88 None
=> continue, // skip like historical compilers
91 sess
.span_err(item
.span(), "`cfg()` must have an argument");
92 } else if let cfg @
Some(..) = cfg
[0].meta_item() {
93 lib
.cfg
= cfg
.cloned();
95 sess
.span_err(cfg
[0].span(), "invalid argument for `cfg(..)`");
97 } else if item
.has_name(sym
::wasm_import_module
) {
98 match item
.value_str() {
99 Some(s
) => lib
.wasm_import_module
= Some(s
),
101 let msg
= "must be of the form `#[link(wasm_import_module = \"...\")]`";
102 sess
.span_err(item
.span(), msg
);
106 // currently, like past compilers, ignore unknown
111 // In general we require #[link(name = "...")] but we allow
112 // #[link(wasm_import_module = "...")] without the `name`.
113 let requires_name
= kind_specified
|| lib
.wasm_import_module
.is_none();
114 if lib
.name
.is_none() && requires_name
{
119 "`#[link(...)]` specified without \
122 .span_label(m
.span
, "missing `name` argument")
125 self.register_native_lib(Some(m
.span
), lib
);
129 fn visit_trait_item(&mut self, _it
: &'tcx hir
::TraitItem
<'tcx
>) {}
130 fn visit_impl_item(&mut self, _it
: &'tcx hir
::ImplItem
<'tcx
>) {}
131 fn visit_foreign_item(&mut self, _it
: &'tcx hir
::ForeignItem
<'tcx
>) {}
134 impl Collector
<'tcx
> {
135 fn register_native_lib(&mut self, span
: Option
<Span
>, lib
: NativeLib
) {
136 if lib
.name
.as_ref().map_or(false, |&s
| s
== kw
::Empty
) {
143 "`#[link(name = \"\")]` given with empty name"
145 .span_label(span
, "empty name given")
149 self.tcx
.sess
.err("empty library name given via `-l`");
154 let is_osx
= self.tcx
.sess
.target
.is_like_osx
;
155 if lib
.kind
== NativeLibKind
::Framework
&& !is_osx
{
156 let msg
= "native frameworks are only available on macOS targets";
158 Some(span
) => struct_span_err
!(self.tcx
.sess
, span
, E0455
, "{}", msg
).emit(),
159 None
=> self.tcx
.sess
.err(msg
),
162 if lib
.cfg
.is_some() && !self.tcx
.features().link_cfg
{
164 &self.tcx
.sess
.parse_sess
,
167 "kind=\"link_cfg\" is unstable",
171 if lib
.kind
== NativeLibKind
::StaticNoBundle
&& !self.tcx
.features().static_nobundle
{
173 &self.tcx
.sess
.parse_sess
,
174 sym
::static_nobundle
,
175 span
.unwrap_or(rustc_span
::DUMMY_SP
),
176 "kind=\"static-nobundle\" is unstable",
180 if lib
.kind
== NativeLibKind
::RawDylib
&& !self.tcx
.features().raw_dylib
{
182 &self.tcx
.sess
.parse_sess
,
184 span
.unwrap_or(rustc_span
::DUMMY_SP
),
185 "kind=\"raw-dylib\" is unstable",
192 // Process libs passed on the command line
193 fn process_command_line(&mut self) {
194 // First, check for errors
195 let mut renames
= FxHashSet
::default();
196 for (name
, new_name
, _
) in &self.tcx
.sess
.opts
.libs
{
197 if let Some(ref new_name
) = new_name
{
198 let any_duplicate
= self
201 .filter_map(|lib
| lib
.name
.as_ref())
202 .any(|n
| &n
.as_str() == name
);
203 if new_name
.is_empty() {
204 self.tcx
.sess
.err(&format
!(
205 "an empty renaming target was specified for library `{}`",
208 } else if !any_duplicate
{
209 self.tcx
.sess
.err(&format
!(
210 "renaming of the library `{}` was specified, \
211 however this crate contains no `#[link(...)]` \
212 attributes referencing this library.",
215 } else if !renames
.insert(name
) {
216 self.tcx
.sess
.err(&format
!(
217 "multiple renamings were \
218 specified for library `{}` .",
225 // Update kind and, optionally, the name of all native libraries
226 // (there may be more than one) with the specified name. If any
227 // library is mentioned more than once, keep the latest mention
228 // of it, so that any possible dependent libraries appear before
229 // it. (This ensures that the linker is able to see symbols from
230 // all possible dependent libraries before linking in the library
232 for &(ref name
, ref new_name
, kind
) in &self.tcx
.sess
.opts
.libs
{
233 // If we've already added any native libraries with the same
234 // name, they will be pulled out into `existing`, so that we
235 // can move them to the end of the list below.
236 let mut existing
= self
238 .drain_filter(|lib
| {
239 if let Some(lib_name
) = lib
.name
{
240 if lib_name
.as_str() == *name
{
241 if kind
!= NativeLibKind
::Unspecified
{
244 if let Some(new_name
) = new_name
{
245 lib
.name
= Some(Symbol
::intern(new_name
));
252 .collect
::<Vec
<_
>>();
253 if existing
.is_empty() {
255 let new_name
= new_name
.as_ref().map(|s
| &**s
); // &Option<String> -> Option<&str>
256 let lib
= NativeLib
{
257 name
: Some(Symbol
::intern(new_name
.unwrap_or(name
))),
260 foreign_module
: None
,
261 wasm_import_module
: None
,
263 self.register_native_lib(None
, lib
);
265 // Move all existing libraries with the same name to the
266 // end of the command line.
267 self.libs
.append(&mut existing
);