Skip to content

Commit 91f8144

Browse files
committed
Implement the "static-nobundle" library kind (RFC 1717).
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.
1 parent 8e29b4d commit 91f8144

File tree

9 files changed

+71
-2
lines changed

9 files changed

+71
-2
lines changed

src/librustc/middle/cstore.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use rustc_back::target::Target;
4646
use hir;
4747
use rustc_back::PanicStrategy;
4848

49-
pub use self::NativeLibraryKind::{NativeStatic, NativeFramework, NativeUnknown};
49+
pub use self::NativeLibraryKind::*;
5050

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

@@ -122,6 +122,7 @@ pub enum LinkagePreference {
122122
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
123123
pub enum NativeLibraryKind {
124124
NativeStatic, // native static library (.a archive)
125+
NativeStaticNobundle, // native static library, which doesn't get bundled into .rlibs
125126
NativeFramework, // OSX-specific
126127
NativeUnknown, // default way to specify a dynamic library
127128
}

src/librustc/session/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,7 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
14731473
(Some(name), "dylib") => (name, cstore::NativeUnknown),
14741474
(Some(name), "framework") => (name, cstore::NativeFramework),
14751475
(Some(name), "static") => (name, cstore::NativeStatic),
1476+
(Some(name), "static-nobundle") => (name, cstore::NativeStaticNobundle),
14761477
(_, s) => {
14771478
early_error(error_format, &format!("unknown library kind `{}`, expected \
14781479
one of dylib, framework, or static",

src/librustc_metadata/creader.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,7 @@ impl<'a> CrateLoader<'a> {
917917
}).and_then(|a| a.value_str()).map(Symbol::as_str);
918918
let kind = match kind.as_ref().map(|s| &s[..]) {
919919
Some("static") => cstore::NativeStatic,
920+
Some("static-nobundle") => cstore::NativeStaticNobundle,
920921
Some("dylib") => cstore::NativeUnknown,
921922
Some("framework") => cstore::NativeFramework,
922923
Some(k) => {

src/librustc_metadata/cstore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use syntax::symbol::Symbol;
3232
use syntax_pos;
3333

3434
pub use rustc::middle::cstore::{NativeLibrary, NativeLibraryKind, LinkagePreference};
35-
pub use rustc::middle::cstore::{NativeStatic, NativeFramework, NativeUnknown};
35+
pub use rustc::middle::cstore::NativeLibraryKind::*;
3636
pub use rustc::middle::cstore::{CrateSource, LinkMeta, LibSource};
3737

3838
// A map from external crate numbers (as decoded from some crate file) to

src/librustc_trans/back/link.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ fn link_rlib<'a>(sess: &'a Session,
476476
for lib in sess.cstore.used_libraries() {
477477
match lib.kind {
478478
NativeLibraryKind::NativeStatic => {}
479+
NativeLibraryKind::NativeStaticNobundle |
479480
NativeLibraryKind::NativeFramework |
480481
NativeLibraryKind::NativeUnknown => continue,
481482
}
@@ -674,6 +675,7 @@ fn link_staticlib(sess: &Session, objects: &[PathBuf], out_filename: &Path,
674675

675676
for lib in all_native_libs.iter().filter(|l| relevant_lib(sess, l)) {
676677
let name = match lib.kind {
678+
NativeLibraryKind::NativeStaticNobundle |
677679
NativeLibraryKind::NativeUnknown => "library",
678680
NativeLibraryKind::NativeFramework => "framework",
679681
// These are included, no need to print them
@@ -985,6 +987,7 @@ fn add_local_native_libraries(cmd: &mut Linker, sess: &Session) {
985987
match lib.kind {
986988
NativeLibraryKind::NativeUnknown => cmd.link_dylib(&lib.name.as_str()),
987989
NativeLibraryKind::NativeFramework => cmd.link_framework(&lib.name.as_str()),
990+
NativeLibraryKind::NativeStaticNobundle => cmd.link_staticlib(&lib.name.as_str()),
988991
NativeLibraryKind::NativeStatic => bug!(),
989992
}
990993
}
@@ -1229,6 +1232,7 @@ fn add_upstream_native_libraries(cmd: &mut Linker, sess: &Session) {
12291232
match lib.kind {
12301233
NativeLibraryKind::NativeUnknown => cmd.link_dylib(&lib.name.as_str()),
12311234
NativeLibraryKind::NativeFramework => cmd.link_framework(&lib.name.as_str()),
1235+
NativeLibraryKind::NativeStaticNobundle => cmd.link_staticlib(&lib.name.as_str()),
12321236

12331237
// ignore statically included native libraries here as we've
12341238
// already included them when we included the rust library
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-include ../tools.mk
2+
3+
all: $(call NATIVE_STATICLIB,foo)
4+
$(RUSTC) bar.rs
5+
6+
# Check that libbar.rlib does not contain the definition of `func`
7+
nm $(TMPDIR)/libbar.rlib | (! grep "T _*func")
8+
nm $(TMPDIR)/libbar.rlib | grep "U _*func"
9+
10+
# Check that foo gets passed to the linker (as either `-l foo` or `foo.lib`)
11+
$(RUSTC) main.rs -Z print-link-args | grep -e "-l[\" ]*foo" -e "foo.lib"
12+
13+
$(call RUN,main)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "rlib"]
12+
13+
#[link(name = "foo", kind = "static-nobundle")]
14+
extern {
15+
pub fn func();
16+
}
17+
18+
pub fn wrapped_func() {
19+
unsafe {
20+
func();
21+
}
22+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
void func() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
extern crate bar;
12+
13+
fn main() {
14+
unsafe { bar::func(); }
15+
bar::wrapped_func();
16+
}

0 commit comments

Comments
 (0)