2015-02-05 21:28:17 +01:00
|
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
2012-12-04 01:48:01 +01:00
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
2012-07-04 23:53:12 +02:00
|
|
|
|
//! Finds crate binaries and loads their metadata
|
2014-07-01 17:37:54 +02:00
|
|
|
|
//!
|
|
|
|
|
//! Might I be the first to welcome you to a world of platform differences,
|
2014-08-02 01:40:21 +02:00
|
|
|
|
//! version requirements, dependency graphs, conflicting desires, and fun! This
|
2014-07-01 17:37:54 +02:00
|
|
|
|
//! is the major guts (along with metadata::creader) of the compiler for loading
|
|
|
|
|
//! crates and resolving dependencies. Let's take a tour!
|
|
|
|
|
//!
|
|
|
|
|
//! # The problem
|
|
|
|
|
//!
|
|
|
|
|
//! Each invocation of the compiler is immediately concerned with one primary
|
|
|
|
|
//! problem, to connect a set of crates to resolved crates on the filesystem.
|
|
|
|
|
//! Concretely speaking, the compiler follows roughly these steps to get here:
|
|
|
|
|
//!
|
|
|
|
|
//! 1. Discover a set of `extern crate` statements.
|
|
|
|
|
//! 2. Transform these directives into crate names. If the directive does not
|
|
|
|
|
//! have an explicit name, then the identifier is the name.
|
|
|
|
|
//! 3. For each of these crate names, find a corresponding crate on the
|
|
|
|
|
//! filesystem.
|
|
|
|
|
//!
|
|
|
|
|
//! Sounds easy, right? Let's walk into some of the nuances.
|
|
|
|
|
//!
|
|
|
|
|
//! ## Transitive Dependencies
|
|
|
|
|
//!
|
|
|
|
|
//! Let's say we've got three crates: A, B, and C. A depends on B, and B depends
|
|
|
|
|
//! on C. When we're compiling A, we primarily need to find and locate B, but we
|
|
|
|
|
//! also end up needing to find and locate C as well.
|
|
|
|
|
//!
|
|
|
|
|
//! The reason for this is that any of B's types could be composed of C's types,
|
|
|
|
|
//! any function in B could return a type from C, etc. To be able to guarantee
|
|
|
|
|
//! that we can always typecheck/translate any function, we have to have
|
|
|
|
|
//! complete knowledge of the whole ecosystem, not just our immediate
|
|
|
|
|
//! dependencies.
|
|
|
|
|
//!
|
|
|
|
|
//! So now as part of the "find a corresponding crate on the filesystem" step
|
|
|
|
|
//! above, this involves also finding all crates for *all upstream
|
|
|
|
|
//! dependencies*. This includes all dependencies transitively.
|
|
|
|
|
//!
|
|
|
|
|
//! ## Rlibs and Dylibs
|
|
|
|
|
//!
|
|
|
|
|
//! The compiler has two forms of intermediate dependencies. These are dubbed
|
|
|
|
|
//! rlibs and dylibs for the static and dynamic variants, respectively. An rlib
|
|
|
|
|
//! is a rustc-defined file format (currently just an ar archive) while a dylib
|
|
|
|
|
//! is a platform-defined dynamic library. Each library has a metadata somewhere
|
|
|
|
|
//! inside of it.
|
|
|
|
|
//!
|
|
|
|
|
//! When translating a crate name to a crate on the filesystem, we all of a
|
|
|
|
|
//! sudden need to take into account both rlibs and dylibs! Linkage later on may
|
|
|
|
|
//! use either one of these files, as each has their pros/cons. The job of crate
|
|
|
|
|
//! loading is to discover what's possible by finding all candidates.
|
|
|
|
|
//!
|
|
|
|
|
//! Most parts of this loading systems keep the dylib/rlib as just separate
|
|
|
|
|
//! variables.
|
|
|
|
|
//!
|
|
|
|
|
//! ## Where to look?
|
|
|
|
|
//!
|
|
|
|
|
//! We can't exactly scan your whole hard drive when looking for dependencies,
|
|
|
|
|
//! so we need to places to look. Currently the compiler will implicitly add the
|
|
|
|
|
//! target lib search path ($prefix/lib/rustlib/$target/lib) to any compilation,
|
|
|
|
|
//! and otherwise all -L flags are added to the search paths.
|
|
|
|
|
//!
|
|
|
|
|
//! ## What criterion to select on?
|
|
|
|
|
//!
|
|
|
|
|
//! This a pretty tricky area of loading crates. Given a file, how do we know
|
|
|
|
|
//! whether it's the right crate? Currently, the rules look along these lines:
|
|
|
|
|
//!
|
|
|
|
|
//! 1. Does the filename match an rlib/dylib pattern? That is to say, does the
|
|
|
|
|
//! filename have the right prefix/suffix?
|
|
|
|
|
//! 2. Does the filename have the right prefix for the crate name being queried?
|
|
|
|
|
//! This is filtering for files like `libfoo*.rlib` and such.
|
|
|
|
|
//! 3. Is the file an actual rust library? This is done by loading the metadata
|
|
|
|
|
//! from the library and making sure it's actually there.
|
|
|
|
|
//! 4. Does the name in the metadata agree with the name of the library?
|
|
|
|
|
//! 5. Does the target in the metadata agree with the current target?
|
|
|
|
|
//! 6. Does the SVH match? (more on this later)
|
|
|
|
|
//!
|
2014-08-02 01:40:21 +02:00
|
|
|
|
//! If the file answers `yes` to all these questions, then the file is
|
2014-07-01 17:37:54 +02:00
|
|
|
|
//! considered as being *candidate* for being accepted. It is illegal to have
|
|
|
|
|
//! more than two candidates as the compiler has no method by which to resolve
|
|
|
|
|
//! this conflict. Additionally, rlib/dylib candidates are considered
|
|
|
|
|
//! separately.
|
|
|
|
|
//!
|
|
|
|
|
//! After all this has happened, we have 1 or two files as candidates. These
|
|
|
|
|
//! represent the rlib/dylib file found for a library, and they're returned as
|
|
|
|
|
//! being found.
|
|
|
|
|
//!
|
|
|
|
|
//! ### What about versions?
|
|
|
|
|
//!
|
|
|
|
|
//! A lot of effort has been put forth to remove versioning from the compiler.
|
|
|
|
|
//! There have been forays in the past to have versioning baked in, but it was
|
|
|
|
|
//! largely always deemed insufficient to the point that it was recognized that
|
|
|
|
|
//! it's probably something the compiler shouldn't do anyway due to its
|
|
|
|
|
//! complicated nature and the state of the half-baked solutions.
|
|
|
|
|
//!
|
|
|
|
|
//! With a departure from versioning, the primary criterion for loading crates
|
|
|
|
|
//! is just the name of a crate. If we stopped here, it would imply that you
|
|
|
|
|
//! could never link two crates of the same name from different sources
|
|
|
|
|
//! together, which is clearly a bad state to be in.
|
|
|
|
|
//!
|
|
|
|
|
//! To resolve this problem, we come to the next section!
|
|
|
|
|
//!
|
|
|
|
|
//! # Expert Mode
|
|
|
|
|
//!
|
|
|
|
|
//! A number of flags have been added to the compiler to solve the "version
|
|
|
|
|
//! problem" in the previous section, as well as generally enabling more
|
|
|
|
|
//! powerful usage of the crate loading system of the compiler. The goal of
|
|
|
|
|
//! these flags and options are to enable third-party tools to drive the
|
|
|
|
|
//! compiler with prior knowledge about how the world should look.
|
|
|
|
|
//!
|
|
|
|
|
//! ## The `--extern` flag
|
|
|
|
|
//!
|
|
|
|
|
//! The compiler accepts a flag of this form a number of times:
|
|
|
|
|
//!
|
2014-12-10 17:50:50 +01:00
|
|
|
|
//! ```text
|
2014-07-01 17:37:54 +02:00
|
|
|
|
//! --extern crate-name=path/to/the/crate.rlib
|
|
|
|
|
//! ```
|
|
|
|
|
//!
|
|
|
|
|
//! This flag is basically the following letter to the compiler:
|
|
|
|
|
//!
|
|
|
|
|
//! > Dear rustc,
|
|
|
|
|
//! >
|
|
|
|
|
//! > When you are attempting to load the immediate dependency `crate-name`, I
|
2015-04-15 09:07:25 +02:00
|
|
|
|
//! > would like you to assume that the library is located at
|
2014-07-01 17:37:54 +02:00
|
|
|
|
//! > `path/to/the/crate.rlib`, and look nowhere else. Also, please do not
|
|
|
|
|
//! > assume that the path I specified has the name `crate-name`.
|
|
|
|
|
//!
|
|
|
|
|
//! This flag basically overrides most matching logic except for validating that
|
|
|
|
|
//! the file is indeed a rust library. The same `crate-name` can be specified
|
|
|
|
|
//! twice to specify the rlib/dylib pair.
|
|
|
|
|
//!
|
|
|
|
|
//! ## Enabling "multiple versions"
|
|
|
|
|
//!
|
|
|
|
|
//! This basically boils down to the ability to specify arbitrary packages to
|
|
|
|
|
//! the compiler. For example, if crate A wanted to use Bv1 and Bv2, then it
|
|
|
|
|
//! would look something like:
|
|
|
|
|
//!
|
|
|
|
|
//! ```ignore
|
|
|
|
|
//! extern crate b1;
|
|
|
|
|
//! extern crate b2;
|
|
|
|
|
//!
|
|
|
|
|
//! fn main() {}
|
|
|
|
|
//! ```
|
|
|
|
|
//!
|
|
|
|
|
//! and the compiler would be invoked as:
|
|
|
|
|
//!
|
2014-12-10 17:50:50 +01:00
|
|
|
|
//! ```text
|
2014-07-01 17:37:54 +02:00
|
|
|
|
//! rustc a.rs --extern b1=path/to/libb1.rlib --extern b2=path/to/libb2.rlib
|
|
|
|
|
//! ```
|
|
|
|
|
//!
|
|
|
|
|
//! In this scenario there are two crates named `b` and the compiler must be
|
|
|
|
|
//! manually driven to be informed where each crate is.
|
|
|
|
|
//!
|
|
|
|
|
//! ## Frobbing symbols
|
|
|
|
|
//!
|
|
|
|
|
//! One of the immediate problems with linking the same library together twice
|
|
|
|
|
//! in the same problem is dealing with duplicate symbols. The primary way to
|
|
|
|
|
//! deal with this in rustc is to add hashes to the end of each symbol.
|
|
|
|
|
//!
|
|
|
|
|
//! In order to force hashes to change between versions of a library, if
|
|
|
|
|
//! desired, the compiler exposes an option `-C metadata=foo`, which is used to
|
|
|
|
|
//! initially seed each symbol hash. The string `foo` is prepended to each
|
|
|
|
|
//! string-to-hash to ensure that symbols change over time.
|
|
|
|
|
//!
|
|
|
|
|
//! ## Loading transitive dependencies
|
|
|
|
|
//!
|
|
|
|
|
//! Dealing with same-named-but-distinct crates is not just a local problem, but
|
2014-08-02 01:40:21 +02:00
|
|
|
|
//! one that also needs to be dealt with for transitive dependencies. Note that
|
2014-07-01 17:37:54 +02:00
|
|
|
|
//! in the letter above `--extern` flags only apply to the *local* set of
|
|
|
|
|
//! dependencies, not the upstream transitive dependencies. Consider this
|
|
|
|
|
//! dependency graph:
|
|
|
|
|
//!
|
2014-12-10 17:50:50 +01:00
|
|
|
|
//! ```text
|
2014-07-01 17:37:54 +02:00
|
|
|
|
//! A.1 A.2
|
|
|
|
|
//! | |
|
|
|
|
|
//! | |
|
|
|
|
|
//! B C
|
|
|
|
|
//! \ /
|
|
|
|
|
//! \ /
|
|
|
|
|
//! D
|
|
|
|
|
//! ```
|
|
|
|
|
//!
|
|
|
|
|
//! In this scenario, when we compile `D`, we need to be able to distinctly
|
|
|
|
|
//! resolve `A.1` and `A.2`, but an `--extern` flag cannot apply to these
|
|
|
|
|
//! transitive dependencies.
|
|
|
|
|
//!
|
|
|
|
|
//! Note that the key idea here is that `B` and `C` are both *already compiled*.
|
|
|
|
|
//! That is, they have already resolved their dependencies. Due to unrelated
|
|
|
|
|
//! technical reasons, when a library is compiled, it is only compatible with
|
|
|
|
|
//! the *exact same* version of the upstream libraries it was compiled against.
|
|
|
|
|
//! We use the "Strict Version Hash" to identify the exact copy of an upstream
|
|
|
|
|
//! library.
|
|
|
|
|
//!
|
|
|
|
|
//! With this knowledge, we know that `B` and `C` will depend on `A` with
|
|
|
|
|
//! different SVH values, so we crawl the normal `-L` paths looking for
|
|
|
|
|
//! `liba*.rlib` and filter based on the contained SVH.
|
|
|
|
|
//!
|
|
|
|
|
//! In the end, this ends up not needing `--extern` to specify upstream
|
|
|
|
|
//! transitive dependencies.
|
|
|
|
|
//!
|
|
|
|
|
//! # Wrapping up
|
|
|
|
|
//!
|
|
|
|
|
//! That's the general overview of loading crates in the compiler, but it's by
|
|
|
|
|
//! no means all of the necessary details. Take a look at the rest of
|
|
|
|
|
//! metadata::loader or metadata::creader for all the juicy details!
|
2012-05-16 05:23:06 +02:00
|
|
|
|
|
2016-09-16 16:25:54 +02:00
|
|
|
|
use cstore::MetadataBlob;
|
2016-10-20 06:21:25 +02:00
|
|
|
|
use creader::Library;
|
2016-10-01 00:24:50 +02:00
|
|
|
|
use schema::{METADATA_HEADER, rustc_version};
|
2015-11-24 23:00:26 +01:00
|
|
|
|
|
2016-03-29 07:50:44 +02:00
|
|
|
|
use rustc::hir::svh::Svh;
|
2015-11-24 23:00:26 +01:00
|
|
|
|
use rustc::session::Session;
|
|
|
|
|
use rustc::session::filesearch::{FileSearch, FileMatches, FileDoesntMatch};
|
|
|
|
|
use rustc::session::search_paths::PathKind;
|
|
|
|
|
use rustc::util::common;
|
2016-08-23 00:57:54 +02:00
|
|
|
|
use rustc::util::nodemap::FnvHashMap;
|
2015-11-24 23:00:26 +01:00
|
|
|
|
|
|
|
|
|
use rustc_llvm as llvm;
|
|
|
|
|
use rustc_llvm::{False, ObjectFile, mk_section_iter};
|
|
|
|
|
use rustc_llvm::archive_ro::ArchiveRO;
|
2016-06-22 00:08:13 +02:00
|
|
|
|
use errors::DiagnosticBuilder;
|
|
|
|
|
use syntax_pos::Span;
|
2015-01-09 02:14:10 +01:00
|
|
|
|
use rustc_back::target::Target;
|
2012-12-23 23:41:37 +01:00
|
|
|
|
|
2014-02-06 08:34:33 +01:00
|
|
|
|
use std::cmp;
|
2016-04-08 07:49:48 +02:00
|
|
|
|
use std::fmt;
|
2015-04-16 08:21:13 +02:00
|
|
|
|
use std::fs;
|
2015-02-27 06:00:43 +01:00
|
|
|
|
use std::io;
|
|
|
|
|
use std::path::{Path, PathBuf};
|
2014-04-03 19:45:36 +02:00
|
|
|
|
use std::ptr;
|
2014-03-09 00:11:52 +01:00
|
|
|
|
use std::slice;
|
2015-12-03 02:31:49 +01:00
|
|
|
|
use std::time::Instant;
|
2014-02-20 04:08:12 +01:00
|
|
|
|
|
2014-01-25 06:00:31 +01:00
|
|
|
|
use flate;
|
2012-05-16 05:23:06 +02:00
|
|
|
|
|
2014-04-17 17:52:25 +02:00
|
|
|
|
pub struct CrateMismatch {
|
2015-02-27 06:00:43 +01:00
|
|
|
|
path: PathBuf,
|
2014-05-23 01:57:53 +02:00
|
|
|
|
got: String,
|
2014-04-03 17:43:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-25 03:13:51 +01:00
|
|
|
|
pub struct Context<'a> {
|
2014-03-28 18:05:27 +01:00
|
|
|
|
pub sess: &'a Session,
|
|
|
|
|
pub span: Span,
|
|
|
|
|
pub ident: &'a str,
|
2014-07-01 07:53:13 +02:00
|
|
|
|
pub crate_name: &'a str,
|
2014-03-28 18:05:27 +01:00
|
|
|
|
pub hash: Option<&'a Svh>,
|
2015-01-09 02:14:10 +01:00
|
|
|
|
// points to either self.sess.target.target or self.sess.host, must match triple
|
|
|
|
|
pub target: &'a Target,
|
2014-04-17 17:52:25 +02:00
|
|
|
|
pub triple: &'a str,
|
|
|
|
|
pub filesearch: FileSearch<'a>,
|
|
|
|
|
pub root: &'a Option<CratePaths>,
|
|
|
|
|
pub rejected_via_hash: Vec<CrateMismatch>,
|
|
|
|
|
pub rejected_via_triple: Vec<CrateMismatch>,
|
2015-02-05 21:28:17 +01:00
|
|
|
|
pub rejected_via_kind: Vec<CrateMismatch>,
|
2016-07-01 23:36:33 +02:00
|
|
|
|
pub rejected_via_version: Vec<CrateMismatch>,
|
2014-07-01 17:37:54 +02:00
|
|
|
|
pub should_match_name: bool,
|
2013-02-19 08:40:42 +01:00
|
|
|
|
}
|
2012-05-23 02:16:26 +02:00
|
|
|
|
|
2013-12-17 05:58:21 +01:00
|
|
|
|
pub struct ArchiveMetadata {
|
2014-06-06 15:51:42 +02:00
|
|
|
|
_archive: ArchiveRO,
|
2014-10-25 21:27:15 +02:00
|
|
|
|
// points into self._archive
|
|
|
|
|
data: *const [u8],
|
2013-12-17 05:58:21 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-03 17:43:57 +02:00
|
|
|
|
pub struct CratePaths {
|
2014-05-23 01:57:53 +02:00
|
|
|
|
pub ident: String,
|
2015-02-27 06:00:43 +01:00
|
|
|
|
pub dylib: Option<PathBuf>,
|
|
|
|
|
pub rlib: Option<PathBuf>
|
2014-04-03 17:43:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
trans: Use LLVM's writeArchive to modify archives
We have previously always relied upon an external tool, `ar`, to modify archives
that the compiler produces (staticlibs, rlibs, etc). This approach, however, has
a number of downsides:
* Spawning a process is relatively expensive for small compilations
* Encoding arguments across process boundaries often incurs unnecessary overhead
or lossiness. For example `ar` has a tough time dealing with files that have
the same name in archives, and the compiler copies many files around to ensure
they can be passed to `ar` in a reasonable fashion.
* Most `ar` programs found do **not** have the ability to target arbitrary
platforms, so this is an extra tool which needs to be found/specified when
cross compiling.
The LLVM project has had a tool called `llvm-ar` for quite some time now, but it
wasn't available in the standard LLVM libraries (it was just a standalone
program). Recently, however, in LLVM 3.7, this functionality has been moved to a
library and is now accessible by consumers of LLVM via the `writeArchive`
function.
This commit migrates our archive bindings to no longer invoke `ar` by default
but instead make a library call to LLVM to do various operations. This solves
all of the downsides listed above:
* Archive management is now much faster, for example creating a "hello world"
staticlib is now 6x faster (50ms => 8ms). Linking dynamic libraries also
recently started requiring modification of rlibs, and linking a hello world
dynamic library is now 2x faster.
* The compiler is now one step closer to "hassle free" cross compilation because
no external tool is needed for managing archives, LLVM does the right thing!
This commit does not remove support for calling a system `ar` utility currently.
We will continue to maintain compatibility with LLVM 3.5 and 3.6 looking forward
(so the system LLVM can be used wherever possible), and in these cases we must
shell out to a system utility. All nightly builds of Rust, however, will stop
needing a system `ar`.
2015-07-09 09:14:20 +02:00
|
|
|
|
pub const METADATA_FILENAME: &'static str = "rust.metadata.bin";
|
|
|
|
|
|
2016-04-08 07:49:48 +02:00
|
|
|
|
#[derive(Copy, Clone, PartialEq)]
|
|
|
|
|
enum CrateFlavor {
|
|
|
|
|
Rlib,
|
|
|
|
|
Dylib
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for CrateFlavor {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
f.write_str(match *self {
|
|
|
|
|
CrateFlavor::Rlib => "rlib",
|
|
|
|
|
CrateFlavor::Dylib => "dylib"
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-03 17:43:57 +02:00
|
|
|
|
impl CratePaths {
|
2015-02-27 06:00:43 +01:00
|
|
|
|
fn paths(&self) -> Vec<PathBuf> {
|
2014-04-03 17:43:57 +02:00
|
|
|
|
match (&self.dylib, &self.rlib) {
|
2014-04-05 03:49:03 +02:00
|
|
|
|
(&None, &None) => vec!(),
|
|
|
|
|
(&Some(ref p), &None) |
|
|
|
|
|
(&None, &Some(ref p)) => vec!(p.clone()),
|
|
|
|
|
(&Some(ref p1), &Some(ref p2)) => vec!(p1.clone(), p2.clone()),
|
2014-04-03 17:43:57 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-25 03:13:51 +01:00
|
|
|
|
impl<'a> Context<'a> {
|
2014-04-17 17:52:25 +02:00
|
|
|
|
pub fn maybe_load_library_crate(&mut self) -> Option<Library> {
|
|
|
|
|
self.find_library_crate()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn load_library_crate(&mut self) -> Library {
|
2016-03-28 23:00:01 +02:00
|
|
|
|
self.find_library_crate().unwrap_or_else(|| self.report_load_errs())
|
2014-04-17 17:52:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-28 23:00:01 +02:00
|
|
|
|
pub fn report_load_errs(&mut self) -> ! {
|
2015-09-11 16:17:15 +02:00
|
|
|
|
let add = match self.root {
|
|
|
|
|
&None => String::new(),
|
|
|
|
|
&Some(ref r) => format!(" which `{}` depends on",
|
|
|
|
|
r.ident)
|
|
|
|
|
};
|
2015-12-20 22:00:43 +01:00
|
|
|
|
let mut err = if !self.rejected_via_hash.is_empty() {
|
|
|
|
|
struct_span_err!(self.sess, self.span, E0460,
|
|
|
|
|
"found possibly newer version of crate `{}`{}",
|
|
|
|
|
self.ident, add)
|
2015-03-25 00:54:09 +01:00
|
|
|
|
} else if !self.rejected_via_triple.is_empty() {
|
2015-12-20 22:00:43 +01:00
|
|
|
|
struct_span_err!(self.sess, self.span, E0461,
|
|
|
|
|
"couldn't find crate `{}` with expected target triple {}{}",
|
|
|
|
|
self.ident, self.triple, add)
|
2015-03-25 00:54:09 +01:00
|
|
|
|
} else if !self.rejected_via_kind.is_empty() {
|
2015-12-20 22:00:43 +01:00
|
|
|
|
struct_span_err!(self.sess, self.span, E0462,
|
|
|
|
|
"found staticlib `{}` instead of rlib or dylib{}",
|
|
|
|
|
self.ident, add)
|
2016-07-01 23:36:33 +02:00
|
|
|
|
} else if !self.rejected_via_version.is_empty() {
|
|
|
|
|
struct_span_err!(self.sess, self.span, E0514,
|
|
|
|
|
"found crate `{}` compiled by an incompatible version of rustc{}",
|
|
|
|
|
self.ident, add)
|
2014-04-17 17:52:25 +02:00
|
|
|
|
} else {
|
2016-08-28 02:38:04 +02:00
|
|
|
|
let mut err = struct_span_err!(self.sess, self.span, E0463,
|
|
|
|
|
"can't find crate for `{}`{}",
|
|
|
|
|
self.ident, add);
|
|
|
|
|
err.span_label(self.span, &format!("can't find crate"));
|
|
|
|
|
err
|
2015-12-20 22:00:43 +01:00
|
|
|
|
};
|
2014-04-17 17:52:25 +02:00
|
|
|
|
|
2015-03-25 00:54:09 +01:00
|
|
|
|
if !self.rejected_via_triple.is_empty() {
|
2014-11-10 11:52:55 +01:00
|
|
|
|
let mismatches = self.rejected_via_triple.iter();
|
2014-04-17 17:52:25 +02:00
|
|
|
|
for (i, &CrateMismatch{ ref path, ref got }) in mismatches.enumerate() {
|
2016-04-20 20:49:16 +02:00
|
|
|
|
err.note(&format!("crate `{}`, path #{}, triple {}: {}",
|
|
|
|
|
self.ident, i+1, got, path.display()));
|
2014-04-17 17:52:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-25 00:54:09 +01:00
|
|
|
|
if !self.rejected_via_hash.is_empty() {
|
2016-07-01 23:36:33 +02:00
|
|
|
|
err.note("perhaps that crate needs to be recompiled?");
|
2014-04-17 17:52:25 +02:00
|
|
|
|
let mismatches = self.rejected_via_hash.iter();
|
|
|
|
|
for (i, &CrateMismatch{ ref path, .. }) in mismatches.enumerate() {
|
2016-04-20 20:49:16 +02:00
|
|
|
|
err.note(&format!("crate `{}` path #{}: {}",
|
|
|
|
|
self.ident, i+1, path.display()));
|
2014-04-17 17:52:25 +02:00
|
|
|
|
}
|
|
|
|
|
match self.root {
|
|
|
|
|
&None => {}
|
2014-05-28 18:24:28 +02:00
|
|
|
|
&Some(ref r) => {
|
|
|
|
|
for (i, path) in r.paths().iter().enumerate() {
|
2016-04-20 20:49:16 +02:00
|
|
|
|
err.note(&format!("crate `{}` path #{}: {}",
|
|
|
|
|
r.ident, i+1, path.display()));
|
2014-05-28 18:24:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
Add generation of static libraries to rustc
This commit implements the support necessary for generating both intermediate
and result static rust libraries. This is an implementation of my thoughts in
https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html.
When compiling a library, we still retain the "lib" option, although now there
are "rlib", "staticlib", and "dylib" as options for crate_type (and these are
stackable). The idea of "lib" is to generate the "compiler default" instead of
having too choose (although all are interchangeable). For now I have left the
"complier default" to be a dynamic library for size reasons.
Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an
rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a
dynamic object. I chose this for size reasons, but also because you're probably
not going to be embedding the rustc compiler anywhere any time soon.
Other than the options outlined above, there are a few defaults/preferences that
are now opinionated in the compiler:
* If both a .dylib and .rlib are found for a rust library, the compiler will
prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option
* If generating a "lib", the compiler will generate a dynamic library. This is
overridable by explicitly saying what flavor you'd like (rlib, staticlib,
dylib).
* If no options are passed to the command line, and no crate_type is found in
the destination crate, then an executable is generated
With this change, you can successfully build a rust program with 0 dynamic
dependencies on rust libraries. There is still a dynamic dependency on
librustrt, but I plan on removing that in a subsequent commit.
This change includes no tests just yet. Our current testing
infrastructure/harnesses aren't very amenable to doing flavorful things with
linking, so I'm planning on adding a new mode of testing which I believe belongs
as a separate commit.
Closes #552
2013-11-15 23:03:29 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-25 00:54:09 +01:00
|
|
|
|
if !self.rejected_via_kind.is_empty() {
|
2016-07-01 23:36:33 +02:00
|
|
|
|
err.help("please recompile that crate using --crate-type lib");
|
2015-02-05 21:28:17 +01:00
|
|
|
|
let mismatches = self.rejected_via_kind.iter();
|
|
|
|
|
for (i, &CrateMismatch { ref path, .. }) in mismatches.enumerate() {
|
2016-04-20 20:49:16 +02:00
|
|
|
|
err.note(&format!("crate `{}` path #{}: {}",
|
|
|
|
|
self.ident, i+1, path.display()));
|
2015-02-05 21:28:17 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-01 23:36:33 +02:00
|
|
|
|
if !self.rejected_via_version.is_empty() {
|
|
|
|
|
err.help(&format!("please recompile that crate using this compiler ({})",
|
2016-10-01 00:24:50 +02:00
|
|
|
|
rustc_version()));
|
2016-07-01 23:36:33 +02:00
|
|
|
|
let mismatches = self.rejected_via_version.iter();
|
|
|
|
|
for (i, &CrateMismatch { ref path, ref got }) in mismatches.enumerate() {
|
|
|
|
|
err.note(&format!("crate `{}` path #{}: {} compiled by {:?}",
|
|
|
|
|
self.ident, i+1, path.display(), got));
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-20 22:00:43 +01:00
|
|
|
|
|
|
|
|
|
err.emit();
|
2014-04-17 17:52:25 +02:00
|
|
|
|
self.sess.abort_if_errors();
|
2016-03-28 23:00:01 +02:00
|
|
|
|
unreachable!();
|
Add generation of static libraries to rustc
This commit implements the support necessary for generating both intermediate
and result static rust libraries. This is an implementation of my thoughts in
https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html.
When compiling a library, we still retain the "lib" option, although now there
are "rlib", "staticlib", and "dylib" as options for crate_type (and these are
stackable). The idea of "lib" is to generate the "compiler default" instead of
having too choose (although all are interchangeable). For now I have left the
"complier default" to be a dynamic library for size reasons.
Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an
rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a
dynamic object. I chose this for size reasons, but also because you're probably
not going to be embedding the rustc compiler anywhere any time soon.
Other than the options outlined above, there are a few defaults/preferences that
are now opinionated in the compiler:
* If both a .dylib and .rlib are found for a rust library, the compiler will
prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option
* If generating a "lib", the compiler will generate a dynamic library. This is
overridable by explicitly saying what flavor you'd like (rlib, staticlib,
dylib).
* If no options are passed to the command line, and no crate_type is found in
the destination crate, then an executable is generated
With this change, you can successfully build a rust program with 0 dynamic
dependencies on rust libraries. There is still a dynamic dependency on
librustrt, but I plan on removing that in a subsequent commit.
This change includes no tests just yet. Our current testing
infrastructure/harnesses aren't very amenable to doing flavorful things with
linking, so I'm planning on adding a new mode of testing which I believe belongs
as a separate commit.
Closes #552
2013-11-15 23:03:29 +01:00
|
|
|
|
}
|
2012-05-16 05:23:06 +02:00
|
|
|
|
|
2014-02-25 04:45:20 +01:00
|
|
|
|
fn find_library_crate(&mut self) -> Option<Library> {
|
2014-07-01 17:37:54 +02:00
|
|
|
|
// If an SVH is specified, then this is a transitive dependency that
|
|
|
|
|
// must be loaded via -L plus some filtering.
|
|
|
|
|
if self.hash.is_none() {
|
|
|
|
|
self.should_match_name = false;
|
2015-02-06 22:40:00 +01:00
|
|
|
|
if let Some(s) = self.sess.opts.externs.get(self.crate_name) {
|
2016-08-02 22:53:58 +02:00
|
|
|
|
return self.find_commandline_library(s.iter());
|
2014-07-01 17:37:54 +02:00
|
|
|
|
}
|
|
|
|
|
self.should_match_name = true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-11 09:48:17 +02:00
|
|
|
|
let dypair = self.dylibname();
|
2015-12-05 04:36:01 +01:00
|
|
|
|
let staticpair = self.staticlibname();
|
2013-02-19 08:40:42 +01:00
|
|
|
|
|
Add generation of static libraries to rustc
This commit implements the support necessary for generating both intermediate
and result static rust libraries. This is an implementation of my thoughts in
https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html.
When compiling a library, we still retain the "lib" option, although now there
are "rlib", "staticlib", and "dylib" as options for crate_type (and these are
stackable). The idea of "lib" is to generate the "compiler default" instead of
having too choose (although all are interchangeable). For now I have left the
"complier default" to be a dynamic library for size reasons.
Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an
rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a
dynamic object. I chose this for size reasons, but also because you're probably
not going to be embedding the rustc compiler anywhere any time soon.
Other than the options outlined above, there are a few defaults/preferences that
are now opinionated in the compiler:
* If both a .dylib and .rlib are found for a rust library, the compiler will
prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option
* If generating a "lib", the compiler will generate a dynamic library. This is
overridable by explicitly saying what flavor you'd like (rlib, staticlib,
dylib).
* If no options are passed to the command line, and no crate_type is found in
the destination crate, then an executable is generated
With this change, you can successfully build a rust program with 0 dynamic
dependencies on rust libraries. There is still a dynamic dependency on
librustrt, but I plan on removing that in a subsequent commit.
This change includes no tests just yet. Our current testing
infrastructure/harnesses aren't very amenable to doing flavorful things with
linking, so I'm planning on adding a new mode of testing which I believe belongs
as a separate commit.
Closes #552
2013-11-15 23:03:29 +01:00
|
|
|
|
// want: crate_name.dir_part() + prefix + crate_name.file_part + "-"
|
2014-12-09 18:21:18 +01:00
|
|
|
|
let dylib_prefix = format!("{}{}", dypair.0, self.crate_name);
|
2014-07-01 07:53:13 +02:00
|
|
|
|
let rlib_prefix = format!("lib{}", self.crate_name);
|
2015-12-05 04:36:01 +01:00
|
|
|
|
let staticlib_prefix = format!("{}{}", staticpair.0, self.crate_name);
|
2012-05-16 05:23:06 +02:00
|
|
|
|
|
2016-08-23 00:57:54 +02:00
|
|
|
|
let mut candidates = FnvHashMap();
|
2015-02-05 21:28:17 +01:00
|
|
|
|
let mut staticlibs = vec!();
|
2012-05-16 05:23:06 +02:00
|
|
|
|
|
2014-02-10 21:50:53 +01:00
|
|
|
|
// First, find all possible candidate rlibs and dylibs purely based on
|
|
|
|
|
// the name of the files themselves. We're trying to match against an
|
2014-07-01 07:53:13 +02:00
|
|
|
|
// exact crate name and a possibly an exact hash.
|
2014-02-10 21:50:53 +01:00
|
|
|
|
//
|
|
|
|
|
// During this step, we can filter all found libraries based on the
|
|
|
|
|
// name and id found in the crate id (we ignore the path portion for
|
|
|
|
|
// filename matching), as well as the exact hash (if specified). If we
|
|
|
|
|
// end up having many candidates, we must look at the metadata to
|
|
|
|
|
// perform exact matches against hashes/crate ids. Note that opening up
|
|
|
|
|
// the metadata is where we do an exact match against the full contents
|
|
|
|
|
// of the crate id (path/name/id).
|
|
|
|
|
//
|
|
|
|
|
// The goal of this step is to look at as little metadata as possible.
|
2015-01-06 17:46:07 +01:00
|
|
|
|
self.filesearch.search(|path, kind| {
|
2015-02-27 06:00:43 +01:00
|
|
|
|
let file = match path.file_name().and_then(|s| s.to_str()) {
|
2014-02-10 21:50:53 +01:00
|
|
|
|
None => return FileDoesntMatch,
|
|
|
|
|
Some(file) => file,
|
|
|
|
|
};
|
2015-02-18 20:48:57 +01:00
|
|
|
|
let (hash, rlib) = if file.starts_with(&rlib_prefix[..]) &&
|
2015-02-05 21:28:17 +01:00
|
|
|
|
file.ends_with(".rlib") {
|
2015-01-18 01:15:52 +01:00
|
|
|
|
(&file[(rlib_prefix.len()) .. (file.len() - ".rlib".len())],
|
2014-07-01 07:53:13 +02:00
|
|
|
|
true)
|
2015-02-02 03:53:25 +01:00
|
|
|
|
} else if file.starts_with(&dylib_prefix) &&
|
|
|
|
|
file.ends_with(&dypair.1) {
|
2015-01-18 01:15:52 +01:00
|
|
|
|
(&file[(dylib_prefix.len()) .. (file.len() - dypair.1.len())],
|
2014-07-01 07:53:13 +02:00
|
|
|
|
false)
|
|
|
|
|
} else {
|
2015-02-18 20:48:57 +01:00
|
|
|
|
if file.starts_with(&staticlib_prefix[..]) &&
|
2015-12-05 04:36:01 +01:00
|
|
|
|
file.ends_with(&staticpair.1) {
|
2015-02-05 21:28:17 +01:00
|
|
|
|
staticlibs.push(CrateMismatch {
|
2015-02-27 06:00:43 +01:00
|
|
|
|
path: path.to_path_buf(),
|
2015-02-05 21:28:17 +01:00
|
|
|
|
got: "static".to_string()
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-07-01 07:53:13 +02:00
|
|
|
|
return FileDoesntMatch
|
|
|
|
|
};
|
|
|
|
|
info!("lib candidate: {}", path.display());
|
2014-09-18 23:05:52 +02:00
|
|
|
|
|
2015-01-04 20:07:32 +01:00
|
|
|
|
let hash_str = hash.to_string();
|
2015-03-20 18:43:01 +01:00
|
|
|
|
let slot = candidates.entry(hash_str)
|
2016-08-23 00:57:54 +02:00
|
|
|
|
.or_insert_with(|| (FnvHashMap(), FnvHashMap()));
|
2014-07-01 07:53:13 +02:00
|
|
|
|
let (ref mut rlibs, ref mut dylibs) = *slot;
|
2015-06-06 00:13:19 +02:00
|
|
|
|
fs::canonicalize(path).map(|p| {
|
|
|
|
|
if rlib {
|
|
|
|
|
rlibs.insert(p, kind);
|
|
|
|
|
} else {
|
|
|
|
|
dylibs.insert(p, kind);
|
|
|
|
|
}
|
|
|
|
|
FileMatches
|
|
|
|
|
}).unwrap_or(FileDoesntMatch)
|
2013-11-29 03:03:38 +01:00
|
|
|
|
});
|
2015-06-10 18:22:20 +02:00
|
|
|
|
self.rejected_via_kind.extend(staticlibs);
|
Add generation of static libraries to rustc
This commit implements the support necessary for generating both intermediate
and result static rust libraries. This is an implementation of my thoughts in
https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html.
When compiling a library, we still retain the "lib" option, although now there
are "rlib", "staticlib", and "dylib" as options for crate_type (and these are
stackable). The idea of "lib" is to generate the "compiler default" instead of
having too choose (although all are interchangeable). For now I have left the
"complier default" to be a dynamic library for size reasons.
Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an
rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a
dynamic object. I chose this for size reasons, but also because you're probably
not going to be embedding the rustc compiler anywhere any time soon.
Other than the options outlined above, there are a few defaults/preferences that
are now opinionated in the compiler:
* If both a .dylib and .rlib are found for a rust library, the compiler will
prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option
* If generating a "lib", the compiler will generate a dynamic library. This is
overridable by explicitly saying what flavor you'd like (rlib, staticlib,
dylib).
* If no options are passed to the command line, and no crate_type is found in
the destination crate, then an executable is generated
With this change, you can successfully build a rust program with 0 dynamic
dependencies on rust libraries. There is still a dynamic dependency on
librustrt, but I plan on removing that in a subsequent commit.
This change includes no tests just yet. Our current testing
infrastructure/harnesses aren't very amenable to doing flavorful things with
linking, so I'm planning on adding a new mode of testing which I believe belongs
as a separate commit.
Closes #552
2013-11-15 23:03:29 +01:00
|
|
|
|
|
2014-02-10 21:50:53 +01:00
|
|
|
|
// We have now collected all known libraries into a set of candidates
|
|
|
|
|
// keyed of the filename hash listed. For each filename, we also have a
|
|
|
|
|
// list of rlibs/dylibs that apply. Here, we map each of these lists
|
|
|
|
|
// (per hash), to a Library candidate for returning.
|
|
|
|
|
//
|
|
|
|
|
// A Library candidate is created if the metadata for the set of
|
|
|
|
|
// libraries corresponds to the crate id and hash criteria that this
|
2014-04-21 06:49:39 +02:00
|
|
|
|
// search is being performed for.
|
2016-08-23 00:57:54 +02:00
|
|
|
|
let mut libraries = FnvHashMap();
|
2015-02-01 02:03:04 +01:00
|
|
|
|
for (_hash, (rlibs, dylibs)) in candidates {
|
2016-04-05 04:07:08 +02:00
|
|
|
|
let mut slot = None;
|
|
|
|
|
let rlib = self.extract_one(rlibs, CrateFlavor::Rlib, &mut slot);
|
|
|
|
|
let dylib = self.extract_one(dylibs, CrateFlavor::Dylib, &mut slot);
|
|
|
|
|
if let Some((h, m)) = slot {
|
|
|
|
|
libraries.insert(h, Library {
|
|
|
|
|
dylib: dylib,
|
|
|
|
|
rlib: rlib,
|
|
|
|
|
metadata: m,
|
|
|
|
|
});
|
2014-02-10 21:50:53 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Having now translated all relevant found hashes into libraries, see
|
|
|
|
|
// what we've got and figure out if we found multiple candidates for
|
|
|
|
|
// libraries or not.
|
|
|
|
|
match libraries.len() {
|
Add generation of static libraries to rustc
This commit implements the support necessary for generating both intermediate
and result static rust libraries. This is an implementation of my thoughts in
https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html.
When compiling a library, we still retain the "lib" option, although now there
are "rlib", "staticlib", and "dylib" as options for crate_type (and these are
stackable). The idea of "lib" is to generate the "compiler default" instead of
having too choose (although all are interchangeable). For now I have left the
"complier default" to be a dynamic library for size reasons.
Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an
rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a
dynamic object. I chose this for size reasons, but also because you're probably
not going to be embedding the rustc compiler anywhere any time soon.
Other than the options outlined above, there are a few defaults/preferences that
are now opinionated in the compiler:
* If both a .dylib and .rlib are found for a rust library, the compiler will
prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option
* If generating a "lib", the compiler will generate a dynamic library. This is
overridable by explicitly saying what flavor you'd like (rlib, staticlib,
dylib).
* If no options are passed to the command line, and no crate_type is found in
the destination crate, then an executable is generated
With this change, you can successfully build a rust program with 0 dynamic
dependencies on rust libraries. There is still a dynamic dependency on
librustrt, but I plan on removing that in a subsequent commit.
This change includes no tests just yet. Our current testing
infrastructure/harnesses aren't very amenable to doing flavorful things with
linking, so I'm planning on adding a new mode of testing which I believe belongs
as a separate commit.
Closes #552
2013-11-15 23:03:29 +01:00
|
|
|
|
0 => None,
|
2016-04-05 04:07:08 +02:00
|
|
|
|
1 => Some(libraries.into_iter().next().unwrap().1),
|
Add generation of static libraries to rustc
This commit implements the support necessary for generating both intermediate
and result static rust libraries. This is an implementation of my thoughts in
https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html.
When compiling a library, we still retain the "lib" option, although now there
are "rlib", "staticlib", and "dylib" as options for crate_type (and these are
stackable). The idea of "lib" is to generate the "compiler default" instead of
having too choose (although all are interchangeable). For now I have left the
"complier default" to be a dynamic library for size reasons.
Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an
rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a
dynamic object. I chose this for size reasons, but also because you're probably
not going to be embedding the rustc compiler anywhere any time soon.
Other than the options outlined above, there are a few defaults/preferences that
are now opinionated in the compiler:
* If both a .dylib and .rlib are found for a rust library, the compiler will
prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option
* If generating a "lib", the compiler will generate a dynamic library. This is
overridable by explicitly saying what flavor you'd like (rlib, staticlib,
dylib).
* If no options are passed to the command line, and no crate_type is found in
the destination crate, then an executable is generated
With this change, you can successfully build a rust program with 0 dynamic
dependencies on rust libraries. There is still a dynamic dependency on
librustrt, but I plan on removing that in a subsequent commit.
This change includes no tests just yet. Our current testing
infrastructure/harnesses aren't very amenable to doing flavorful things with
linking, so I'm planning on adding a new mode of testing which I believe belongs
as a separate commit.
Closes #552
2013-11-15 23:03:29 +01:00
|
|
|
|
_ => {
|
2015-12-20 22:00:43 +01:00
|
|
|
|
let mut err = struct_span_err!(self.sess, self.span, E0464,
|
|
|
|
|
"multiple matching crates for `{}`",
|
|
|
|
|
self.crate_name);
|
|
|
|
|
err.note("candidates:");
|
2016-04-05 04:07:08 +02:00
|
|
|
|
for (_, lib) in libraries {
|
2016-07-03 23:38:37 +02:00
|
|
|
|
if let Some((ref p, _)) = lib.dylib {
|
|
|
|
|
err.note(&format!("path: {}", p.display()));
|
Add generation of static libraries to rustc
This commit implements the support necessary for generating both intermediate
and result static rust libraries. This is an implementation of my thoughts in
https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html.
When compiling a library, we still retain the "lib" option, although now there
are "rlib", "staticlib", and "dylib" as options for crate_type (and these are
stackable). The idea of "lib" is to generate the "compiler default" instead of
having too choose (although all are interchangeable). For now I have left the
"complier default" to be a dynamic library for size reasons.
Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an
rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a
dynamic object. I chose this for size reasons, but also because you're probably
not going to be embedding the rustc compiler anywhere any time soon.
Other than the options outlined above, there are a few defaults/preferences that
are now opinionated in the compiler:
* If both a .dylib and .rlib are found for a rust library, the compiler will
prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option
* If generating a "lib", the compiler will generate a dynamic library. This is
overridable by explicitly saying what flavor you'd like (rlib, staticlib,
dylib).
* If no options are passed to the command line, and no crate_type is found in
the destination crate, then an executable is generated
With this change, you can successfully build a rust program with 0 dynamic
dependencies on rust libraries. There is still a dynamic dependency on
librustrt, but I plan on removing that in a subsequent commit.
This change includes no tests just yet. Our current testing
infrastructure/harnesses aren't very amenable to doing flavorful things with
linking, so I'm planning on adding a new mode of testing which I believe belongs
as a separate commit.
Closes #552
2013-11-15 23:03:29 +01:00
|
|
|
|
}
|
2016-07-03 23:38:37 +02:00
|
|
|
|
if let Some((ref p, _)) = lib.rlib {
|
|
|
|
|
err.note(&format!("path: {}", p.display()));
|
Add generation of static libraries to rustc
This commit implements the support necessary for generating both intermediate
and result static rust libraries. This is an implementation of my thoughts in
https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html.
When compiling a library, we still retain the "lib" option, although now there
are "rlib", "staticlib", and "dylib" as options for crate_type (and these are
stackable). The idea of "lib" is to generate the "compiler default" instead of
having too choose (although all are interchangeable). For now I have left the
"complier default" to be a dynamic library for size reasons.
Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an
rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a
dynamic object. I chose this for size reasons, but also because you're probably
not going to be embedding the rustc compiler anywhere any time soon.
Other than the options outlined above, there are a few defaults/preferences that
are now opinionated in the compiler:
* If both a .dylib and .rlib are found for a rust library, the compiler will
prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option
* If generating a "lib", the compiler will generate a dynamic library. This is
overridable by explicitly saying what flavor you'd like (rlib, staticlib,
dylib).
* If no options are passed to the command line, and no crate_type is found in
the destination crate, then an executable is generated
With this change, you can successfully build a rust program with 0 dynamic
dependencies on rust libraries. There is still a dynamic dependency on
librustrt, but I plan on removing that in a subsequent commit.
This change includes no tests just yet. Our current testing
infrastructure/harnesses aren't very amenable to doing flavorful things with
linking, so I'm planning on adding a new mode of testing which I believe belongs
as a separate commit.
Closes #552
2013-11-15 23:03:29 +01:00
|
|
|
|
}
|
2016-09-16 16:25:54 +02:00
|
|
|
|
note_crate_name(&mut err, &lib.metadata.get_root().name);
|
Add generation of static libraries to rustc
This commit implements the support necessary for generating both intermediate
and result static rust libraries. This is an implementation of my thoughts in
https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html.
When compiling a library, we still retain the "lib" option, although now there
are "rlib", "staticlib", and "dylib" as options for crate_type (and these are
stackable). The idea of "lib" is to generate the "compiler default" instead of
having too choose (although all are interchangeable). For now I have left the
"complier default" to be a dynamic library for size reasons.
Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an
rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a
dynamic object. I chose this for size reasons, but also because you're probably
not going to be embedding the rustc compiler anywhere any time soon.
Other than the options outlined above, there are a few defaults/preferences that
are now opinionated in the compiler:
* If both a .dylib and .rlib are found for a rust library, the compiler will
prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option
* If generating a "lib", the compiler will generate a dynamic library. This is
overridable by explicitly saying what flavor you'd like (rlib, staticlib,
dylib).
* If no options are passed to the command line, and no crate_type is found in
the destination crate, then an executable is generated
With this change, you can successfully build a rust program with 0 dynamic
dependencies on rust libraries. There is still a dynamic dependency on
librustrt, but I plan on removing that in a subsequent commit.
This change includes no tests just yet. Our current testing
infrastructure/harnesses aren't very amenable to doing flavorful things with
linking, so I'm planning on adding a new mode of testing which I believe belongs
as a separate commit.
Closes #552
2013-11-15 23:03:29 +01:00
|
|
|
|
}
|
2015-12-20 22:00:43 +01:00
|
|
|
|
err.emit();
|
2013-06-24 21:38:14 +02:00
|
|
|
|
None
|
Add generation of static libraries to rustc
This commit implements the support necessary for generating both intermediate
and result static rust libraries. This is an implementation of my thoughts in
https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html.
When compiling a library, we still retain the "lib" option, although now there
are "rlib", "staticlib", and "dylib" as options for crate_type (and these are
stackable). The idea of "lib" is to generate the "compiler default" instead of
having too choose (although all are interchangeable). For now I have left the
"complier default" to be a dynamic library for size reasons.
Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an
rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a
dynamic object. I chose this for size reasons, but also because you're probably
not going to be embedding the rustc compiler anywhere any time soon.
Other than the options outlined above, there are a few defaults/preferences that
are now opinionated in the compiler:
* If both a .dylib and .rlib are found for a rust library, the compiler will
prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option
* If generating a "lib", the compiler will generate a dynamic library. This is
overridable by explicitly saying what flavor you'd like (rlib, staticlib,
dylib).
* If no options are passed to the command line, and no crate_type is found in
the destination crate, then an executable is generated
With this change, you can successfully build a rust program with 0 dynamic
dependencies on rust libraries. There is still a dynamic dependency on
librustrt, but I plan on removing that in a subsequent commit.
This change includes no tests just yet. Our current testing
infrastructure/harnesses aren't very amenable to doing flavorful things with
linking, so I'm planning on adding a new mode of testing which I believe belongs
as a separate commit.
Closes #552
2013-11-15 23:03:29 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-10 21:50:53 +01:00
|
|
|
|
// Attempts to extract *one* library from the set `m`. If the set has no
|
|
|
|
|
// elements, `None` is returned. If the set has more than one element, then
|
|
|
|
|
// the errors and notes are emitted about the set of libraries.
|
|
|
|
|
//
|
|
|
|
|
// With only one library in the set, this function will extract it, and then
|
|
|
|
|
// read the metadata from it if `*slot` is `None`. If the metadata couldn't
|
|
|
|
|
// be read, it is assumed that the file isn't a valid rust library (no
|
|
|
|
|
// errors are emitted).
|
2016-08-23 00:57:54 +02:00
|
|
|
|
fn extract_one(&mut self, m: FnvHashMap<PathBuf, PathKind>, flavor: CrateFlavor,
|
2016-04-05 04:07:08 +02:00
|
|
|
|
slot: &mut Option<(Svh, MetadataBlob)>) -> Option<(PathBuf, PathKind)> {
|
|
|
|
|
let mut ret: Option<(PathBuf, PathKind)> = None;
|
2015-01-25 11:58:43 +01:00
|
|
|
|
let mut error = 0;
|
Add generation of static libraries to rustc
This commit implements the support necessary for generating both intermediate
and result static rust libraries. This is an implementation of my thoughts in
https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html.
When compiling a library, we still retain the "lib" option, although now there
are "rlib", "staticlib", and "dylib" as options for crate_type (and these are
stackable). The idea of "lib" is to generate the "compiler default" instead of
having too choose (although all are interchangeable). For now I have left the
"complier default" to be a dynamic library for size reasons.
Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an
rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a
dynamic object. I chose this for size reasons, but also because you're probably
not going to be embedding the rustc compiler anywhere any time soon.
Other than the options outlined above, there are a few defaults/preferences that
are now opinionated in the compiler:
* If both a .dylib and .rlib are found for a rust library, the compiler will
prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option
* If generating a "lib", the compiler will generate a dynamic library. This is
overridable by explicitly saying what flavor you'd like (rlib, staticlib,
dylib).
* If no options are passed to the command line, and no crate_type is found in
the destination crate, then an executable is generated
With this change, you can successfully build a rust program with 0 dynamic
dependencies on rust libraries. There is still a dynamic dependency on
librustrt, but I plan on removing that in a subsequent commit.
This change includes no tests just yet. Our current testing
infrastructure/harnesses aren't very amenable to doing flavorful things with
linking, so I'm planning on adding a new mode of testing which I believe belongs
as a separate commit.
Closes #552
2013-11-15 23:03:29 +01:00
|
|
|
|
|
2014-04-07 22:13:21 +02:00
|
|
|
|
if slot.is_some() {
|
|
|
|
|
// FIXME(#10786): for an optimization, we only read one of the
|
2016-04-05 04:07:08 +02:00
|
|
|
|
// libraries' metadata sections. In theory we should
|
2014-04-07 22:13:21 +02:00
|
|
|
|
// read both, but reading dylib metadata is quite
|
|
|
|
|
// slow.
|
2015-03-25 00:53:34 +01:00
|
|
|
|
if m.is_empty() {
|
2014-04-07 22:13:21 +02:00
|
|
|
|
return None
|
|
|
|
|
} else if m.len() == 1 {
|
2014-09-15 05:27:36 +02:00
|
|
|
|
return Some(m.into_iter().next().unwrap())
|
2014-04-07 22:13:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-20 22:00:43 +01:00
|
|
|
|
let mut err: Option<DiagnosticBuilder> = None;
|
2015-02-01 02:03:04 +01:00
|
|
|
|
for (lib, kind) in m {
|
2014-03-07 23:37:18 +01:00
|
|
|
|
info!("{} reading metadata from: {}", flavor, lib.display());
|
2016-04-05 04:07:08 +02:00
|
|
|
|
let (hash, metadata) = match get_metadata_section(self.target, flavor, &lib) {
|
2014-03-07 23:37:18 +01:00
|
|
|
|
Ok(blob) => {
|
2016-09-15 10:04:00 +02:00
|
|
|
|
if let Some(h) = self.crate_matches(&blob, &lib) {
|
2016-04-05 04:07:08 +02:00
|
|
|
|
(h, blob)
|
2014-02-10 21:50:53 +01:00
|
|
|
|
} else {
|
|
|
|
|
info!("metadata mismatch");
|
2014-03-19 16:47:59 +01:00
|
|
|
|
continue
|
2014-02-10 21:50:53 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-28 19:08:20 +01:00
|
|
|
|
Err(err) => {
|
|
|
|
|
info!("no metadata found: {}", err);
|
2014-03-19 16:47:59 +01:00
|
|
|
|
continue
|
Add generation of static libraries to rustc
This commit implements the support necessary for generating both intermediate
and result static rust libraries. This is an implementation of my thoughts in
https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html.
When compiling a library, we still retain the "lib" option, although now there
are "rlib", "staticlib", and "dylib" as options for crate_type (and these are
stackable). The idea of "lib" is to generate the "compiler default" instead of
having too choose (although all are interchangeable). For now I have left the
"complier default" to be a dynamic library for size reasons.
Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an
rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a
dynamic object. I chose this for size reasons, but also because you're probably
not going to be embedding the rustc compiler anywhere any time soon.
Other than the options outlined above, there are a few defaults/preferences that
are now opinionated in the compiler:
* If both a .dylib and .rlib are found for a rust library, the compiler will
prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option
* If generating a "lib", the compiler will generate a dynamic library. This is
overridable by explicitly saying what flavor you'd like (rlib, staticlib,
dylib).
* If no options are passed to the command line, and no crate_type is found in
the destination crate, then an executable is generated
With this change, you can successfully build a rust program with 0 dynamic
dependencies on rust libraries. There is still a dynamic dependency on
librustrt, but I plan on removing that in a subsequent commit.
This change includes no tests just yet. Our current testing
infrastructure/harnesses aren't very amenable to doing flavorful things with
linking, so I'm planning on adding a new mode of testing which I believe belongs
as a separate commit.
Closes #552
2013-11-15 23:03:29 +01:00
|
|
|
|
}
|
2014-03-19 16:47:59 +01:00
|
|
|
|
};
|
2016-04-05 04:07:08 +02:00
|
|
|
|
// If we see multiple hashes, emit an error about duplicate candidates.
|
|
|
|
|
if slot.as_ref().map_or(false, |s| s.0 != hash) {
|
2015-12-20 22:00:43 +01:00
|
|
|
|
let mut e = struct_span_err!(self.sess, self.span, E0465,
|
|
|
|
|
"multiple {} candidates for `{}` found",
|
|
|
|
|
flavor, self.crate_name);
|
|
|
|
|
e.span_note(self.span,
|
|
|
|
|
&format!(r"candidate #1: {}",
|
|
|
|
|
ret.as_ref().unwrap().0
|
|
|
|
|
.display()));
|
|
|
|
|
if let Some(ref mut e) = err {
|
|
|
|
|
e.emit();
|
|
|
|
|
}
|
|
|
|
|
err = Some(e);
|
2014-03-19 16:47:59 +01:00
|
|
|
|
error = 1;
|
2016-04-05 04:07:08 +02:00
|
|
|
|
*slot = None;
|
2014-03-19 16:47:59 +01:00
|
|
|
|
}
|
|
|
|
|
if error > 0 {
|
|
|
|
|
error += 1;
|
2015-12-20 22:00:43 +01:00
|
|
|
|
err.as_mut().unwrap().span_note(self.span,
|
|
|
|
|
&format!(r"candidate #{}: {}", error,
|
|
|
|
|
lib.display()));
|
2014-03-19 16:47:59 +01:00
|
|
|
|
continue
|
Add generation of static libraries to rustc
This commit implements the support necessary for generating both intermediate
and result static rust libraries. This is an implementation of my thoughts in
https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html.
When compiling a library, we still retain the "lib" option, although now there
are "rlib", "staticlib", and "dylib" as options for crate_type (and these are
stackable). The idea of "lib" is to generate the "compiler default" instead of
having too choose (although all are interchangeable). For now I have left the
"complier default" to be a dynamic library for size reasons.
Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an
rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a
dynamic object. I chose this for size reasons, but also because you're probably
not going to be embedding the rustc compiler anywhere any time soon.
Other than the options outlined above, there are a few defaults/preferences that
are now opinionated in the compiler:
* If both a .dylib and .rlib are found for a rust library, the compiler will
prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option
* If generating a "lib", the compiler will generate a dynamic library. This is
overridable by explicitly saying what flavor you'd like (rlib, staticlib,
dylib).
* If no options are passed to the command line, and no crate_type is found in
the destination crate, then an executable is generated
With this change, you can successfully build a rust program with 0 dynamic
dependencies on rust libraries. There is still a dynamic dependency on
librustrt, but I plan on removing that in a subsequent commit.
This change includes no tests just yet. Our current testing
infrastructure/harnesses aren't very amenable to doing flavorful things with
linking, so I'm planning on adding a new mode of testing which I believe belongs
as a separate commit.
Closes #552
2013-11-15 23:03:29 +01:00
|
|
|
|
}
|
2016-04-05 04:07:08 +02:00
|
|
|
|
*slot = Some((hash, metadata));
|
2015-01-06 17:46:07 +01:00
|
|
|
|
ret = Some((lib, kind));
|
Add generation of static libraries to rustc
This commit implements the support necessary for generating both intermediate
and result static rust libraries. This is an implementation of my thoughts in
https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html.
When compiling a library, we still retain the "lib" option, although now there
are "rlib", "staticlib", and "dylib" as options for crate_type (and these are
stackable). The idea of "lib" is to generate the "compiler default" instead of
having too choose (although all are interchangeable). For now I have left the
"complier default" to be a dynamic library for size reasons.
Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an
rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a
dynamic object. I chose this for size reasons, but also because you're probably
not going to be embedding the rustc compiler anywhere any time soon.
Other than the options outlined above, there are a few defaults/preferences that
are now opinionated in the compiler:
* If both a .dylib and .rlib are found for a rust library, the compiler will
prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option
* If generating a "lib", the compiler will generate a dynamic library. This is
overridable by explicitly saying what flavor you'd like (rlib, staticlib,
dylib).
* If no options are passed to the command line, and no crate_type is found in
the destination crate, then an executable is generated
With this change, you can successfully build a rust program with 0 dynamic
dependencies on rust libraries. There is still a dynamic dependency on
librustrt, but I plan on removing that in a subsequent commit.
This change includes no tests just yet. Our current testing
infrastructure/harnesses aren't very amenable to doing flavorful things with
linking, so I'm planning on adding a new mode of testing which I believe belongs
as a separate commit.
Closes #552
2013-11-15 23:03:29 +01:00
|
|
|
|
}
|
2015-12-20 22:00:43 +01:00
|
|
|
|
|
|
|
|
|
if error > 0 {
|
|
|
|
|
err.unwrap().emit();
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
ret
|
|
|
|
|
}
|
Add generation of static libraries to rustc
This commit implements the support necessary for generating both intermediate
and result static rust libraries. This is an implementation of my thoughts in
https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html.
When compiling a library, we still retain the "lib" option, although now there
are "rlib", "staticlib", and "dylib" as options for crate_type (and these are
stackable). The idea of "lib" is to generate the "compiler default" instead of
having too choose (although all are interchangeable). For now I have left the
"complier default" to be a dynamic library for size reasons.
Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an
rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a
dynamic object. I chose this for size reasons, but also because you're probably
not going to be embedding the rustc compiler anywhere any time soon.
Other than the options outlined above, there are a few defaults/preferences that
are now opinionated in the compiler:
* If both a .dylib and .rlib are found for a rust library, the compiler will
prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option
* If generating a "lib", the compiler will generate a dynamic library. This is
overridable by explicitly saying what flavor you'd like (rlib, staticlib,
dylib).
* If no options are passed to the command line, and no crate_type is found in
the destination crate, then an executable is generated
With this change, you can successfully build a rust program with 0 dynamic
dependencies on rust libraries. There is still a dynamic dependency on
librustrt, but I plan on removing that in a subsequent commit.
This change includes no tests just yet. Our current testing
infrastructure/harnesses aren't very amenable to doing flavorful things with
linking, so I'm planning on adding a new mode of testing which I believe belongs
as a separate commit.
Closes #552
2013-11-15 23:03:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 10:04:00 +02:00
|
|
|
|
fn crate_matches(&mut self, metadata: &MetadataBlob, libpath: &Path) -> Option<Svh> {
|
2016-09-16 16:25:54 +02:00
|
|
|
|
let root = metadata.get_root();
|
2016-10-01 00:24:50 +02:00
|
|
|
|
let rustc_version = rustc_version();
|
|
|
|
|
if root.rustc_version != rustc_version {
|
2016-09-16 16:25:54 +02:00
|
|
|
|
info!("Rejecting via version: expected {} got {}",
|
2016-10-01 00:24:50 +02:00
|
|
|
|
rustc_version, root.rustc_version);
|
2016-07-01 23:36:33 +02:00
|
|
|
|
self.rejected_via_version.push(CrateMismatch {
|
|
|
|
|
path: libpath.to_path_buf(),
|
2016-09-16 16:25:54 +02:00
|
|
|
|
got: root.rustc_version
|
2016-07-01 23:36:33 +02:00
|
|
|
|
});
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-01 17:37:54 +02:00
|
|
|
|
if self.should_match_name {
|
2016-09-16 16:25:54 +02:00
|
|
|
|
if self.crate_name != root.name {
|
2016-09-08 18:05:50 +02:00
|
|
|
|
info!("Rejecting via crate name"); return None;
|
2014-07-01 17:37:54 +02:00
|
|
|
|
}
|
2014-03-01 20:09:30 +01:00
|
|
|
|
}
|
2014-04-17 17:52:25 +02:00
|
|
|
|
|
2016-09-16 16:25:54 +02:00
|
|
|
|
if root.triple != self.triple {
|
2016-09-08 18:05:50 +02:00
|
|
|
|
info!("Rejecting via crate triple: expected {} got {}",
|
2016-09-16 16:25:54 +02:00
|
|
|
|
self.triple, root.triple);
|
2014-05-10 03:45:36 +02:00
|
|
|
|
self.rejected_via_triple.push(CrateMismatch {
|
2015-02-27 06:00:43 +01:00
|
|
|
|
path: libpath.to_path_buf(),
|
2016-09-16 16:25:54 +02:00
|
|
|
|
got: root.triple
|
2014-05-10 03:45:36 +02:00
|
|
|
|
});
|
2016-04-05 04:07:08 +02:00
|
|
|
|
return None;
|
2014-04-17 17:52:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-05 04:07:08 +02:00
|
|
|
|
if let Some(myhash) = self.hash {
|
2016-09-16 16:25:54 +02:00
|
|
|
|
if *myhash != root.hash {
|
2016-09-08 18:05:50 +02:00
|
|
|
|
info!("Rejecting via hash: expected {} got {}",
|
2016-09-16 16:25:54 +02:00
|
|
|
|
*myhash, root.hash);
|
2016-04-05 04:07:08 +02:00
|
|
|
|
self.rejected_via_hash.push(CrateMismatch {
|
|
|
|
|
path: libpath.to_path_buf(),
|
2016-05-06 11:01:09 +02:00
|
|
|
|
got: myhash.to_string()
|
2016-04-05 04:07:08 +02:00
|
|
|
|
});
|
|
|
|
|
return None;
|
2014-02-25 04:45:20 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-05 04:07:08 +02:00
|
|
|
|
|
2016-09-16 16:25:54 +02:00
|
|
|
|
Some(root.hash)
|
2014-02-25 04:45:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-17 17:52:25 +02:00
|
|
|
|
|
Add generation of static libraries to rustc
This commit implements the support necessary for generating both intermediate
and result static rust libraries. This is an implementation of my thoughts in
https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html.
When compiling a library, we still retain the "lib" option, although now there
are "rlib", "staticlib", and "dylib" as options for crate_type (and these are
stackable). The idea of "lib" is to generate the "compiler default" instead of
having too choose (although all are interchangeable). For now I have left the
"complier default" to be a dynamic library for size reasons.
Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an
rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a
dynamic object. I chose this for size reasons, but also because you're probably
not going to be embedding the rustc compiler anywhere any time soon.
Other than the options outlined above, there are a few defaults/preferences that
are now opinionated in the compiler:
* If both a .dylib and .rlib are found for a rust library, the compiler will
prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option
* If generating a "lib", the compiler will generate a dynamic library. This is
overridable by explicitly saying what flavor you'd like (rlib, staticlib,
dylib).
* If no options are passed to the command line, and no crate_type is found in
the destination crate, then an executable is generated
With this change, you can successfully build a rust program with 0 dynamic
dependencies on rust libraries. There is still a dynamic dependency on
librustrt, but I plan on removing that in a subsequent commit.
This change includes no tests just yet. Our current testing
infrastructure/harnesses aren't very amenable to doing flavorful things with
linking, so I'm planning on adding a new mode of testing which I believe belongs
as a separate commit.
Closes #552
2013-11-15 23:03:29 +01:00
|
|
|
|
// Returns the corresponding (prefix, suffix) that files need to have for
|
|
|
|
|
// dynamic libraries
|
2014-07-23 20:56:36 +02:00
|
|
|
|
fn dylibname(&self) -> (String, String) {
|
2015-01-09 02:14:10 +01:00
|
|
|
|
let t = &self.target;
|
2014-07-23 20:56:36 +02:00
|
|
|
|
(t.options.dll_prefix.clone(), t.options.dll_suffix.clone())
|
2013-07-31 22:47:32 +02:00
|
|
|
|
}
|
2014-04-17 17:52:25 +02:00
|
|
|
|
|
2015-12-05 04:36:01 +01:00
|
|
|
|
// Returns the corresponding (prefix, suffix) that files need to have for
|
|
|
|
|
// static libraries
|
|
|
|
|
fn staticlibname(&self) -> (String, String) {
|
|
|
|
|
let t = &self.target;
|
|
|
|
|
(t.options.staticlib_prefix.clone(), t.options.staticlib_suffix.clone())
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 22:53:58 +02:00
|
|
|
|
fn find_commandline_library<'b, LOCS> (&mut self, locs: LOCS) -> Option<Library>
|
|
|
|
|
where LOCS: Iterator<Item=&'b String>
|
|
|
|
|
{
|
2014-07-01 17:37:54 +02:00
|
|
|
|
// First, filter out all libraries that look suspicious. We only accept
|
|
|
|
|
// files which actually exist that have the correct naming scheme for
|
|
|
|
|
// rlibs/dylibs.
|
|
|
|
|
let sess = self.sess;
|
|
|
|
|
let dylibname = self.dylibname();
|
2016-08-23 00:57:54 +02:00
|
|
|
|
let mut rlibs = FnvHashMap();
|
|
|
|
|
let mut dylibs = FnvHashMap();
|
2014-10-13 05:38:04 +02:00
|
|
|
|
{
|
2016-08-02 22:53:58 +02:00
|
|
|
|
let locs = locs.map(|l| PathBuf::from(l)).filter(|loc| {
|
2014-10-13 05:38:04 +02:00
|
|
|
|
if !loc.exists() {
|
2015-01-07 17:58:31 +01:00
|
|
|
|
sess.err(&format!("extern location for {} does not exist: {}",
|
2015-02-20 20:08:14 +01:00
|
|
|
|
self.crate_name, loc.display()));
|
2014-10-13 05:38:04 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-02-27 06:00:43 +01:00
|
|
|
|
let file = match loc.file_name().and_then(|s| s.to_str()) {
|
2014-10-13 05:38:04 +02:00
|
|
|
|
Some(file) => file,
|
|
|
|
|
None => {
|
2015-01-07 17:58:31 +01:00
|
|
|
|
sess.err(&format!("extern location for {} is not a file: {}",
|
2015-02-20 20:08:14 +01:00
|
|
|
|
self.crate_name, loc.display()));
|
2014-10-13 05:38:04 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
if file.starts_with("lib") && file.ends_with(".rlib") {
|
|
|
|
|
return true
|
|
|
|
|
} else {
|
2014-07-23 20:56:36 +02:00
|
|
|
|
let (ref prefix, ref suffix) = dylibname;
|
2015-02-18 20:48:57 +01:00
|
|
|
|
if file.starts_with(&prefix[..]) &&
|
|
|
|
|
file.ends_with(&suffix[..]) {
|
2014-07-23 20:56:36 +02:00
|
|
|
|
return true
|
2014-07-01 17:37:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-23 22:54:37 +01:00
|
|
|
|
sess.struct_err(&format!("extern location for {} is of an unknown type: {}",
|
|
|
|
|
self.crate_name, loc.display()))
|
|
|
|
|
.help(&format!("file name should be lib*.rlib or {}*.{}",
|
|
|
|
|
dylibname.0, dylibname.1))
|
|
|
|
|
.emit();
|
2014-10-13 05:38:04 +02:00
|
|
|
|
false
|
|
|
|
|
});
|
2014-07-01 17:37:54 +02:00
|
|
|
|
|
2015-01-06 17:46:07 +01:00
|
|
|
|
// Now that we have an iterator of good candidates, make sure
|
|
|
|
|
// there's at most one rlib and at most one dylib.
|
2014-10-13 05:38:04 +02:00
|
|
|
|
for loc in locs {
|
2015-02-27 06:00:43 +01:00
|
|
|
|
if loc.file_name().unwrap().to_str().unwrap().ends_with(".rlib") {
|
2015-04-16 08:21:13 +02:00
|
|
|
|
rlibs.insert(fs::canonicalize(&loc).unwrap(),
|
2015-01-06 17:46:07 +01:00
|
|
|
|
PathKind::ExternFlag);
|
2014-10-13 05:38:04 +02:00
|
|
|
|
} else {
|
2015-04-16 08:21:13 +02:00
|
|
|
|
dylibs.insert(fs::canonicalize(&loc).unwrap(),
|
2015-01-06 17:46:07 +01:00
|
|
|
|
PathKind::ExternFlag);
|
2014-10-13 05:38:04 +02:00
|
|
|
|
}
|
2014-07-01 17:37:54 +02:00
|
|
|
|
}
|
2014-10-13 05:38:04 +02:00
|
|
|
|
};
|
2014-07-01 17:37:54 +02:00
|
|
|
|
|
|
|
|
|
// Extract the rlib/dylib pair.
|
2016-04-05 04:07:08 +02:00
|
|
|
|
let mut slot = None;
|
|
|
|
|
let rlib = self.extract_one(rlibs, CrateFlavor::Rlib, &mut slot);
|
|
|
|
|
let dylib = self.extract_one(dylibs, CrateFlavor::Dylib, &mut slot);
|
2014-07-01 17:37:54 +02:00
|
|
|
|
|
|
|
|
|
if rlib.is_none() && dylib.is_none() { return None }
|
2016-04-05 04:07:08 +02:00
|
|
|
|
match slot {
|
|
|
|
|
Some((_, metadata)) => Some(Library {
|
2014-07-01 17:37:54 +02:00
|
|
|
|
dylib: dylib,
|
|
|
|
|
rlib: rlib,
|
|
|
|
|
metadata: metadata,
|
|
|
|
|
}),
|
|
|
|
|
None => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-05-16 05:23:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-20 22:00:43 +01:00
|
|
|
|
pub fn note_crate_name(err: &mut DiagnosticBuilder, name: &str) {
|
|
|
|
|
err.note(&format!("crate name: {}", name));
|
2012-05-16 05:23:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-17 05:58:21 +01:00
|
|
|
|
impl ArchiveMetadata {
|
|
|
|
|
fn new(ar: ArchiveRO) -> Option<ArchiveMetadata> {
|
2015-04-15 01:28:50 +02:00
|
|
|
|
let data = {
|
2015-10-23 07:07:19 +02:00
|
|
|
|
let section = ar.iter().filter_map(|s| s.ok()).find(|sect| {
|
2015-04-15 01:28:50 +02:00
|
|
|
|
sect.name() == Some(METADATA_FILENAME)
|
|
|
|
|
});
|
|
|
|
|
match section {
|
|
|
|
|
Some(s) => s.data() as *const [u8],
|
|
|
|
|
None => {
|
|
|
|
|
debug!("didn't find '{}' in the archive", METADATA_FILENAME);
|
|
|
|
|
return None;
|
|
|
|
|
}
|
2014-10-25 21:27:15 +02:00
|
|
|
|
}
|
2013-12-17 05:58:21 +01:00
|
|
|
|
};
|
2014-10-25 21:27:15 +02:00
|
|
|
|
|
2013-12-17 05:58:21 +01:00
|
|
|
|
Some(ArchiveMetadata {
|
2014-06-06 15:51:42 +02:00
|
|
|
|
_archive: ar,
|
2013-12-17 05:58:21 +01:00
|
|
|
|
data: data,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-25 21:27:15 +02:00
|
|
|
|
pub fn as_slice<'a>(&'a self) -> &'a [u8] { unsafe { &*self.data } }
|
2013-12-17 05:58:21 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-02 13:41:31 +02:00
|
|
|
|
fn verify_decompressed_encoding_version(blob: &MetadataBlob, filename: &Path)
|
|
|
|
|
-> Result<(), String>
|
|
|
|
|
{
|
2016-09-16 16:25:54 +02:00
|
|
|
|
if !blob.is_compatible() {
|
2016-07-02 13:41:31 +02:00
|
|
|
|
Err((format!("incompatible metadata version found: '{}'",
|
|
|
|
|
filename.display())))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-17 05:58:21 +01:00
|
|
|
|
// Just a small wrapper to time how long reading metadata takes.
|
2016-04-08 07:49:48 +02:00
|
|
|
|
fn get_metadata_section(target: &Target, flavor: CrateFlavor, filename: &Path)
|
2015-05-11 23:31:06 +02:00
|
|
|
|
-> Result<MetadataBlob, String> {
|
2015-12-03 02:31:49 +01:00
|
|
|
|
let start = Instant::now();
|
2016-04-08 07:49:48 +02:00
|
|
|
|
let ret = get_metadata_section_imp(target, flavor, filename);
|
2015-12-03 02:31:49 +01:00
|
|
|
|
info!("reading {:?} => {:?}", filename.file_name().unwrap(),
|
|
|
|
|
start.elapsed());
|
|
|
|
|
return ret
|
2013-12-17 05:58:21 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-08 07:49:48 +02:00
|
|
|
|
fn get_metadata_section_imp(target: &Target, flavor: CrateFlavor, filename: &Path)
|
2015-05-11 23:31:06 +02:00
|
|
|
|
-> Result<MetadataBlob, String> {
|
2014-03-07 23:37:18 +01:00
|
|
|
|
if !filename.exists() {
|
2014-05-28 05:44:58 +02:00
|
|
|
|
return Err(format!("no such file: '{}'", filename.display()));
|
2014-03-07 23:37:18 +01:00
|
|
|
|
}
|
2016-04-08 07:49:48 +02:00
|
|
|
|
if flavor == CrateFlavor::Rlib {
|
2013-12-17 05:58:21 +01:00
|
|
|
|
// Use ArchiveRO for speed here, it's backed by LLVM and uses mmap
|
|
|
|
|
// internally to read the file. We also avoid even using a memcpy by
|
|
|
|
|
// just keeping the archive along while the metadata is in use.
|
|
|
|
|
let archive = match ArchiveRO::open(filename) {
|
|
|
|
|
Some(ar) => ar,
|
|
|
|
|
None => {
|
|
|
|
|
debug!("llvm didn't like `{}`", filename.display());
|
2014-05-28 05:44:58 +02:00
|
|
|
|
return Err(format!("failed to read rlib metadata: '{}'",
|
|
|
|
|
filename.display()));
|
2013-12-17 05:58:21 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
2016-09-16 16:25:54 +02:00
|
|
|
|
return match ArchiveMetadata::new(archive).map(|ar| MetadataBlob::Archive(ar)) {
|
2015-02-24 09:50:36 +01:00
|
|
|
|
None => Err(format!("failed to read rlib metadata: '{}'",
|
|
|
|
|
filename.display())),
|
2016-07-02 13:41:31 +02:00
|
|
|
|
Some(blob) => {
|
2016-08-27 16:53:05 +02:00
|
|
|
|
verify_decompressed_encoding_version(&blob, filename)?;
|
2016-07-02 13:41:31 +02:00
|
|
|
|
Ok(blob)
|
|
|
|
|
}
|
2015-02-24 09:50:36 +01:00
|
|
|
|
};
|
Store metadata separately in rlib files
Right now whenever an rlib file is linked against, all of the metadata from the
rlib is pulled in to the final staticlib or binary. The reason for this is that
the metadata is currently stored in a section of the object file. Note that this
is intentional for dynamic libraries in order to distribute metadata bundled
with static libraries.
This commit alters the situation for rlib libraries to instead store the
metadata in a separate file in the archive. In doing so, when the archive is
passed to the linker, none of the metadata will get pulled into the result
executable. Furthermore, the metadata file is skipped when assembling rlibs into
an archive.
The snag in this implementation comes with multiple output formats. When
generating a dylib, the metadata needs to be in the object file, but when
generating an rlib this needs to be separate. In order to accomplish this, the
metadata variable is inserted into an entirely separate LLVM Module which is
then codegen'd into a different location (foo.metadata.o). This is then linked
into dynamic libraries and silently ignored for rlib files.
While changing how metadata is inserted into archives, I have also stopped
compressing metadata when inserted into rlib files. We have wanted to stop
compressing metadata, but the sections it creates in object file sections are
apparently too large. Thankfully if it's just an arbitrary file it doesn't
matter how large it is.
I have seen massive reductions in executable sizes, as well as staticlib output
sizes (to confirm that this is all working).
2013-12-04 02:41:01 +01:00
|
|
|
|
}
|
2013-01-23 20:43:58 +01:00
|
|
|
|
unsafe {
|
2015-02-27 06:00:43 +01:00
|
|
|
|
let buf = common::path2cstr(filename);
|
2014-11-25 22:28:35 +01:00
|
|
|
|
let mb = llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf.as_ptr());
|
2015-03-26 01:06:52 +01:00
|
|
|
|
if mb as isize == 0 {
|
2014-05-28 05:44:58 +02:00
|
|
|
|
return Err(format!("error reading library: '{}'",
|
|
|
|
|
filename.display()))
|
2014-03-07 23:37:18 +01:00
|
|
|
|
}
|
Add generation of static libraries to rustc
This commit implements the support necessary for generating both intermediate
and result static rust libraries. This is an implementation of my thoughts in
https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html.
When compiling a library, we still retain the "lib" option, although now there
are "rlib", "staticlib", and "dylib" as options for crate_type (and these are
stackable). The idea of "lib" is to generate the "compiler default" instead of
having too choose (although all are interchangeable). For now I have left the
"complier default" to be a dynamic library for size reasons.
Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an
rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a
dynamic object. I chose this for size reasons, but also because you're probably
not going to be embedding the rustc compiler anywhere any time soon.
Other than the options outlined above, there are a few defaults/preferences that
are now opinionated in the compiler:
* If both a .dylib and .rlib are found for a rust library, the compiler will
prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option
* If generating a "lib", the compiler will generate a dynamic library. This is
overridable by explicitly saying what flavor you'd like (rlib, staticlib,
dylib).
* If no options are passed to the command line, and no crate_type is found in
the destination crate, then an executable is generated
With this change, you can successfully build a rust program with 0 dynamic
dependencies on rust libraries. There is still a dynamic dependency on
librustrt, but I plan on removing that in a subsequent commit.
This change includes no tests just yet. Our current testing
infrastructure/harnesses aren't very amenable to doing flavorful things with
linking, so I'm planning on adding a new mode of testing which I believe belongs
as a separate commit.
Closes #552
2013-11-15 23:03:29 +01:00
|
|
|
|
let of = match ObjectFile::new(mb) {
|
|
|
|
|
Some(of) => of,
|
2014-05-10 03:45:36 +02:00
|
|
|
|
_ => {
|
2014-05-28 05:44:58 +02:00
|
|
|
|
return Err((format!("provided path not an object file: '{}'",
|
|
|
|
|
filename.display())))
|
2014-05-10 03:45:36 +02:00
|
|
|
|
}
|
2013-01-23 20:43:58 +01:00
|
|
|
|
};
|
2013-08-26 05:21:13 +02:00
|
|
|
|
let si = mk_section_iter(of.llof);
|
2013-01-23 20:43:58 +01:00
|
|
|
|
while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
|
2014-04-03 19:45:36 +02:00
|
|
|
|
let mut name_buf = ptr::null();
|
|
|
|
|
let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf);
|
2015-02-04 00:00:38 +01:00
|
|
|
|
let name = slice::from_raw_parts(name_buf as *const u8,
|
2015-03-26 01:06:52 +01:00
|
|
|
|
name_len as usize).to_vec();
|
2014-11-25 22:28:35 +01:00
|
|
|
|
let name = String::from_utf8(name).unwrap();
|
2013-10-21 22:08:31 +02:00
|
|
|
|
debug!("get_metadata_section: name {}", name);
|
2015-05-11 23:31:06 +02:00
|
|
|
|
if read_meta_section_name(target) == name {
|
2013-01-23 20:43:58 +01:00
|
|
|
|
let cbuf = llvm::LLVMGetSectionContents(si.llsi);
|
2015-03-26 01:06:52 +01:00
|
|
|
|
let csz = llvm::LLVMGetSectionSize(si.llsi) as usize;
|
2014-10-25 21:27:15 +02:00
|
|
|
|
let cvbuf: *const u8 = cbuf as *const u8;
|
2016-09-16 16:25:54 +02:00
|
|
|
|
let vlen = METADATA_HEADER.len();
|
2013-10-21 22:08:31 +02:00
|
|
|
|
debug!("checking {} bytes of metadata-version stamp",
|
2013-06-20 07:52:02 +02:00
|
|
|
|
vlen);
|
2014-02-06 08:34:33 +01:00
|
|
|
|
let minsz = cmp::min(vlen, csz);
|
2015-02-04 00:00:38 +01:00
|
|
|
|
let buf0 = slice::from_raw_parts(cvbuf, minsz);
|
2016-09-16 16:25:54 +02:00
|
|
|
|
let version_ok = buf0 == METADATA_HEADER;
|
2014-05-10 03:45:36 +02:00
|
|
|
|
if !version_ok {
|
2014-05-28 05:44:58 +02:00
|
|
|
|
return Err((format!("incompatible metadata version found: '{}'",
|
2014-05-10 03:45:36 +02:00
|
|
|
|
filename.display())));
|
|
|
|
|
}
|
2013-06-20 07:52:02 +02:00
|
|
|
|
|
2015-03-26 01:06:52 +01:00
|
|
|
|
let cvbuf1 = cvbuf.offset(vlen as isize);
|
2013-10-21 22:08:31 +02:00
|
|
|
|
debug!("inflating {} bytes of compressed metadata",
|
2013-08-26 05:21:13 +02:00
|
|
|
|
csz - vlen);
|
2015-02-04 00:00:38 +01:00
|
|
|
|
let bytes = slice::from_raw_parts(cvbuf1, csz - vlen);
|
2014-11-20 19:11:15 +01:00
|
|
|
|
match flate::inflate_bytes(bytes) {
|
2016-07-02 13:41:31 +02:00
|
|
|
|
Ok(inflated) => {
|
2016-09-16 16:25:54 +02:00
|
|
|
|
let blob = MetadataBlob::Inflated(inflated);
|
2016-08-27 16:53:05 +02:00
|
|
|
|
verify_decompressed_encoding_version(&blob, filename)?;
|
2016-07-02 13:41:31 +02:00
|
|
|
|
return Ok(blob);
|
|
|
|
|
}
|
2015-03-15 21:35:48 +01:00
|
|
|
|
Err(_) => {}
|
2013-08-26 05:21:13 +02:00
|
|
|
|
}
|
2012-05-16 05:23:06 +02:00
|
|
|
|
}
|
2013-01-23 20:43:58 +01:00
|
|
|
|
llvm::LLVMMoveToNextSection(si.llsi);
|
2012-05-16 05:23:06 +02:00
|
|
|
|
}
|
2015-02-24 09:50:36 +01:00
|
|
|
|
Err(format!("metadata not found: '{}'", filename.display()))
|
2012-05-16 05:23:06 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-11 23:31:06 +02:00
|
|
|
|
pub fn meta_section_name(target: &Target) -> &'static str {
|
2016-08-14 10:16:28 +02:00
|
|
|
|
// Historical note:
|
|
|
|
|
//
|
|
|
|
|
// When using link.exe it was seen that the section name `.note.rustc`
|
|
|
|
|
// was getting shortened to `.note.ru`, and according to the PE and COFF
|
|
|
|
|
// specification:
|
|
|
|
|
//
|
|
|
|
|
// > Executable images do not use a string table and do not support
|
|
|
|
|
// > section names longer than 8 characters
|
|
|
|
|
//
|
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/hardware/gg463119.aspx
|
|
|
|
|
//
|
|
|
|
|
// As a result, we choose a slightly shorter name! As to why
|
|
|
|
|
// `.note.rustc` works on MinGW, that's another good question...
|
|
|
|
|
|
2015-05-11 23:31:06 +02:00
|
|
|
|
if target.options.is_like_osx {
|
2016-08-14 10:16:28 +02:00
|
|
|
|
"__DATA,.rustc"
|
2014-07-23 20:56:36 +02:00
|
|
|
|
} else {
|
2016-08-14 10:16:28 +02:00
|
|
|
|
".rustc"
|
2012-05-23 02:16:26 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-14 10:16:28 +02:00
|
|
|
|
pub fn read_meta_section_name(_target: &Target) -> &'static str {
|
|
|
|
|
".rustc"
|
2013-03-13 09:22:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
2012-05-16 05:23:06 +02:00
|
|
|
|
// A diagnostic function for dumping crate metadata to an output stream
|
2015-05-11 23:31:06 +02:00
|
|
|
|
pub fn list_file_metadata(target: &Target, path: &Path,
|
2015-02-27 06:00:43 +01:00
|
|
|
|
out: &mut io::Write) -> io::Result<()> {
|
2016-04-08 07:49:48 +02:00
|
|
|
|
let filename = path.file_name().unwrap().to_str().unwrap();
|
|
|
|
|
let flavor = if filename.ends_with(".rlib") { CrateFlavor::Rlib } else { CrateFlavor::Dylib };
|
|
|
|
|
match get_metadata_section(target, flavor, path) {
|
2016-09-15 10:04:00 +02:00
|
|
|
|
Ok(metadata) => metadata.list_crate_metadata(out),
|
2014-03-07 23:37:18 +01:00
|
|
|
|
Err(msg) => {
|
|
|
|
|
write!(out, "{}\n", msg)
|
2014-02-10 21:50:53 +01:00
|
|
|
|
}
|
2012-05-16 05:23:06 +02:00
|
|
|
|
}
|
|
|
|
|
}
|