1 use rustc
::middle
::cstore
::{self, NativeLibrary}
;
2 use rustc
::session
::parse
::feature_err
;
3 use rustc
::session
::Session
;
5 use rustc_data_structures
::fx
::FxHashSet
;
6 use rustc_errors
::struct_span_err
;
8 use rustc_hir
::itemlikevisit
::ItemLikeVisitor
;
9 use rustc_span
::source_map
::Span
;
10 use rustc_span
::symbol
::{kw, sym, Symbol}
;
11 use rustc_target
::spec
::abi
::Abi
;
14 crate fn collect(tcx
: TyCtxt
<'_
>) -> Vec
<NativeLibrary
> {
15 let mut collector
= Collector { tcx, libs: Vec::new() }
;
16 tcx
.hir().krate().visit_all_item_likes(&mut collector
);
17 collector
.process_command_line();
18 return collector
.libs
;
21 crate fn relevant_lib(sess
: &Session
, lib
: &NativeLibrary
) -> bool
{
23 Some(ref cfg
) => attr
::cfg_matches(cfg
, &sess
.parse_sess
, None
),
28 struct Collector
<'tcx
> {
30 libs
: Vec
<NativeLibrary
>,
33 impl ItemLikeVisitor
<'tcx
> for Collector
<'tcx
> {
34 fn visit_item(&mut self, it
: &'tcx hir
::Item
<'tcx
>) {
35 let fm
= match it
.kind
{
36 hir
::ItemKind
::ForeignMod(ref fm
) => fm
,
40 if fm
.abi
== Abi
::Rust
|| fm
.abi
== Abi
::RustIntrinsic
|| fm
.abi
== Abi
::PlatformIntrinsic
{
44 // Process all of the #[link(..)]-style arguments
45 for m
in it
.attrs
.iter().filter(|a
| a
.check_name(sym
::link
)) {
46 let items
= match m
.meta_item_list() {
50 let mut lib
= NativeLibrary
{
52 kind
: cstore
::NativeUnknown
,
54 foreign_module
: Some(self.tcx
.hir().local_def_id(it
.hir_id
)),
55 wasm_import_module
: None
,
57 let mut kind_specified
= false;
59 for item
in items
.iter() {
60 if item
.check_name(sym
::kind
) {
61 kind_specified
= true;
62 let kind
= match item
.value_str() {
64 None
=> continue, // skip like historical compilers
66 lib
.kind
= match &*kind
.as_str() {
67 "static" => cstore
::NativeStatic
,
68 "static-nobundle" => cstore
::NativeStaticNobundle
,
69 "dylib" => cstore
::NativeUnknown
,
70 "framework" => cstore
::NativeFramework
,
71 "raw-dylib" => cstore
::NativeRawDylib
,
80 .span_label(item
.span(), "unknown kind")
81 .span_label(m
.span
, "")
86 } else if item
.check_name(sym
::name
) {
87 lib
.name
= item
.value_str();
88 } else if item
.check_name(sym
::cfg
) {
89 let cfg
= match item
.meta_item_list() {
91 None
=> continue, // skip like historical compilers
94 self.tcx
.sess
.span_err(item
.span(), "`cfg()` must have an argument");
95 } else if let cfg @
Some(..) = cfg
[0].meta_item() {
96 lib
.cfg
= cfg
.cloned();
98 self.tcx
.sess
.span_err(cfg
[0].span(), "invalid argument for `cfg(..)`");
100 } else if item
.check_name(sym
::wasm_import_module
) {
101 match item
.value_str() {
102 Some(s
) => lib
.wasm_import_module
= Some(s
),
104 let msg
= "must be of the form `#[link(wasm_import_module = \"...\")]`";
105 self.tcx
.sess
.span_err(item
.span(), msg
);
109 // currently, like past compilers, ignore unknown
114 // In general we require #[link(name = "...")] but we allow
115 // #[link(wasm_import_module = "...")] without the `name`.
116 let requires_name
= kind_specified
|| lib
.wasm_import_module
.is_none();
117 if lib
.name
.is_none() && requires_name
{
122 "`#[link(...)]` specified without \
125 .span_label(m
.span
, "missing `name` argument")
128 self.register_native_lib(Some(m
.span
), lib
);
132 fn visit_trait_item(&mut self, _it
: &'tcx hir
::TraitItem
<'tcx
>) {}
133 fn visit_impl_item(&mut self, _it
: &'tcx hir
::ImplItem
<'tcx
>) {}
136 impl Collector
<'tcx
> {
137 fn register_native_lib(&mut self, span
: Option
<Span
>, lib
: NativeLibrary
) {
138 if lib
.name
.as_ref().map(|&s
| s
== kw
::Invalid
).unwrap_or(false) {
145 "`#[link(name = \"\")]` given with empty name"
147 .span_label(span
, "empty name given")
151 self.tcx
.sess
.err("empty library name given via `-l`");
156 let is_osx
= self.tcx
.sess
.target
.target
.options
.is_like_osx
;
157 if lib
.kind
== cstore
::NativeFramework
&& !is_osx
{
158 let msg
= "native frameworks are only available on macOS targets";
160 Some(span
) => struct_span_err
!(self.tcx
.sess
, span
, E0455
, "{}", msg
).emit(),
161 None
=> self.tcx
.sess
.err(msg
),
164 if lib
.cfg
.is_some() && !self.tcx
.features().link_cfg
{
165 feature_err(&self.tcx
.sess
.parse_sess
, sym
::link_cfg
, span
.unwrap(), "is unstable")
168 if lib
.kind
== cstore
::NativeStaticNobundle
&& !self.tcx
.features().static_nobundle
{
170 &self.tcx
.sess
.parse_sess
,
171 sym
::static_nobundle
,
172 span
.unwrap_or_else(|| rustc_span
::DUMMY_SP
),
173 "kind=\"static-nobundle\" is unstable",
177 if lib
.kind
== cstore
::NativeRawDylib
&& !self.tcx
.features().raw_dylib
{
179 &self.tcx
.sess
.parse_sess
,
181 span
.unwrap_or_else(|| rustc_span
::DUMMY_SP
),
182 "kind=\"raw-dylib\" is unstable",
189 // Process libs passed on the command line
190 fn process_command_line(&mut self) {
191 // First, check for errors
192 let mut renames
= FxHashSet
::default();
193 for &(ref name
, ref new_name
, _
) in &self.tcx
.sess
.opts
.libs
{
194 if let &Some(ref new_name
) = new_name
{
195 let any_duplicate
= self
198 .filter_map(|lib
| lib
.name
.as_ref())
199 .any(|n
| n
.as_str() == *name
);
200 if new_name
.is_empty() {
201 self.tcx
.sess
.err(&format
!(
202 "an empty renaming target was specified for library `{}`",
205 } else if !any_duplicate
{
206 self.tcx
.sess
.err(&format
!(
207 "renaming of the library `{}` was specified, \
208 however this crate contains no `#[link(...)]` \
209 attributes referencing this library.",
212 } else if !renames
.insert(name
) {
213 self.tcx
.sess
.err(&format
!(
214 "multiple renamings were \
215 specified for library `{}` .",
222 // Update kind and, optionally, the name of all native libraries
223 // (there may be more than one) with the specified name. If any
224 // library is mentioned more than once, keep the latest mention
225 // of it, so that any possible dependent libraries appear before
226 // it. (This ensures that the linker is able to see symbols from
227 // all possible dependent libraries before linking in the library
229 for &(ref name
, ref new_name
, kind
) in &self.tcx
.sess
.opts
.libs
{
230 // If we've already added any native libraries with the same
231 // name, they will be pulled out into `existing`, so that we
232 // can move them to the end of the list below.
233 let mut existing
= self
235 .drain_filter(|lib
| {
236 if let Some(lib_name
) = lib
.name
{
237 if lib_name
.as_str() == *name
{
238 if let Some(k
) = kind
{
241 if let &Some(ref new_name
) = new_name
{
242 lib
.name
= Some(Symbol
::intern(new_name
));
249 .collect
::<Vec
<_
>>();
250 if existing
.is_empty() {
252 let new_name
= new_name
.as_ref().map(|s
| &**s
); // &Option<String> -> Option<&str>
253 let lib
= NativeLibrary
{
254 name
: Some(Symbol
::intern(new_name
.unwrap_or(name
))),
255 kind
: if let Some(k
) = kind { k }
else { cstore::NativeUnknown }
,
257 foreign_module
: None
,
258 wasm_import_module
: None
,
260 self.register_native_lib(None
, lib
);
262 // Move all existing libraries with the same name to the
263 // end of the command line.
264 self.libs
.append(&mut existing
);