Skip to content

Commit ebde617

Browse files
authored
Rollup merge of rust-lang#40022 - wagenet:lib-defaults, r=alexcrichton
Better handling of lib defaults r? @alexcrichton
2 parents 3ece892 + 69c5359 commit ebde617

File tree

4 files changed

+80
-31
lines changed

4 files changed

+80
-31
lines changed

src/librustc/session/config.rs

+30-29
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ top_level_options!(
288288
// much sense: The search path can stay the same while the
289289
// things discovered there might have changed on disk.
290290
search_paths: SearchPaths [TRACKED],
291-
libs: Vec<(String, Option<String>, cstore::NativeLibraryKind)> [TRACKED],
291+
libs: Vec<(String, Option<String>, Option<cstore::NativeLibraryKind>)> [TRACKED],
292292
maybe_sysroot: Option<PathBuf> [TRACKED],
293293

294294
target_triple: String [TRACKED],
@@ -1495,18 +1495,18 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
14951495
let mut parts = s.splitn(2, '=');
14961496
let kind = parts.next().unwrap();
14971497
let (name, kind) = match (parts.next(), kind) {
1498-
(None, name) |
1499-
(Some(name), "dylib") => (name, cstore::NativeUnknown),
1500-
(Some(name), "framework") => (name, cstore::NativeFramework),
1501-
(Some(name), "static") => (name, cstore::NativeStatic),
1502-
(Some(name), "static-nobundle") => (name, cstore::NativeStaticNobundle),
1498+
(None, name) => (name, None),
1499+
(Some(name), "dylib") => (name, Some(cstore::NativeUnknown)),
1500+
(Some(name), "framework") => (name, Some(cstore::NativeFramework)),
1501+
(Some(name), "static") => (name, Some(cstore::NativeStatic)),
1502+
(Some(name), "static-nobundle") => (name, Some(cstore::NativeStaticNobundle)),
15031503
(_, s) => {
15041504
early_error(error_format, &format!("unknown library kind `{}`, expected \
15051505
one of dylib, framework, or static",
15061506
s));
15071507
}
15081508
};
1509-
if kind == cstore::NativeStaticNobundle && !nightly_options::is_nightly_build() {
1509+
if kind == Some(cstore::NativeStaticNobundle) && !nightly_options::is_nightly_build() {
15101510
early_error(error_format, &format!("the library kind 'static-nobundle' is only \
15111511
accepted on the nightly compiler"));
15121512
}
@@ -1772,6 +1772,7 @@ mod dep_tracking {
17721772
impl_dep_tracking_hash_via_hash!(Option<PanicStrategy>);
17731773
impl_dep_tracking_hash_via_hash!(Option<lint::Level>);
17741774
impl_dep_tracking_hash_via_hash!(Option<PathBuf>);
1775+
impl_dep_tracking_hash_via_hash!(Option<cstore::NativeLibraryKind>);
17751776
impl_dep_tracking_hash_via_hash!(CrateType);
17761777
impl_dep_tracking_hash_via_hash!(PanicStrategy);
17771778
impl_dep_tracking_hash_via_hash!(Passes);
@@ -1788,7 +1789,7 @@ mod dep_tracking {
17881789
impl_dep_tracking_hash_for_sortable_vec_of!(CrateType);
17891790
impl_dep_tracking_hash_for_sortable_vec_of!((String, lint::Level));
17901791
impl_dep_tracking_hash_for_sortable_vec_of!((String, Option<String>,
1791-
cstore::NativeLibraryKind));
1792+
Option<cstore::NativeLibraryKind>));
17921793
impl DepTrackingHash for SearchPaths {
17931794
fn hash(&self, hasher: &mut DefaultHasher, _: ErrorOutputType) {
17941795
let mut elems: Vec<_> = self
@@ -2232,24 +2233,24 @@ mod tests {
22322233
let mut v4 = super::basic_options();
22332234

22342235
// Reference
2235-
v1.libs = vec![(String::from("a"), None, cstore::NativeStatic),
2236-
(String::from("b"), None, cstore::NativeFramework),
2237-
(String::from("c"), None, cstore::NativeUnknown)];
2236+
v1.libs = vec![(String::from("a"), None, Some(cstore::NativeStatic)),
2237+
(String::from("b"), None, Some(cstore::NativeFramework)),
2238+
(String::from("c"), None, Some(cstore::NativeUnknown))];
22382239

22392240
// Change label
2240-
v2.libs = vec![(String::from("a"), None, cstore::NativeStatic),
2241-
(String::from("X"), None, cstore::NativeFramework),
2242-
(String::from("c"), None, cstore::NativeUnknown)];
2241+
v2.libs = vec![(String::from("a"), None, Some(cstore::NativeStatic)),
2242+
(String::from("X"), None, Some(cstore::NativeFramework)),
2243+
(String::from("c"), None, Some(cstore::NativeUnknown))];
22432244

22442245
// Change kind
2245-
v3.libs = vec![(String::from("a"), None, cstore::NativeStatic),
2246-
(String::from("b"), None, cstore::NativeStatic),
2247-
(String::from("c"), None, cstore::NativeUnknown)];
2246+
v3.libs = vec![(String::from("a"), None, Some(cstore::NativeStatic)),
2247+
(String::from("b"), None, Some(cstore::NativeStatic)),
2248+
(String::from("c"), None, Some(cstore::NativeUnknown))];
22482249

22492250
// Change new-name
2250-
v4.libs = vec![(String::from("a"), None, cstore::NativeStatic),
2251-
(String::from("b"), Some(String::from("X")), cstore::NativeFramework),
2252-
(String::from("c"), None, cstore::NativeUnknown)];
2251+
v4.libs = vec![(String::from("a"), None, Some(cstore::NativeStatic)),
2252+
(String::from("b"), Some(String::from("X")), Some(cstore::NativeFramework)),
2253+
(String::from("c"), None, Some(cstore::NativeUnknown))];
22532254

22542255
assert!(v1.dep_tracking_hash() != v2.dep_tracking_hash());
22552256
assert!(v1.dep_tracking_hash() != v3.dep_tracking_hash());
@@ -2269,17 +2270,17 @@ mod tests {
22692270
let mut v3 = super::basic_options();
22702271

22712272
// Reference
2272-
v1.libs = vec![(String::from("a"), None, cstore::NativeStatic),
2273-
(String::from("b"), None, cstore::NativeFramework),
2274-
(String::from("c"), None, cstore::NativeUnknown)];
2273+
v1.libs = vec![(String::from("a"), None, Some(cstore::NativeStatic)),
2274+
(String::from("b"), None, Some(cstore::NativeFramework)),
2275+
(String::from("c"), None, Some(cstore::NativeUnknown))];
22752276

2276-
v2.libs = vec![(String::from("b"), None, cstore::NativeFramework),
2277-
(String::from("a"), None, cstore::NativeStatic),
2278-
(String::from("c"), None, cstore::NativeUnknown)];
2277+
v2.libs = vec![(String::from("b"), None, Some(cstore::NativeFramework)),
2278+
(String::from("a"), None, Some(cstore::NativeStatic)),
2279+
(String::from("c"), None, Some(cstore::NativeUnknown))];
22792280

2280-
v3.libs = vec![(String::from("c"), None, cstore::NativeUnknown),
2281-
(String::from("a"), None, cstore::NativeStatic),
2282-
(String::from("b"), None, cstore::NativeFramework)];
2281+
v3.libs = vec![(String::from("c"), None, Some(cstore::NativeUnknown)),
2282+
(String::from("a"), None, Some(cstore::NativeStatic)),
2283+
(String::from("b"), None, Some(cstore::NativeFramework))];
22832284

22842285
assert!(v1.dep_tracking_hash() == v2.dep_tracking_hash());
22852286
assert!(v1.dep_tracking_hash() == v3.dep_tracking_hash());

src/librustc_metadata/creader.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1077,10 +1077,20 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
10771077
let mut found = false;
10781078
for lib in self.cstore.get_used_libraries().borrow_mut().iter_mut() {
10791079
if lib.name == name as &str {
1080-
lib.kind = kind;
1080+
let mut changed = false;
1081+
if let Some(k) = kind {
1082+
lib.kind = k;
1083+
changed = true;
1084+
}
10811085
if let &Some(ref new_name) = new_name {
10821086
lib.name = Symbol::intern(new_name);
1087+
changed = true;
1088+
}
1089+
if !changed {
1090+
self.sess.warn(&format!("redundant linker flag specified for library `{}`",
1091+
name));
10831092
}
1093+
10841094
found = true;
10851095
}
10861096
}
@@ -1089,7 +1099,7 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
10891099
let new_name = new_name.as_ref().map(|s| &**s); // &Option<String> -> Option<&str>
10901100
let lib = NativeLibrary {
10911101
name: Symbol::intern(new_name.unwrap_or(name)),
1092-
kind: kind,
1102+
kind: if let Some(k) = kind { k } else { cstore::NativeUnknown },
10931103
cfg: None,
10941104
foreign_items: Vec::new(),
10951105
};
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2016 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+
// no-prefer-dynamic
12+
#![crate_type = "staticlib"]
13+
14+
#[no_mangle]
15+
pub extern "C" fn foo(x:i32) -> i32 { x }

src/test/run-pass/lib-defaults.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2017 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+
// aux-build:clibrary.rs
12+
// compile-flags: -lclibrary
13+
14+
#[link(name = "clibrary", kind = "static")]
15+
extern "C" {
16+
pub fn foo(x:i32) -> i32;
17+
}
18+
19+
fn main() {
20+
unsafe {
21+
foo(42);
22+
}
23+
}

0 commit comments

Comments
 (0)