Skip to content

Add whole_archive option to cc::Build #688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub struct Build {
compiler: Option<PathBuf>,
archiver: Option<PathBuf>,
cargo_metadata: bool,
whole_archive: Option<bool>,
pic: Option<bool>,
use_plt: Option<bool>,
static_crt: Option<bool>,
Expand Down Expand Up @@ -312,6 +313,7 @@ impl Build {
compiler: None,
archiver: None,
cargo_metadata: true,
whole_archive: None,
pic: None,
use_plt: None,
static_crt: None,
Expand Down Expand Up @@ -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`.
///
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down