Skip to content

Commit 6bc87b3

Browse files
committed
Add vendored feature to zstd-sys for providing pre-built zstd libs
1 parent 87cce79 commit 6bc87b3

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

zstd-safe/zstd-sys/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ no_asm = [] # Disable ASM files (only on amd64 for decompression)
7979
zdict_builder = [] # Enable dictionary building (dictionary _using_ is always supported).
8080
no_wasm_shim = [] # Disable wasm shims (in case your wasm toolchain includes a C stdlib).
8181
seekable = [] # Enable support of the seekable format
82+
vendored = [] # Allow users to provide explicit lib and inlcude paths for the zstd C library.
8283

8384
# These two are for cross-language LTO.
8485
# Will only work if `clang` is used to build the C library.

zstd-safe/zstd-sys/build.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::ffi::OsStr;
22
use std::path::{Path, PathBuf};
3-
use std::{env, fmt, fs};
3+
use std::{env, fmt, fs, io};
44

55
#[cfg(feature = "bindgen")]
66
fn generate_bindings(defs: Vec<&str>, headerpaths: Vec<PathBuf>) {
@@ -254,6 +254,23 @@ fn compile_zstd() {
254254
cargo_print(&format_args!("root={}", dst.display()));
255255
}
256256

257+
fn copy_dir_all(
258+
src: impl AsRef<Path>,
259+
dst: impl AsRef<Path>,
260+
) -> io::Result<()> {
261+
fs::create_dir_all(&dst)?;
262+
for entry in fs::read_dir(src)? {
263+
let entry = entry?;
264+
let ty = entry.file_type()?;
265+
if ty.is_dir() {
266+
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
267+
} else {
268+
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
269+
}
270+
}
271+
Ok(())
272+
}
273+
257274
/// Print a line for cargo.
258275
///
259276
/// If non-cargo is set, do not print anything.
@@ -265,6 +282,9 @@ fn cargo_print(content: &dyn fmt::Display) {
265282

266283
fn main() {
267284
cargo_print(&"rerun-if-env-changed=ZSTD_SYS_USE_PKG_CONFIG");
285+
cargo_print(&"rerun-if-env-changed=ZSTD_SYS_VENDORED");
286+
cargo_print(&"rerun-if-env-changed=ZSTD_SYS_VENDORED_INCLUDE");
287+
cargo_print(&"rerun-if-env-changed=ZSTD_SYS_VENDORED_LIBS");
268288

269289
let target_arch =
270290
std::env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
@@ -274,8 +294,29 @@ fn main() {
274294
cargo_print(&"rustc-cfg=feature=\"std\"");
275295
}
276296

277-
// println!("cargo:rustc-link-lib=zstd");
278-
let (defs, headerpaths) = if cfg!(feature = "pkg-config")
297+
let (defs, headerpaths) = if cfg!(feature = "vendored")
298+
|| env::var_os("ZSTD_SYS_VENDORED").is_some()
299+
{
300+
let dst = PathBuf::from(env::var_os("OUT_DIR").unwrap());
301+
let lib_dir = dst.join("lib");
302+
fs::create_dir_all(&lib_dir).unwrap();
303+
let zstd_libs: Vec<PathBuf> = env::var("ZSTD_SYS_VENDORED_LIBS")
304+
.unwrap()
305+
.split(" ")
306+
.map(PathBuf::from)
307+
.collect();
308+
for lib in zstd_libs {
309+
fs::copy(&lib, lib_dir.join(lib.file_name().unwrap())).unwrap();
310+
}
311+
cargo_print(&"rustc-link-lib=static=zstd");
312+
cargo_print(&format_args!("rustc-link-search={}", lib_dir.display()));
313+
let include_dir = dst.join("include");
314+
if let Some(zstd_include) = env::var_os("ZSTD_SYS_VENDORED_INCLUDE") {
315+
copy_dir_all(zstd_include, &include_dir).unwrap();
316+
}
317+
cargo_print(&format_args!("root={}", dst.display()));
318+
(vec![], vec![include_dir])
319+
} else if cfg!(feature = "pkg-config")
279320
|| env::var_os("ZSTD_SYS_USE_PKG_CONFIG").is_some()
280321
{
281322
pkg_config()

0 commit comments

Comments
 (0)