Browse Source

Allow choosing which C++ standard library to use

add-rc-path
TimNN 10 years ago
parent
commit
94fd496aa5
  1. 41
      src/lib.rs

41
src/lib.rs

@ -59,6 +59,17 @@ pub struct Config {
flags: Vec<String>, flags: Vec<String>,
files: Vec<PathBuf>, files: Vec<PathBuf>,
cpp: bool, cpp: bool,
cpp_stdlib: Option<String>,
}
/// Returns the default C++ standard library for the current target: `libc++`
/// for OS X and `libstdc++` for anything else.
fn target_default_cpp_stdlib() -> Option<&'static str> {
if getenv_unwrap("TARGET").contains("darwin") {
Some("c++")
} else {
Some("stdc++")
}
} }
fn getenv(v: &str) -> Option<String> { fn getenv(v: &str) -> Option<String> {
@ -107,6 +118,7 @@ impl Config {
flags: Vec::new(), flags: Vec::new(),
files: Vec::new(), files: Vec::new(),
cpp: false, cpp: false,
cpp_stdlib: target_default_cpp_stdlib().map(|s| s.into()),
} }
} }
@ -146,6 +158,25 @@ impl Config {
self self
} }
/// Set the standard library to use when compiling with C++ support.
///
/// The default value of this property depends on the current target: On
/// OS X `Some("c++")` is used, for any other target `Some("stdc++")`.
///
/// 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.
///
/// 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());
self
}
/// Run the compiler, generating the file `output` /// Run the compiler, generating the file `output`
/// ///
/// The name `output` must begin with `lib` and end with `.a` /// The name `output` must begin with `lib` and end with `.a`
@ -177,7 +208,9 @@ impl Config {
// Add specific C++ libraries, if enabled. // Add specific C++ libraries, if enabled.
if self.cpp { if self.cpp {
println!("cargo:rustc-link-lib=stdc++"); if let Some(ref stdlib) = self.cpp_stdlib {
println!("cargo:rustc-link-lib={}", stdlib);
}
} }
} }
@ -228,6 +261,12 @@ 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() { for directory in self.include_directories.iter() {
cmd.arg("-I").arg(directory); cmd.arg("-I").arg(directory);
} }

Loading…
Cancel
Save