Skip to content

Commit

Permalink
Revert cpp_{set, link}_stdlib changes
Browse files Browse the repository at this point in the history
Signed-off-by: Jiahao XU <[email protected]>
  • Loading branch information
NobodyXu committed Jun 23, 2024
1 parent 355924b commit fe98c2c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 70 deletions.
107 changes: 38 additions & 69 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ pub struct Build {
no_default_flags: bool,
files: Vec<Arc<Path>>,
cpp: bool,
cpp_link_stdlib: Option<Option<Arc<Path>>>,
cpp_set_stdlib: Option<Arc<Path>>,
cpp_link_stdlib: Option<Option<Arc<str>>>,
cpp_set_stdlib: Option<Arc<str>>,
cuda: bool,
cudart: Option<Arc<str>>,
ccbin: bool,
Expand Down Expand Up @@ -965,112 +965,81 @@ impl Build {
self
}

/// Backward compatible version of [`Build::cpp_link_stdlib_path`]
///
/// # Example
/// Force the C++ compiler to use the specified standard library.
///
/// ```no_run
/// cc::Build::new()
/// .file("src/foo.c")
/// .shared_flag(true)
/// .cpp_link_stdlib("stdc++")
/// .compile("libfoo.so");
/// ```
pub fn cpp_link_stdlib<'a, V: Into<Option<&'a str>>>(
&mut self,
cpp_link_stdlib: V,
) -> &mut Build {
self.cpp_link_stdlib_path(cpp_link_stdlib.into())
}

/// Backward compatible version of [`Build::cpp_set_stdlib_path`]
/// Setting this option will automatically set `cpp_link_stdlib` to the same
/// value.
///
/// # Example
/// The default value of this option is always `None`.
///
/// ```no_run
/// cc::Build::new()
/// .file("src/foo.c")
/// .cpp_set_stdlib("c++")
/// .compile("libfoo.a");
/// ```
pub fn cpp_set_stdlib<'a, V: Into<Option<&'a str>>>(
&mut self,
cpp_set_stdlib: V,
) -> &mut Build {
self.cpp_set_stdlib_path(cpp_set_stdlib.into())
}

/// Set the standard library to link against when compiling with C++
/// support.
/// This option has no effect when compiling for a Visual Studio based
/// target.
///
/// If the `CXXSTDLIB` environment variable is set, its value will
/// override the default value, but not the value explicitly set by calling
/// this function.
/// 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 conjunction with a
/// compiler which supports the `-stdlib` flag.
///
/// A value of `None` indicates that no automatic linking should happen,
/// otherwise cargo will link against the specified library.
/// 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.
///
/// Common values:
/// - `stdc++` for GNU
/// - `c++` for Clang
/// - `c++_shared` or `c++_static` for Android
///
///
/// # Example
///
/// ```no_run
/// cc::Build::new()
/// .file("src/foo.c")
/// .shared_flag(true)
/// .cpp_link_stdlib_path(Some("stdc++"))
/// .cpp_link_stdlib("stdc++")
/// .compile("libfoo.so");
/// ```
pub fn cpp_link_stdlib_path(
pub fn cpp_link_stdlib<'a, V: Into<Option<&'a str>>>(
&mut self,
cpp_link_stdlib: Option<impl AsRef<Path>>,
cpp_link_stdlib: V,
) -> &mut Build {
self.cpp_link_stdlib = Some(cpp_link_stdlib.as_ref().map(AsRef::as_ref).map(Arc::from));
self.cpp_link_stdlib = Some(cpp_link_stdlib.into().map(Arc::from));
self
}

/// Force the C++ compiler to use the specified standard library.
///
/// Setting this option will automatically set `cpp_link_stdlib` to the same
/// value.
///
/// The default value of this option is always `None`.
///
/// This option has no effect when compiling for a Visual Studio based
/// target.
/// Set the standard library to link against when compiling with C++
/// support.
///
/// 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 conjunction with a
/// compiler which supports the `-stdlib` flag.
/// If the `CXXSTDLIB` environment variable is set, its value will
/// override the default value, but not the value explicitly set by calling
/// this function.
///
/// A value of `None` indicates that no specific C++ standard library should
/// be used, otherwise `-stdlib` is added to the compile invocation.
/// 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.
///
/// Common values:
/// - `stdc++` for GNU
/// - `c++` for Clang
/// - `c++_shared` or `c++_static` for Android
///
/// # Example
///
/// ```no_run
/// cc::Build::new()
/// .file("src/foo.c")
/// .cpp_set_stdlib_path(Some("c++"))
/// .cpp_set_stdlib("c++")
/// .compile("libfoo.a");
/// ```
pub fn cpp_set_stdlib_path(&mut self, cpp_set_stdlib: Option<impl AsRef<Path>>) -> &mut Build {
let cpp_set_stdlib = cpp_set_stdlib.as_ref().map(AsRef::as_ref);
self.cpp_set_stdlib = cpp_set_stdlib.map(|s| s.into());
self.cpp_link_stdlib_path(cpp_set_stdlib);
pub fn cpp_set_stdlib<'a, V: Into<Option<&'a str>>>(
&mut self,
cpp_set_stdlib: V,
) -> &mut Build {
let cpp_set_stdlib = cpp_set_stdlib.into().map(Arc::from);
self.cpp_set_stdlib = cpp_set_stdlib.clone();
self.cpp_link_stdlib = Some(cpp_set_stdlib);
self
}

Expand Down Expand Up @@ -2436,7 +2405,7 @@ impl Build {
match (self.cpp_set_stdlib.as_ref(), cmd.family) {
(None, _) => {}
(Some(stdlib), ToolFamily::Gnu) | (Some(stdlib), ToolFamily::Clang { .. }) => {
cmd.push_cc_arg(format!("-stdlib=lib{}", stdlib.display()).into());
cmd.push_cc_arg(format!("-stdlib=lib{}", stdlib).into());
}
_ => {
self.cargo_output.print_warning(&format_args!("cpp_set_stdlib is specified, but the {:?} compiler does not support this option, ignored", cmd.family));
Expand Down Expand Up @@ -3153,7 +3122,7 @@ impl Build {
/// `None` for MSVC and `libstdc++` for anything else.
fn get_cpp_link_stdlib(&self) -> Result<Option<Cow<'_, Path>>, Error> {
match &self.cpp_link_stdlib {
Some(s) => Ok(s.as_deref().map(Cow::Borrowed)),
Some(s) => Ok(s.as_deref().map(Path::new).map(Cow::Borrowed)),
None => {
if let Ok(stdlib) = self.getenv_with_target_prefixes("CXXSTDLIB") {
if stdlib.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ fn gnu_set_stdlib() {

let test = Test::gnu();
test.gcc()
.cpp_set_stdlib_path(Some("foo"))
.cpp_set_stdlib(Some("foo"))
.file("foo.c")
.compile("foo");

Expand Down

0 comments on commit fe98c2c

Please sign in to comment.