Skip to content

Commit

Permalink
Avoid build require system zlib (rust-rocksdb#303)
Browse files Browse the repository at this point in the history
`register_dep("Z")` correctly set the include path for zlib, but fail to set the path to libz.a. RocksDB cmake script expect libz.a be place under ${DEP_Z_ROOT}/lib, but libz-sys place it under ${DEP_Z_ROOT}/build. Fixed it.

Also added a test to make sure possible compression types are all linked.

Signed-off-by: Yi Wu <yiwu@pingcap.com>
  • Loading branch information
yiwu-arbug authored Jul 25, 2019
1 parent 9246b9c commit 6ead62c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
18 changes: 12 additions & 6 deletions librocksdb_sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ fn link_cpp(build: &mut Build) {

fn build_rocksdb() -> Build {
let target = env::var("TARGET").expect("TARGET was not set");

let mut build = Build::new();
for e in env::vars() {
println!("{:?}", e);
}

let mut cfg = Config::new("rocksdb");
if cfg!(feature = "jemalloc") && NO_JEMALLOC_TARGETS.iter().all(|i| !target.contains(i)) {
cfg.register_dep("JEMALLOC").define("WITH_JEMALLOC", "ON");
Expand All @@ -93,6 +87,16 @@ fn build_rocksdb() -> Build {
if cfg!(feature = "sse") {
cfg.define("FORCE_SSE42", "ON");
}
// RocksDB cmake script expect libz.a being under ${DEP_Z_ROOT}/lib, but libz-sys crate put it
// under ${DEP_Z_ROOT}/build. Append the path to CMAKE_PREFIX_PATH to get around it.
env::set_var("CMAKE_PREFIX_PATH", {
let zlib_path = format!("{}/build", env::var("DEP_Z_ROOT").unwrap());
if let Ok(prefix_path) = env::var("CMAKE_PREFIX_PATH") {
format!("{};{}", prefix_path, zlib_path)
} else {
zlib_path
}
});
let dst = cfg
.define("WITH_GFLAGS", "OFF")
.register_dep("Z")
Expand All @@ -106,8 +110,10 @@ fn build_rocksdb() -> Build {
.register_dep("SNAPPY")
.define("WITH_SNAPPY", "ON")
.build_target("rocksdb")
.very_verbose(true)
.build();
let build_dir = format!("{}/build", dst.display());
let mut build = Build::new();
if cfg!(target_os = "windows") {
let profile = match &*env::var("PROFILE").unwrap_or("debug".to_owned()) {
"bench" | "release" => "Release",
Expand Down
13 changes: 13 additions & 0 deletions librocksdb_sys/libtitan_sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
extern crate cc;
extern crate cmake;

use std::env;

fn main() {
// RocksDB cmake script expect libz.a being under ${DEP_Z_ROOT}/lib, but libz-sys crate put it
// under ${DEP_Z_ROOT}/build. Append the path to CMAKE_PREFIX_PATH to get around it.
env::set_var("CMAKE_PREFIX_PATH", {
let zlib_path = format!("{}/build", env::var("DEP_Z_ROOT").unwrap());
if let Ok(prefix_path) = env::var("CMAKE_PREFIX_PATH") {
format!("{};{}", prefix_path, zlib_path)
} else {
zlib_path
}
});
let cur_dir = std::env::current_dir().unwrap();
let mut cfg = cmake::Config::new("titan");
if cfg!(feature = "portable") {
Expand All @@ -25,6 +37,7 @@ fn main() {
.register_dep("SNAPPY")
.define("WITH_SNAPPY", "ON")
.build_target("titan")
.very_verbose(true)
.build();
println!("cargo:rustc-link-search=native={}/build", dst.display());
println!("cargo:rustc-link-lib=static=titan");
Expand Down
1 change: 1 addition & 0 deletions tests/cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern crate tempdir;
mod test_column_family;
mod test_compact_range;
mod test_compaction_filter;
mod test_compression;
mod test_delete_files_in_range;
mod test_delete_range;
mod test_encryption;
Expand Down
42 changes: 42 additions & 0 deletions tests/cases/test_compression.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

use rocksdb::{ColumnFamilyOptions, DBCompressionType, DBOptions, DB};
use tempdir::TempDir;

#[test]
// Make sure all compression types are supported.
fn test_compression() {
let path = TempDir::new("_rust_rocksdb_test_metadata").unwrap();
let compression_types = [
DBCompressionType::Snappy,
DBCompressionType::Zlib,
DBCompressionType::Bz2,
DBCompressionType::Lz4,
DBCompressionType::Lz4hc,
DBCompressionType::Zstd,
];
for compression_type in compression_types.iter() {
let mut opts = DBOptions::new();
opts.create_if_missing(true);
let mut cf_opts = ColumnFamilyOptions::new();
cf_opts.compression(*compression_type);
// DB open will fail if compression type is not supported.
DB::open_cf(
opts,
path.path().to_str().unwrap(),
vec![("default", cf_opts)],
)
.unwrap();
}
}

0 comments on commit 6ead62c

Please sign in to comment.