1 // Copyright 2012-2014 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 #![allow(non_camel_case_types)]
13 //! Validates all used crates and extern libraries and loads their metadata
16 use session::{config, Session};
18 use metadata::cstore::{CStore, CrateSource};
19 use metadata::decoder;
21 use metadata::loader::CratePaths;
22 use plugin::load::PluginMetadata;
23 use util::nodemap::FnvHashMap;
26 use std::collections::hash_map::{Occupied, Vacant};
30 use syntax::attr::AttrMetaMethods;
31 use syntax::codemap::{Span};
32 use syntax::diagnostic::SpanHandler;
33 use syntax::parse::token::InternedString;
34 use syntax::parse::token;
40 next_crate_num: ast::CrateNum,
43 // Traverses an AST, reading all the information about use'd crates and extern
44 // libraries necessary for later resolving, typechecking, linking, etc.
45 pub fn read_crates(sess: &Session,
49 next_crate_num: sess.cstore.next_crate_num(),
51 visit_crate(&e, krate);
52 visit::walk_crate(&mut e, krate);
53 dump_crates(&sess.cstore);
54 warn_if_multiple_versions(sess.diagnostic(), &sess.cstore);
56 for &(ref name, kind) in sess.opts.libs.iter() {
57 register_native_lib(sess, None, name.clone(), kind);
61 impl<'a, 'v> visit::Visitor<'v> for Env<'a> {
62 fn visit_view_item(&mut self, a: &ast::ViewItem) {
63 visit_view_item(self, a);
64 visit::walk_view_item(self, a);
66 fn visit_item(&mut self, a: &ast::Item) {
68 visit::walk_item(self, a);
72 fn dump_crates(cstore: &CStore) {
73 debug!("resolved crates:");
74 cstore.iter_crate_data_origins(|_, data, opt_source| {
75 debug!(" name: {}", data.name());
76 debug!(" cnum: {}", data.cnum);
77 debug!(" hash: {}", data.hash());
79 let CrateSource { dylib, rlib, cnum: _ } = cs;
80 dylib.map(|dl| debug!(" dylib: {}", dl.display()));
81 rlib.map(|rl| debug!(" rlib: {}", rl.display()));
86 fn warn_if_multiple_versions(diag: &SpanHandler, cstore: &CStore) {
87 let mut map = FnvHashMap::new();
88 cstore.iter_crate_data(|cnum, data| {
89 match map.entry(data.name()) {
90 Vacant(entry) => { entry.set(vec![cnum]); },
91 Occupied(mut entry) => { entry.get_mut().push(cnum); },
95 for (name, dupes) in map.into_iter() {
96 if dupes.len() == 1 { continue }
98 format!("using multiple versions of crate `{}`", name).as_slice());
99 for dupe in dupes.into_iter() {
100 let data = cstore.get_crate_data(dupe);
101 diag.span_note(data.span, "used here");
102 loader::note_crate_name(diag, data.name().as_slice());
107 fn visit_crate(e: &Env, c: &ast::Crate) {
108 for a in c.attrs.iter().filter(|m| m.name() == "link_args") {
109 match a.value_str() {
110 Some(ref linkarg) => e.sess.cstore.add_used_link_args(linkarg.get()),
111 None => { /* fallthrough */ }
116 fn should_link(i: &ast::ViewItem) -> bool {
117 i.attrs.iter().all(|attr| {
118 attr.name().get() != "phase" ||
119 attr.meta_item_list().map_or(false, |phases| {
120 attr::contains_name(phases.as_slice(), "link")
125 fn visit_view_item(e: &mut Env, i: &ast::ViewItem) {
130 match extract_crate_info(e, i) {
132 let (cnum, _, _) = resolve_crate(e,
134 info.ident.as_slice(),
135 info.name.as_slice(),
138 e.sess.cstore.add_extern_mod_stmt_cnum(info.id, cnum);
151 fn extract_crate_info(e: &Env, i: &ast::ViewItem) -> Option<CrateInfo> {
153 ast::ViewItemExternCrate(ident, ref path_opt, id) => {
154 let ident = token::get_ident(ident);
155 debug!("resolving extern crate stmt. ident: {} path_opt: {}",
157 let name = match *path_opt {
158 Some((ref path_str, _)) => {
159 let name = path_str.get().to_string();
160 validate_crate_name(Some(e.sess), name.as_slice(),
164 None => ident.get().to_string(),
167 ident: ident.get().to_string(),
170 should_link: should_link(i),
177 pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option<Span>) {
178 let err = |s: &str| {
180 (_, None) => panic!("{}", s),
181 (Some(sp), Some(sess)) => sess.span_err(sp, s),
182 (None, Some(sess)) => sess.err(s),
186 err("crate name must not be empty");
189 if c.is_alphanumeric() { continue }
190 if c == '_' || c == '-' { continue }
191 err(format!("invalid character `{}` in crate name: `{}`", c, s).as_slice());
194 Some(sess) => sess.abort_if_errors(),
199 fn visit_item(e: &Env, i: &ast::Item) {
201 ast::ItemForeignMod(ref fm) => {
202 if fm.abi == abi::Rust || fm.abi == abi::RustIntrinsic {
206 // First, add all of the custom link_args attributes
207 let link_args = i.attrs.iter()
208 .filter_map(|at| if at.name() == "link_args" {
213 .collect::<Vec<&ast::Attribute>>();
214 for m in link_args.iter() {
215 match m.value_str() {
216 Some(linkarg) => e.sess.cstore.add_used_link_args(linkarg.get()),
217 None => { /* fallthrough */ }
221 // Next, process all of the #[link(..)]-style arguments
222 let link_args = i.attrs.iter()
223 .filter_map(|at| if at.name() == "link" {
228 .collect::<Vec<&ast::Attribute>>();
229 for m in link_args.iter() {
230 match m.meta_item_list() {
232 let kind = items.iter().find(|k| {
234 }).and_then(|a| a.value_str());
235 let kind = match kind {
239 } else if e.sess.target.target.options.is_like_osx
240 && k == "framework" {
241 cstore::NativeFramework
242 } else if k == "framework" {
243 cstore::NativeFramework
244 } else if k == "dylib" {
245 cstore::NativeUnknown
247 e.sess.span_err(m.span,
248 format!("unknown kind: `{}`",
250 cstore::NativeUnknown
253 None => cstore::NativeUnknown
255 let n = items.iter().find(|n| {
257 }).and_then(|a| a.value_str());
261 e.sess.span_err(m.span,
262 "#[link(...)] specified without \
264 InternedString::new("foo")
267 register_native_lib(e.sess, Some(m.span),
268 n.get().to_string(), kind);
278 fn register_native_lib(sess: &Session, span: Option<Span>, name: String,
279 kind: cstore::NativeLibaryKind) {
280 if name.as_slice().is_empty() {
283 sess.span_err(span, "#[link(name = \"\")] given with \
287 sess.err("empty library name given via `-l`");
292 let is_osx = sess.target.target.options.is_like_osx;
293 if kind == cstore::NativeFramework && !is_osx {
294 let msg = "native frameworks are only available on OSX targets";
296 Some(span) => sess.span_err(span, msg),
297 None => sess.err(msg),
300 sess.cstore.add_used_library(name, kind);
303 fn existing_match(e: &Env, name: &str,
304 hash: Option<&Svh>) -> Option<ast::CrateNum> {
306 e.sess.cstore.iter_crate_data(|cnum, data| {
307 if data.name.as_slice() != name { return }
310 Some(hash) if *hash == data.hash() => { ret = Some(cnum); return }
315 // When the hash is None we're dealing with a top-level dependency in
316 // which case we may have a specification on the command line for this
317 // library. Even though an upstream library may have loaded something of
318 // the same name, we have to make sure it was loaded from the exact same
321 // We're also sure to compare *paths*, not actual byte slices. The
322 // `source` stores paths which are normalized which may be different
323 // from the strings on the command line.
324 let source = e.sess.cstore.get_used_crate_source(cnum).unwrap();
325 match e.sess.opts.externs.get(name) {
327 let found = locs.iter().any(|l| {
328 let l = fs::realpath(&Path::new(l.as_slice())).ok();
329 l == source.dylib || l == source.rlib
335 None => ret = Some(cnum),
341 fn register_crate<'a>(e: &mut Env,
342 root: &Option<CratePaths>,
346 lib: loader::Library)
347 -> (ast::CrateNum, Rc<cstore::crate_metadata>,
348 cstore::CrateSource) {
349 // Claim this crate number and cache it
350 let cnum = e.next_crate_num;
351 e.next_crate_num += 1;
353 // Stash paths for top-most crate locally if necessary.
354 let crate_paths = if root.is_none() {
356 ident: ident.to_string(),
357 dylib: lib.dylib.clone(),
358 rlib: lib.rlib.clone(),
363 // Maintain a reference to the top most crate.
364 let root = if root.is_some() { root } else { &crate_paths };
366 let cnum_map = resolve_crate_deps(e, root, lib.metadata.as_slice(), span);
368 let loader::Library{ dylib, rlib, metadata } = lib;
370 let cmeta = Rc::new( cstore::crate_metadata {
371 name: name.to_string(),
378 let source = cstore::CrateSource {
384 e.sess.cstore.set_crate_data(cnum, cmeta.clone());
385 e.sess.cstore.add_used_crate_source(source.clone());
386 (cnum, cmeta, source)
389 fn resolve_crate<'a>(e: &mut Env,
390 root: &Option<CratePaths>,
395 -> (ast::CrateNum, Rc<cstore::crate_metadata>,
396 cstore::CrateSource) {
397 match existing_match(e, name, hash) {
399 let mut load_ctxt = loader::Context {
404 hash: hash.map(|a| &*a),
405 filesearch: e.sess.target_filesearch(),
406 triple: e.sess.opts.target_triple.as_slice(),
408 rejected_via_hash: vec!(),
409 rejected_via_triple: vec!(),
410 should_match_name: true,
412 let library = load_ctxt.load_library_crate();
413 register_crate(e, root, ident, name, span, library)
416 e.sess.cstore.get_crate_data(cnum),
417 e.sess.cstore.get_used_crate_source(cnum).unwrap())
421 // Go through the crate metadata and load any crates that it references
422 fn resolve_crate_deps(e: &mut Env,
423 root: &Option<CratePaths>,
424 cdata: &[u8], span : Span)
425 -> cstore::cnum_map {
426 debug!("resolving deps of external crate");
427 // The map from crate numbers in the crate we're resolving to local crate
429 decoder::get_crate_deps(cdata).iter().map(|dep| {
430 debug!("resolving dep crate {} hash: `{}`", dep.name, dep.hash);
431 let (local_cnum, _, _) = resolve_crate(e, root,
436 (dep.cnum, local_cnum)
440 pub struct PluginMetadataReader<'a> {
444 impl<'a> PluginMetadataReader<'a> {
445 pub fn new(sess: &'a Session) -> PluginMetadataReader<'a> {
446 PluginMetadataReader {
449 next_crate_num: sess.cstore.next_crate_num(),
454 pub fn read_plugin_metadata(&mut self, krate: &ast::ViewItem) -> PluginMetadata {
455 let info = extract_crate_info(&self.env, krate).unwrap();
456 let target_triple = self.env.sess.opts.target_triple.as_slice();
457 let is_cross = target_triple != config::host_triple();
458 let mut should_link = info.should_link && !is_cross;
459 let mut load_ctxt = loader::Context {
462 ident: info.ident.as_slice(),
463 crate_name: info.name.as_slice(),
465 filesearch: self.env.sess.host_filesearch(),
466 triple: config::host_triple(),
468 rejected_via_hash: vec!(),
469 rejected_via_triple: vec!(),
470 should_match_name: true,
472 let library = match load_ctxt.maybe_load_library_crate() {
474 None if is_cross => {
475 // try loading from target crates (only valid if there are
476 // no syntax extensions)
477 load_ctxt.triple = target_triple;
478 load_ctxt.filesearch = self.env.sess.target_filesearch();
479 let lib = load_ctxt.load_library_crate();
480 if decoder::get_plugin_registrar_fn(lib.metadata.as_slice()).is_some() {
481 let message = format!("crate `{}` contains a plugin_registrar fn but \
482 only a version for triple `{}` could be found (need {})",
483 info.ident, target_triple, config::host_triple());
484 self.env.sess.span_err(krate.span, message.as_slice());
485 // need to abort now because the syntax expansion
486 // code will shortly attempt to load and execute
487 // code from the found library.
488 self.env.sess.abort_if_errors();
490 should_link = info.should_link;
493 None => { load_ctxt.report_load_errs(); unreachable!() },
495 let macros = decoder::get_exported_macros(library.metadata.as_slice());
496 let registrar = decoder::get_plugin_registrar_fn(library.metadata.as_slice()).map(|id| {
497 decoder::get_symbol(library.metadata.as_slice(), id)
499 if library.dylib.is_none() && registrar.is_some() {
500 let message = format!("plugin crate `{}` only found in rlib format, \
501 but must be available in dylib format",
503 self.env.sess.span_err(krate.span, message.as_slice());
504 // No need to abort because the loading code will just ignore this
507 let pc = PluginMetadata {
508 lib: library.dylib.clone(),
510 registrar_symbol: registrar,
512 if should_link && existing_match(&self.env, info.name.as_slice(),
514 // register crate now to avoid double-reading metadata
515 register_crate(&mut self.env, &None, info.ident.as_slice(),
516 info.name.as_slice(), krate.span, library);