Commit ab16db78 authored by Jay's avatar Jay Committed by GitHub

adjust build (#94)

- always build crocksdb explicitly
- fix linking on windows
parent d593411a
...@@ -5,6 +5,8 @@ use std::{env, fs, str}; ...@@ -5,6 +5,8 @@ use std::{env, fs, str};
use std::path::PathBuf; use std::path::PathBuf;
use std::process::Command; use std::process::Command;
use gcc::Config;
macro_rules! t { macro_rules! t {
($e:expr) => (match $e { ($e:expr) => (match $e {
Ok(n) => n, Ok(n) => n,
...@@ -13,24 +15,40 @@ macro_rules! t { ...@@ -13,24 +15,40 @@ macro_rules! t {
} }
fn main() { fn main() {
let mut cfg = build_rocksdb();
cfg.cpp(true).file("crocksdb/c.cc");
if !cfg!(target_os = "windows") {
cfg.flag("-std=c++11");
}
cfg.compile("libcrocksdb.a");
println!("cargo:rustc-link-lib=static=crocksdb");
}
fn build_rocksdb() -> Config {
let mut cfg = Config::new();
if !cfg!(feature = "static-link") { if !cfg!(feature = "static-link") {
gcc::Config::new() if cfg!(target_os = "windows") {
.cpp(true) println!("cargo:rustc-link-lib=rocksdb-shared");
.file("crocksdb/c.cc") } else {
.flag("-std=c++11") println!("cargo:rustc-link-lib=rocksdb");
.flag("-fPIC") }
.flag("-O2") return cfg;
.compile("libcrocksdb.a");
println!("cargo:rustc-link-lib=static=crocksdb");
println!("cargo:rustc-link-lib=rocksdb");
return;
} }
println!("cargo:rustc-link-lib=static=rocksdb");
println!("cargo:rustc-link-lib=static=z");
println!("cargo:rustc-link-lib=static=bz2");
println!("cargo:rustc-link-lib=static=lz4");
println!("cargo:rustc-link-lib=static=zstd");
println!("cargo:rustc-link-lib=static=snappy");
if !cfg!(target_os = "linux") && !cfg!(target_os = "macos") { if !cfg!(target_os = "linux") && !cfg!(target_os = "macos") {
// only linux and apple support static link right now // Compilation is not tested in other platform, so hopefully
return; // the static library is built already.
return cfg;
} }
let dst = PathBuf::from(env::var_os("OUT_DIR").unwrap()); let dst = PathBuf::from(env::var_os("OUT_DIR").unwrap());
...@@ -39,8 +57,6 @@ fn main() { ...@@ -39,8 +57,6 @@ fn main() {
let fest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); let fest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let p = PathBuf::from(fest_dir.clone()).join("build.sh"); let p = PathBuf::from(fest_dir.clone()).join("build.sh");
let crocksdb_path = PathBuf::from(fest_dir).join("crocksdb");
env::set_var("CROCKSDB_PATH", crocksdb_path.to_str().unwrap());
for lib in &["z", "snappy", "bz2", "lz4", "zstd", "rocksdb"] { for lib in &["z", "snappy", "bz2", "lz4", "zstd", "rocksdb"] {
let lib_name = format!("lib{}.a", lib); let lib_name = format!("lib{}.a", lib);
let src = build.join(&lib_name); let src = build.join(&lib_name);
...@@ -80,13 +96,8 @@ fn main() { ...@@ -80,13 +96,8 @@ fn main() {
} }
} }
println!("cargo:rustc-link-lib=static=rocksdb");
println!("cargo:rustc-link-lib=static=z");
println!("cargo:rustc-link-lib=static=bz2");
println!("cargo:rustc-link-lib=static=lz4");
println!("cargo:rustc-link-lib=static=zstd");
println!("cargo:rustc-link-lib=static=snappy");
println!("cargo:rustc-link-search=native={}", dst.display()); println!("cargo:rustc-link-search=native={}", dst.display());
cfg.include(dst.join("build").join("rocksdb").join("include"));
let mut cpp_linked = false; let mut cpp_linked = false;
let std_lib_name = if cfg!(target_os = "linux") { let std_lib_name = if cfg!(target_os = "linux") {
...@@ -108,25 +119,29 @@ fn main() { ...@@ -108,25 +119,29 @@ fn main() {
} }
} }
} }
if !cpp_linked { if cpp_linked {
let output = cfg.cpp_link_stdlib(None);
Command::new(p.as_path()).args(&["find_library", std_lib_name]).output().unwrap(); return cfg;
if output.status.success() && !output.stdout.is_empty() { }
if let Ok(path_str) = str::from_utf8(&output.stdout) {
let path = PathBuf::from(path_str); let output =
if path.is_absolute() { Command::new(p.as_path()).args(&["find_library", std_lib_name]).output().unwrap();
println!("cargo:rustc-link-lib=static=stdc++"); if output.status.success() && !output.stdout.is_empty() {
println!("cargo:rustc-link-search=native={}", if let Ok(path_str) = str::from_utf8(&output.stdout) {
path.parent().unwrap().display()); let path = PathBuf::from(path_str);
return; if path.is_absolute() {
} println!("cargo:rustc-link-lib=static=stdc++");
println!("cargo:rustc-link-search=native={}",
path.parent().unwrap().display());
cfg.cpp_link_stdlib(None);
return cfg;
} }
} }
println!("failed to detect {}: {:?}, fallback to dynamic",
std_lib_name,
output);
println!("cargo:rustc-link-lib={}", &short_std_lib_name);
} }
println!("failed to detect {}: {:?}, fallback to dynamic",
std_lib_name,
output);
cfg
} }
fn run(cmd: &mut Command) { fn run(cmd: &mut Command) {
......
...@@ -127,13 +127,12 @@ function compile_rocksdb() { ...@@ -127,13 +127,12 @@ function compile_rocksdb() {
version=v5.5.1 version=v5.5.1
vernum=5.5.1 vernum=5.5.1
echo building rocksdb-$version echo building rocksdb-$version
rm -rf rocksdb-$vernum rm -rf rocksdb rocksdb-$vernum
download https://github.com/facebook/rocksdb/archive/$version.tar.gz rocksdb-$version.tar.gz d10fe596fe4c926ed3d68a8179f8edaa download https://github.com/facebook/rocksdb/archive/$version.tar.gz rocksdb-$version.tar.gz d10fe596fe4c926ed3d68a8179f8edaa
tar xf rocksdb-$version.tar.gz tar xf rocksdb-$version.tar.gz
wd=`pwd` wd=`pwd`
cd rocksdb-$vernum mv rocksdb-$vernum rocksdb
cp $CROCKSDB_PATH/c.cc ./db/c.cc cd rocksdb
cp $CROCKSDB_PATH/rocksdb/c.h ./include/rocksdb/c.h
export EXTRA_CFLAGS="-fPIC -I${wd}/zlib-1.2.11 -I${wd}/bzip2-1.0.6 -I${wd}/snappy-1.1.1 -I${wd}/lz4-r131/lib -I${wd}/zstd-1.2.0/lib" export EXTRA_CFLAGS="-fPIC -I${wd}/zlib-1.2.11 -I${wd}/bzip2-1.0.6 -I${wd}/snappy-1.1.1 -I${wd}/lz4-r131/lib -I${wd}/zstd-1.2.0/lib"
export EXTRA_CXXFLAGS="-DZLIB -DBZIP2 -DSNAPPY -DLZ4 -DZSTD $EXTRA_CFLAGS" export EXTRA_CXXFLAGS="-DZLIB -DBZIP2 -DSNAPPY -DLZ4 -DZSTD $EXTRA_CFLAGS"
make static_lib -j $con make static_lib -j $con
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors. // found in the LICENSE file. See the AUTHORS file for names of contributors.
#include "rocksdb/c.h" #include "crocksdb/c.h"
#include <stdlib.h> #include <stdlib.h>
#include "rocksdb/cache.h" #include "rocksdb/cache.h"
......
...@@ -148,6 +148,7 @@ fn test_set_wal_opt() { ...@@ -148,6 +148,7 @@ fn test_set_wal_opt() {
drop(db); drop(db);
} }
#[cfg(not(windows))]
#[test] #[test]
fn test_sync_wal() { fn test_sync_wal() {
let path = TempDir::new("_rust_rocksdb_test_sync_wal").expect(""); let path = TempDir::new("_rust_rocksdb_test_sync_wal").expect("");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment