• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::env;
2 
build_zlib_ng(target: &str, compat: bool)3 pub fn build_zlib_ng(target: &str, compat: bool) {
4     let mut cmake = cmake::Config::new("src/zlib-ng");
5     cmake
6         .define("BUILD_SHARED_LIBS", "OFF")
7         .define("ZLIB_COMPAT", if compat { "ON" } else { "OFF" })
8         .define("ZLIB_ENABLE_TESTS", "OFF")
9         .define("WITH_GZFILEOP", "ON");
10     if target.contains("s390x") {
11         // Enable hardware compression on s390x.
12         cmake
13             .define("WITH_DFLTCC_DEFLATE", "1")
14             .define("WITH_DFLTCC_INFLATE", "1")
15             .cflag("-DDFLTCC_LEVEL_MASK=0x7e");
16     }
17     if target.contains("riscv") {
18         // Check if we should pass on an explicit boolean value of the WITH_RVV build option.
19         // See: https://github.com/zlib-ng/zlib-ng?tab=readme-ov-file#advanced-build-options
20         if let Ok(value) = env::var("RISCV_WITH_RVV") {
21             match value.trim().to_uppercase().as_str() {
22                 "OFF" | "NO" | "FALSE" | "0" => {
23                     // Force RVV off. This turns off RVV entirely, as well as the runtime check for it.
24                     // This is not usually necessary, but can be useful for building binaries portable
25                     // to systems that do not support RVV but where auto-detection fails to identify
26                     // this (as in https://github.com/zlib-ng/zlib-ng/issues/1705).
27                     cmake.define("WITH_RVV", "OFF");
28                 }
29                 "ON" | "YES" | "TRUE" | "1" => {
30                     // Try to use RVV, but still don't do so if a runtime check finds it unavailable.
31                     // This has the same effect as omitting WITH_RVV, unless it has already been set.
32                     cmake.define("WITH_RVV", "ON");
33                 }
34                 _ => {}
35             }
36         }
37     }
38     if target == "i686-pc-windows-msvc" {
39         cmake.define("CMAKE_GENERATOR_PLATFORM", "Win32");
40     }
41 
42     // libz-ng uses the GNUInstallDirs convention, so we can use the following
43     // to ensure libraries are placed in a consistent place in the
44     // installation dir.
45     cmake.define("CMAKE_INSTALL_LIBDIR", "lib");
46 
47     let install_dir = cmake.build();
48 
49     let includedir = install_dir.join("include");
50     let libdir = install_dir.join("lib");
51     println!(
52         "cargo:rustc-link-search=native={}",
53         libdir.to_str().unwrap()
54     );
55     let mut debug_suffix = "";
56     let libname = if target.contains("windows") && target.contains("msvc") {
57         if env::var("OPT_LEVEL").unwrap() == "0" {
58             debug_suffix = "d";
59         }
60         "zlibstatic"
61     } else {
62         "z"
63     };
64     println!(
65         "cargo:rustc-link-lib=static={}{}{}",
66         libname,
67         if compat { "" } else { "-ng" },
68         debug_suffix,
69     );
70     println!("cargo:root={}", install_dir.to_str().unwrap());
71     println!("cargo:include={}", includedir.to_str().unwrap());
72     if !compat {
73         println!("cargo:rustc-cfg=zng");
74     }
75 }
76 
77 #[allow(dead_code)]
main()78 fn main() {
79     let target = env::var("TARGET").unwrap();
80     build_zlib_ng(&target, false);
81 }
82