From c56ccf6a9fdd930da0b3c165546ecab582a161d8 Mon Sep 17 00:00:00 2001 From: Tim Neumann Date: Wed, 3 Jun 2015 15:21:16 +0200 Subject: [PATCH 1/2] simplify cpp stdlib handling, don't use -stdlib= --- src/lib.rs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7b3e7bb..04c5f75 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -162,19 +162,16 @@ impl Config { self } - /// Set the standard library to use when compiling with C++ support. + /// Set the standard library to link against when compiling with C++ + /// support. /// /// The default value of this property depends on the current target: On /// OS X `Some("c++")` is used and for other supported targets /// `Some("stdc++")` is used. /// - /// The choosen library is specified when compiling using the `-stdlib` flag - /// and Cargo is instructed to link to the given library. - /// /// A value of `None` indicates that no special actions regarding the C++ - /// standard library should be take. This means that neither are linking - /// instructions provided to Cargo nor is the compiler told to use a - /// specific standard library. + /// standard library should be take. This means that you have to link + /// against the correct standard library yourself if required. /// /// The given library name must not contain the `lib` prefix. pub fn cpp_stdlib(&mut self, cpp_stdlib: Option<&str>) -> &mut Config { @@ -294,12 +291,6 @@ impl Config { } } - if self.cpp { - if let Some(ref stdlib) = self.cpp_stdlib { - cmd.arg(&format!("-stdlib=lib{}", stdlib)); - } - } - for directory in self.include_directories.iter() { cmd.arg(if target.contains("msvc") {"/I"} else {"-I"}); cmd.arg(directory); From 62306018ae2ebac0ffefd780067a76c1065af4bd Mon Sep 17 00:00:00 2001 From: Tim Neumann Date: Sat, 6 Jun 2015 18:54:57 +0200 Subject: [PATCH 2/2] re-add support for -stdlib --- src/lib.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 04c5f75..41421c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -60,7 +60,8 @@ pub struct Config { flags: Vec, files: Vec, cpp: bool, - cpp_stdlib: Option, + cpp_link_stdlib: Option, + cpp_set_stdlib: Option, } /// Returns the default C++ standard library for the current target: `libc++` @@ -122,7 +123,8 @@ impl Config { flags: Vec::new(), files: Vec::new(), cpp: false, - cpp_stdlib: target_default_cpp_stdlib().map(|s| s.into()), + cpp_link_stdlib: target_default_cpp_stdlib().map(|s| s.into()), + cpp_set_stdlib: None, } } @@ -156,7 +158,10 @@ impl Config { self } - /// Set C++ support + /// Set C++ support. + /// + /// The other `cpp_*` options will only become active if this is set to + /// `true`. pub fn cpp(&mut self, cpp: bool) -> &mut Config { self.cpp = cpp; self @@ -166,16 +171,43 @@ impl Config { /// support. /// /// The default value of this property depends on the current target: On - /// OS X `Some("c++")` is used and for other supported targets - /// `Some("stdc++")` is used. + /// OS X `Some("c++")` is used, when compiling for a Visual Studio based + /// target `None` is used and for other targets `Some("stdc++")` is used. + /// + /// A value of `None` indicates that no automatic linking should happen, + /// otherwise cargo will link against the specified library. + /// + /// The given library name must not contain the `lib` prefix. + pub fn cpp_link_stdlib(&mut self, cpp_link_stdlib: Option<&str>) -> &mut Config { + self.cpp_link_stdlib = cpp_link_stdlib.map(|s| s.into()); + self + } + + /// Force the C++ compiler to use the specified standard library. + /// + /// Setting this option will automatically set `cpp_link_stdlib` to the same + /// value. /// - /// A value of `None` indicates that no special actions regarding the C++ - /// standard library should be take. This means that you have to link - /// against the correct standard library yourself if required. + /// The default value of this option is always `None`. + /// + /// This option has no effect when compiling for a Visual Studio based + /// target. + /// + /// This option sets the `-stdlib` flag, which is only supported by some + /// compilers (clang, icc) but not by others (gcc). The library will not + /// detect which compiler is used, as such it is the responsibility of the + /// caller to ensure that this option is only used in conjuction with a + /// compiler which supports the `-stdlib` flag. + /// + /// A value of `None` indicates that no specific C++ standard library should + /// be used, otherwise `-stdlib` is added to the compile invocation. /// /// The given library name must not contain the `lib` prefix. - pub fn cpp_stdlib(&mut self, cpp_stdlib: Option<&str>) -> &mut Config { - self.cpp_stdlib = cpp_stdlib.map(|s| s.into()); + pub fn cpp_set_stdlib(&mut self, cpp_set_stdlib: Option<&str>) -> &mut Config { + self.cpp_set_stdlib = cpp_set_stdlib.map(|s| s.into()); + + self.cpp_link_stdlib(cpp_set_stdlib); + self } @@ -233,7 +265,7 @@ impl Config { // Add specific C++ libraries, if enabled. if self.cpp { - if let Some(ref stdlib) = self.cpp_stdlib { + if let Some(ref stdlib) = self.cpp_link_stdlib { println!("cargo:rustc-link-lib={}", stdlib); } } @@ -291,6 +323,12 @@ impl Config { } } + if self.cpp && !target.contains("msvc") { + if let Some(ref stdlib) = self.cpp_set_stdlib { + cmd.arg(&format!("-stdlib=lib{}", stdlib)); + } + } + for directory in self.include_directories.iter() { cmd.arg(if target.contains("msvc") {"/I"} else {"-I"}); cmd.arg(directory);