From 9fa1ad103c9ec98cffdf2d315fdbdfda4df2902f Mon Sep 17 00:00:00 2001 From: onalante-msft <89409054+onalante-msft@users.noreply.github.com> Date: Mon, 13 Jun 2022 15:21:23 -0700 Subject: [PATCH] Add `whole_archive` option to `cc::Build` --- src/lib.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 527d1d67d..0d8f592de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -114,6 +114,7 @@ pub struct Build { compiler: Option, archiver: Option, cargo_metadata: bool, + whole_archive: Option, pic: Option, use_plt: Option, static_crt: Option, @@ -312,6 +313,7 @@ impl Build { compiler: None, archiver: None, cargo_metadata: true, + whole_archive: None, pic: None, use_plt: None, static_crt: None, @@ -883,6 +885,7 @@ impl Build { self.archiver = Some(archiver.as_ref().to_owned()); self } + /// Define whether metadata should be emitted for cargo allowing it to /// automatically link the binary. Defaults to `true`. /// @@ -898,6 +901,22 @@ impl Build { self } + /// Configures whether "+whole-archive" will be emitted in the Cargo linking + /// metadata. This option does nothing if `cargo_metadata` is `false`. + /// + /// Without `whole_archive`, the emitted `rustc-link-lib` directive is: + /// + /// - `rustc-link-lib=static=`*compiled lib* + /// + /// With `whole_archive`, the emitted `rustc-link-lib` directive is: + /// + /// - `rustc-link-lib=static:+whole-archive=`*compiled lib* + /// + pub fn whole_archive(&mut self, whole_archive: bool) -> &mut Build { + self.whole_archive = Some(whole_archive); + self + } + /// Configures whether the compiler will emit position independent code. /// /// This option defaults to `false` for `windows-gnu` and bare metal targets and @@ -1014,7 +1033,14 @@ impl Build { } } - self.print(&format!("cargo:rustc-link-lib=static={}", lib_name)); + if self.whole_archive.unwrap_or_default() { + self.print(&format!( + "cargo:rustc-link-lib=static:+whole-archive={}", + lib_name + )); + } else { + self.print(&format!("cargo:rustc-link-lib=static={}", lib_name)); + } self.print(&format!("cargo:rustc-link-search=native={}", dst.display())); // Add specific C++ libraries, if enabled.