diff --git a/Cargo.toml b/Cargo.toml index 9ed446cc..f10134ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] diff --git a/bzip2-sys/Cargo.toml b/bzip2-sys/Cargo.toml index bc2318f6..01998bfe 100644 --- a/bzip2-sys/Cargo.toml +++ b/bzip2-sys/Cargo.toml @@ -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 diff --git a/bzip2-sys/build.rs b/bzip2-sys/build.rs index feb004ca..cac0e670 100644 --- a/bzip2-sys/build.rs +++ b/bzip2-sys/build.rs @@ -25,6 +25,24 @@ 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"); + + 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"); + } + } + + 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)