Skip to content

Optimize build for libbz2.a #88

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ tokio-core = "0.1"
tokio = ["tokio-io", "futures"]
# Enable this feature if you want to have a statically linked bzip2
static = ["bzip2-sys/static"]

fat-lto = ["bzip2-sys/fat-lto"]
thin-lto = ["bzip2-sys/thin-lto"]

thin = ["bzip2-sys/thin"]
5 changes: 5 additions & 0 deletions bzip2-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ cc = "1.0"
[features]
# Enable this feature if you want to have a statically linked bzip2
static = []

fat-lto = [] # Enable fat-lto, will override thin-lto if specified
thin-lto = [] # Enable thin-lto, will fallback to fat-lto if not supported

thin = [] # Use -Oz is possible, otherwise fallback to -Os or -O2. For msvc it will be /O1
20 changes: 20 additions & 0 deletions bzip2-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ fn main() {

let dst = PathBuf::from(env::var_os("OUT_DIR").unwrap());

cfg.flag_if_supported("-ffunction-sections")
.flag_if_supported("-fdata-sections")
.flag_if_supported("-fmerge-all-constants")
.flag_if_supported("-Wl,--gc-sections")
.flag_if_supported("-Wl,--icf=safe");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to have -ffunction-sections and co. -Wl,--gc-sections and -Wl,--icf=safe can be omitted though as those only apply to linking by the cc crate, but we only build a staticlib here. They don't have any effect on linking done by rustc.


if cfg!(feature = "fat-lto") {
cfg.flag_if_supported("-flto");
} else if cfg!(feature = "thin-lto") {
if cfg.is_flag_supported("-flto=thin").unwrap_or(false) {
cfg.flag("-flto=thin");
} else {
cfg.flag_if_supported("-flto");
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've asked at https://rust-lang.zulipchat.com/#narrow/channel/246057-t-cargo/topic/read.20lto.20config.20from.20build.20script if there is a way to read the LTO state from cargo without having to use feature flags. In any case this won't actually LTO between bzip2 and any rust code. That is only possible when using the rust port of bzip2 rather than the C original or by using -Clinker-plugin-lto and making sure that the exact right clang version is used to build the C code.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out the cc crate itself nowadays handles -Clto and -Copt-level already, so the second and third commit can be dropped. Only the first and fourth commit (which should be squashed) are still necessary.


if cfg!(feature = "thin") {
cfg.opt_level_str("z");
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably forward the OPT_LEVEL env var instead of adding a feature.


cfg.include("bzip2-1.0.8")
.define("_FILE_OFFSET_BITS", Some("64"))
.define("BZ_NO_STDIO", None)
Expand Down