|
2 | 2 | compile_error!("feature 'portable' and feature 'march-native' cannot be enabled at the same time"); |
3 | 3 |
|
4 | 4 | use std::env; |
| 5 | +use std::ffi::OsStr; |
5 | 6 | use std::fs; |
6 | 7 | use std::path::{Path, PathBuf}; |
7 | 8 | use std::process::Command; |
@@ -128,23 +129,18 @@ fn build_rocksdb() { |
128 | 129 |
|
129 | 130 | if cfg!(feature = "zstd") { |
130 | 131 | config.define("ZSTD", Some("1")); |
131 | | - if let Some(path) = env::var_os("DEP_ZSTD_INCLUDE") { |
132 | | - config.include(path); |
133 | | - } |
| 132 | + config.include("zstd/lib/"); |
| 133 | + config.include("zstd/lib/dictBuilder/"); |
134 | 134 | } |
135 | 135 |
|
136 | 136 | if cfg!(feature = "zlib") { |
137 | 137 | config.define("ZLIB", Some("1")); |
138 | | - if let Some(path) = env::var_os("DEP_Z_INCLUDE") { |
139 | | - config.include(path); |
140 | | - } |
| 138 | + config.include("zlib/"); |
141 | 139 | } |
142 | 140 |
|
143 | 141 | if cfg!(feature = "bzip2") { |
144 | 142 | config.define("BZIP2", Some("1")); |
145 | | - if let Some(path) = env::var_os("DEP_BZIP2_INCLUDE") { |
146 | | - config.include(path); |
147 | | - } |
| 143 | + config.include("bzip2/"); |
148 | 144 | } |
149 | 145 |
|
150 | 146 | if cfg!(feature = "rtti") { |
@@ -385,6 +381,105 @@ fn build_lz4() { |
385 | 381 | compiler.compile("liblz4.a"); |
386 | 382 | } |
387 | 383 |
|
| 384 | +fn build_zstd() { |
| 385 | + let mut compiler = cc::Build::new(); |
| 386 | + |
| 387 | + compiler.include("zstd/lib/"); |
| 388 | + compiler.include("zstd/lib/common"); |
| 389 | + compiler.include("zstd/lib/legacy"); |
| 390 | + |
| 391 | + let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default(); |
| 392 | + |
| 393 | + for dir in &[ |
| 394 | + "zstd/lib/common", |
| 395 | + "zstd/lib/compress", |
| 396 | + "zstd/lib/decompress", |
| 397 | + "zstd/lib/dictBuilder", |
| 398 | + "zstd/lib/legacy", |
| 399 | + ] { |
| 400 | + let mut entries: Vec<_> = fs::read_dir(dir) |
| 401 | + .unwrap() |
| 402 | + .map(Result::unwrap) |
| 403 | + .filter_map(|entry| { |
| 404 | + let filename = entry.file_name(); |
| 405 | + |
| 406 | + if Path::new(&filename).extension() == Some(OsStr::new("c")) |
| 407 | + // Skip xxhash*.c files: since we are using the "PRIVATE API" |
| 408 | + // mode, it will be inlined in the headers. |
| 409 | + && !filename.to_string_lossy().contains("xxhash") |
| 410 | + { |
| 411 | + Some(entry.path()) |
| 412 | + } else { |
| 413 | + None |
| 414 | + } |
| 415 | + }) |
| 416 | + .collect(); |
| 417 | + entries.sort(); |
| 418 | + |
| 419 | + compiler.files(entries); |
| 420 | + } |
| 421 | + |
| 422 | + if target_arch.contains("x86_64") { |
| 423 | + if env::var("CARGO_CFG_WINDOWS").is_ok() { |
| 424 | + compiler.define("ZSTD_DISABLE_ASM", Some("")); |
| 425 | + } else { |
| 426 | + compiler.file("zstd/lib/decompress/huf_decompress_amd64.S"); |
| 427 | + } |
| 428 | + } else { |
| 429 | + compiler.define("ZSTD_DISABLE_ASM", Some("")); |
| 430 | + } |
| 431 | + |
| 432 | + compiler.opt_level(3); |
| 433 | + compiler.extra_warnings(false); |
| 434 | + compiler |
| 435 | + .flag_if_supported("-ffunction-sections") |
| 436 | + .flag_if_supported("-fdata-sections") |
| 437 | + .flag_if_supported("-fmerge-all-constants"); |
| 438 | + |
| 439 | + compiler.define("ZSTD_LIB_DEPRECATED", Some("0")); |
| 440 | + compiler.compile("libzstd.a"); |
| 441 | +} |
| 442 | + |
| 443 | +fn build_zlib() { |
| 444 | + let mut compiler = cc::Build::new(); |
| 445 | + |
| 446 | + let globs = &["zlib/*.c"]; |
| 447 | + |
| 448 | + for pattern in globs { |
| 449 | + for path in glob::glob(pattern).unwrap() { |
| 450 | + let path = path.unwrap(); |
| 451 | + compiler.file(path); |
| 452 | + } |
| 453 | + } |
| 454 | + |
| 455 | + compiler.flag_if_supported("-Wno-implicit-function-declaration"); |
| 456 | + compiler.opt_level(3); |
| 457 | + compiler.extra_warnings(false); |
| 458 | + compiler.compile("libz.a"); |
| 459 | +} |
| 460 | + |
| 461 | +fn build_bzip2() { |
| 462 | + let mut compiler = cc::Build::new(); |
| 463 | + |
| 464 | + compiler |
| 465 | + .file("bzip2/blocksort.c") |
| 466 | + .file("bzip2/bzlib.c") |
| 467 | + .file("bzip2/compress.c") |
| 468 | + .file("bzip2/crctable.c") |
| 469 | + .file("bzip2/decompress.c") |
| 470 | + .file("bzip2/huffman.c") |
| 471 | + .file("bzip2/randtable.c"); |
| 472 | + |
| 473 | + compiler |
| 474 | + .define("_FILE_OFFSET_BITS", Some("64")) |
| 475 | + .define("BZ_NO_STDIO", None); |
| 476 | + |
| 477 | + compiler.extra_warnings(false); |
| 478 | + compiler.opt_level(3); |
| 479 | + compiler.extra_warnings(false); |
| 480 | + compiler.compile("libbz2.a"); |
| 481 | +} |
| 482 | + |
388 | 483 | fn try_to_find_and_link_lib(lib_name: &str) -> bool { |
389 | 484 | if let Ok(v) = env::var(&format!("{}_COMPILE", lib_name)) { |
390 | 485 | if v.to_lowercase() == "true" || v == "1" { |
@@ -433,6 +528,21 @@ fn main() { |
433 | 528 | fail_on_empty_directory("lz4"); |
434 | 529 | build_lz4(); |
435 | 530 | } |
| 531 | + if cfg!(feature = "zstd") && !try_to_find_and_link_lib("ZSTD") { |
| 532 | + println!("cargo:rerun-if-changed=zstd/"); |
| 533 | + fail_on_empty_directory("zstd"); |
| 534 | + build_zstd(); |
| 535 | + } |
| 536 | + if cfg!(feature = "zlib") && !try_to_find_and_link_lib("Z") { |
| 537 | + println!("cargo:rerun-if-changed=zlib/"); |
| 538 | + fail_on_empty_directory("zlib"); |
| 539 | + build_zlib(); |
| 540 | + } |
| 541 | + if cfg!(feature = "bzip2") && !try_to_find_and_link_lib("BZ2") { |
| 542 | + println!("cargo:rerun-if-changed=bzip2/"); |
| 543 | + fail_on_empty_directory("bzip2"); |
| 544 | + build_bzip2(); |
| 545 | + } |
436 | 546 |
|
437 | 547 | println!("cargo:out_dir={}", env::var("OUT_DIR").unwrap()); |
438 | 548 | } |
0 commit comments