Skip to content

Commit

Permalink
Implement the "static-nobundle" library kind (RFC 1717).
Browse files Browse the repository at this point in the history
These are static libraries that are not bundled (as the name implies) into rlibs and staticlibs that rustc generates,
and must be present when the final binary artifact is being linked.
  • Loading branch information
vadimcn committed Jan 19, 2017
1 parent 8e29b4d commit 91f8144
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/librustc/middle/cstore.rs
Expand Up @@ -46,7 +46,7 @@ use rustc_back::target::Target;
use hir;
use rustc_back::PanicStrategy;

pub use self::NativeLibraryKind::{NativeStatic, NativeFramework, NativeUnknown};
pub use self::NativeLibraryKind::*;

// lonely orphan structs and enums looking for a better home

Expand Down Expand Up @@ -122,6 +122,7 @@ pub enum LinkagePreference {
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
pub enum NativeLibraryKind {
NativeStatic, // native static library (.a archive)
NativeStaticNobundle, // native static library, which doesn't get bundled into .rlibs
NativeFramework, // OSX-specific
NativeUnknown, // default way to specify a dynamic library
}
Expand Down
1 change: 1 addition & 0 deletions src/librustc/session/config.rs
Expand Up @@ -1473,6 +1473,7 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
(Some(name), "dylib") => (name, cstore::NativeUnknown),
(Some(name), "framework") => (name, cstore::NativeFramework),
(Some(name), "static") => (name, cstore::NativeStatic),
(Some(name), "static-nobundle") => (name, cstore::NativeStaticNobundle),
(_, s) => {
early_error(error_format, &format!("unknown library kind `{}`, expected \
one of dylib, framework, or static",
Expand Down
1 change: 1 addition & 0 deletions src/librustc_metadata/creader.rs
Expand Up @@ -917,6 +917,7 @@ impl<'a> CrateLoader<'a> {
}).and_then(|a| a.value_str()).map(Symbol::as_str);
let kind = match kind.as_ref().map(|s| &s[..]) {
Some("static") => cstore::NativeStatic,
Some("static-nobundle") => cstore::NativeStaticNobundle,
Some("dylib") => cstore::NativeUnknown,
Some("framework") => cstore::NativeFramework,
Some(k) => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/cstore.rs
Expand Up @@ -32,7 +32,7 @@ use syntax::symbol::Symbol;
use syntax_pos;

pub use rustc::middle::cstore::{NativeLibrary, NativeLibraryKind, LinkagePreference};
pub use rustc::middle::cstore::{NativeStatic, NativeFramework, NativeUnknown};
pub use rustc::middle::cstore::NativeLibraryKind::*;
pub use rustc::middle::cstore::{CrateSource, LinkMeta, LibSource};

// A map from external crate numbers (as decoded from some crate file) to
Expand Down
4 changes: 4 additions & 0 deletions src/librustc_trans/back/link.rs
Expand Up @@ -476,6 +476,7 @@ fn link_rlib<'a>(sess: &'a Session,
for lib in sess.cstore.used_libraries() {
match lib.kind {
NativeLibraryKind::NativeStatic => {}
NativeLibraryKind::NativeStaticNobundle |
NativeLibraryKind::NativeFramework |
NativeLibraryKind::NativeUnknown => continue,
}
Expand Down Expand Up @@ -674,6 +675,7 @@ fn link_staticlib(sess: &Session, objects: &[PathBuf], out_filename: &Path,

for lib in all_native_libs.iter().filter(|l| relevant_lib(sess, l)) {
let name = match lib.kind {
NativeLibraryKind::NativeStaticNobundle |
NativeLibraryKind::NativeUnknown => "library",
NativeLibraryKind::NativeFramework => "framework",
// These are included, no need to print them
Expand Down Expand Up @@ -985,6 +987,7 @@ fn add_local_native_libraries(cmd: &mut Linker, sess: &Session) {
match lib.kind {
NativeLibraryKind::NativeUnknown => cmd.link_dylib(&lib.name.as_str()),
NativeLibraryKind::NativeFramework => cmd.link_framework(&lib.name.as_str()),
NativeLibraryKind::NativeStaticNobundle => cmd.link_staticlib(&lib.name.as_str()),
NativeLibraryKind::NativeStatic => bug!(),
}
}
Expand Down Expand Up @@ -1229,6 +1232,7 @@ fn add_upstream_native_libraries(cmd: &mut Linker, sess: &Session) {
match lib.kind {
NativeLibraryKind::NativeUnknown => cmd.link_dylib(&lib.name.as_str()),
NativeLibraryKind::NativeFramework => cmd.link_framework(&lib.name.as_str()),
NativeLibraryKind::NativeStaticNobundle => cmd.link_staticlib(&lib.name.as_str()),

// ignore statically included native libraries here as we've
// already included them when we included the rust library
Expand Down
13 changes: 13 additions & 0 deletions src/test/run-make/static-nobundle/Makefile
@@ -0,0 +1,13 @@
-include ../tools.mk

all: $(call NATIVE_STATICLIB,foo)
$(RUSTC) bar.rs

# Check that libbar.rlib does not contain the definition of `func`
nm $(TMPDIR)/libbar.rlib | (! grep "T _*func")
nm $(TMPDIR)/libbar.rlib | grep "U _*func"

# Check that foo gets passed to the linker (as either `-l foo` or `foo.lib`)
$(RUSTC) main.rs -Z print-link-args | grep -e "-l[\" ]*foo" -e "foo.lib"

$(call RUN,main)
22 changes: 22 additions & 0 deletions src/test/run-make/static-nobundle/bar.rs
@@ -0,0 +1,22 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// 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.

#![crate_type = "rlib"]

#[link(name = "foo", kind = "static-nobundle")]
extern {
pub fn func();
}

pub fn wrapped_func() {
unsafe {
func();
}
}
11 changes: 11 additions & 0 deletions src/test/run-make/static-nobundle/foo.c
@@ -0,0 +1,11 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// 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.

void func() {}
16 changes: 16 additions & 0 deletions src/test/run-make/static-nobundle/main.rs
@@ -0,0 +1,16 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// 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.

extern crate bar;

fn main() {
unsafe { bar::func(); }
bar::wrapped_func();
}

0 comments on commit 91f8144

Please sign in to comment.