From f8d1029a6fdd1e78fdba2800c533ad395e026dd9 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Sat, 28 Jan 2023 01:34:38 +1100 Subject: [PATCH 1/4] Use `--gc-sections` to remove unused symbols from `libbz2.a` Signed-off-by: Jiahao XU --- bzip2-sys/build.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bzip2-sys/build.rs b/bzip2-sys/build.rs index feb004ca..1bc8d51c 100644 --- a/bzip2-sys/build.rs +++ b/bzip2-sys/build.rs @@ -25,6 +25,12 @@ 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"); + cfg.include("bzip2-1.0.8") .define("_FILE_OFFSET_BITS", Some("64")) .define("BZ_NO_STDIO", None) From e95261770ac4150a188e5aaee5774e50ba44e984 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Sat, 28 Jan 2023 01:35:58 +1100 Subject: [PATCH 2/4] Add new feature `fat-lto` & `thin-lto` Signed-off-by: Jiahao XU --- Cargo.toml | 3 +++ bzip2-sys/Cargo.toml | 3 +++ bzip2-sys/build.rs | 10 ++++++++++ 3 files changed, 16 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 9ed446cc..c4557412 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,3 +33,6 @@ 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"] diff --git a/bzip2-sys/Cargo.toml b/bzip2-sys/Cargo.toml index bc2318f6..399bcc2e 100644 --- a/bzip2-sys/Cargo.toml +++ b/bzip2-sys/Cargo.toml @@ -28,3 +28,6 @@ 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 diff --git a/bzip2-sys/build.rs b/bzip2-sys/build.rs index 1bc8d51c..ff79e9bd 100644 --- a/bzip2-sys/build.rs +++ b/bzip2-sys/build.rs @@ -31,6 +31,16 @@ fn main() { .flag_if_supported("-Wl,--gc-sections") .flag_if_supported("-Wl,--icf=safe"); + 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"); + } + } + cfg.include("bzip2-1.0.8") .define("_FILE_OFFSET_BITS", Some("64")) .define("BZ_NO_STDIO", None) From 0f8f115d5b305740f778cce758d43c3e1bd4160d Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Sat, 28 Jan 2023 12:01:17 +1100 Subject: [PATCH 3/4] Add new feat `thin` to use opt-level `z` Signed-off-by: Jiahao XU --- Cargo.toml | 2 ++ bzip2-sys/Cargo.toml | 2 ++ bzip2-sys/build.rs | 4 ++++ 3 files changed, 8 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index c4557412..f10134ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,3 +36,5 @@ static = ["bzip2-sys/static"] fat-lto = ["bzip2-sys/fat-lto"] thin-lto = ["bzip2-sys/thin-lto"] + +thin = ["bzip2-sys/thin"] diff --git a/bzip2-sys/Cargo.toml b/bzip2-sys/Cargo.toml index 399bcc2e..01998bfe 100644 --- a/bzip2-sys/Cargo.toml +++ b/bzip2-sys/Cargo.toml @@ -31,3 +31,5 @@ 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 diff --git a/bzip2-sys/build.rs b/bzip2-sys/build.rs index ff79e9bd..ab5e0333 100644 --- a/bzip2-sys/build.rs +++ b/bzip2-sys/build.rs @@ -41,6 +41,10 @@ fn main() { } } + if cfg!(feature = "thin") { + cfg.opt_level_str("z"); + } + cfg.include("bzip2-1.0.8") .define("_FILE_OFFSET_BITS", Some("64")) .define("BZ_NO_STDIO", None) From 814695e55be05d9677fd229e70fa37849e5c0127 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Wed, 1 Feb 2023 16:20:40 +1100 Subject: [PATCH 4/4] Remove unused `-Wl` linker args Signed-off-by: Jiahao XU --- bzip2-sys/build.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bzip2-sys/build.rs b/bzip2-sys/build.rs index ab5e0333..cac0e670 100644 --- a/bzip2-sys/build.rs +++ b/bzip2-sys/build.rs @@ -27,9 +27,7 @@ fn main() { 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"); + .flag_if_supported("-fmerge-all-constants"); if cfg!(feature = "fat-lto") { cfg.flag_if_supported("-flto");